mirror of
https://github.com/openai/codex.git
synced 2026-11-23 07:17:11 +00:00
Handle copied credentials in the broker and shell snapshots (#44038)
## Why Shell startup can copy a credential into another variable and unset its original source. These aliases need credential brokering without losing source restrictions or mistaking ordinary shell content for credentials. ## What changed - Discover supported GitHub and OpenAI credentials embedded in environment values, even without their canonical variables. Preserve source ownership so unbound enterprise tokens cannot acquire a default host binding. - Redact unregistered supported credentials during text virtualization, distinguish adjacent credentials, and avoid matching unrelated provider prefixes and common hashed paths. - Expose helpers for checking allowed credential sources and restoring known dummy credentials in trusted text for fail-open execution. - Render allowed credential aliases as snapshot exports and replace known credential values in shell state, preserving exported functions and credential-shaped function names. ## Testing Add regression coverage for copied aliases, enterprise host binding, source filtering, adjacent tokens, path false positives, and dummy restoration. Add a Bash snapshot test that preserves exported functions while replacing credentials in heredoc content. GitOrigin-RevId: f1c3d531405a96ac6bfa3882a11b74643fa6f123
This commit is contained in:
1 parent
45134c0463
commit
a548463b78
12 files changed
+1126
-53
No files matched your search
@@ -86,6 +86,260 @@ fn virtualize_child_env_replaces_supported_credentials() {
|
||||
let mut excluded_dummies = format!("{github_dummy}\n{openai_dummy}");
|
||||
assert!(!broker.virtualize_text(&mut excluded_dummies, &filtered_env));
|
||||
assert_eq!(excluded_dummies, format!("{github_dummy}\n"));
|
||||
let unknown_github_token = "ghp_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh";
|
||||
let unknown_openai_key = "sk-proj-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh";
|
||||
let unknown_legacy_openai_key = format!("sk-{}", "a".repeat(48));
|
||||
let mut unregistered = format!(
|
||||
"{unknown_github_token}\n{unknown_openai_key}\n{unknown_legacy_openai_key}\nghp_x sk-proj-x"
|
||||
);
|
||||
assert!(!broker.virtualize_text(&mut unregistered, &env));
|
||||
assert_eq!(unregistered, "\n\n\nghp_x sk-proj-x");
|
||||
for key in [
|
||||
"sk-ant-api03-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno",
|
||||
"sk-ant-oat01-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno",
|
||||
"sk-or-v1-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno",
|
||||
] {
|
||||
let mut unrelated_provider = key.to_string();
|
||||
assert!(broker.virtualize_text(&mut unrelated_provider, &env));
|
||||
assert_eq!(unrelated_provider, key);
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
key,
|
||||
key,
|
||||
&HashMap::new(),
|
||||
|_| true,
|
||||
));
|
||||
}
|
||||
let mut embedded_tokens = format!("v1{unknown_github_token}\n/opt/ta{unknown_openai_key}/bin");
|
||||
assert!(!broker.virtualize_text(&mut embedded_tokens, &env));
|
||||
assert_eq!(embedded_tokens, "v1\n/opt/ta/bin");
|
||||
let qualified_legacy = format!(
|
||||
"sk-{}-{}T3BlbkFJ{}",
|
||||
"a".repeat(20),
|
||||
"b".repeat(19),
|
||||
"c".repeat(20)
|
||||
);
|
||||
let legacy_broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let mut legacy_env = env_map([("OPENAI_API_KEY", qualified_legacy.as_str())]);
|
||||
legacy_broker.virtualize_child_env(&mut legacy_env);
|
||||
assert_credential_shape(&qualified_legacy, &legacy_env["OPENAI_API_KEY"], "sk-");
|
||||
let unmarked_legacy = format!("sk-{}-{}", "a".repeat(15), "b".repeat(35));
|
||||
let mut unmarked_legacy_alias = format!("Bearer {unmarked_legacy}");
|
||||
assert!(!broker.virtualize_text(&mut unmarked_legacy_alias, &env));
|
||||
assert_eq!(unmarked_legacy_alias, "Bearer ");
|
||||
let collision = format!(
|
||||
"sk-{}-sk-{}T3BlbkFJ{}",
|
||||
"a".repeat(20),
|
||||
"b".repeat(16),
|
||||
"c".repeat(20)
|
||||
);
|
||||
let mut collision_alias = format!("Bearer {collision}");
|
||||
assert!(!broker.virtualize_text(&mut collision_alias, &env));
|
||||
assert_eq!(collision_alias, "Bearer ");
|
||||
let unrelated_collision = format!("sk-ant-api03-{}-sk-{}", "a".repeat(40), "b".repeat(48));
|
||||
let mut redacted_collision = unrelated_collision.clone();
|
||||
assert!(!broker.virtualize_text(&mut redacted_collision, &env));
|
||||
assert_eq!(
|
||||
redacted_collision,
|
||||
format!("sk-ant-api03-{}-", "a".repeat(40))
|
||||
);
|
||||
assert!(credential_broker_provider_sources_allowed(
|
||||
&unrelated_collision,
|
||||
&redacted_collision,
|
||||
&HashMap::new(),
|
||||
|_| true,
|
||||
));
|
||||
let unrelated = "sk-ant-api03-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno";
|
||||
let mut known_provider_env = env.clone();
|
||||
known_provider_env.insert("ANTHROPIC_API_KEY".to_string(), unrelated.to_string());
|
||||
for separator in ["_", "__", "--", "-_", "_-", "", "_openai_", "_Bearer_"] {
|
||||
let mut known_adjacent = format!("{unrelated}{separator}{unknown_legacy_openai_key}");
|
||||
assert!(!broker.virtualize_text(&mut known_adjacent, &known_provider_env));
|
||||
assert_eq!(known_adjacent, format!("{unrelated}{separator}"));
|
||||
}
|
||||
let first_bundle = format!("{unrelated}_{unknown_legacy_openai_key}");
|
||||
known_provider_env.insert("FIRST_BUNDLE".to_string(), first_bundle.clone());
|
||||
let openrouter = "sk-or-v1-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno";
|
||||
for ignored in [unrelated, openrouter] {
|
||||
for separator in ["_", "-", "__", "--", "", "_openai_", "_Bearer_"] {
|
||||
let mut unknown_adjacent = format!("{ignored}{separator}{unknown_legacy_openai_key}");
|
||||
assert!(!broker.virtualize_text(&mut unknown_adjacent, &env));
|
||||
assert_eq!(unknown_adjacent, format!("{ignored}{separator}"));
|
||||
}
|
||||
}
|
||||
let mut nested_bundle = format!("{first_bundle}_{openrouter}");
|
||||
assert!(!broker.virtualize_text(&mut nested_bundle, &known_provider_env));
|
||||
assert_eq!(nested_bundle, format!("{unrelated}__{openrouter}"));
|
||||
let mixed_providers = format!("{unknown_github_token}_{unrelated_collision}");
|
||||
let mut virtualized_providers = mixed_providers.clone();
|
||||
assert!(!broker.virtualize_text(&mut virtualized_providers, &env));
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
&mixed_providers,
|
||||
&virtualized_providers,
|
||||
&HashMap::new(),
|
||||
|source| source != "OPENAI_API_KEY",
|
||||
));
|
||||
let mixed_credentials =
|
||||
format!("{unknown_github_token}_{unknown_legacy_openai_key}_{unrelated}");
|
||||
let mut virtualized_credentials = mixed_credentials.clone();
|
||||
assert!(!broker.virtualize_text(&mut virtualized_credentials, &env));
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
&mixed_credentials,
|
||||
&virtualized_credentials,
|
||||
&HashMap::new(),
|
||||
|source| source != "OPENAI_API_KEY",
|
||||
));
|
||||
let equivalent_sources = env_map([
|
||||
("GH_TOKEN", unknown_github_token),
|
||||
("GITHUB_TOKEN", unknown_github_token),
|
||||
]);
|
||||
assert!(credential_broker_provider_sources_allowed(
|
||||
unknown_github_token,
|
||||
"",
|
||||
&equivalent_sources,
|
||||
|source| source == "GH_TOKEN",
|
||||
));
|
||||
let distinct_github_token = "ghp_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGH";
|
||||
let distinct_sources = env_map([
|
||||
("GH_TOKEN", unknown_github_token),
|
||||
("GITHUB_TOKEN", distinct_github_token),
|
||||
]);
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
&format!("{unknown_github_token}\n{distinct_github_token}"),
|
||||
"",
|
||||
&distinct_sources,
|
||||
|source| source == "GH_TOKEN",
|
||||
));
|
||||
for unrelated in [
|
||||
unrelated,
|
||||
"sk-or-v1-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno",
|
||||
] {
|
||||
for separator in ['_', '-'] {
|
||||
for (mut adjacent, expected) in [
|
||||
(
|
||||
format!("{unrelated}{separator}{qualified_legacy}"),
|
||||
format!("{unrelated}{separator}"),
|
||||
),
|
||||
(
|
||||
format!("{unknown_legacy_openai_key}{separator}{unrelated}"),
|
||||
format!("{separator}{unrelated}"),
|
||||
),
|
||||
] {
|
||||
assert!(!broker.virtualize_text(&mut adjacent, &env));
|
||||
assert_eq!(adjacent, expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
let hash = "a".repeat(64);
|
||||
let copied_openai_key = format!("sk-proj-{hash}");
|
||||
let mut copied_credential = format!("ta{copied_openai_key}");
|
||||
assert!(!broker.virtualize_text(&mut copied_credential, &env));
|
||||
assert_eq!(copied_credential, "ta");
|
||||
let mut path_embedded_credentials =
|
||||
format!("/prefix/{unknown_legacy_openai_key}:/next\n/prefix/sk-proj-{hash}");
|
||||
assert!(!broker.virtualize_text(&mut path_embedded_credentials, &env));
|
||||
assert_eq!(path_embedded_credentials, "/prefix/:/next\n/prefix/");
|
||||
for separator in ['-', '_'] {
|
||||
let mut embedded_path = format!("/workspace/token{separator}sk-proj-{hash}/bin");
|
||||
assert!(!broker.virtualize_text(&mut embedded_path, &env));
|
||||
assert_eq!(embedded_path, format!("/workspace/token{separator}/bin"));
|
||||
}
|
||||
let mut adjacent_path = format!("/workspace/tokensk-proj-{hash}/bin");
|
||||
assert!(!broker.virtualize_text(&mut adjacent_path, &env));
|
||||
assert_eq!(adjacent_path, "/workspace/token/bin");
|
||||
let mut word_adjacent_path = format!("/workspace/datask-proj-{hash}/bin");
|
||||
assert!(!broker.virtualize_text(&mut word_adjacent_path, &env));
|
||||
assert_eq!(word_adjacent_path, "/workspace/data/bin");
|
||||
for prefix in ["a", "di", "ma", "ri", "bri"] {
|
||||
let mut embedded_credential = format!("Bearer {prefix}sk-{hash}");
|
||||
assert!(!broker.virtualize_text(&mut embedded_credential, &env));
|
||||
assert_eq!(embedded_credential, format!("Bearer {prefix}"));
|
||||
}
|
||||
for component in ["cafe\u{e9}task", "cafe\u{301}task"] {
|
||||
let mut unicode_adjacent_path = format!("/workspace/{component}-proj-{hash}/bin");
|
||||
assert!(!broker.virtualize_text(&mut unicode_adjacent_path, &env));
|
||||
assert_eq!(
|
||||
unicode_adjacent_path,
|
||||
format!("/workspace/{}/bin", component.strip_suffix("sk").unwrap())
|
||||
);
|
||||
}
|
||||
let mut hashed_credential_path =
|
||||
format!("/workspace/task-proj-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh-{hash}/bin");
|
||||
assert!(!broker.virtualize_text(&mut hashed_credential_path, &env));
|
||||
assert_eq!(hashed_credential_path, "/workspace/ta/bin");
|
||||
let mut watermarked_path = format!("/workspace/tokensk-proj-{hash}-T3BlbkFJsuffix/bin");
|
||||
assert!(!broker.virtualize_text(&mut watermarked_path, &env));
|
||||
assert_eq!(watermarked_path, "/workspace/token/bin");
|
||||
for suffix in ["build-", "", "proj-", "admin-", "svcacct-"] {
|
||||
let path_value =
|
||||
format!("/task-{suffix}{hash}:/task-{suffix}{hash}/x:/task-{suffix}{hash}");
|
||||
for path in [
|
||||
path_value,
|
||||
format!("declare -x PATH=\"/task-{suffix}{hash}-build:/task-{suffix}{hash}-release\""),
|
||||
format!("export -UT PATH path=(/task-{suffix}{hash} /usr/bin)"),
|
||||
format!("alias activate='source /task-{suffix}{hash}/bin/activate'"),
|
||||
format!(r"C:\task-{suffix}{hash}\Scripts"),
|
||||
] {
|
||||
let mut virtualized_path = path.clone();
|
||||
assert!(broker.virtualize_text(&mut virtualized_path, &env));
|
||||
assert_eq!(virtualized_path, path);
|
||||
}
|
||||
}
|
||||
for component in [
|
||||
"my_task",
|
||||
"flask",
|
||||
"disk",
|
||||
"mask",
|
||||
"risk",
|
||||
"brisk",
|
||||
"subtask",
|
||||
"mytask",
|
||||
"devtask",
|
||||
"multitask",
|
||||
"buildtask",
|
||||
"mydisk",
|
||||
"harddisk",
|
||||
"MY_Task",
|
||||
"Flask",
|
||||
"cafe\u{e9}_task",
|
||||
"cafe\u{301}_task",
|
||||
] {
|
||||
for suffix in ["", "proj-", "admin-", "svcacct-"] {
|
||||
for path in [
|
||||
format!("/workspace/{component}-{suffix}{hash}/bin"),
|
||||
format!("VIRTUAL_ENV=/workspace/{component}-{suffix}{hash}"),
|
||||
format!(
|
||||
"alias activate='source /workspace/{component}-{suffix}{hash}/bin/activate'"
|
||||
),
|
||||
format!(r"C:\\workspace\\{component}-{suffix}{hash}\\Scripts"),
|
||||
] {
|
||||
let mut virtualized_path = path.clone();
|
||||
assert!(broker.virtualize_text(&mut virtualized_path, &env));
|
||||
assert_eq!(virtualized_path, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
let registered_hex_credential = format!("sk-{hash}");
|
||||
let registered_broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let mut registered_env = env_map([("OPENAI_API_KEY", registered_hex_credential.as_str())]);
|
||||
registered_broker.virtualize_child_env(&mut registered_env);
|
||||
let registered_dummy = ®istered_env["OPENAI_API_KEY"];
|
||||
let mut credential_path = format!("/workspace/multita{registered_hex_credential}/bin");
|
||||
assert!(registered_broker.virtualize_text(&mut credential_path, ®istered_env));
|
||||
assert_eq!(
|
||||
credential_path,
|
||||
format!("/workspace/multita{registered_dummy}/bin")
|
||||
);
|
||||
for (key, placeholder, credential) in [
|
||||
("GH_TOKEN", "ghp_", unknown_github_token),
|
||||
("OPENAI_API_KEY", "sk-", unknown_legacy_openai_key.as_str()),
|
||||
] {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let mut env = env_map([(key, placeholder)]);
|
||||
broker.virtualize_child_env(&mut env);
|
||||
let mut alias = format!("Bearer {credential}");
|
||||
assert!(!broker.virtualize_text(&mut alias, &env));
|
||||
assert!(!alias.contains(credential));
|
||||
}
|
||||
let mut command = vec![
|
||||
format!("Authorization: Bearer {github_dummy}"),
|
||||
format!("Authorization: Bearer {openai_dummy}"),
|
||||
@@ -242,6 +496,45 @@ fn virtualize_child_env_preserves_live_dummy_mappings() {
|
||||
assert_eq!(distinct_credentials["GH_TOKEN"], secondary_dummy);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbound_enterprise_aliases_retain_source_ownership() {
|
||||
let token = "ghp_abcdefghijklmnopqrstuvwxyz0123456789";
|
||||
let real_header = format!("Bearer {token}");
|
||||
for source in ["GH_ENTERPRISE_TOKEN", "GITHUB_ENTERPRISE_TOKEN"] {
|
||||
for host in [None, Some("")] {
|
||||
for parent_discovery in [false, true] {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let mut parent = env_map([(source, token)]);
|
||||
if let Some(host) = host {
|
||||
parent.insert("GH_HOST".to_string(), host.to_string());
|
||||
}
|
||||
let mut child = env_map([("AUTH_HEADER", real_header.as_str())]);
|
||||
if parent_discovery {
|
||||
broker.discover_parent_credentials(&parent, &child);
|
||||
} else {
|
||||
broker.virtualize_child_env(&mut parent);
|
||||
}
|
||||
broker.virtualize_child_env(&mut child);
|
||||
assert_eq!(child["AUTH_HEADER"], real_header, "{source}, {host:?}");
|
||||
assert!(broker.read_state().credentials.is_empty());
|
||||
|
||||
// A later explicit source and destination can still register normally.
|
||||
child.insert(source.to_string(), token.to_string());
|
||||
child.insert("GH_HOST".to_string(), "enterprise.example".to_string());
|
||||
broker.virtualize_child_env(&mut child);
|
||||
let dummy = &child[source];
|
||||
assert_ne!(dummy, token);
|
||||
let mut headers = headers_with_bearer(dummy);
|
||||
let original = headers.clone();
|
||||
broker.inject_request_headers("api.github.com", &mut headers);
|
||||
assert_eq!(headers, original);
|
||||
broker.inject_request_headers("enterprise.example", &mut headers);
|
||||
assert_eq!(authorization(&headers), Some(real_header.as_str()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn virtualize_child_env_replaces_aliases_of_filtered_parent_credentials() {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
@@ -264,6 +557,10 @@ fn virtualize_child_env_replaces_aliases_of_filtered_parent_credentials() {
|
||||
assert_eq!(child_env["AUTH_HEADER"], format!("Bearer {dummy}"));
|
||||
assert!(!child_env.contains_key("GH_TOKEN"));
|
||||
|
||||
let mut virtualized_alias = child_env["AUTH_HEADER"].clone();
|
||||
assert!(broker.virtualize_text(&mut virtualized_alias, &child_env));
|
||||
assert_eq!(virtualized_alias, child_env["AUTH_HEADER"]);
|
||||
|
||||
let mut headers = headers_with_bearer(&dummy);
|
||||
broker.inject_request_headers("api.github.com", &mut headers);
|
||||
assert_eq!(authorization(&headers), Some(authorization_header.as_str()));
|
||||
@@ -272,6 +569,139 @@ fn virtualize_child_env_replaces_aliases_of_filtered_parent_credentials() {
|
||||
assert_eq!(child_env["HOMEBREW_GITHUB_API_TOKEN"], github_token);
|
||||
assert_eq!(child_env["AUTH_HEADER"], authorization_header);
|
||||
assert!(!child_env.contains_key("GH_TOKEN"));
|
||||
|
||||
let openai_token = "sk-proj-abcdefghijklmnopqrstuvwxyz1234567890";
|
||||
let mixed_bundle = format!("GitHub {github_token}\nOpenAI {openai_token}");
|
||||
let mut mixed_env = env_map([
|
||||
("GH_TOKEN", github_token),
|
||||
("OPENAI_API_KEY", openai_token),
|
||||
("AUTH_BUNDLE", mixed_bundle.as_str()),
|
||||
]);
|
||||
broker.virtualize_child_env(&mut mixed_env);
|
||||
let mut mixed_alias = mixed_env["AUTH_BUNDLE"].clone();
|
||||
let excluded_dummy = mixed_env.remove("OPENAI_API_KEY").expect("OpenAI dummy");
|
||||
assert!(!broker.virtualize_text(&mut mixed_alias, &mixed_env));
|
||||
assert!(!mixed_alias.contains(&excluded_dummy));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn virtualize_child_env_preserves_paths_unless_the_credential_is_known() {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let real = format!("sk-proj-{}", "a".repeat(64));
|
||||
let path = format!("/workspace/my_ta{real}/bin");
|
||||
let mut env = env_map([("VIRTUAL_ENV", &path)]);
|
||||
|
||||
broker.virtualize_child_env(&mut env);
|
||||
|
||||
assert_eq!(
|
||||
env,
|
||||
env_map([
|
||||
("VIRTUAL_ENV", &path),
|
||||
(CREDENTIAL_BROKER_ACTIVE_ENV_KEY, "1"),
|
||||
(BROKERED_CREDENTIALS_ENV_KEY, "[]"),
|
||||
])
|
||||
);
|
||||
|
||||
let copied = format!("ta{real}");
|
||||
let mut copied_env = env_map([("COPIED", &copied)]);
|
||||
broker.virtualize_child_env(&mut copied_env);
|
||||
let dummy = copied_env["COPIED"].strip_prefix("ta").unwrap();
|
||||
assert_credential_shape(&real, dummy, "sk-proj-");
|
||||
let mut headers = headers_with_bearer(dummy);
|
||||
broker.inject_request_headers("api.openai.com", &mut headers);
|
||||
assert_eq!(
|
||||
authorization(&headers),
|
||||
Some(format!("Bearer {real}").as_str())
|
||||
);
|
||||
|
||||
broker.virtualize_child_env(&mut env);
|
||||
assert_eq!(env["VIRTUAL_ENV"], path.replace(&real, dummy));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn virtualize_child_env_discovers_credentials_without_canonical_variables() {
|
||||
for (token, canonical_key, host) in [
|
||||
(
|
||||
"ghp_abcdefghijklmnopqrstuvwxyz1234567890",
|
||||
"GH_TOKEN",
|
||||
"api.github.com",
|
||||
),
|
||||
(
|
||||
"sk-proj-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
|
||||
"OPENAI_API_KEY",
|
||||
"api.openai.com",
|
||||
),
|
||||
] {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let authorization_header = format!("Bearer {token}");
|
||||
let mut env = env_map([("AUTH_HEADER", authorization_header.as_str())]);
|
||||
|
||||
broker.virtualize_child_env(&mut env);
|
||||
|
||||
let dummy_header = &env["AUTH_HEADER"];
|
||||
assert_ne!(dummy_header, &authorization_header);
|
||||
assert!(!env.contains_key(canonical_key));
|
||||
let mut headers = headers_with_authorization(dummy_header);
|
||||
broker.inject_request_headers(host, &mut headers);
|
||||
assert_eq!(authorization(&headers), Some(authorization_header.as_str()));
|
||||
|
||||
let mut snapshot = format!("export AUTH_HEADER='{authorization_header}'");
|
||||
assert!(broker.virtualize_text(&mut snapshot, &env));
|
||||
assert_eq!(snapshot, format!("export AUTH_HEADER='{dummy_header}'"));
|
||||
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let mut env = env_map([
|
||||
(canonical_key, "another-canonical-credential"),
|
||||
("AUTH_HEADER", authorization_header.as_str()),
|
||||
]);
|
||||
broker.virtualize_child_env(&mut env);
|
||||
assert_ne!(env["AUTH_HEADER"], authorization_header);
|
||||
let mut headers = headers_with_authorization(&env["AUTH_HEADER"]);
|
||||
broker.inject_request_headers(host, &mut headers);
|
||||
assert_eq!(authorization(&headers), Some(authorization_header.as_str()));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn virtualize_child_env_keeps_adjacent_provider_credentials_separate() {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let github_token = "ghp_abcdefghijklmnopqrstuvwxyz1234567890ABCD";
|
||||
let openai_token = "sk-proj-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
let real_bundle = format!("{github_token}_{openai_token}");
|
||||
let mut env = env_map([("AUTH_BUNDLE", real_bundle.as_str())]);
|
||||
|
||||
broker.virtualize_child_env(&mut env);
|
||||
|
||||
let dummy_bundle = env["AUTH_BUNDLE"].clone();
|
||||
assert!(!dummy_bundle.contains(github_token));
|
||||
assert!(!dummy_bundle.contains(openai_token));
|
||||
let github_dummy = &dummy_bundle[..github_token.len()];
|
||||
let openai_dummy = &dummy_bundle[github_token.len() + 1..];
|
||||
|
||||
let mut github_headers = headers_with_bearer(github_dummy);
|
||||
broker.inject_request_headers("api.github.com", &mut github_headers);
|
||||
assert_eq!(
|
||||
authorization(&github_headers),
|
||||
Some(format!("Bearer {github_token}").as_str())
|
||||
);
|
||||
|
||||
let mut openai_headers = headers_with_bearer(openai_dummy);
|
||||
broker.inject_request_headers("api.openai.com", &mut openai_headers);
|
||||
assert_eq!(
|
||||
authorization(&openai_headers),
|
||||
Some(format!("Bearer {openai_token}").as_str())
|
||||
);
|
||||
|
||||
let mut bundled_headers = headers_with_bearer(&dummy_bundle);
|
||||
broker.inject_request_headers("api.github.com", &mut bundled_headers);
|
||||
assert_eq!(
|
||||
authorization(&bundled_headers),
|
||||
Some(format!("Bearer {dummy_bundle}").as_str())
|
||||
);
|
||||
|
||||
let mut restored_bundle = dummy_bundle;
|
||||
assert!(broker.restore_text(&mut restored_bundle));
|
||||
assert_eq!(restored_bundle, real_bundle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -362,24 +792,80 @@ fn child_without_dummy_cannot_use_previous_child_credential() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn virtualize_child_env_preserves_unbound_enterprise_token() {
|
||||
fn virtualize_child_env_keeps_unbound_enterprise_token_out_of_persisted_text() {
|
||||
let broker = CredentialBroker::new(/*enabled*/ true);
|
||||
let mut env = env_map([("GH_ENTERPRISE_TOKEN", "ghp-enterprise-real")]);
|
||||
let token = "ghp_abcdefghijklmnopqrstuvwxyz1234567890";
|
||||
let authorization_header = format!("Bearer {token}");
|
||||
let mut env = env_map([
|
||||
("GH_ENTERPRISE_TOKEN", token),
|
||||
("AUTH_HEADER", authorization_header.as_str()),
|
||||
]);
|
||||
|
||||
broker.virtualize_child_env(&mut env);
|
||||
let inert_token = "ghp_abcdefghijklmnopqrstuvwxyz1234567890";
|
||||
let mut headers = headers_with_bearer(inert_token);
|
||||
assert_eq!(env["GH_ENTERPRISE_TOKEN"], token);
|
||||
assert_eq!(env["AUTH_HEADER"], authorization_header);
|
||||
for alias in [
|
||||
format!("export GH_ENTERPRISE_TOKEN={token}"),
|
||||
format!("export AUTH_HEADER='Bearer {token}_suffix'"),
|
||||
format!("export AUTH_HEADER='Bearer {token}-suffix'"),
|
||||
] {
|
||||
let mut persisted = alias;
|
||||
assert!(!broker.virtualize_text(&mut persisted, &env));
|
||||
assert!(!persisted.contains(token));
|
||||
}
|
||||
let distinct_token = "ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijkl";
|
||||
let mut adjacent = format!("{token}_{distinct_token}");
|
||||
assert!(!broker.virtualize_text(&mut adjacent, &env));
|
||||
assert_eq!(adjacent, "_");
|
||||
|
||||
let mut truncated_env = env_map([("GH_ENTERPRISE_TOKEN", "ghp_abcdefghijkl")]);
|
||||
broker.virtualize_child_env(&mut truncated_env);
|
||||
let mut hidden = token.to_string();
|
||||
assert!(!broker.virtualize_text(&mut hidden, &truncated_env));
|
||||
assert!(hidden.is_empty());
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
token,
|
||||
"",
|
||||
&truncated_env,
|
||||
|source| source != "GH_TOKEN",
|
||||
));
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
token,
|
||||
"",
|
||||
&HashMap::new(),
|
||||
|source| source != "GH_TOKEN",
|
||||
));
|
||||
|
||||
let fine_grained = "github_pat_11AA0bbCC_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGH";
|
||||
let mut truncated_env = env_map([(
|
||||
"GH_ENTERPRISE_TOKEN",
|
||||
"github_pat_11AA0bbCC_abcdefghijklmnopqrs",
|
||||
)]);
|
||||
broker.virtualize_child_env(&mut truncated_env);
|
||||
let mut hidden = fine_grained.to_string();
|
||||
assert!(!broker.virtualize_text(&mut hidden, &truncated_env));
|
||||
assert!(hidden.is_empty());
|
||||
assert!(!credential_broker_provider_sources_allowed(
|
||||
fine_grained,
|
||||
"",
|
||||
&env_map([("GH_TOKEN", "github_pat_11AA0bbCC")]),
|
||||
|source| source == "GH_TOKEN",
|
||||
));
|
||||
let mut headers = headers_with_bearer(token);
|
||||
broker.inject_request_headers("attacker.example", &mut headers);
|
||||
|
||||
assert_eq!(env["GH_ENTERPRISE_TOKEN"], "ghp-enterprise-real");
|
||||
assert_eq!(headers, headers_with_bearer(inert_token));
|
||||
assert_eq!(env["GH_ENTERPRISE_TOKEN"], token);
|
||||
assert_eq!(headers, headers_with_bearer(token));
|
||||
assert!(!broker.host_requires_mitm("attacker.example"));
|
||||
|
||||
env.insert("GH_HOST".to_string(), "github.example.com".to_string());
|
||||
broker.virtualize_child_env(&mut env);
|
||||
let mut headers = headers_with_bearer(&env["GH_ENTERPRISE_TOKEN"]);
|
||||
broker.inject_request_headers("github.example.com", &mut headers);
|
||||
assert_eq!(authorization(&headers), Some("Bearer ghp-enterprise-real"));
|
||||
assert_eq!(
|
||||
authorization(&headers),
|
||||
Some(format!("Bearer {token}").as_str())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user