@@ -3069,13 +3069,19 @@ async fn build_sandbox_config_snapshot_with_sources(
30693069 &policy_credential_bindings,
30703070 )?;
30713071 }
3072+ let environment_overrides = super::provider::non_secret_provider_environment_overrides(
3073+ provider_profile_catalog,
3074+ provider_records,
3075+ sandbox,
3076+ );
30723077 let provider_env_revision = compute_provider_env_revision_from_records_and_policy_bindings(
30733078 provider_profile_catalog,
30743079 provider_records,
30753080 &policy_credential_bindings,
30763081 policy
30773082 .as_ref()
30783083 .filter(|_| matches!(policy_source, PolicySource::Global)),
3084+ &environment_overrides,
30793085 )?;
30803086
30813087 let service_routes = match service_routes {
@@ -3378,6 +3384,7 @@ fn compute_provider_env_revision_from_records(
33783384 records,
33793385 &HashMap::new(),
33803386 None,
3387+ &BTreeMap::new(),
33813388 )
33823389}
33833390
@@ -3386,6 +3393,7 @@ fn compute_provider_env_revision_from_records_and_policy_bindings(
33863393 records: &[super::provider::ProviderEnvironmentRecord],
33873394 policy_bindings: &HashMap<String, Vec<StaticCredentialEndpointBinding>>,
33883395 global_policy: Option<&ProtoSandboxPolicy>,
3396+ environment_overrides: &BTreeMap<String, String>,
33893397) -> Result<u64, Status> {
33903398 let mut hasher = Sha256::new();
33913399 hasher.update(b"openshell-provider-env-revision-v4");
@@ -3429,6 +3437,18 @@ fn compute_provider_env_revision_from_records_and_policy_bindings(
34293437 hasher.update(deterministic_policy_hash(policy).as_bytes());
34303438 }
34313439
3440+ if !environment_overrides.is_empty() {
3441+ // Hash the same effective values that the environment snapshot installs.
3442+ // Length prefixes preserve boundaries, including explicitly empty values.
3443+ hasher.update(b"caller-non-secret-environment");
3444+ for (key, value) in environment_overrides {
3445+ hasher.update((key.len() as u64).to_le_bytes());
3446+ hasher.update(key.as_bytes());
3447+ hasher.update((value.len() as u64).to_le_bytes());
3448+ hasher.update(value.as_bytes());
3449+ }
3450+ }
3451+
34323452 let digest = hasher.finalize();
34333453 Ok(u64::from_le_bytes(digest[..8].try_into().map_err(
34343454 |_| Status::internal("provider env revision digest too short"),
@@ -3895,6 +3915,7 @@ pub async fn build_provider_environment_snapshot(
38953915/// is resolved.
38963916struct ProviderEnvironmentTarget<'a> {
38973917 spec: &'a openshell_core::proto::SandboxSpec,
3918+ environment_overrides: BTreeMap<String, String>,
38983919 effective_policy: ProtoSandboxPolicy,
38993920 policy_source: PolicySource,
39003921 policy_credential_bindings: HashMap<String, Vec<StaticCredentialEndpointBinding>>,
@@ -3926,14 +3947,21 @@ fn provider_environment_target(
39263947 &effective_policy,
39273948 &policy_credential_bindings,
39283949 )?;
3950+ let environment_overrides = super::provider::non_secret_provider_environment_overrides(
3951+ provider_profile_catalog,
3952+ provider_records,
3953+ sandbox,
3954+ );
39293955 let provider_env_revision = compute_provider_env_revision_from_records_and_policy_bindings(
39303956 provider_profile_catalog,
39313957 provider_records,
39323958 &policy_credential_bindings,
39333959 matches!(policy_source, PolicySource::Global).then_some(&effective_policy),
3960+ &environment_overrides,
39343961 )?;
39353962 Ok(ProviderEnvironmentTarget {
39363963 spec,
3964+ environment_overrides,
39373965 effective_policy,
39383966 policy_source,
39393967 policy_credential_bindings,
@@ -3952,6 +3980,7 @@ pub async fn build_provider_environment_snapshot_from_inputs(
39523980 let sandbox_id = inputs.sandbox.object_id().to_string();
39533981 let ProviderEnvironmentTarget {
39543982 spec,
3983+ environment_overrides,
39553984 effective_policy,
39563985 policy_source,
39573986 policy_credential_bindings,
@@ -3989,20 +4018,12 @@ pub async fn build_provider_environment_snapshot_from_inputs(
39894018 credential.token_grant_owners.clone_from(&owners);
39904019 }
39914020 }
3992- // Sandbox template and spec values are caller-owned. Drop only non-secret
3993- // profile defaults for those keys before sending the snapshot to either
3994- // the canonical process or later execs. Credential keys keep their
3995- // existing binding and placeholder precedence.
3996- let caller_has_key = |key: &str| {
3997- spec.environment.contains_key(key)
3998- || spec
3999- .template
4000- .as_ref()
4001- .is_some_and(|template| template.environment.contains_key(key))
4002- };
4003- provider_environment.environment.retain(|key, _| {
4004- provider_environment.static_credential_keys.contains(key) || !caller_has_key(key)
4005- });
4021+ // Retain effective non-secret values for both process environments and
4022+ // supervisor consumers such as metadata. Credential keys are excluded
4023+ // from overrides and keep their binding and placeholder precedence.
4024+ provider_environment
4025+ .environment
4026+ .extend(environment_overrides);
40064027
40074028 let mut readiness_reason = provider_environment.readiness_reason;
40084029
@@ -14871,6 +14892,10 @@ mod tests {
1487114892 .environment
1487214893 .config
1487314894 .insert("CUSTOM_PROJECT".to_string(), "project".to_string());
14895+ profile
14896+ .environment
14897+ .config
14898+ .insert("CUSTOM_MISSING".to_string(), "missing".to_string());
1487414899 profile
1487514900 .environment
1487614901 .fixed
@@ -14903,17 +14928,39 @@ mod tests {
1490314928 .insert("CUSTOM_PROJECT".to_string(), "caller-project".to_string());
1490414929 spec.environment
1490514930 .insert("GITHUB_TOKEN".to_string(), "caller-token".to_string());
14906- spec.template
14907- .get_or_insert_with(Default::default)
14908- .environment
14909- .insert("CUSTOM_MODE".to_string(), String::new());
14931+ spec.environment
14932+ .insert("CUSTOM_MISSING".to_string(), " caller value ".to_string());
14933+ spec.environment
14934+ .insert("UNRELATED".to_string(), "caller-only".to_string());
14935+ let template = spec.template.get_or_insert_with(Default::default);
14936+ template.environment = HashMap::from([
14937+ ("CUSTOM_PROJECT".to_string(), "template-project".to_string()),
14938+ ("CUSTOM_MODE".to_string(), String::new()),
14939+ ]);
1491014940 state.store.put_message(&sandbox).await.unwrap();
1491114941
1491214942 let snapshot = load_sandbox_provider_environment(&state, &sandbox, true, true)
1491314943 .await
1491414944 .unwrap();
14915- assert!(!snapshot.environment.contains_key("CUSTOM_PROJECT"));
14916- assert!(!snapshot.environment.contains_key("CUSTOM_MODE"));
14945+ assert_eq!(
14946+ snapshot
14947+ .environment
14948+ .get("CUSTOM_PROJECT")
14949+ .map(String::as_str),
14950+ Some("caller-project")
14951+ );
14952+ assert_eq!(
14953+ snapshot.environment.get("CUSTOM_MODE").map(String::as_str),
14954+ Some("")
14955+ );
14956+ assert_eq!(
14957+ snapshot
14958+ .environment
14959+ .get("CUSTOM_MISSING")
14960+ .map(String::as_str),
14961+ Some(" caller value ")
14962+ );
14963+ assert!(!snapshot.environment.contains_key("UNRELATED"));
1491714964 assert_eq!(
1491814965 snapshot.environment.get("CUSTOM_UNSET").map(String::as_str),
1491914966 Some("default")
@@ -14927,7 +14974,129 @@ mod tests {
1492714974 .static_credential_bindings
1492814975 .contains_key("GITHUB_TOKEN")
1492914976 );
14930- assert_eq!(snapshot.non_secret_environment_keys, ["CUSTOM_UNSET"]);
14977+ assert_eq!(
14978+ snapshot.non_secret_environment_keys,
14979+ [
14980+ "CUSTOM_MISSING",
14981+ "CUSTOM_MODE",
14982+ "CUSTOM_PROJECT",
14983+ "CUSTOM_UNSET"
14984+ ]
14985+ );
14986+ }
14987+
14988+ #[tokio::test]
14989+ async fn caller_environment_revision_tracks_effective_non_secret_values() {
14990+ let state = test_server_state().await;
14991+ let mut provider = test_provider("work-gcp", "google-cloud");
14992+ provider.credentials.clear();
14993+ state.store.put_message(&provider).await.unwrap();
14994+ let sandbox = test_sandbox(
14995+ "sb-caller-revision",
14996+ "caller-revision",
14997+ ProtoSandboxPolicy::default(),
14998+ vec!["work-gcp".to_string()],
14999+ );
15000+ let mut inputs = load_sandbox_config_inputs(&state, sandbox).await.unwrap();
15001+ let mut revisions = Vec::new();
15002+ for (spec_value, template_value, unrelated, expected) in [
15003+ (None, None, "one", None),
15004+ (
15005+ None,
15006+ Some("template-project"),
15007+ "one",
15008+ Some("template-project"),
15009+ ),
15010+ (
15011+ Some("caller-project"),
15012+ Some("template-project"),
15013+ "one",
15014+ Some("caller-project"),
15015+ ),
15016+ (
15017+ Some("caller-project"),
15018+ Some("shadowed-project"),
15019+ "two",
15020+ Some("caller-project"),
15021+ ),
15022+ (Some(""), Some("template-project"), "one", Some("")),
15023+ ] {
15024+ let spec = inputs.sandbox.spec.as_mut().unwrap();
15025+ spec.environment = HashMap::from([("UNRELATED".to_string(), unrelated.to_string())]);
15026+ if let Some(value) = spec_value {
15027+ spec.environment
15028+ .insert("GCP_PROJECT_ID".to_string(), value.to_string());
15029+ }
15030+ spec.template
15031+ .get_or_insert_with(Default::default)
15032+ .environment = template_value
15033+ .map(|value| HashMap::from([("GCP_PROJECT_ID".to_string(), value.to_string())]))
15034+ .unwrap_or_default();
15035+ let config = build_sandbox_config_snapshot_from_inputs(&state, &inputs)
15036+ .await
15037+ .unwrap();
15038+ let snapshot =
15039+ build_provider_environment_snapshot_from_inputs(&state, &inputs, true, true)
15040+ .await
15041+ .unwrap();
15042+ assert_eq!(config.provider_env_revision, snapshot.provider_env_revision);
15043+ let value = snapshot
15044+ .values
15045+ .iter()
15046+ .find(|value| value.name == "GCP_PROJECT_ID");
15047+ assert_eq!(value.map(|value| value.value.as_str()), expected);
15048+ if let Some(value) = value {
15049+ assert_eq!(
15050+ value.classification,
15051+ ProviderEnvironmentValueClassification::NonSecret as i32
15052+ );
15053+ }
15054+ revisions.push(snapshot.provider_env_revision);
15055+ }
15056+ assert_ne!(revisions[0], revisions[1]);
15057+ assert_ne!(revisions[1], revisions[2]);
15058+ assert_eq!(revisions[2], revisions[3]);
15059+ assert_ne!(revisions[2], revisions[4]);
15060+ assert_ne!(revisions[0], revisions[4]);
15061+ }
15062+
15063+ #[tokio::test]
15064+ async fn caller_environment_does_not_reclassify_expired_credentials_as_non_secret() {
15065+ let state = test_server_state().await;
15066+ let mut provider = test_provider("work-gcp", "google-cloud");
15067+ provider.credentials.clear();
15068+ state.store.put_message(&provider).await.unwrap();
15069+ let mut legacy_provider = test_provider("legacy-project", "legacy-custom");
15070+ legacy_provider.credentials =
15071+ HashMap::from([("GCP_PROJECT_ID".to_string(), "expired-secret".to_string())]);
15072+ legacy_provider.credential_expiration_times.insert(
15073+ "GCP_PROJECT_ID".to_string(),
15074+ openshell_core::time::timestamp_from_millis(1).unwrap(),
15075+ );
15076+ state.store.put_message(&legacy_provider).await.unwrap();
15077+ let mut sandbox = test_sandbox(
15078+ "sb-caller-expired",
15079+ "caller-expired",
15080+ ProtoSandboxPolicy::default(),
15081+ vec!["work-gcp".to_string(), "legacy-project".to_string()],
15082+ );
15083+ sandbox
15084+ .spec
15085+ .as_mut()
15086+ .unwrap()
15087+ .environment
15088+ .insert("GCP_PROJECT_ID".to_string(), "caller-project".to_string());
15089+
15090+ let snapshot = load_sandbox_provider_environment(&state, &sandbox, true, true)
15091+ .await
15092+ .unwrap();
15093+ assert!(!snapshot.environment.contains_key("GCP_PROJECT_ID"));
15094+ assert!(
15095+ !snapshot
15096+ .non_secret_environment_keys
15097+ .iter()
15098+ .any(|key| key == "GCP_PROJECT_ID")
15099+ );
1493115100 }
1493215101
1493315102 #[tokio::test]
0 commit comments