Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 2 additions & 13 deletions crates/openshell-sandbox/data/sandbox-policy.rego
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,11 @@ binary_allowed(policy, exec) if {
b.path == ancestor
}

# Binary matching: cmdline exact path (script interpreters — e.g. node runs claude script).
# When /usr/local/bin/claude has shebang #!/usr/bin/env node, the exe is /usr/bin/node
# but cmdline contains /usr/local/bin/claude as an argv entry.
binary_allowed(policy, exec) if {
some b
b := policy.binaries[_]
not contains(b.path, "*")
cp := exec.cmdline_paths[_]
b.path == cp
}

# Binary matching: glob pattern against path, any ancestor, or any cmdline path.
# Binary matching: glob pattern against path or any ancestor.
binary_allowed(policy, exec) if {
some b in policy.binaries
contains(b.path, "*")
all_paths := array.concat(array.concat([exec.path], exec.ancestors), exec.cmdline_paths)
all_paths := array.concat([exec.path], exec.ancestors)
some p in all_paths
glob.match(b.path, ["/"], p)
}
Expand Down
44 changes: 17 additions & 27 deletions crates/openshell-sandbox/src/opa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,10 @@ mod tests {
let input = NetworkInput {
host: "api.anthropic.com".into(),
port: 443,
binary_path: PathBuf::from("/usr/bin/node"),
binary_path: PathBuf::from("/usr/local/bin/claude"),
binary_sha256: "unused".into(),
ancestors: vec![],
cmdline_paths: vec![PathBuf::from("/usr/local/bin/claude")],
cmdline_paths: vec![],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(
Expand Down Expand Up @@ -828,10 +828,10 @@ mod tests {
let input = NetworkInput {
host: "API.ANTHROPIC.COM".into(),
port: 443,
binary_path: PathBuf::from("/usr/bin/node"),
binary_path: PathBuf::from("/usr/local/bin/claude"),
binary_sha256: "unused".into(),
ancestors: vec![],
cmdline_paths: vec![PathBuf::from("/usr/local/bin/claude")],
cmdline_paths: vec![],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(
Expand Down Expand Up @@ -885,10 +885,10 @@ mod tests {
let input = NetworkInput {
host: "api.anthropic.com".into(),
port: 443,
binary_path: PathBuf::from("/usr/bin/node"),
binary_path: PathBuf::from("/usr/local/bin/claude"),
binary_sha256: "unused".into(),
ancestors: vec![],
cmdline_paths: vec![PathBuf::from("/usr/local/bin/claude")],
cmdline_paths: vec![],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(decision.allowed);
Expand All @@ -902,10 +902,10 @@ mod tests {
let input = NetworkInput {
host: "api.anthropic.com".into(),
port: 443,
binary_path: PathBuf::from("/usr/bin/node"),
binary_path: PathBuf::from("/usr/local/bin/claude"),
binary_sha256: "unused".into(),
ancestors: vec![],
cmdline_paths: vec![PathBuf::from("/usr/local/bin/claude")],
cmdline_paths: vec![],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(decision.allowed);
Expand Down Expand Up @@ -1098,9 +1098,8 @@ network_policies:
}

#[test]
fn cmdline_path_matches_script_binary() {
// Simulates: node runs /usr/local/bin/my-tool (a script with shebang)
// exe = /usr/bin/node, cmdline contains /usr/local/bin/my-tool
fn cmdline_path_does_not_grant_identity() {
// cmdline is attacker-controlled and must not grant binary identity.
let cmdline_data = r"
network_policies:
script_test:
Expand All @@ -1120,12 +1119,7 @@ network_policies:
cmdline_paths: vec![PathBuf::from("/usr/local/bin/my-tool")],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(
decision.allowed,
"Expected allow via cmdline path match, got deny: {}",
decision.reason
);
assert_eq!(decision.matched_policy.as_deref(), Some("script_test"));
assert!(!decision.allowed);
}

#[test]
Expand Down Expand Up @@ -1156,7 +1150,7 @@ network_policies:
}

#[test]
fn cmdline_glob_pattern_matches() {
fn cmdline_glob_pattern_does_not_match() {
let glob_data = r#"
network_policies:
glob_test:
Expand All @@ -1170,17 +1164,13 @@ network_policies:
let input = NetworkInput {
host: "example.com".into(),
port: 443,
binary_path: PathBuf::from("/usr/bin/node"),
binary_path: PathBuf::from("/usr/local/bin/claude"),
binary_sha256: "unused".into(),
ancestors: vec![],
cmdline_paths: vec![PathBuf::from("/usr/local/bin/claude")],
cmdline_paths: vec![],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(
decision.allowed,
"Expected glob to match cmdline path, got deny: {}",
decision.reason
);
assert!(decision.allowed, "Expected glob to match executable path");
}

#[test]
Expand All @@ -1190,10 +1180,10 @@ network_policies:
let input = NetworkInput {
host: "api.anthropic.com".into(),
port: 443,
binary_path: PathBuf::from("/usr/bin/node"),
binary_path: PathBuf::from("/usr/local/bin/claude"),
binary_sha256: "unused".into(),
ancestors: vec![],
cmdline_paths: vec![PathBuf::from("/usr/local/bin/claude")],
cmdline_paths: vec![],
};
let decision = engine.evaluate_network(&input).unwrap();
assert!(
Expand Down
Loading