Honor shell environment policy in legacy snapshots (#48099)

## Why

Legacy shell snapshots could retain exports excluded by `shell_environment_policy` and restore captured values over explicit overrides when replayed.

## What changed

- Build snapshots with the resolved environment's policy and omit filtered exports and original values for variables overridden by `set`.
- Skip eager snapshot creation when configuration is unavailable, and reject snapshots whose policy differs from the current turn's policy.

## Testing

Add a regression test covering default and custom exclusions, `include_only`, preservation of multiline values, and `set` overrides that survive snapshot replay. Verify excluded and overridden captured values are absent from the snapshot file.

GitOrigin-RevId: 4b785130f6c5a4d268ab86fd36ab9d77d8c4a3b9
This commit is contained in:
jif
2026-09-25 14:15:05 +00:00
committed by copyberry
parent b35a7afbe8
commit b63a296775
7 changed files with 180 additions and 14 deletions
@@ -1,7 +1,9 @@
//! Serialize prepared snapshot values without inspecting credentials or executing shell code.
//! Serialize prepared snapshot values and apply export-name policy without executing shell code.
//! Native declarations stay unchanged unless the credential stage supplies a replacement.
use super::capture::CapturedSnapshot;
use codex_protocol::config_types::ShellEnvironmentPolicy;
use codex_protocol::shell_environment::create_env_from_vars;
impl CapturedSnapshot<'_> {
/// Render native functions, options, and aliases; the executor restores environment separately.
@@ -9,12 +11,22 @@ impl CapturedSnapshot<'_> {
format!("{}{}", self.state, self.aliases)
}
/// Render a complete replay script without applying credential or environment policy.
pub fn render_script(&self) -> String {
/// Persist only exports admitted by the policy. Explicit overrides are supplied
/// by the command environment; never retain their original captured values.
pub fn render_script(&self, policy: &ShellEnvironmentPolicy) -> String {
let allowed = create_env_from_vars(
self.exports
.iter()
.map(|export| (export.key.to_string(), String::new())),
policy,
/*thread_id*/ None,
);
let mut script = self.render_state();
script.push_str("# exports (native declarations)\n");
for export in &self.exports {
script.push_str(&export.source);
if allowed.contains_key(export.key) && !policy.r#set.contains_key(export.key) {
script.push_str(&export.source);
}
}
script
}
@@ -2231,7 +2231,7 @@ fn rewrite_snapshot_credentials(
fn captured_script(shell_type: ShellType, source: &str) -> Result<String> {
let captured =
CapturedSnapshot::parse(shell_type, source.as_bytes()).context("invalid native capture")?;
Ok(captured.render_script())
Ok(captured.render_script(&codex_protocol::config_types::ShellEnvironmentPolicy::default()))
}
#[test]