Skip to content

Commit 663e552

Browse files
authored
fix(sandbox): thread L4-resolved pid into interactive decision request
The three relay Interactive enforcement arms previously sent pid: None in the decision request body. Carry the PID resolved for the L4 network decision (decision.binary_pid -- the same identity the allow/deny path binds to) through a new additive L7EvalContext.binary_pid field, and forward it as InteractiveContext.pid. The shared L4 resolve_process_identity / ConnectDecision are untouched, so existing allow/deny behavior is unchanged; only the Interactive decision request now reports the pid. Resolves REVIEW_CHANGES.md W5 and CLAUDE.md limitation NVIDIA#5. Signed-off-by: Vishal Punjabi <[email protected]>
1 parent 4ac551a commit 663e552

8 files changed

Lines changed: 388 additions & 5 deletions

File tree

‎crates/openshell-sandbox/src/bypass_monitor.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,9 @@ mod tests {
568568
libc::waitpid(child_pid, std::ptr::null_mut(), 0);
569569
}
570570

571+
// The parent (this test process) and the forked child both hold the same
572+
// socket inode with different identities, so the audit row must surface
573+
// the ambiguity rather than silently attributing to one PID.
571574
assert_eq!(binary, "ambiguous");
572575
assert!(pid.contains(&std::process::id().to_string()));
573576
assert!(pid.contains(&child_pid.to_string()));

‎crates/openshell-sandbox/src/l7/graphql.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,7 @@ network_policies:
798798
port: 8080,
799799
policy_name: "test_graphql_l7".to_string(),
800800
binary_path: "/usr/bin/python3".to_string(),
801+
binary_pid: None,
801802
ancestors: Vec::new(),
802803
cmdline_paths: Vec::new(),
803804
secret_resolver: None,

‎crates/openshell-sandbox/src/l7/interactive.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ pub(crate) struct InteractiveContext<'a> {
141141
/// Absolute path of the initiating binary.
142142
pub binary: &'a str,
143143
/// PID of the initiating binary, if available.
144-
/// Relay call sites pass `None` because `L7EvalContext` does not carry
145-
/// PID; threading it through is a future improvement.
144+
/// Relay call sites pass the PID resolved at L4 CONNECT time (carried on
145+
/// `L7EvalContext`), i.e. the same PID the allow/deny path bound to. `None`
146+
/// when L4 could not resolve a single owner.
146147
pub pid: Option<u32>,
147148
/// HTTP method (e.g. `"GET"`).
148149
pub method: &'a str,

