Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(policy): close reviewed parser gaps
Signed-off-by: Johnny Greco <[email protected]>
  • Loading branch information
johnnygreco committed Sep 15, 2026
commit 7e3df3f6c149271653fb947062f4ed947cdc44c8
26 changes: 25 additions & 1 deletion crates/openshell-policy-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ pub enum ParameterMatcher {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AnyMatcher {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub any: Vec<String>,
Expand Down Expand Up @@ -1320,6 +1319,31 @@ mod tests {
assert!(parse_document(source, ParseProfile::RuntimeStrict).is_err());
}

#[test]
fn containment_retains_unknown_matcher_fields() {
let source = r#"
version: 1
network_policies:
api:
endpoints:
- host: example.com
port: 443
rules:
- allow:
query:
q:
any: ["one"]
future_constraint: true
"#;
let parsed = parse_document(source, ParseProfile::ContainmentInput).unwrap();
assert_eq!(parsed.extensions.len(), 1);
assert_eq!(
parsed.extensions[0].path,
"network_policies.api.endpoints[0].rules[0].allow.query.q.future_constraint"
);
assert!(parse_document(source, ParseProfile::RuntimeStrict).is_err());
}

#[test]
fn open_user_maps_do_not_become_extensions() {
let source = r#"
Expand Down
26 changes: 14 additions & 12 deletions crates/openshell-policy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,16 @@ pub fn parse_sandbox_policy(yaml: &str) -> Result<SandboxPolicy> {
to_proto(raw)
}

/// Parse a sandbox policy from a regular file using the shared bounded reader.
pub fn parse_sandbox_policy_file(path: &Path) -> Result<SandboxPolicy> {
let raw = openshell_policy_schema::parse_policy_file(
path,
openshell_policy_schema::ParseProfile::RuntimeStrict,
openshell_policy_schema::ParseLimits::default(),
)?;
to_proto(raw)
}

/// Serialize a proto sandbox policy to a YAML string.
///
/// This is the inverse of [`parse_sandbox_policy`] — the output uses the
Expand Down Expand Up @@ -914,22 +924,14 @@ pub fn serialize_sandbox_policy_json(policy: &SandboxPolicy) -> Result<String> {
/// caller to omit the policy and let the server / sandbox apply its own
/// default.
pub fn load_sandbox_policy(cli_path: Option<&str>) -> Result<Option<SandboxPolicy>> {
let document = if let Some(p) = cli_path {
openshell_policy_schema::parse_policy_file(
Path::new(p),
openshell_policy_schema::ParseProfile::RuntimeStrict,
openshell_policy_schema::ParseLimits::default(),
)?
let policy = if let Some(p) = cli_path {
parse_sandbox_policy_file(Path::new(p))?
} else if let Ok(policy_path) = std::env::var("OPENSHELL_SANDBOX_POLICY") {
openshell_policy_schema::parse_policy_file(
Path::new(&policy_path),
openshell_policy_schema::ParseProfile::RuntimeStrict,
openshell_policy_schema::ParseLimits::default(),
)?
parse_sandbox_policy_file(Path::new(&policy_path))?
} else {
return Ok(None);
};
to_proto(document).map(Some)
Ok(Some(policy))
}

/// Well-known path where a sandbox container image can ship a policy YAML file.
Expand Down
45 changes: 30 additions & 15 deletions crates/openshell-sandbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2641,10 +2641,10 @@ fn discover_policy_from_disk_or_default() -> openshell_core::proto::SandboxPolic
/// hardcoded restrictive default if the file is missing or invalid.
fn discover_policy_from_path(path: &std::path::Path) -> openshell_core::proto::SandboxPolicy {
use openshell_policy::{
parse_sandbox_policy, restrictive_default_policy, validate_sandbox_policy,
parse_sandbox_policy_file, restrictive_default_policy, validate_sandbox_policy,
};

let Ok(yaml) = std::fs::read_to_string(path) else {
if !path.exists() {
ocsf_emit!(
ConfigStateChangeBuilder::new(ocsf_ctx())
.severity(SeverityId::Informational)
Expand All @@ -2657,20 +2657,20 @@ fn discover_policy_from_path(path: &std::path::Path) -> openshell_core::proto::S
.build()
);
return restrictive_default_policy();
};
ocsf_emit!(
ConfigStateChangeBuilder::new(ocsf_ctx())
.severity(SeverityId::Informational)
.status(StatusId::Success)
.state(StateId::Enabled, "loaded")
.message(format!(
"Loaded sandbox policy from container disk [path:{}]",
path.display()
))
.build()
);
match parse_sandbox_policy(&yaml) {
}
match parse_sandbox_policy_file(path) {
Ok(policy) => {
ocsf_emit!(
ConfigStateChangeBuilder::new(ocsf_ctx())
.severity(SeverityId::Informational)
.status(StatusId::Success)
.state(StateId::Enabled, "loaded")
.message(format!(
"Loaded sandbox policy from container disk [path:{}]",
path.display()
))
.build()
);
// Validate the disk-loaded policy for safety.
if let Err(violations) = validate_sandbox_policy(&policy) {
let messages: Vec<String> = violations.iter().map(ToString::to_string).collect();
Expand Down Expand Up @@ -4884,6 +4884,21 @@ network_policies:
assert!(policy.filesystem.is_some());
}

#[test]
fn discover_policy_from_oversized_file_returns_restrictive_default() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("policy.yaml");
let oversized = format!("version: 1\n{}", " ".repeat(4 * 1024 * 1024));
std::fs::write(&path, oversized).unwrap();

let policy = discover_policy_from_path(&path);
assert!(policy.network_policies.is_empty());
assert!(
policy.filesystem.is_some(),
"an oversized otherwise-valid policy must fall back instead of parsing"
);
}

#[test]
fn discover_policy_from_unsafe_yaml_falls_back_to_default() {
let dir = tempfile::tempdir().unwrap();
Expand Down
Loading