mirror of
https://github.com/openai/codex.git
synced 2026-11-22 15:15:18 +00:00
Enforce managed authentication requirements locally (#37132)
## Why Authentication restrictions must apply before stored or environment-provided credentials can be used, including during bootstrap before cloud requirements are fetched. ## What changed - Add local `requirements.toml` allowlists for login methods and ChatGPT workspaces. Ignore these fields in cloud-provided requirements. - Combine managed workspace allowlists with existing workspace restrictions by intersection, and fail closed when the resulting policy permits no usable login method. - Centralize policy checks in the authentication manager so CLI, TUI, app-server, external-auth, and credential-loading paths consistently reject disallowed authentication before token hydration or network requests. ## Testing - Cover policy composition, workspace intersection, invalid stored and external credentials, bootstrap enforcement, and login endpoint restrictions. GitOrigin-RevId: efef22b248f3c3333e9aa55423e539efa2d2dd48
This commit is contained in:
1 parent
4cb8676d3a
commit
2994f545a7
36 files changed
+1121
-328
No files matched your search
@@ -1,11 +1,14 @@
|
||||
use super::Config;
|
||||
use super::ConfigTomlLoadResult;
|
||||
use super::ManagedFeatures;
|
||||
use super::resolve_bootstrap_auth_route_config;
|
||||
use codex_config::types::AuthKeyringBackendKind;
|
||||
use codex_features::Feature;
|
||||
use codex_features::FeatureConfigSource;
|
||||
use codex_features::FeatureOverrides;
|
||||
use codex_features::Features;
|
||||
use codex_login::AuthConfig;
|
||||
use std::path::Path;
|
||||
|
||||
impl Config {
|
||||
pub fn auth_keyring_backend_kind(&self) -> AuthKeyringBackendKind {
|
||||
@@ -13,6 +16,66 @@ impl Config {
|
||||
self.features.enabled(Feature::SecretAuthStorage),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn auth_config(&self) -> AuthConfig {
|
||||
AuthConfig {
|
||||
codex_home: self.codex_home.to_path_buf(),
|
||||
auth_credentials_store_mode: self.cli_auth_credentials_store_mode,
|
||||
keyring_backend_kind: self.auth_keyring_backend_kind(),
|
||||
forced_login_method: self.forced_login_method,
|
||||
chatgpt_base_url: Some(self.chatgpt_base_url.clone()),
|
||||
forced_chatgpt_workspace_id: self.forced_chatgpt_workspace_id.clone(),
|
||||
managed_auth_policy: self.config_layer_stack.requirements().managed_auth_policy(),
|
||||
auth_route_config: self.auth_route_config(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds authentication settings from the locally resolved bootstrap config.
|
||||
///
|
||||
/// Use this before fetching cloud requirements, when a full [`Config`] is not
|
||||
/// yet available. Preserves the configured credential store, keyring backend,
|
||||
/// ChatGPT base URL, auth routing, and managed login/workspace restrictions.
|
||||
pub fn bootstrap_auth_config(
|
||||
codex_home: &Path,
|
||||
bootstrap_config: &ConfigTomlLoadResult,
|
||||
) -> std::io::Result<AuthConfig> {
|
||||
let config = &bootstrap_config.config_toml;
|
||||
// Empty legacy workspace settings mean unrestricted, not an empty allowlist.
|
||||
let forced_chatgpt_workspace_id = config
|
||||
.forced_chatgpt_workspace_id
|
||||
.clone()
|
||||
.map(|workspaces| {
|
||||
workspaces
|
||||
.into_vec()
|
||||
.into_iter()
|
||||
.map(|workspace| workspace.trim().to_string())
|
||||
.filter(|workspace| !workspace.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.filter(|workspaces| !workspaces.is_empty());
|
||||
let auth_config = AuthConfig {
|
||||
codex_home: codex_home.to_path_buf(),
|
||||
auth_credentials_store_mode: config.cli_auth_credentials_store.unwrap_or_default(),
|
||||
keyring_backend_kind: resolve_bootstrap_auth_keyring_backend_kind(bootstrap_config)?,
|
||||
forced_login_method: config.forced_login_method,
|
||||
chatgpt_base_url: config.chatgpt_base_url.clone(),
|
||||
forced_chatgpt_workspace_id,
|
||||
managed_auth_policy: bootstrap_config
|
||||
.config_layer_stack
|
||||
.requirements()
|
||||
.managed_auth_policy(),
|
||||
auth_route_config: resolve_bootstrap_auth_route_config(
|
||||
config,
|
||||
bootstrap_config
|
||||
.config_layer_stack
|
||||
.requirements()
|
||||
.feature_requirements
|
||||
.as_ref(),
|
||||
)?,
|
||||
};
|
||||
auth_config.validate()?;
|
||||
Ok(auth_config)
|
||||
}
|
||||
|
||||
/// Resolve the auth keyring backend from a partially loaded bootstrap config.
|
||||
|
||||
@@ -6,7 +6,9 @@ use codex_config::FeatureRequirementsToml;
|
||||
use codex_config::RequirementSource;
|
||||
use codex_config::Sourced;
|
||||
use codex_config::config_toml::ConfigToml;
|
||||
use codex_config::config_toml::ForcedChatgptWorkspaceIds;
|
||||
use codex_features::FeaturesToml;
|
||||
use codex_protocol::config_types::ForcedLoginMethod;
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
@@ -60,6 +62,70 @@ fn resolve_bootstrap_auth_keyring_backend_kind_uses_secret_auth_storage_feature(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn managed_auth_restrictions_intersect_workspaces_and_fail_closed() {
|
||||
let config = ConfigToml {
|
||||
forced_login_method: None,
|
||||
forced_chatgpt_workspace_id: Some(ForcedChatgptWorkspaceIds::Multiple(vec![
|
||||
" denied ".to_string(),
|
||||
" allowed ".to_string(),
|
||||
])),
|
||||
..Default::default()
|
||||
};
|
||||
let mut requirements = ConfigRequirements {
|
||||
allowed_login_methods: Some(Sourced::new(
|
||||
vec![ForcedLoginMethod::Chatgpt],
|
||||
RequirementSource::Unknown,
|
||||
)),
|
||||
allowed_chatgpt_workspaces: Some(Sourced::new(
|
||||
vec!["allowed".to_string()],
|
||||
RequirementSource::Unknown,
|
||||
)),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let bootstrap_config = ConfigTomlLoadResult {
|
||||
config_toml: config.clone(),
|
||||
config_layer_stack: ConfigLayerStack::new(
|
||||
Vec::new(),
|
||||
requirements.clone(),
|
||||
ConfigRequirementsToml::default(),
|
||||
)
|
||||
.expect("requirements should stack"),
|
||||
};
|
||||
let auth_config = bootstrap_auth_config(Path::new("codex-home"), &bootstrap_config)
|
||||
.expect("policy should resolve");
|
||||
assert_eq!(auth_config.forced_login_method, None);
|
||||
assert!(auth_config.is_login_method_allowed(ForcedLoginMethod::Chatgpt));
|
||||
assert!(!auth_config.is_login_method_allowed(ForcedLoginMethod::Api));
|
||||
assert_eq!(
|
||||
auth_config.forced_chatgpt_workspace_id,
|
||||
Some(vec!["denied".to_string(), "allowed".to_string()])
|
||||
);
|
||||
assert_eq!(
|
||||
auth_config.effective_chatgpt_workspaces(),
|
||||
Some(vec!["allowed".to_string()])
|
||||
);
|
||||
|
||||
requirements.allowed_chatgpt_workspaces =
|
||||
Some(Sourced::new(Vec::new(), RequirementSource::Unknown));
|
||||
let bootstrap_config = ConfigTomlLoadResult {
|
||||
config_toml: config,
|
||||
config_layer_stack: ConfigLayerStack::new(
|
||||
Vec::new(),
|
||||
requirements,
|
||||
ConfigRequirementsToml::default(),
|
||||
)
|
||||
.expect("requirements should stack"),
|
||||
};
|
||||
assert_eq!(
|
||||
bootstrap_auth_config(Path::new("codex-home"), &bootstrap_config)
|
||||
.expect_err("ChatGPT-only policy without an allowed workspace must fail")
|
||||
.kind(),
|
||||
std::io::ErrorKind::PermissionDenied
|
||||
);
|
||||
}
|
||||
|
||||
fn config_toml_load_result(
|
||||
config_toml: ConfigToml,
|
||||
feature_requirements: Option<Sourced<FeatureRequirementsToml>>,
|
||||
|
||||
@@ -9446,6 +9446,8 @@ async fn test_requirements_web_search_mode_allowlist_does_not_warn_when_unset()
|
||||
let fixture = create_test_fixture()?;
|
||||
|
||||
let requirements_toml = codex_config::ConfigRequirementsToml {
|
||||
allowed_login_methods: None,
|
||||
allowed_chatgpt_workspaces: None,
|
||||
sqlite_home: None,
|
||||
log_dir: None,
|
||||
model_catalog_json: None,
|
||||
|
||||
@@ -13,6 +13,7 @@ use codex_config::ConfigRequirements;
|
||||
use codex_config::ConfigRequirementsToml;
|
||||
use codex_config::ConstrainedWithSource;
|
||||
use codex_config::FeatureRequirementsToml;
|
||||
use codex_config::ManagedAuthPolicy;
|
||||
use codex_config::McpServerRequirement;
|
||||
use codex_config::PluginRequirementsToml;
|
||||
use codex_config::ProfileV2Name;
|
||||
@@ -160,6 +161,7 @@ mod requirements;
|
||||
mod resolved_permission_profile;
|
||||
#[cfg(test)]
|
||||
mod schema;
|
||||
pub use auth_keyring::bootstrap_auth_config;
|
||||
pub use auth_keyring::resolve_bootstrap_auth_keyring_backend_kind;
|
||||
pub use codex_config::ConfigLoadOptions;
|
||||
pub use codex_config::Constrained;
|
||||
@@ -1337,10 +1339,18 @@ impl AuthManagerConfig for Config {
|
||||
Config::auth_keyring_backend_kind(self)
|
||||
}
|
||||
|
||||
fn forced_login_method(&self) -> Option<ForcedLoginMethod> {
|
||||
self.forced_login_method
|
||||
}
|
||||
|
||||
fn forced_chatgpt_workspace_id(&self) -> Option<Vec<String>> {
|
||||
self.forced_chatgpt_workspace_id.clone()
|
||||
}
|
||||
|
||||
fn managed_auth_policy(&self) -> ManagedAuthPolicy {
|
||||
self.config_layer_stack.requirements().managed_auth_policy()
|
||||
}
|
||||
|
||||
fn chatgpt_base_url(&self) -> String {
|
||||
self.chatgpt_base_url.clone()
|
||||
}
|
||||
@@ -3198,10 +3208,11 @@ impl Config {
|
||||
config_layer_stack.requirements(),
|
||||
&mut startup_warnings,
|
||||
);
|
||||
|
||||
// Destructure every field to ensure ConfigRequirements additions are
|
||||
// either applied above or handled while constructing the final Config.
|
||||
let ConfigRequirements {
|
||||
allowed_login_methods: _,
|
||||
allowed_chatgpt_workspaces: _,
|
||||
sqlite_home: _,
|
||||
log_dir: _,
|
||||
model_catalog_json: _,
|
||||
|
||||
Reference in new issue
Block a user