diff --git a/architecture/build.md b/architecture/build.md index 85d407d54e..cb60838630 100644 --- a/architecture/build.md +++ b/architecture/build.md @@ -88,9 +88,8 @@ SONAMEs. The workload-side `openshell-sandbox` binary is statically linked with musl so drivers can stage it into an arbitrary agent image without depending on that -image's libc. The supervisor is also statically linked: release images use the -default musl build, while distribution-specific builds may select the -`glibc-static` variant. +image's libc. The separate `openshell-supervisor` binary is dynamically linked +with GNU libc and uses the same glibc 2.28 compatibility floor as the gateway. ## Container Builds @@ -115,11 +114,8 @@ package-managed VM support does not raise the package runtime requirement. Gateway staging and release workflows set up the Zig C/C++ wrapper before bundled Z3 builds and verify the maximum referenced `GLIBC_*` symbol version before publishing or copying artifacts. -Supervisor binaries are static in every configuration. The default `musl` -variant uses `cargo zigbuild` when available, including native CPU -architectures, so C dependencies are compiled for the musl target instead of the -host GNU libc target. The `glibc-static` variant uses plain `cargo build` with -`+crt-static` and requires a native per-architecture build. Local Docker image tasks infer the +Supervisor staging uses the GNU build path and verifies the glibc 2.28 floor. +Sandbox staging uses the static musl build path. Local Docker image tasks infer the target architecture from `DOCKER_PLATFORM` when set. Otherwise, they require valid container engine host metadata and fail when the engine query is unavailable or reports an unsupported architecture, avoiding host-kernel @@ -184,9 +180,16 @@ Runtime layout: - **Sandbox**: Alpine-based `openshell/sandbox` image containing the static musl `/openshell-sandbox` binary and its static VM guest-init helper. Drivers stage this binary into the workload trust domain. -- **Supervisor**: Debian-based `openshell/supervisor` image containing only the - dynamically linked GNU `/openshell-supervisor` binary. GNU supervisor builds - must not reference `GLIBC_*` symbols newer than `GLIBC_2.28`. +- **Supervisor**: digest-pinned `gcr.io/distroless/base-nossl-debian13` base + with the dynamically linked GNU `/openshell-supervisor` binary. The base + supplies glibc and CA roots without a shell, package manager, OpenSSL or zlib. + GNU supervisor builds must not reference `GLIBC_*` symbols newer than + `GLIBC_2.28`. Image defaults remain UID 0 and working directory `/`; compute + drivers set the runtime identity and writable mounts. Docker stages private + files with the same numeric identity as the supervisor so archive uploads + preserve access regardless of the base image's default user. Health probes execute + the supervisor binary directly. Base updates require refreshing the + multi-architecture digest and rebuilding the image. Gateway image builds bake the corresponding supervisor image tag into the gateway binary so Docker sandboxes do not depend on `:latest` by default. diff --git a/architecture/sandbox.md b/architecture/sandbox.md index 60b8aa245e..6d828752f6 100644 --- a/architecture/sandbox.md +++ b/architecture/sandbox.md @@ -381,6 +381,8 @@ enhancement and out of scope.) Workload proxy variables are removed from the protected launch environment; transparent socket mediation does not depend on them. +The canonical main process receives the declared workload environment before +supervisor-only values are stripped and provider placeholders are injected. Template environment is treated like user-provided sandbox environment. It can shape the workload child, but it cannot override driver-controlled identity, gateway callback, TLS, relay socket, proxy, provider, or supervisor coordination diff --git a/architecture/security-policy.md b/architecture/security-policy.md index e669ea2fd5..5507f42709 100644 --- a/architecture/security-policy.md +++ b/architecture/security-policy.md @@ -89,9 +89,7 @@ proxy. | `foo.**.example.com` | No | — | Recursive wildcard outside the first label is not allowed. | | `foo**.example.com` | No | — | Recursive `**` mixed inside a label; allowed only as the entire first label. | -Validation rejects the disallowed patterns at policy load time with a message -that names the offending host. Exact hosts and IP addresses do not use this -path. +Validation rejects the disallowed patterns at policy load time. OPA load errors omit the supplied hostname. Exact hosts and IP addresses do not use this wildcard-validation path. ## TLS and L7 Inspection @@ -201,6 +199,14 @@ configure the separately isolated supervisor. When a supervisor override is combined with injected provider credentials, the supervisor emits a high-severity detection finding at startup naming the inactive controls. +## Policy Load Diagnostics + +`OpaEngine` bounds the error messages returned when loading or reloading policy from files, strings, or protobuf. Each message contains at most eight error items and 512 UTF-8 bytes, including its heading, separators, and any `additional violations omitted` marker. The loader reports complete, fixed categories and discards authored names, values, paths, source snippets, and nested error chains. + +Typed validation categories distinguish process identity, filesystem paths and limits, Landlock compatibility, endpoint hosts and ports, credential signing and rewriting, MCP configuration, and middleware configuration. Opaque L7 errors identify the protocol-configuration or policy-validation stage; endpoint conflicts report ambiguous selectors. YAML errors retain a fixed parser category and numeric line and column when available. File I/O, Rego loading, and internal policy-data errors use fixed messages. + +A candidate rejected during validation does not replace the active engine or advance its generation. The supervisor separately applies `policy_validation_failure_mode` and may publish a quarantine generation as described below. The diagnostic bounds cover returned OPA load errors; accepted-policy warnings, runtime request diagnostics, and gateway-authored policy parser messages have separate reporting contracts. + ## Live Updates The gateway stores sandbox-authored policy revisions separately from derived diff --git a/crates/openshell-driver-docker/src/lib.rs b/crates/openshell-driver-docker/src/lib.rs index 89d77b3d9d..1bdfac927f 100644 --- a/crates/openshell-driver-docker/src/lib.rs +++ b/crates/openshell-driver-docker/src/lib.rs @@ -4723,6 +4723,10 @@ async fn stage_docker_supervisor_bundle( ), ContainerCreateBody { image: Some(config.supervisor_image_id.clone()), + // Docker's copyUIDGID upload uses Config.User when it is set, + // overriding the archive's numeric ownership. Match the reader + // explicitly instead of inheriting the staging image's user. + user: Some(format!("{SUPERVISOR_UID}:{SUPERVISOR_GID}")), entrypoint: Some(vec![SUPERVISOR_IMAGE_CONTROL_BINARY_PATH.to_string()]), labels: Some(docker_auxiliary_container_labels( sandbox, diff --git a/crates/openshell-sandbox/src/process.rs b/crates/openshell-sandbox/src/process.rs index 5e07337214..56d4f244ad 100644 --- a/crates/openshell-sandbox/src/process.rs +++ b/crates/openshell-sandbox/src/process.rs @@ -247,6 +247,7 @@ fn apply_canonical_process_environment( interactive: bool, user_environment: &HashMap, ) { + cmd.envs(user_environment); let (session_user, session_home) = session_user_and_home(policy, workspace.home()); // Resolve a shell present in the sandbox image (minimal images such as // Alpine ship only `/bin/sh`, not bash). Runs in the supervisor. @@ -562,9 +563,6 @@ impl ProcessHandle { // inherited environment. The entrypoint drops to the sandbox user // before `exec`; without this strip, sandbox code could recover // supervisor credentials from its inherited environment. - strip_supervisor_only_env(&mut cmd); - - inject_provider_env(&mut cmd, provider_env); apply_canonical_process_environment( &mut cmd, policy, @@ -572,6 +570,9 @@ impl ProcessHandle { interactive, &configured_user_environment(), ); + strip_supervisor_only_env(&mut cmd); + + inject_provider_env(&mut cmd, provider_env); if let Some(dir) = workspace.root() { cmd.current_dir(dir); @@ -727,9 +728,6 @@ impl ProcessHandle { // Strip supervisor-only identity material from the entrypoint's // inherited environment. - strip_supervisor_only_env(&mut cmd); - - inject_provider_env(&mut cmd, provider_env); apply_canonical_process_environment( &mut cmd, policy, @@ -737,6 +735,9 @@ impl ProcessHandle { interactive, &configured_user_environment(), ); + strip_supervisor_only_env(&mut cmd); + + inject_provider_env(&mut cmd, provider_env); if let Some(dir) = workspace.root() { cmd.current_dir(dir); @@ -2241,6 +2242,58 @@ mod tests { assert_eq!(variables.get("TERM"), Some(&"xterm-256color")); } + #[cfg(unix)] + #[tokio::test] + async fn canonical_process_receives_declared_environment_and_home() { + let current_user = User::from_uid(nix::unistd::geteuid()).unwrap().unwrap(); + let policy = policy_with_process(ProcessPolicy { + run_as_user: Some(current_user.name), + run_as_group: None, + }); + for interactive in [false, true] { + let mut cmd = Command::new("/usr/bin/env"); + cmd.env_clear().stdout(StdStdio::piped()); + let declared = HashMap::from([ + ("APPLICATION_AGENT".into(), "researcher".into()), + ( + openshell_core::sandbox_env::SANDBOX_TOKEN.into(), + "must-not-reach-child".into(), + ), + ("ANTHROPIC_API_KEY".into(), "caller-value".into()), + ("HOME".into(), "/sandbox".into()), + ]); + apply_canonical_process_environment( + &mut cmd, + &policy, + &ResolvedWorkspace::default(), + interactive, + &declared, + ); + strip_supervisor_only_env(&mut cmd); + inject_provider_env( + &mut cmd, + &HashMap::from([( + "ANTHROPIC_API_KEY".into(), + "openshell:resolve:env:ANTHROPIC_API_KEY".into(), + )]), + ); + let output = cmd.output().await.expect("run environment probe"); + assert!(output.status.success()); + let environment = String::from_utf8(output.stdout).unwrap(); + let variables: HashMap<_, _> = environment + .lines() + .filter_map(|line| line.split_once('=')) + .collect(); + assert_eq!(variables.get("APPLICATION_AGENT"), Some(&"researcher")); + assert!(!variables.contains_key(openshell_core::sandbox_env::SANDBOX_TOKEN)); + assert_eq!( + variables.get("ANTHROPIC_API_KEY"), + Some(&"openshell:resolve:env:ANTHROPIC_API_KEY") + ); + assert_eq!(variables.get("HOME"), Some(&"/sandbox")); + } + } + /// Unknown names may yield `Ok(None)` (`… not found …`) or `Err` when NSS fails first /// (e.g. `ENOENT: No such file or directory`). fn assert_unknown_identity_lookup_failed(msg: &str) { diff --git a/crates/openshell-supervisor-network/src/opa.rs b/crates/openshell-supervisor-network/src/opa.rs index 34fc81a6e2..ccb9e76d2d 100644 --- a/crates/openshell-supervisor-network/src/opa.rs +++ b/crates/openshell-supervisor-network/src/opa.rs @@ -15,7 +15,7 @@ use openshell_core::policy::{ }; use openshell_core::policy_identity::deterministic_policy_hash; use openshell_core::proto::SandboxPolicy as ProtoSandboxPolicy; -use openshell_policy::{L7ConfigStanza, L7Protocol as PolicyL7Protocol}; +use openshell_policy::{L7ConfigStanza, L7Protocol as PolicyL7Protocol, PolicyViolation}; use openshell_supervisor_middleware::{ChainEntry, ChainRunner, MiddlewareRegistry}; use std::path::{Path, PathBuf}; use std::sync::{ @@ -30,6 +30,11 @@ use tracing::info; /// passthroughs. They reference `data.sandbox.*` for policy data. const BAKED_POLICY_RULES: &str = include_str!("../data/sandbox-policy.rego"); +/// Maximum number of fixed categories in one policy-load error. +const POLICY_VALIDATION_DIAGNOSTIC_MAX_ITEMS: usize = 8; +/// Maximum byte length of one policy-load error, including its omission marker. +const POLICY_VALIDATION_DIAGNOSTIC_MAX_BYTES: usize = 512; + /// Implementation-owned middleware config validation supplied by the active /// in-process catalog for local policy files. pub type MiddlewareConfigValidator = @@ -341,13 +346,14 @@ impl OpaEngine { require_binary_identity: bool, validate_middleware_config: Option<&MiddlewareConfigValidator>, ) -> Result { - let yaml_str = std::fs::read_to_string(data_path).map_err(|e| { - miette::miette!("failed to read YAML data from {}: {e}", data_path.display()) - })?; + // File paths and parser errors can contain policy contents. Discard the + // original error, including its source chain, at the load boundary. + let yaml_str = std::fs::read_to_string(data_path) + .map_err(|_| miette::miette!("failed to read YAML policy data file"))?; let mut engine = regorus::Engine::new(); engine .add_policy_from_file(policy_path) - .map_err(|e| miette::miette!("{e}"))?; + .map_err(|_| miette::miette!("failed to load Rego policy"))?; emit_binary_identity_mode(require_binary_identity, "files"); let data_json = preprocess_yaml_data( &yaml_str, @@ -356,7 +362,7 @@ impl OpaEngine { )?; engine .add_data_json(&data_json) - .map_err(|e| miette::miette!("{e}"))?; + .map_err(|_| miette::miette!("failed to load OPA policy data"))?; Ok(Self::with_engine(engine, require_binary_identity)) } @@ -367,6 +373,7 @@ impl OpaEngine { Self::from_strings_with_options(policy, data_yaml, true, None) } + /// Load policy strings and validate middleware config through the supplied catalog. pub fn from_strings_with_middleware_config( policy: &str, data_yaml: &str, @@ -393,7 +400,7 @@ impl OpaEngine { let mut engine = regorus::Engine::new(); engine .add_policy("policy.rego".into(), policy.into()) - .map_err(|e| miette::miette!("{e}"))?; + .map_err(|_| miette::miette!("failed to load Rego policy"))?; emit_binary_identity_mode(require_binary_identity, "strings"); let data_json = preprocess_yaml_data( data_yaml, @@ -402,7 +409,7 @@ impl OpaEngine { )?; engine .add_data_json(&data_json) - .map_err(|e| miette::miette!("{e}"))?; + .map_err(|_| miette::miette!("failed to load OPA policy data"))?; Ok(Self::with_engine(engine, require_binary_identity)) } @@ -438,25 +445,22 @@ impl OpaEngine { // the policy so both representations select the pinned default. let proto = openshell_policy::validate_and_canonicalize_sandbox_policy(proto.clone()) .map_err(|error| { - let errors = error - .violations() - .iter() - .map(ToString::to_string) - .collect::>() - .join("\n"); - miette::miette!("policy validation failed:\n{errors}") + miette::miette!(render_bounded_validation_diagnostics( + "policy validation failed", + error + .violations() + .iter() + .map(redacted_policy_violation_category), + )) })?; let ambiguities = openshell_policy::find_endpoint_ambiguities(&proto); if !ambiguities.is_empty() { - return Err(miette::miette!( - "network endpoint ambiguity validation failed:\n{}", - ambiguities - .iter() - .map(ToString::to_string) - .collect::>() - .join("\n") - )); + return Err(miette::miette!(render_repeated_validation_diagnostic( + "network endpoint ambiguity validation failed", + ambiguities.len(), + "ambiguous network endpoint selectors", + ))); } emit_binary_identity_mode(require_binary_identity, "proto"); @@ -464,19 +468,21 @@ impl OpaEngine { // Parse back to Value for preprocessing, then re-serialize let mut data: serde_json::Value = serde_json::from_str(&data_json_str) - .map_err(|e| miette::miette!("internal: failed to parse proto JSON: {e}"))?; + .map_err(|_| miette::miette!("internal: failed to parse proto JSON"))?; inject_runtime_policy_data(&mut data, require_binary_identity); normalize_endpoint_protocols(&mut data); // Validate BEFORE expanding presets let (errors, warnings) = crate::l7::validate_l7_policies(&data); - emit_l7_config_warnings(&warnings, "L7 policy validation warning"); if !errors.is_empty() { - return Err(miette::miette!( - "L7 policy validation failed:\n{}", - errors.join("\n") - )); + return Err(miette::miette!(render_repeated_validation_diagnostic( + "L7 policy validation failed", + errors.len(), + "invalid L7 policy configuration", + ))); } + // Rejected candidates must not leak authored values through warnings. + emit_l7_config_warnings(&warnings, "L7 policy validation warning"); normalize_l7_policy_rule_aliases(&mut data); @@ -488,10 +494,10 @@ impl OpaEngine { let mut engine = regorus::Engine::new(); engine .add_policy("policy.rego".into(), BAKED_POLICY_RULES.into()) - .map_err(|e| miette::miette!("{e}"))?; + .map_err(|_| miette::miette!("failed to load Rego policy"))?; engine .add_data_json(&data_json) - .map_err(|e| miette::miette!("{e}"))?; + .map_err(|_| miette::miette!("failed to load OPA policy data"))?; Ok(Self::with_engine(engine, require_binary_identity)) } @@ -1472,14 +1478,120 @@ fn validate_opa_object_array<'a>( Ok(entries) } +/// Select a fixed category without formatting authored fields or nested reasons. +/// Keep this match exhaustive so new validator variants require an explicit +/// decision before their diagnostics can cross the supervisor load boundary. +fn redacted_policy_violation_category(violation: &PolicyViolation) -> &'static str { + match violation { + PolicyViolation::InvalidProcessIdentity { .. } => "invalid process identity", + PolicyViolation::InvalidLandlockCompatibility { .. } => "invalid Landlock compatibility", + PolicyViolation::PathTraversal { .. } + | PolicyViolation::RelativePath { .. } + | PolicyViolation::OverlyBroadPath { .. } + | PolicyViolation::FieldTooLong { .. } => "invalid filesystem path", + PolicyViolation::TooManyPaths { .. } => "filesystem path limit exceeded", + PolicyViolation::TldWildcard { .. } + | PolicyViolation::TcpEndpointIpLiteral { .. } + | PolicyViolation::InvalidTcpEndpointHost { .. } + | PolicyViolation::InvalidHostWildcard { .. } => "invalid network endpoint host", + PolicyViolation::MissingEndpointHost { .. } + | PolicyViolation::MissingTcpEndpointHost { .. } => "missing network endpoint host", + PolicyViolation::MissingEndpointPort { .. } + | PolicyViolation::InvalidEndpointPort { .. } => "invalid network endpoint port", + PolicyViolation::MissingSigningService { .. } => { + "incomplete credential signing configuration" + } + PolicyViolation::UnknownCredentialSigning { .. } => { + "invalid credential signing configuration" + } + PolicyViolation::CredentialSigningWithBodyRewrite { .. } => { + "conflicting credential rewrite configuration" + } + PolicyViolation::InvalidL7Endpoint { .. } => "invalid L7 endpoint configuration", + PolicyViolation::InvalidMiddlewareConfig { .. } => "invalid middleware configuration", + PolicyViolation::TooManyMiddlewareConfigs { .. } => { + "middleware configuration limit exceeded" + } + PolicyViolation::DuplicateMiddlewareOrder { .. } => "duplicate middleware order", + PolicyViolation::TooManyMiddlewareSelectorPatterns { .. } => { + "middleware selector limit exceeded" + } + PolicyViolation::MiddlewareTlsSkipConflict { .. } => { + "middleware conflicts with TLS inspection" + } + PolicyViolation::MissingMcpVersions { .. } => "missing MCP protocol version", + PolicyViolation::McpOptionsOnNonMcpEndpoint { .. } => "MCP options require MCP protocol", + PolicyViolation::UnsupportedMcpVersion { .. } => "unsupported MCP protocol version", + PolicyViolation::DuplicateMcpVersion { .. } => "duplicate MCP protocol version", + } +} + +/// Render only fixed, implementation-owned headings and categories. Reserve +/// space for the omission marker before adding each complete item; never copy +/// or truncate a validator's payload-bearing Display, Debug, or source chain. +fn render_bounded_validation_diagnostics( + heading: &'static str, + diagnostics: impl Iterator, +) -> String { + const OMITTED_SUFFIX: &str = "; additional violations omitted"; + let mut rendered = heading.to_string(); + for (index, category) in diagnostics.enumerate() { + let separator = if index == 0 { ": " } else { "; " }; + if index == POLICY_VALIDATION_DIAGNOSTIC_MAX_ITEMS + || rendered.len() + separator.len() + category.len() + OMITTED_SUFFIX.len() + > POLICY_VALIDATION_DIAGNOSTIC_MAX_BYTES + { + rendered.push_str(OMITTED_SUFFIX); + break; + } + rendered.push_str(separator); + rendered.push_str(category); + } + rendered +} + +/// String-only validators have no safe fields to expose; retain their stage +/// and bounded item count without inspecting their authored error text. +fn render_repeated_validation_diagnostic( + heading: &'static str, + count: usize, + category: &'static str, +) -> String { + render_bounded_validation_diagnostics(heading, std::iter::repeat_n(category, count)) +} + /// Preprocess YAML policy data: parse, validate shapes, normalize, expand presets, return JSON. fn preprocess_yaml_data( yaml_str: &str, require_binary_identity: bool, validate_middleware_config: Option<&MiddlewareConfigValidator>, ) -> Result { - let mut data: serde_json::Value = serde_yml::from_str(yaml_str) - .map_err(|e| miette::miette!("failed to parse YAML data: {e}"))?; + let mut data: serde_json::Value = serde_yml::from_str(yaml_str).map_err(|error| { + // Parser text and source chains can include keys, values, and snippets. + // Numeric source positions are safe and help locate the authored error. + let category = match error.kind() { + serde_yml::ErrorKind::Syntax => "invalid syntax", + serde_yml::ErrorKind::Io => "input read failure", + serde_yml::ErrorKind::Budget => "parser limit exceeded", + serde_yml::ErrorKind::Policy => "unsupported YAML construct", + serde_yml::ErrorKind::KeyCollision => "key collision", + serde_yml::ErrorKind::DuplicateKey => "duplicate key", + serde_yml::ErrorKind::EndOfStream => "unexpected end of input", + serde_yml::ErrorKind::Data => "invalid data", + // ErrorKind is non-exhaustive; future kinds stay payload-free. + _ => "invalid YAML", + }; + error.location().map_or_else( + || miette::miette!("failed to parse YAML data: {category}"), + |location| { + miette::miette!( + "failed to parse YAML data: {category} at line {}, column {}", + location.line(), + location.column(), + ) + }, + ) + })?; validate_opa_data_structure(&data)?; inject_runtime_policy_data(&mut data, require_binary_identity); normalize_endpoint_protocols(&mut data); @@ -1488,10 +1600,11 @@ fn preprocess_yaml_data( normalize_endpoint_ports(&mut data); let config_errors = normalize_l7_config_aliases(&mut data); if !config_errors.is_empty() { - return Err(miette::miette!( - "L7 policy validation failed:\n{}", - config_errors.join("\n") - )); + return Err(miette::miette!(render_repeated_validation_diagnostic( + "L7 policy validation failed", + config_errors.len(), + "invalid L7 protocol configuration", + ))); } // Validate BEFORE expanding presets (catches user errors like rules+access) @@ -1502,26 +1615,28 @@ fn preprocess_yaml_data( openshell_policy::validate_network_middleware_json_with_config(&data, validate) }, ) - .map_err(|error| miette::miette!(error))?; + .map_err(|_| { + miette::miette!("failed to parse or convert middleware policy configuration") + })?; if !middleware_errors.is_empty() { - return Err(miette::miette!( - "middleware policy validation failed:\n{}", + return Err(miette::miette!(render_bounded_validation_diagnostics( + "middleware policy validation failed", middleware_errors .iter() - .map(ToString::to_string) - .collect::>() - .join("\n") - )); + .map(redacted_policy_violation_category), + ))); } let (errors, warnings) = crate::l7::validate_l7_policies(&data); - emit_l7_config_warnings(&warnings, "L7 policy validation warning"); if !errors.is_empty() { - return Err(miette::miette!( - "L7 policy validation failed:\n{}", - errors.join("\n") - )); + return Err(miette::miette!(render_repeated_validation_diagnostic( + "L7 policy validation failed", + errors.len(), + "invalid L7 policy configuration", + ))); } + // Emit authored warnings only after the candidate passes validation. + emit_l7_config_warnings(&warnings, "L7 policy validation warning"); normalize_l7_policy_rule_aliases(&mut data); @@ -1529,7 +1644,7 @@ fn preprocess_yaml_data( let expansion_warnings = crate::l7::expand_access_presets(&mut data); emit_l7_config_warnings(&expansion_warnings, "L7 access preset expansion warning"); - serde_json::to_string(&data).map_err(|e| miette::miette!("failed to serialize data: {e}")) + serde_json::to_string(&data).map_err(|_| miette::miette!("failed to serialize OPA policy data")) } /// Canonicalize recognized protocol spellings before L7 validation or Rego use. @@ -2412,6 +2527,406 @@ mod tests { const TEST_POLICY: &str = include_str!("../data/sandbox-policy.rego"); const TEST_DATA_YAML: &str = include_str!("../testdata/sandbox-policy.yaml"); + fn assert_safe_load_error(error: &miette::Report, payloads: &[&str]) { + assert!(error.to_string().len() <= POLICY_VALIDATION_DIAGNOSTIC_MAX_BYTES); + assert_eq!( + error.chain().count(), + 1, + "raw error source must be discarded" + ); + for rendered in [ + format!("{error}"), + format!("{error:#}"), + format!("{error:?}"), + format!("{error:#?}"), + ] { + // Debug adds renderer-owned decoration to the bounded message. + assert!(rendered.len() <= 2048, "{rendered}"); + for payload in payloads { + assert!( + !rendered.contains(payload), + "diagnostic leaked {payload}: {rendered}" + ); + } + } + } + + #[test] + fn load_diagnostics_enforce_item_and_byte_limits_with_omission() { + const LONG_CATEGORY: &str = concat!( + "固定診断固定診断固定診断固定診断固定診断固定診断固定診断固定診断", + "固定診断固定診断固定診断固定診断固定診断固定診断固定診断固定診断", + ); + for count in [0, 1, 8, 9, 100_000] { + let message = render_repeated_validation_diagnostic( + "policy validation failed", + count, + "invalid filesystem path", + ); + assert!(message.len() <= 512); + assert_eq!( + message.matches("invalid filesystem path").count(), + count.min(8) + ); + assert_eq!(message.contains("additional violations omitted"), count > 8); + } + + // A future long fixed category must hit the byte ceiling before the + // item ceiling, retain complete UTF-8 items, and always mark omission. + let message = + render_repeated_validation_diagnostic("policy validation failed", 8, LONG_CATEGORY); + assert!(message.len() <= 512); + assert_eq!(message.matches(LONG_CATEGORY).count(), 2); + assert!(message.ends_with("; additional violations omitted")); + } + + #[test] + fn load_diagnostics_yaml_errors_are_safe_across_file_load_and_reload() { + let secret = "private-payload-秘密".repeat(1024); + let candidate = |endpoint: serde_json::Value| { + serde_json::json!({ + "network_policies": { &secret: {"endpoints": [endpoint]} } + }) + .to_string() + }; + let cases = [ + ( + format!("private-key: *{secret}"), + "failed to parse YAML data", + ), + ( + format!("private-key: [\"{secret}"), + "failed to parse YAML data", + ), + ( + candidate(serde_json::json!({"host": "example.test", "port": 443, + "protocol": "mcp", "mcp": {"max_body_bytes": &secret}})), + "invalid L7 protocol configuration", + ), + ( + candidate(serde_json::json!({"host": "example.test", "port": 443, + "protocol": "rest", "enforcement": &secret})), + "invalid L7 policy configuration", + ), + ( + serde_json::json!({"network_middlewares": {&secret: { + "middleware": &secret, "order": &secret + }}}) + .to_string(), + "failed to parse or convert middleware policy configuration", + ), + ( + serde_json::json!({"network_middlewares": {&secret: { + "middleware": &secret, "on_error": &secret, + "endpoints": {"include": ["example.test"]} + }}}) + .to_string(), + "invalid middleware configuration", + ), + ]; + let directory = tempfile::tempdir().unwrap(); + let policy_path = directory.path().join("private-policy.rego"); + let data_path = directory.path().join("private-data.yaml"); + std::fs::write(&policy_path, TEST_POLICY).unwrap(); + let engine = l7_engine(); + let generation = engine.current_generation(); + let guard = engine.generation_guard(generation).unwrap(); + let allowed = l7_input("api.example.com", 8080, "GET", "/repos/myorg/foo"); + let denied = l7_input("api.example.com", 8080, "DELETE", "/repos/myorg/foo"); + assert!(eval_l7(&engine, &allowed)); + assert!(!eval_l7(&engine, &denied)); + for (data, expected) in cases { + std::fs::write(&data_path, &data).unwrap(); + for error in [ + OpaEngine::from_strings(TEST_POLICY, &data) + .err() + .expect("invalid initial load"), + OpaEngine::from_files(&policy_path, &data_path) + .err() + .expect("invalid file load"), + OpaEngine::from_files_for_endpoint_only_proxy(&policy_path, &data_path, None) + .err() + .expect("invalid endpoint-only file load"), + engine + .reload(TEST_POLICY, &data) + .expect_err("invalid reload"), + ] { + assert!(error.to_string().contains(expected), "{error}"); + assert_safe_load_error( + &error, + &["private-payload", "秘密", "private-key", "example.test"], + ); + } + assert_eq!(engine.current_generation(), generation); + assert!(!guard.is_stale()); + assert!(eval_l7(&engine, &allowed)); + assert!(!eval_l7(&engine, &denied)); + } + } + + #[test] + fn load_diagnostics_catalog_errors_drop_callback_payloads_and_bound_items() { + let secret = "private-catalog-秘密".repeat(1024); + let calls = Arc::new(AtomicU64::new(0)); + let callback_calls = Arc::clone(&calls); + let validate = move |_: &str, _: &prost_types::Struct| { + callback_calls.fetch_add(1, Ordering::Relaxed); + Err(secret.clone()) + }; + let middlewares: serde_json::Map = (0..10) + .map(|order| { + ( + format!("private-stage-{order}"), + serde_json::json!({"middleware": "private-implementation", "order": order, + "endpoints": {"include": ["private.example.test"]}, + "config": {"private-key": "private-value"}}), + ) + }) + .collect(); + let data = serde_json::json!({"network_middlewares": middlewares}).to_string(); + let directory = tempfile::tempdir().unwrap(); + let policy_path = directory.path().join("policy.rego"); + let data_path = directory.path().join("data.yaml"); + std::fs::write(&policy_path, TEST_POLICY).unwrap(); + std::fs::write(&data_path, &data).unwrap(); + for error in [ + OpaEngine::from_strings_with_middleware_config(TEST_POLICY, &data, Some(&validate)) + .err() + .unwrap(), + OpaEngine::from_files_with_middleware_config(&policy_path, &data_path, Some(&validate)) + .err() + .unwrap(), + OpaEngine::from_files_for_endpoint_only_proxy( + &policy_path, + &data_path, + Some(&validate), + ) + .err() + .unwrap(), + ] { + assert_safe_load_error(&error, &["private-", "秘密", "private.example.test"]); + let message = error.to_string(); + assert_eq!( + message.matches("invalid middleware configuration").count(), + 8 + ); + assert!(message.ends_with("additional violations omitted")); + } + assert_eq!(calls.load(Ordering::Relaxed), 30); + let accept = |_: &str, _: &prost_types::Struct| Ok(()); + assert!( + OpaEngine::from_strings_with_middleware_config(TEST_POLICY, &data, Some(&accept)) + .is_ok() + ); + let required = + OpaEngine::from_files_with_middleware_config(&policy_path, &data_path, Some(&accept)) + .unwrap(); + let endpoint_only = + OpaEngine::from_files_for_endpoint_only_proxy(&policy_path, &data_path, Some(&accept)) + .unwrap(); + assert!(required.binary_identity_required()); + assert!(!endpoint_only.binary_identity_required()); + } + + #[test] + fn load_diagnostics_rego_and_file_errors_drop_source_and_paths() { + let private_rego = "package private_package\nprivate_rule := \"private-secret"; + let engine = test_engine(); + let generation = engine.current_generation(); + let directory = tempfile::tempdir().unwrap(); + let policy_path = directory.path().join("private-policy.rego"); + let data_path = directory.path().join("private-data.yaml"); + std::fs::write(&policy_path, private_rego).unwrap(); + std::fs::write(&data_path, TEST_DATA_YAML).unwrap(); + for error in [ + OpaEngine::from_strings(private_rego, TEST_DATA_YAML) + .err() + .unwrap(), + OpaEngine::from_files(&policy_path, &data_path) + .err() + .unwrap(), + OpaEngine::from_files_for_endpoint_only_proxy(&policy_path, &data_path, None) + .err() + .unwrap(), + engine.reload(private_rego, TEST_DATA_YAML).unwrap_err(), + ] { + assert_eq!(error.to_string(), "failed to load Rego policy"); + assert_safe_load_error(&error, &["private", directory.path().to_str().unwrap()]); + } + assert_eq!(engine.current_generation(), generation); + std::fs::remove_file(&policy_path).unwrap(); + let error = OpaEngine::from_files(&policy_path, &data_path) + .err() + .unwrap(); + assert_safe_load_error(&error, &["private", directory.path().to_str().unwrap()]); + std::fs::remove_file(&data_path).unwrap(); + let error = OpaEngine::from_files(&policy_path, &data_path) + .err() + .unwrap(); + assert_eq!(error.to_string(), "failed to read YAML policy data file"); + assert_safe_load_error(&error, &["private", directory.path().to_str().unwrap()]); + std::fs::write(&data_path, [0xff]).unwrap(); + let error = OpaEngine::from_files(&policy_path, &data_path) + .err() + .unwrap(); + assert_safe_load_error(&error, &["private", directory.path().to_str().unwrap()]); + } + + #[test] + fn load_diagnostics_proto_ambiguities_are_bounded_and_reloads_preserve_decisions() { + let valid = defaultable_mcp_proto(None); + let engine = OpaEngine::from_proto(&valid).unwrap(); + let guard = engine + .generation_guard(engine.current_generation()) + .unwrap(); + let mut candidate = valid.clone(); + for index in 0..16 { + candidate.network_policies.insert( + format!("private-policy-{index}"), + NetworkPolicyRule { + name: format!("private-rule-{index}"), + endpoints: vec![NetworkEndpoint { + host: "*.example.com".into(), + port: 443, + tls: "skip".into(), + ..Default::default() + }], + binaries: vec![NetworkBinary { + path: "/private/binary".into(), + }], + }, + ); + } + for error in [ + OpaEngine::from_proto(&candidate).err().unwrap(), + OpaEngine::from_proto_with_pid(&candidate, 0).err().unwrap(), + engine.reload_from_proto(&candidate).unwrap_err(), + engine + .reload_from_proto_with_pid(&candidate, 0) + .unwrap_err(), + ] { + assert_safe_load_error( + &error, + &["private-", "/private/binary", "example.com", "skip"], + ); + let message = error.to_string(); + assert_eq!( + message + .matches("ambiguous network endpoint selectors") + .count(), + 8 + ); + assert!(message.ends_with("additional violations omitted")); + assert_eq!(engine.current_generation(), 0); + assert!(!guard.is_stale()); + assert!(eval_l7( + &engine, + &l7_jsonrpc_input("mcp.example.com", 443, "/", "tools/list") + )); + assert!(!eval_l7( + &engine, + &l7_jsonrpc_input("mcp.example.com", 443, "/", "tools/call") + )); + } + engine.reload_from_proto(&valid).unwrap(); + assert_eq!(engine.current_generation(), 1); + assert!(guard.is_stale()); + } + + #[test] + fn load_diagnostics_proto_landlock_values_are_redacted() { + let mut policy = openshell_policy::restrictive_default_policy(); + policy.landlock = Some(openshell_core::proto::LandlockPolicy { + compatibility: "private-landlock-秘密".repeat(1024), + }); + let error = OpaEngine::from_proto(&policy).err().unwrap(); + assert!(error.to_string().contains("invalid Landlock compatibility")); + assert_safe_load_error(&error, &["private-landlock", "秘密"]); + } + + #[test] + fn load_diagnostics_rejected_l7_candidates_do_not_emit_authored_warnings() { + use tracing_subscriber::layer::{Context, Layer, SubscriberExt}; + + const CHILD: &str = "OPENSHELL_TEST_OPA_DIAGNOSTICS_CHILD"; + struct Capture(Arc>>); + impl Layer for Capture { + fn on_event(&self, _: &tracing::Event<'_>, _: Context<'_, S>) { + if let Some(event) = openshell_ocsf::tracing_layers::clone_current_event() { + self.0 + .lock() + .unwrap() + .push(serde_json::to_string(&event).unwrap()); + } + } + } + // Tracing callsite interest is process-wide, so concurrent tests with + // other subscribers can disable this thread's capture. Exercise the + // real loader in an isolated test process with the same executable. + if std::env::var_os(CHILD).is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "opa::tests::load_diagnostics_rejected_l7_candidates_do_not_emit_authored_warnings", "--nocapture"]) + .env(CHILD, "1") + .output() + .unwrap(); + assert!( + output.status.success(), + "{}\n{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + return; + } + let events = Arc::new(Mutex::new(Vec::new())); + let subscriber = tracing_subscriber::registry().with(Capture(Arc::clone(&events))); + let mut data = serde_json::json!({"network_policies": {"private-warning-policy": { + "endpoints": [{"host": "example.test", "port": 443, "protocol": "rest", + "path": "/private-warning-path[", "access": "read-only", + "rules": [{"allow": {"method": "GET", "path": "/private-rule"}}], + "enforcement": "private-invalid-enforcement"}] + }}}); + // Confirm the fixture really has both a warning and a rejection. + let (errors, warnings) = crate::l7::validate_l7_policies(&data); + assert!(!errors.is_empty()); + assert!(!warnings.is_empty()); + tracing::subscriber::with_default(subscriber, || { + let error = OpaEngine::from_strings(TEST_POLICY, &data.to_string()) + .err() + .unwrap(); + assert_safe_load_error(&error, &["private-"]); + assert!( + !events.lock().unwrap().is_empty(), + "capture must see loader events" + ); + assert!( + events + .lock() + .unwrap() + .iter() + .all(|event| !event.contains("private-")) + ); + + // Accepted-candidate warnings retain their existing operator contract. + events.lock().unwrap().clear(); + data["network_policies"]["private-warning-policy"]["endpoints"][0]["enforcement"] = + "audit".into(); + data["network_policies"]["private-warning-policy"]["endpoints"][0] + .as_object_mut() + .unwrap() + .remove("rules"); + let (errors, _) = crate::l7::validate_l7_policies(&data); + assert!(errors.is_empty(), "positive control: {errors:?}"); + assert!(OpaEngine::from_strings(TEST_POLICY, &data.to_string()).is_ok()); + assert!( + events + .lock() + .unwrap() + .iter() + .any(|event| event.contains("private-warning")) + ); + }); + } + fn test_engine() -> OpaEngine { OpaEngine::from_strings(TEST_POLICY, TEST_DATA_YAML).expect("Failed to load test policy") } @@ -2668,11 +3183,11 @@ mod tests { let error = OpaEngine::from_strings(TEST_POLICY, &data.to_string()) .err() .expect("an empty deny-rule list remains semantically invalid"); - assert!( - error - .to_string() - .contains("deny_rules list cannot be empty") + assert_eq!( + error.to_string(), + "L7 policy validation failed: invalid L7 policy configuration" ); + assert_safe_load_error(&error, &["admin.example.test", "/admin/**"]); } #[test] @@ -5256,10 +5771,23 @@ network_policies: panic!("JSON-RPC params matchers should fail validation"); }; + let message = err.to_string(); assert!( - err.to_string().contains("do not support params"), - "unexpected validation error: {err}" + message.contains("invalid L7 policy configuration"), + "unexpected validation error: {message}" ); + for authored in [ + "invalid_jsonrpc_params", + "jsonrpc.invalid.test", + "/rpc", + "reports.search", + "quarterly", + ] { + assert!( + !message.contains(authored), + "diagnostic leaked {authored}: {message}" + ); + } } #[test] @@ -5288,9 +5816,21 @@ network_policies: let message = err.to_string(); assert!( - message.contains("json_rpc") && message.contains("on_parse_error"), - "unexpected validation error: {err}" + message.contains("invalid L7 protocol configuration"), + "unexpected validation error: {message}" ); + for authored in [ + "invalid_jsonrpc_config", + "jsonrpc.invalid-config.test", + "/rpc", + "json_rpc", + "on_parse_error", + ] { + assert!( + !message.contains(authored), + "diagnostic leaked {authored}: {message}" + ); + } } #[test] @@ -5319,9 +5859,21 @@ network_policies: let message = err.to_string(); assert!( - message.contains("mcp") && message.contains("large"), - "unexpected validation error: {err}" + message.contains("invalid L7 protocol configuration"), + "unexpected validation error: {message}" ); + for authored in [ + "invalid_mcp_config", + "mcp.invalid-config.test", + "/mcp", + "max_body_bytes", + "large", + ] { + assert!( + !message.contains(authored), + "diagnostic leaked {authored}: {message}" + ); + } } #[test] @@ -5720,12 +6272,17 @@ network_policies: let Err(error) = OpaEngine::from_strings(TEST_POLICY, data) else { panic!("mixed-case MCP must not bypass MCP rule validation"); }; + let message = error.to_string(); assert!( - error - .to_string() - .contains("mcp L7 rules must use method/tool, not path/query"), - "{error}" + message.contains("invalid L7 policy configuration"), + "{message}" ); + for authored in ["mcp.example.com", "POST", "**"] { + assert!( + !message.contains(authored), + "diagnostic leaked {authored}: {message}" + ); + } } #[test] @@ -5758,10 +6315,12 @@ network_policies: let Err(error) = OpaEngine::from_strings(TEST_POLICY, &data) else { panic!("invalid MCP runtime metadata must reject activation: {case}"); }; + let message = error.to_string(); assert!( - error.to_string().contains("mcp.versions"), - "{case}: {error}" + message.contains("invalid L7 policy configuration"), + "{case}: {message}" ); + assert!(!message.contains("2026-01-01"), "{case}: {message}"); } } @@ -5788,12 +6347,17 @@ network_policies: let Err(error) = OpaEngine::from_strings(TEST_POLICY, data) else { panic!("ambiguous MCP revision sources must reject activation"); }; + let message = error.to_string(); assert!( - error - .to_string() - .contains("mcp.versions and mcp_versions cannot both be set"), - "{error}" + message.contains("invalid L7 protocol configuration"), + "{message}" ); + for authored in ["mcp_versions", "2025-03-26", "2025-11-25"] { + assert!( + !message.contains(authored), + "diagnostic leaked {authored}: {message}" + ); + } } #[test] @@ -5823,10 +6387,12 @@ network_policies: let Err(error) = OpaEngine::from_strings(TEST_POLICY, &data) else { panic!("null MCP config must reject activation"); }; + let message = error.to_string(); assert!( - error.to_string().contains("mcp config must be an object"), - "{error}" + message.contains("invalid L7 protocol configuration"), + "{message}" ); + assert!(!message.contains("2025-11-25"), "{message}"); } } @@ -5857,12 +6423,17 @@ network_policies: let Err(error) = OpaEngine::from_strings(TEST_POLICY, &data) else { panic!("MCP revision policy must not apply to generic JSON-RPC"); }; + let message = error.to_string(); assert!( - error - .to_string() - .contains("mcp.versions is only valid for protocol mcp"), - "{error}" + message.contains("invalid L7 policy configuration"), + "{message}" ); + for authored in ["rpc.example.com", "ping", "2025-11-25"] { + assert!( + !message.contains(authored), + "diagnostic leaked {authored}: {message}" + ); + } } } @@ -6348,7 +6919,7 @@ network_policies: } #[test] - fn proto_load_rejects_ambiguous_endpoint_metadata_with_rationale() { + fn proto_load_redacts_ambiguous_endpoint_metadata() { let mut policy = ProtoSandboxPolicy::default(); policy.network_policies.insert( "wildcard".to_string(), @@ -6385,9 +6956,18 @@ network_policies: }; let message = error.to_string(); assert!(message.contains("ambiguity validation failed")); - assert!(message.contains("wildcard")); - assert!(message.contains("exact")); - assert!(message.contains("tls")); + assert!(message.contains("ambiguous network endpoint selectors")); + assert_safe_load_error( + &error, + &[ + "wildcard", + "exact", + "*.example.com", + "api.example.com", + "/usr/bin/curl", + "/usr/bin/bash", + ], + ); } #[test] @@ -6463,6 +7043,104 @@ network_policies: ); } + #[test] + fn proto_load_bounds_validation_error_without_policy_values() { + let endpoints = (0..16) + .map(|index| NetworkEndpoint { + host: format!("private-{index}.sensitive.example.test"), + port: 443, + protocol: "mcp".to_string(), + mcp: Some(McpOptions { + versions: vec![format!("private-version-{index}")], + ..Default::default() + }), + ..Default::default() + }) + .collect(); + let policy = ProtoSandboxPolicy { + version: 1, + network_policies: std::collections::HashMap::from([( + "private-policy-name".to_string(), + NetworkPolicyRule { + name: "private-rule-name".to_string(), + endpoints, + binaries: vec![NetworkBinary { + path: "/private/sensitive/binary".to_string(), + }], + }, + )]), + ..Default::default() + }; + + let error = OpaEngine::from_proto(&policy) + .err() + .expect("invalid MCP revisions must reject protobuf activation") + .to_string(); + + assert!( + error.len() <= POLICY_VALIDATION_DIAGNOSTIC_MAX_BYTES, + "validation error exceeded byte bound: {error}" + ); + let rendered_categories = error.matches("invalid L7 endpoint configuration").count() + + error.matches("unsupported MCP protocol version").count(); + assert_eq!( + rendered_categories, POLICY_VALIDATION_DIAGNOSTIC_MAX_ITEMS, + "validation error must stop at the item bound: {error}" + ); + assert!( + error.contains("additional violations omitted"), + "validation error must mark omitted categories: {error}" + ); + for authored in [ + "private-policy-name", + "private-rule-name", + "private-0.sensitive.example.test", + "private-15.sensitive.example.test", + "private-version-0", + "private-version-15", + "/private/sensitive/binary", + ] { + assert!( + !error.contains(authored), + "validation error echoed authored value {authored}: {error}" + ); + } + } + + #[test] + fn proto_reload_reuses_redacted_validation_and_preserves_last_known_good() { + let valid = defaultable_mcp_proto(None); + let engine = OpaEngine::from_proto(&valid).expect("initial policy must load"); + let generation = engine.current_generation(); + let mut invalid = valid; + invalid + .network_policies + .get_mut("mcp") + .expect("MCP policy") + .endpoints[0] + .mcp = Some(McpOptions { + versions: vec!["private-authored-version".to_string()], + ..Default::default() + }); + + let error = engine + .reload_from_proto(&invalid) + .expect_err("invalid reload must fail closed") + .to_string(); + + assert!( + error.contains("unsupported MCP protocol version"), + "{error}" + ); + assert!(!error.contains("private-authored-version"), "{error}"); + assert!(error.len() <= POLICY_VALIDATION_DIAGNOSTIC_MAX_BYTES); + assert_eq!(engine.current_generation(), generation); + assert!(eval_l7( + &engine, + &l7_jsonrpc_input("mcp.example.com", 443, "/", "tools/list") + )); + } + #[test] fn proto_load_rejects_unsupported_mcp_versions() { let policy = defaultable_mcp_proto(Some(McpOptions { @@ -6476,9 +7154,17 @@ network_policies: let error = error.to_string(); assert!(error.contains("policy validation failed"), "{error}"); assert!( - error.contains("unsupported protocol version 'latest'"), + error.contains("unsupported MCP protocol version"), "{error}" ); + assert!( + !error.contains("latest"), + "validation diagnostics must not echo authored protocol values: {error}" + ); + assert!( + error.len() <= POLICY_VALIDATION_DIAGNOSTIC_MAX_BYTES, + "validation diagnostics must be byte-bounded: {error}" + ); } #[tokio::test] @@ -7476,8 +8162,12 @@ network_policies: .err() .expect("explicit TCP must reject credential binding during activation"); - assert!(error.to_string().contains("credential_binding")); - assert!(error.to_string().contains("protocol tcp")); + assert!( + error + .to_string() + .contains("invalid L7 policy configuration") + ); + assert!(error.to_string().contains("L7 policy validation failed")); } #[test] @@ -9298,9 +9988,18 @@ network_policies: .await .expect("empty registry"); - engine + let error = engine .reload_policy_and_middleware_from_proto_with_pid(&invalid, 0, empty_registry) .expect_err("invalid policy must reject the combined reload"); + assert_safe_load_error( + &error, + &[openshell_supervisor_middleware_builtins::BUILTIN_REGEX], + ); + assert!( + error + .to_string() + .contains("invalid middleware configuration") + ); assert_eq!(engine.current_generation(), 1); let claude_input = NetworkInput { @@ -9887,7 +10586,7 @@ network_middlewares: endpoints: include: ["api.example.com"] "#, - "invalid on_error", + "invalid middleware configuration", ), ( "duplicate order", @@ -9904,7 +10603,7 @@ network_middlewares: endpoints: include: ["other.example.com"] "#, - "duplicate order 10", + "duplicate middleware order", ), ( "missing selector", @@ -9913,7 +10612,7 @@ network_middlewares: redactor: middleware: openshell/regex "#, - "endpoint selector is required", + "invalid middleware configuration", ), ( "malformed selector", @@ -9924,7 +10623,7 @@ network_middlewares: endpoints: include: ["api[.example.com"] "#, - "invalid host pattern", + "invalid middleware configuration", ), ( "tls skip selector", @@ -9943,7 +10642,7 @@ network_policies: binaries: - { path: /usr/bin/curl } "#, - "tls: skip", + "middleware conflicts with TLS inspection", ), ( "tls skip wildcard overlap", @@ -9962,7 +10661,7 @@ network_policies: binaries: - { path: /usr/bin/curl } "#, - "tls: skip", + "middleware conflicts with TLS inspection", ), ]; @@ -9994,7 +10693,7 @@ network_middlewares: endpoints: include: ["api.example.com"] "#, - "not a registered OpenShell built-in", + "invalid middleware configuration", ), ( "invalid regex config", @@ -10007,7 +10706,7 @@ network_middlewares: endpoints: include: ["api.example.com"] "#, - "supports only mode: redact", + "invalid middleware configuration", ), ] { let error = @@ -10042,7 +10741,10 @@ network_middlewares: .expect("supervisor must reject invalid effective middleware policy") .to_string(); assert!(error.contains("policy validation failed"), "{error}"); - assert!(error.contains("invalid host pattern"), "{error}"); + assert!( + error.contains("invalid middleware configuration"), + "{error}" + ); } #[test] diff --git a/deploy/docker/Dockerfile.supervisor b/deploy/docker/Dockerfile.supervisor index c25b5a90d6..26fa44a765 100644 --- a/deploy/docker/Dockerfile.supervisor +++ b/deploy/docker/Dockerfile.supervisor @@ -6,14 +6,12 @@ # The dynamically linked GNU supervisor binary is staged under # deploy/docker/.build/prebuilt-binaries//. -FROM debian:bookworm-slim AS supervisor +# Keep the GNU runtime and CA roots without a shell or unused native TLS libraries. +# The default variant preserves UID 0 and /; compute drivers set the runtime UID. +FROM gcr.io/distroless/base-nossl-debian13@sha256:af5cb8dd589b8520b8c06bebb9efb73d7e16406cab58e85c51761fff49d370a0 AS supervisor ARG TARGETARCH -RUN apt-get update \ - && apt-get install -y --no-install-recommends ca-certificates \ - && rm -rf /var/lib/apt/lists/* - COPY --chmod=0555 deploy/docker/.build/prebuilt-binaries/${TARGETARCH}/openshell-supervisor /openshell-supervisor ENTRYPOINT ["/openshell-supervisor"] diff --git a/docs/reference/policy-schema.mdx b/docs/reference/policy-schema.mdx index a721718e2e..10e596d29b 100644 --- a/docs/reference/policy-schema.mdx +++ b/docs/reference/policy-schema.mdx @@ -62,6 +62,22 @@ omitted. Because proto3 scalar fields do not preserve presence, protobuf `versio `version: 1`; any other unsupported protobuf version is rejected. A protobuf port above 65535 is rejected rather than clamped. +## Policy Load Diagnostics + +When the supervisor's OPA engine rejects a policy load or reload, its error message identifies the validation category without copying policy or middleware names, hostnames, paths, values, or source text. The message also omits the original error chain. This applies to the engine's file, string, and protobuf loaders. + +Each message contains at most eight error items and 512 UTF-8 bytes, including the heading, separators, and the `additional violations omitted` marker when more items cannot fit. Categories help you choose which part of the submitted policy to inspect: + +- Typed validation distinguishes process identity, filesystem paths and limits, Landlock compatibility, endpoint hosts and ports, credential signing and rewriting, MCP configuration, and middleware configuration. +- L7 validation reports `invalid L7 policy configuration` for semantic errors and `invalid L7 protocol configuration` for protocol configuration or alias errors. +- Endpoint conflicts report `ambiguous network endpoint selectors`. +- YAML errors report a fixed parser category, with numeric line and column when the parser provides them. +- File I/O, Rego loading, and internal policy-data failures return fixed messages. + +A candidate rejected during validation does not replace the active engine or advance its generation. The supervisor then applies the gateway's `policy_validation_failure_mode`; `fail_closed` can publish a quarantine generation that denies egress, while `retain_last_valid` keeps an existing valid generation active. See [Gateway Configuration](/reference/gateway-config) for that setting and startup behavior. + +These bounds apply to returned OPA load errors. Warnings for accepted policies, runtime request diagnostics, and gateway-authored policy parser messages have separate reporting behavior. + ## Version The version field identifies which schema the policy uses: diff --git a/docs/reference/sandbox-compute-drivers.mdx b/docs/reference/sandbox-compute-drivers.mdx index 76548f60c7..d24ff67ef4 100644 --- a/docs/reference/sandbox-compute-drivers.mdx +++ b/docs/reference/sandbox-compute-drivers.mdx @@ -162,6 +162,11 @@ HTTP requests. A `PermissionDenied` response from an additional callback-only listener is expected for those requests. Do not broaden the primary listener to `0.0.0.0` solely to make sandbox callbacks reachable. +The published supervisor container uses a shell-free distroless Debian 13 image. +Inspect its container logs and health status with your runtime tools; it does not +provide a shell or package manager for interactive debugging. This does not +change the tools available inside your workload image. + ## Docker Driver [Docker](https://www.docker.com/get-started/)-backed sandboxes run as containers on the gateway host. Use Docker for local development, single-machine gateways, and hosts that already use Docker Desktop or Docker Engine. diff --git a/docs/sandboxes/manage-sandboxes.mdx b/docs/sandboxes/manage-sandboxes.mdx index 5415d993b0..86968a9910 100644 --- a/docs/sandboxes/manage-sandboxes.mdx +++ b/docs/sandboxes/manage-sandboxes.mdx @@ -371,7 +371,7 @@ Inject environment variables into the sandbox at creation time: openshell sandbox create --env API_KEY=sk-test --env DEBUG=1 -- my-agent ``` -Variables set with `--env` are available to all processes in the sandbox, including interactive shells and exec commands. +Variables set with `--env` are available to all processes in the sandbox, including the initial command, interactive shells, and exec commands. When an `--env` key looks like a credential — a known provider variable, or a name whose underscore-separated segments include a credential word such as `TOKEN`, `SECRET`, `PASSWORD`, `CREDENTIAL`, `API_KEY`, `ACCESS_KEY`, or `SECRET_KEY` (for example `DB_TOKEN` or `MY_ACCESS_KEY`) — `sandbox create` prints a non-blocking warning. Matching is on whole segments, so unrelated names like `TOKENIZERS_PARALLELISM` or `PASSWORDLESS_LOGIN` do not warn. The agent inside the sandbox can read plain environment values directly, so to hide a secret from the agent, attach it through a [profile-backed provider](/providers/profiles) with `--provider` instead. Suppress the warning with `--no-credential-warnings`. Detection uses the key name only; values are never inspected or printed. diff --git a/e2e/parity/test.sh b/e2e/parity/test.sh index f3ca47a433..f3052b7a7e 100755 --- a/e2e/parity/test.sh +++ b/e2e/parity/test.sh @@ -8,6 +8,7 @@ set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +TEST_SUPERVISOR_BASE="$(awk '$1 == "FROM" { print $2; exit }' "${ROOT}/deploy/docker/Dockerfile.supervisor")" TMP_ROOT="${TMPDIR:-/tmp}" TMP_ROOT="${TMP_ROOT%/}" WORKDIR="$(mktemp -d "${TMP_ROOT}/openshell-parity-test.XXXXXX")" @@ -18,6 +19,37 @@ assert_contains() { grep -F -- "$2" "$1" >/dev/null || fail "expected $1 to cont assert_not_contains() { ! grep -F -- "$2" "$1" >/dev/null || fail "expected $1 not to contain: $2"; } assert_status() { [ "$1" -eq "$2" ] || fail "expected status $2, got $1"; } +# Package provenance must work for both Debian status and distroless status.d. +uv run --no-project python - "${ROOT}" "${WORKDIR}" <<'PYTEST' +import runpy +import sys +from pathlib import Path + +manifest = runpy.run_path(str(Path(sys.argv[1]) / "e2e/support/debian-package-manifest.py"))["package_manifest"] +root = Path(sys.argv[2]) / "dpkg-fixture" +root.mkdir() +(root / "status").write_text("Package: removed\nStatus: deinstall ok config-files\n\n") +(root / "status.d").mkdir() +(root / "status.d/libc6").write_text("Package: libc6\nVersion: 2.41\nArchitecture: arm64\nMulti-Arch: same\n") +(root / "status.d/libc6.md5sums").write_text("ignored checksum file") +assert manifest(root) == ["libc6:arm64=2.41"] +(root / "status").write_text("Package: ca-certificates\nStatus: install ok installed\nVersion: 20250419\nArchitecture: all\n") +assert manifest(root) == ["ca-certificates=20250419", "libc6:arm64=2.41"] +(root / "status.d/libc6").write_text("Package: broken\n") +try: + manifest(root) +except ValueError: + pass +else: + raise AssertionError("incomplete package metadata accepted") +try: + manifest(root / "missing") +except ValueError: + pass +else: + raise AssertionError("missing package metadata accepted") +PYTEST + # Schema generator behavior is separately deterministic and does not need a # gateway, certificates, or Podman. # shellcheck source=e2e/support/gateway-common.sh @@ -111,7 +143,7 @@ expected_sandbox="ghcr.io/nvidia/openshell-community/sandboxes/base@sha256:$(pri [ "${OPENSHELL_E2E_PODMAN_SANDBOX_IMAGE:-}" = "${expected_sandbox}" ] || exit 25 [ "${OPENSHELL_COMMUNITY_REGISTRY:-}" = "ghcr.io/nvidia/openshell-community/sandboxes" ] || exit 28 expected_base="docker.io/library/debian@sha256:$(printf '%064d' 0)" -[ "${OPENSHELL_E2E_SUPERVISOR_BASE_IMAGE:-}" = debian:bookworm-slim ] || exit 26 +[ "${OPENSHELL_E2E_SUPERVISOR_BASE_IMAGE:-}" = "${OPENSHELL_PARITY_TEST_SUPERVISOR_BASE}" ] || exit 26 [ "${OPENSHELL_E2E_SUPERVISOR_BASE_RUNTIME_IMAGE:-}" = "${expected_base}" ] || exit 27 printf '%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\n' "$OPENSHELL_PARITY_VARIANT" "$OPENSHELL_E2E_CONFIG_SCHEMA_VERSION" "$OPENSHELL_GATEWAY_BIN" "$OPENSHELL_BIN" "$OPENSHELL_CONFORMANCE_BIN" "$MISE_TRUSTED_CONFIG_PATHS" "${OPENSHELL_E2E_PODMAN_OPTION_PROFILE:-}" "${OPENSHELL_PARITY_ORACLE_RESULT:-}" "${OPENSHELL_E2E_EXTERNAL_COMPUTE_DRIVER:-}" "${OPENSHELL_EXTERNAL_DRIVER_BIN:-}" "${OPENSHELL_E2E_SUPERVISOR_BIN:-}" >>"$OPENSHELL_PARITY_TEST_CALLS" mkdir -p "$XDG_DATA_HOME/containers/storage" @@ -174,7 +206,7 @@ launch = { "supervisor_image_id": zero, "supervisor_image_digest": image_digest, "supervisor_runtime_image": supervisor_runtime, - "supervisor_base_image": "debian:bookworm-slim", + "supervisor_base_image": os.environ["OPENSHELL_PARITY_TEST_SUPERVISOR_BASE"], "supervisor_base_image_id": zero, "supervisor_base_image_digest": image_digest, "supervisor_base_runtime_image": base_runtime, @@ -335,6 +367,7 @@ run_harness() { if [ "${OPENSHELL_PARITY_TEST_KEEP_RESULTS_DIR:-0}" != 1 ]; then rm -rf -- "${WORKDIR}/results" fi + OPENSHELL_PARITY_TEST_SUPERVISOR_BASE="${TEST_SUPERVISOR_BASE}" \ OPENSHELL_PARITY_CAPABILITY_MANIFEST="${WORKDIR}/manifest.toml" \ OPENSHELL_PARITY_BASELINE_WORKTREE="${ROOT}" \ OPENSHELL_PARITY_PODMAN_WRAPPER="${WORKDIR}/bin/fake-wrapper" \ @@ -407,7 +440,7 @@ assert_not_contains "${WORKDIR}/results/baseline.json" '"scenarios"' assert_contains "${WORKDIR}/results/baseline.log" '"scenarios"' assert_contains "${WORKDIR}/results/baseline.conformance.json" '"passed":true' assert_contains "${WORKDIR}/podman-calls" 'pull ghcr.io/nvidia/openshell-community/sandboxes/base:latest' -assert_contains "${WORKDIR}/podman-calls" 'pull debian:bookworm-slim' +assert_contains "${WORKDIR}/podman-calls" "pull ${TEST_SUPERVISOR_BASE}" assert_contains "${WORKDIR}/podman-calls" 'unshare rm -rf -- ' assert_contains "${WORKDIR}/podman-calls" 'openshell-parity-run.' diff --git a/e2e/rust/tests/sandbox_lifecycle.rs b/e2e/rust/tests/sandbox_lifecycle.rs index 389fb72f0f..946d20802f 100644 --- a/e2e/rust/tests/sandbox_lifecycle.rs +++ b/e2e/rust/tests/sandbox_lifecycle.rs @@ -476,6 +476,44 @@ async fn detached_canonical_main_nonzero_exit_reaches_error() { sandbox.cleanup().await; } +#[tokio::test] +async fn canonical_main_and_exec_receive_declared_environment() { + for mode in ["--tty", "--no-tty"] { + let script = r#"printf 'declared_env=%s\n' "${REPRO_SENTINEL:-missing}"; while true; do sleep 1; done"#; + let mut sandbox = SandboxGuard::create_keep_with_args( + &[ + mode, + "--no-auto-providers", + "--env", + "REPRO_SENTINEL=present", + ], + &["sh", "-c", script], + "declared_env=", + ) + .await + .expect("create canonical process with declared environment"); + let initial = normalize_output(&sandbox.create_output); + let later = sandbox + .exec(&[ + "sh", + "-c", + r#"printf 'declared_env=%s\n' "${REPRO_SENTINEL:-missing}""#, + ]) + .await; + sandbox.cleanup().await; + + assert!( + initial.lines().any(|line| line == "declared_env=present"), + "initial process must receive declared environment ({mode}): {initial}" + ); + let later = normalize_output(&later.expect("exec environment probe")); + assert!( + later.lines().any(|line| line == "declared_env=present"), + "exec must receive the same declared environment ({mode}): {later}" + ); + } +} + #[tokio::test] async fn canonical_tty_main_uses_sandbox_environment() { let script = r#"printf 'canonical_env home=%s user=%s term=%s\n' "$HOME" "$USER" "$TERM"; while true; do sleep 1; done"#; diff --git a/e2e/support/debian-package-manifest.py b/e2e/support/debian-package-manifest.py new file mode 100644 index 0000000000..4a72025031 --- /dev/null +++ b/e2e/support/debian-package-manifest.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Read copied Debian/distroless package metadata without executing image tools.""" + +import sys +from pathlib import Path + + +def package_manifest(root: Path) -> list[str]: + status = root / "status" + sources = [status] if status.is_file() else [] + sources.extend( + path + for path in sorted((root / "status.d").glob("*")) + if path.is_file() and not path.name.endswith(".md5sums") + ) + packages = set() + for source in sources: + for stanza in source.read_text().strip().split("\n\n"): + if not stanza.strip(): + continue + fields = dict( + line.split(": ", 1) + for line in stanza.splitlines() + if not line.startswith((" ", "\t")) and ": " in line + ) + if "Status" in fields and not fields["Status"].endswith(" ok installed"): + continue + if not {"Package", "Version", "Architecture"} <= fields.keys(): + raise ValueError(f"Incomplete package metadata in {source}") + name = fields["Package"] + if fields.get("Multi-Arch") == "same": + name += ":" + fields["Architecture"] + packages.add(f"{name}={fields['Version']}") + if not packages: + raise ValueError(f"No installed Debian packages found in {root}") + return sorted(packages) + + +if __name__ == "__main__": + print("\n".join(package_manifest(Path(sys.argv[1])))) diff --git a/e2e/with-podman-gateway.sh b/e2e/with-podman-gateway.sh index 4240e5587e..058b389af1 100755 --- a/e2e/with-podman-gateway.sh +++ b/e2e/with-podman-gateway.sh @@ -151,6 +151,10 @@ export XDG_CONFIG_HOME="${WORKDIR}/config" cleanup() { local exit_code=$? + if [ -n "${SUPERVISOR_METADATA_CONTAINER:-}" ]; then + podman_cmd rm -f "${SUPERVISOR_METADATA_CONTAINER}" >/dev/null 2>&1 || true + fi + e2e_stop_gateway "${GATEWAY_PID}" "${GATEWAY_PID_FILE}" e2e_stop_process "${DRIVER_PID}" "external Podman compute driver" @@ -594,9 +598,14 @@ if ! [[ "${SUPERVISOR_BASE_IMAGE_ID}" =~ ^[0-9a-f]{64}$ ]] \ fi SUPERVISOR_PACKAGE_MANIFEST="${OPENSHELL_PARITY_SUPERVISOR_PACKAGE_CAPTURE:-${WORKDIR}/supervisor.packages.txt}" mkdir -p "$(dirname "${SUPERVISOR_PACKAGE_MANIFEST}")" -podman_cmd run --rm --network none --entrypoint /usr/bin/dpkg-query \ - "${SUPERVISOR_RUNTIME_IMAGE}" -W '-f=${binary:Package}=${Version}\n' \ - | LC_ALL=C sort >"${SUPERVISOR_PACKAGE_MANIFEST}" +# Distroless retains package metadata but has no dpkg-query executable. +# Copy from a stopped container so inventory does not execute image contents. +SUPERVISOR_METADATA_CONTAINER="$(podman_cmd create --network none "${SUPERVISOR_RUNTIME_IMAGE}")" +podman_cmd cp "${SUPERVISOR_METADATA_CONTAINER}:/var/lib/dpkg" "${WORKDIR}/supervisor-dpkg" +podman_cmd rm "${SUPERVISOR_METADATA_CONTAINER}" >/dev/null +SUPERVISOR_METADATA_CONTAINER="" +uv run --no-project python "${ROOT}/e2e/support/debian-package-manifest.py" \ + "${WORKDIR}/supervisor-dpkg" >"${SUPERVISOR_PACKAGE_MANIFEST}" SUPERVISOR_PACKAGE_MANIFEST_SHA256="$(sha256sum "${SUPERVISOR_PACKAGE_MANIFEST}" | cut -d' ' -f1)" echo "Using Podman supervisor image: ${SUPERVISOR_RUNTIME_IMAGE} (ID ${SUPERVISOR_IMAGE_ID}, digest ${SUPERVISOR_IMAGE_DIGEST}, base ${SUPERVISOR_BASE_IMAGE} ID ${SUPERVISOR_BASE_IMAGE_ID} digest ${SUPERVISOR_BASE_IMAGE_DIGEST}, packages ${SUPERVISOR_PACKAGE_MANIFEST_SHA256})" @@ -762,7 +771,7 @@ if [ -n "${OPENSHELL_PARITY_LAUNCH_MANIFEST_CAPTURE:-}" ]; then "${SUPERVISOR_BASE_IMAGE}" \ "${SUPERVISOR_BASE_IMAGE_ID}" \ "${SUPERVISOR_BASE_IMAGE_DIGEST}" \ - "${OPENSHELL_E2E_SUPERVISOR_BASE_RUNTIME_IMAGE:-${SUPERVISOR_BASE_IMAGE}@${SUPERVISOR_BASE_IMAGE_DIGEST}}" \ + "${OPENSHELL_E2E_SUPERVISOR_BASE_RUNTIME_IMAGE:-${SUPERVISOR_BASE_IMAGE%@*}@${SUPERVISOR_BASE_IMAGE_DIGEST}}" \ "${SUPERVISOR_PACKAGE_MANIFEST_SHA256}" \ "${SANDBOX_IMAGE_REQUEST}" \ "${SANDBOX_IMAGE_ID}" \ diff --git a/skills/debug-openshell-cluster/SKILL.md b/skills/debug-openshell-cluster/SKILL.md index 2b753cf3f9..a6b54ec677 100644 --- a/skills/debug-openshell-cluster/SKILL.md +++ b/skills/debug-openshell-cluster/SKILL.md @@ -187,6 +187,13 @@ Inspect sandbox OCSF configuration and finding events for the validation rationale, configured and effective modes, active generation, and the explicit `previous_policy_active` state. +The published supervisor image uses a shell-free distroless Debian 13 base. +Use container logs, engine inspection and the configured exec health probe for +diagnostics; `exec ... sh`, package installation and in-container shell scripts +are unavailable. Workload shells belong to the separate sandbox image. Preserve +the driver-selected UID and writable runtime/log mounts when reproducing a +supervisor startup failure. + ### Step 4: Check Docker-Backed Gateways ```bash