‎crates/openshell-sandbox/src/l7/relay.rs‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ pub struct L7EvalContext {
3131
pub policy_name: String,
3232
/// Binary path (for cross-layer Rego evaluation).
3333
pub binary_path: String,
34+
/// PID that owned the socket at L4 CONNECT time, as resolved for the
35+
/// network decision. Carried so interactive enforcement can report the
36+
/// same PID the allow/deny path bound to. `None` when L4 could not resolve
37+
/// a single owner.
38+
pub binary_pid: Option<u32>,
3439
/// Ancestor paths.
3540
pub ancestors: Vec<String>,
3641
/// Cmdline paths.
@@ -343,7 +348,7 @@ where
343348
host: &ctx.host,
344349
port: ctx.port,
345350
binary: &ctx.binary_path,
346-
pid: None,
351+
pid: ctx.binary_pid,
347352
method: &request_info.action,
348353
path: &redacted_target,
349354
protocol: protocol_str,
@@ -779,7 +784,7 @@ where
779784
host: &ctx.host,
780785
port: ctx.port,
781786
binary: &ctx.binary_path,
782-
pid: None,
787+
pid: ctx.binary_pid,
783788
method: &request_info.action,
784789
path: &redacted_target,
785790
protocol: protocol_str,
@@ -1054,7 +1059,7 @@ where
10541059
host: &ctx.host,
10551060
port: ctx.port,
10561061
binary: &ctx.binary_path,
1057-
pid: None,
1062+
pid: ctx.binary_pid,
10581063
method: &request_info.action,
10591064
path: &redacted_target,
10601065
// S1: "graphql" is hardcoded here (not derived from
@@ -1496,6 +1501,7 @@ network_policies:
14961501
port: 443,
14971502
policy_name: "ws_api".into(),
14981503
binary_path: "/usr/bin/node".into(),
1504+
binary_pid: None,
14991505
ancestors: vec![],
15001506
cmdline_paths: vec![],
15011507
secret_resolver: None,
@@ -1554,6 +1560,7 @@ network_policies:
15541560
port: 443,
15551561
policy_name: "route_api".into(),
15561562
binary_path: "/usr/bin/node".into(),
1563+
binary_pid: None,
15571564
ancestors: vec![],
15581565
cmdline_paths: vec![],
15591566
secret_resolver: None,
@@ -1658,6 +1665,7 @@ network_policies:
16581665
port: 443,
16591666
policy_name: "route_api".into(),
16601667
binary_path: "/usr/bin/node".into(),
1668+
binary_pid: None,
16611669
ancestors: vec![],
16621670
cmdline_paths: vec![],
16631671
secret_resolver: resolver.map(Arc::new),
@@ -1775,6 +1783,7 @@ network_policies:
17751783
port: 443,
17761784
policy_name: "route_api".into(),
17771785
binary_path: "/usr/bin/node".into(),
1786+
binary_pid: None,
17781787
ancestors: vec![],
17791788
cmdline_paths: vec![],
17801789
secret_resolver: resolver.map(Arc::new),
@@ -1945,6 +1954,7 @@ network_policies:
19451954
port: 8080,
19461955
policy_name: "rest_api".into(),
19471956
binary_path: "/usr/bin/curl".into(),
1957+
binary_pid: None,
19481958
ancestors: vec![],
19491959
cmdline_paths: vec![],
19501960
secret_resolver: None,
@@ -2032,6 +2042,7 @@ network_policies:
20322042
port: 8080,
20332043
policy_name: "rest_api".into(),
20342044
binary_path: "/usr/bin/curl".into(),
2045+
binary_pid: None,
20352046
ancestors: vec![],
20362047
cmdline_paths: vec![],
20372048
secret_resolver: None,

‎crates/openshell-sandbox/src/l7/websocket.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,7 @@ network_policies:
13051305
port: 443,
13061306
policy_name: "graphql_ws".into(),
13071307
binary_path: "/usr/bin/node".into(),
1308+
binary_pid: None,
13081309
ancestors: vec![],
13091310
cmdline_paths: vec![],
13101311
secret_resolver: None,

‎crates/openshell-sandbox/src/proxy.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ async fn handle_tcp_connection(
834834
.as_ref()
835835
.map(|p| p.to_string_lossy().into_owned())
836836
.unwrap_or_default(),
837+
binary_pid: decision.binary_pid,
837838
ancestors: decision
838839
.ancestors
839840
.iter()
@@ -2912,6 +2913,7 @@ async fn handle_forward_proxy(
29122913
.as_ref()
29132914
.map(|p| p.to_string_lossy().into_owned())
29142915
.unwrap_or_default(),
2916+
binary_pid: decision.binary_pid,
29152917
ancestors: decision
29162918
.ancestors
29172919
.iter()
@@ -3885,6 +3887,7 @@ mod tests {
38853887
port,
38863888
policy_name: policy_name.to_string(),
38873889
binary_path: "/usr/bin/node".to_string(),
3890+
binary_pid: None,
38883891
ancestors: vec![],
38893892
cmdline_paths: vec![],
38903893
secret_resolver: None,
@@ -4050,6 +4053,7 @@ mod tests {
40504053
port: 80,
40514054
policy_name: "ws_api".to_string(),
40524055
binary_path: "/usr/bin/node".to_string(),
4056+
binary_pid: None,
40534057
ancestors: vec![],
40544058
cmdline_paths: vec![],
40554059
secret_resolver: resolver,
@@ -4090,6 +4094,7 @@ mod tests {
40904094
port: 80,
40914095
policy_name: "rest_api".to_string(),
40924096
binary_path: "/usr/bin/node".to_string(),
4097+
binary_pid: None,
40934098
ancestors: vec![],
40944099
cmdline_paths: vec![],
40954100
secret_resolver: None,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# CLAUDE.md — guidance for a Claude Code session picking up interactive enforcement
2+
3+
You're working on `vshalpnjabi/OpenShell`, branch
4+
`1-interactive-enforcement/vshalpnjabi`. The feature adds
5+
`EnforcementMode::Interactive` to the sandbox proxy — holds a denied request
6+
open while consulting an HTTP decision endpoint.
7+
8+
Full design background: `DESIGN.md` in this directory.
9+
agentbox integration guide: `AGENTBOX_INTEGRATION.md` in this directory.
10+
11+
## Current status (as of 2026-05-28)
12+
13+
**All phases complete. PR is open at `vshalpnjabi/OpenShell#2` (draft).**
14+
15+
| Phase | Status |
16+
|-------|--------|
17+
| 1 — Types + policy schema | ✅ Done |
18+
| 2 — `consult_interactive_endpoint()` + tests | ✅ Done |
19+
| 3 — Wired into proxy decision path | ✅ Done |
20+
| 4 — Compile-check + tests + commits | ✅ Done |
21+
| 5 — Security review findings addressed | ✅ Done |
22+
| 6 — PR open (`vshalpnjabi/OpenShell#2`) | ✅ Done (draft) |
23+
24+
## Latest commit (HEAD)
25+
26+
```
27+
4ac551a fix(sandbox): warn when interactive enforcement falls back in WebSocket path
28+
bc9175e fix(sandbox): address security review findings in interactive enforcement
29+
491afc7 fix(sandbox): cap interactive enforcement timeout at 300 s
30+
e0f6152 fix(sandbox): remove unnecessary clone in websocket enforcement match
31+
25a1ac3 style: apply rustfmt to all changed files
32+
d203fdf docs(sandbox): document interactive enforcement mode
33+
ed6734c chore(docs): add fork-local Claude Code session instructions
34+
```
35+
36+
## Key implementation files
37+
38+
| File | What changed |
39+
|------|-------------|
40+
| `crates/openshell-sandbox/src/l7/interactive.rs` | Core decision client; 15 tests |
41+
| `crates/openshell-sandbox/src/l7/mod.rs` | `EnforcementMode::Interactive` with `secret`; `INTERACTIVE_MAX_TIMEOUT = 300s` |
42+
| `crates/openshell-sandbox/src/l7/relay.rs` | Interactive arms destructure `secret`, pass to client |
43+
| `crates/openshell-sandbox/src/l7/websocket.rs` | Interactive fallback arms with `tracing::warn!` |
44+
| `crates/openshell-sandbox/src/proxy.rs` | Interactive arm: strips query string, passes `secret` |
45+
| `crates/openshell-policy/src/lib.rs` | `InteractiveEnforcementDef { secret }`, SSRF validation |
46+
| `crates/openshell-sandbox/Cargo.toml` | Added `serial_test = "3"` to dev-deps |
47+
48+
## Known limitations (documented, not blocking)
49+
50+
1. **`{`-heuristic proto encoding** — the policy value is stored as a JSON
51+
string in the proto `enforcement` field; detected by `{`-prefix. Needs a
52+
wire-breaking proto schema change to fix cleanly.
53+
2. **Hostname-based SSRF gap** — SSRF check blocks loopback/link-local IPs but
54+
not hostnames that resolve to them. DNS resolution at validation time is
55+
unreliable; fix belongs in a separate PR.
56+
3. **`secret` in plaintext YAML** — no secret injection mechanism yet.
57+
Separate feature request.
58+
4. **WebSocket per-message fallback** — Interactive cannot consult the async
59+
decision endpoint from the sync WebSocket per-message path; applies
60+
`fallback` and logs `warn!`. Fixing this requires an async WebSocket
61+
refactor phase.
62+
5. ~~**`pid` is `None` at relay sites**~~ — RESOLVED. `L7EvalContext` now
63+
carries `binary_pid` (the PID resolved for the L4 network decision, i.e. the
64+
same identity the allow/deny path bound to), and the three relay Interactive
65+
arms forward it as `InteractiveContext.pid`. The shared L4 identity
66+
resolution is unchanged, so allow/deny behavior is unaffected — only the
67+
Interactive decision request now includes the pid.
68+
69+
## Test results (last full run)
70+
71+
- `cargo test -p openshell-sandbox --lib` — 868 tests, all pass
72+
- `cargo test -p openshell-policy --lib` — all pass
73+
74+
## Wire protocol summary
75+
76+
POST `<endpoint>` with:
77+
78+
```json
79+
{
80+
"schema_version": 1,
81+
"request_id": "<uuid>",
82+
"sandbox_name": "<name>",
83+
"binary": "/path/to/binary",
84+
"pid": 1234,
85+
"host": "api.example.com",
86+
"port": 443,
87+
"method": "GET",
88+
"path": "/some/path",
89+
"protocol": "rest",
90+
"policy_name": "my-policy"
91+
}
92+
```
93+
94+
`Authorization: Bearer <secret>` header is added when `secret` is set.
95+
96+
Response: `{"decision":"allow"|"deny","reason":"..."}`, HTTP 2xx.
97+
98+
## Build notes
99+
100+
See `SANDBOX_BUILD_ENVIRONMENT.md` (in memory) for tarball bootstrap and
101+
gold-linker OOM workaround details.
102+
103+
```bash
104+
cargo build --workspace
105+
cargo test -p openshell-policy
106+
cargo test -p openshell-sandbox --lib -- l7::interactive
107+
```

0 commit comments

Comments
 (0)