Skip to content

Commit 5e95b85

Browse files
committed
feat(sandbox): L7 protocol-aware inspection with TLS termination (!29)
## Summary Implements L7 (application-layer) protocol-aware policy enforcement for the sandbox proxy, enabling per-request allow/deny decisions based on HTTP method and path — not just host:port. ### Phase 1: HTTP/REST L7 Inspection (plaintext) - New `l7/` module with provider trait, REST parser, and relay loop - Parses HTTP/1.1 requests inside CONNECT tunnels, evaluates method+path against OPA/Rego policy - Supports `access` presets (`full`, `read-only`, `read-write`) and explicit `rules` with glob path matching - `enforcement` modes: `enforce` (deny with 403 JSON) vs `audit` (log only, forward traffic) - Structured logging for every L7 decision (`L7_REQUEST` with protocol, action, target, decision) ### Phase 2: MITM TLS Termination for HTTPS - Ephemeral CA generated at sandbox startup (`rcgen`) - Per-hostname leaf certificate cache (256 cap) for dynamic cert presentation - Client-side TLS termination (sandbox trusts ephemeral CA via `SSL_CERT_FILE`, `NODE_EXTRA_CA_CERTS`, etc.) - Upstream TLS connection with `webpki-roots` verification - Endpoints with `tls: terminate` get full L7 inspection; others remain passthrough ### Additional - Benign TLS/connection errors (close_notify, handshake EOF, reset) downgraded from WARN to DEBUG - Generic `AsyncRead + AsyncWrite` bounds throughout L7 module (works with both `TcpStream` and `TlsStream`) - Proto schema extended with `protocol`, `tls`, `enforcement`, `access`, `rules` fields on `NetworkEndpoint` - CLI `nav sandbox run` wired with `--rego-policy`/`--rego-data` flags and L7 policy display ### E2E Test Coverage - 7 L4 tests: no-matching-policy deny, wildcard binary, binary-restricted, wrong port, cross-policy isolation, non-CONNECT 405, structured log fields - 7 L7 TLS tests: full access allow, read-only deny POST, audit mode, explicit path rules, CA trust store injection, deny response JSON format, structured log fields ## Key Files | Area | Files | |------|-------| | L7 core | `l7/mod.rs`, `l7/provider.rs`, `l7/relay.rs`, `l7/rest.rs` | | TLS termination | `l7/tls.rs` | | Proxy integration | `proxy.rs`, `lib.rs`, `main.rs` | | Env/process | `process.rs`, `ssh.rs` | | OPA policy | `opa.rs`, `dev-sandbox-policy.rego` | | Proto schema | `proto/sandbox.proto` | | CLI | `navigator-cli/src/run.rs` | | E2E tests | `e2e/python/test_sandbox_policy.py` | ## Test plan - [x] 67 unit tests pass (`cargo test --workspace`) - [x] `cargo clippy --workspace --all-targets` clean - [x] 16 e2e policy tests pass (`mise run test:e2e:sandbox`) - [x] Manual smoke test: sandbox with `tls: terminate` on `api.anthropic.com:443` Closes #25
1 parent bc7ccfc commit 5e95b85

20 files changed

Lines changed: 3189 additions & 31 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,6 @@ mise.local.toml
185185

186186
# Ignore plans for now
187187
architecture/plans
188+
189+
# Claude
190+
.claude/settings.local.json

‎Cargo.lock‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ http-body-util = "0.1"
3333
tokio-rustls = "0.26"
3434
rustls = "0.23"
3535
rustls-pemfile = "2"
36+
rcgen = { version = "0.13", features = ["crypto", "pem"] }
37+
webpki-roots = "1"
3638

3739
# CLI
3840
clap = { version = "4.5", features = ["derive", "env"] }

‎crates/navigator-cli/src/run.rs‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,33 @@ struct DevNetworkPolicyRule {
12201220
struct DevNetworkEndpoint {
12211221
host: String,
12221222
port: u32,
1223+
#[serde(default)]
1224+
protocol: String,
1225+
#[serde(default)]
1226+
tls: String,
1227+
#[serde(default)]
1228+
enforcement: String,
1229+
#[serde(default)]
1230+
access: String,
1231+
#[serde(default)]
1232+
rules: Vec<DevL7Rule>,
1233+
}
1234+
1235+
#[derive(Debug, Deserialize)]
1236+
#[serde(deny_unknown_fields)]
1237+
struct DevL7Rule {
1238+
allow: DevL7Allow,
1239+
}
1240+
1241+
#[derive(Debug, Deserialize)]
1242+
#[serde(deny_unknown_fields)]
1243+
struct DevL7Allow {
1244+
#[serde(default)]
1245+
method: String,
1246+
#[serde(default)]
1247+
path: String,
1248+
#[serde(default)]
1249+
command: String,
12231250
}
12241251

12251252
#[derive(Debug, Deserialize)]
@@ -1260,6 +1287,21 @@ fn load_dev_sandbox_policy() -> Result<SandboxPolicy> {
12601287
.map(|e| NetworkEndpoint {
12611288
host: e.host,
12621289
port: e.port,
1290+
protocol: e.protocol,
1291+
tls: e.tls,
1292+
enforcement: e.enforcement,
1293+
access: e.access,
1294+
rules: e
1295+
.rules
1296+
.into_iter()
1297+
.map(|r| navigator_core::proto::L7Rule {
1298+
allow: Some(navigator_core::proto::L7Allow {
1299+
method: r.allow.method,
1300+
path: r.allow.path,
1301+
command: r.allow.command,
1302+
}),
1303+
})
1304+
.collect(),
12631305
})
12641306
.collect(),
12651307
binaries: rule
@@ -1387,6 +1429,31 @@ struct NetworkPolicyRuleYaml {
13871429
struct NetworkEndpointYaml {
13881430
host: String,
13891431
port: u32,
1432+
#[serde(skip_serializing_if = "String::is_empty")]
1433+
protocol: String,
1434+
#[serde(skip_serializing_if = "String::is_empty")]
1435+
tls: String,
1436+
#[serde(skip_serializing_if = "String::is_empty")]
1437+
enforcement: String,
1438+
#[serde(skip_serializing_if = "String::is_empty")]
1439+
access: String,
1440+
#[serde(skip_serializing_if = "Vec::is_empty")]
1441+
rules: Vec<L7RuleYaml>,
1442+
}
1443+
1444+
#[derive(Serialize)]
1445+
struct L7RuleYaml {
1446+
allow: L7AllowYaml,
1447+
}
1448+
1449+
#[derive(Serialize)]
1450+
struct L7AllowYaml {
1451+
#[serde(skip_serializing_if = "String::is_empty")]
1452+
method: String,
1453+
#[serde(skip_serializing_if = "String::is_empty")]
1454+
path: String,
1455+
#[serde(skip_serializing_if = "String::is_empty")]
1456+
command: String,
13901457
}
13911458

13921459
#[derive(Serialize)]
@@ -1432,6 +1499,24 @@ fn policy_to_yaml(policy: &SandboxPolicy) -> PolicyYaml {
14321499
.map(|e| NetworkEndpointYaml {
14331500
host: e.host.clone(),
14341501
port: e.port,
1502+
protocol: e.protocol.clone(),
1503+
tls: e.tls.clone(),
1504+
enforcement: e.enforcement.clone(),
1505+
access: e.access.clone(),
1506+
rules: e
1507+
.rules
1508+
.iter()
1509+
.map(|r| {
1510+
let a = r.allow.clone().unwrap_or_default();
1511+
L7RuleYaml {
1512+
allow: L7AllowYaml {
1513+
method: a.method,
1514+
path: a.path,
1515+
command: a.command,
1516+
},
1517+
}
1518+
})
1519+
.collect(),
14351520
})
14361521
.collect(),
14371522
binaries: rule

‎crates/navigator-sandbox/Cargo.toml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ rand_core = "0.6"
3636
# OPA policy evaluation (no default features — full-opa pulls in opa-runtime which requires git in build)
3737
regorus = { version = "0.9", default-features = false, features = ["std", "arc", "glob", "yaml"] }
3838

39+
# TLS
40+
tokio-rustls = { workspace = true }
41+
rustls = { workspace = true }
42+
rustls-pemfile = { workspace = true }
43+
rcgen = { workspace = true }
44+
webpki-roots = { workspace = true }
45+
3946
# Serialization
4047
serde_json = { workspace = true }
48+
serde_yaml = { workspace = true }
4149

4250
# Logging
4351
tracing = { workspace = true }

0 commit comments

Comments
 (0)