Skip to content

Commit fc09297

Browse files
authored
fix(policy): harden advisor transport proposals (ai-openshell#3136)
Signed-off-by: John Myers <johntmyers@users.noreply.github.com> Co-authored-by: John Myers <johntmyers@users.noreply.github.com>
1 parent 8605943 commit fc09297

10 files changed

Lines changed: 218 additions & 14 deletions

File tree

‎architecture/security-policy.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ raw relay by default. A `protocol: rest` endpoint can opt in to
9797
after an allowed `101` upgrade; server-to-client traffic and all other upgraded
9898
protocols remain raw passthrough.
9999

100+
A `protocol: tcp` hostname is a connection-routing constraint, not an
101+
application-authority boundary. Transparent capture validates the approved DNS
102+
name, pinned destination address, port, and calling binary before opening the
103+
stream, but it does not inspect TLS SNI, HTTP `Host`, or another protocol-level
104+
destination. Compatible shared infrastructure can therefore let a client
105+
select another tenant, virtual host, or service behind the approved front door.
106+
100107
## Credentialed Endpoints
101108

102109
OpenShell keeps provider credentials on paths it can inspect or rewrite by
@@ -216,6 +223,12 @@ through the proposal loop instead of treating the denial as terminal.
216223

217224
1. **Submit.** Both proposers POST through the same `SubmitPolicyAnalysis`
218225
path. Each chunk is persisted with its `analysis_mode` for audit provenance.
226+
Agent-authored chunks cannot request `protocol: tcp` or `tls: skip`; the
227+
sandbox-local API rejects those transport choices for immediate feedback,
228+
and the gateway repeats the check before persistence.
229+
Omitted-protocol endpoints remain available through the explicit proxy with
230+
default TLS termination and HTTP authority checks. Administrators can still
231+
author native TCP and raw TLS policy directly.
219232
2. **Build and validate the candidate.** The gateway first canonicalizes a
220233
mechanistic proposal against the live effective policy. If an endpoint is
221234
already governed by an inspected or provider-owned contract, the candidate

‎crates/openshell-policy/src/l7_validate.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ pub fn is_explicit_tcp_protocol(protocol: &str) -> bool {
4949
protocol.eq_ignore_ascii_case("tcp")
5050
}
5151

52+
/// Reject transport choices that an in-sandbox agent must not grant itself.
53+
///
54+
/// An omitted protocol remains allowed: it uses the established explicit
55+
/// proxy, which canonicalizes forward HTTP authorities and terminates TLS by
56+
/// default. Native transparent TCP and `tls: skip` bypass those application
57+
/// authority checks, so only an administrator may author them directly.
58+
pub fn agent_authored_transport_rejection(protocol: &str, tls: &str) -> Option<&'static str> {
59+
if is_explicit_tcp_protocol(protocol) {
60+
return Some(
61+
"agent-authored proposals cannot request protocol tcp; ask an administrator to add native TCP access explicitly",
62+
);
63+
}
64+
if tls.eq_ignore_ascii_case("skip") {
65+
return Some(
66+
"agent-authored proposals cannot request tls: skip; ask an administrator to add raw TLS access explicitly",
67+
);
68+
}
69+
None
70+
}
71+
5272
/// Reject additional L7-only fields represented outside
5373
/// [`L7EndpointFields`] by the runtime and provider-profile schemas.
5474
///
@@ -69,6 +89,24 @@ pub fn validate_explicit_tcp_additional_fields(
6989
)]
7090
}
7191

92+
#[cfg(test)]
93+
mod agent_transport_tests {
94+
use super::agent_authored_transport_rejection;
95+
96+
#[test]
97+
fn omitted_protocol_with_default_tls_remains_available_to_agents() {
98+
assert_eq!(agent_authored_transport_rejection("", ""), None);
99+
}
100+
101+
#[test]
102+
fn agent_cannot_request_native_tcp_or_skip_tls_inspection() {
103+
assert!(agent_authored_transport_rejection("tcp", "").is_some());
104+
assert!(agent_authored_transport_rejection("TCP", "terminate").is_some());
105+
assert!(agent_authored_transport_rejection("", "skip").is_some());
106+
assert!(agent_authored_transport_rejection("rest", "SKIP").is_some());
107+
}
108+
}
109+
72110
/// Fields extracted from an endpoint definition needed for L7 semantic
73111
/// validation. Both profile lint and the runtime validator construct this
74112
/// from their own data representation.

‎crates/openshell-policy/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub use compose::{
3737
is_provider_rule_name, provider_rule_name, strip_provider_rule_names,
3838
};
3939
pub use l7_validate::{
40-
L7EndpointFields, L7Protocol, validate_explicit_tcp_additional_fields,
41-
validate_l7_endpoint_semantics,
40+
L7EndpointFields, L7Protocol, agent_authored_transport_rejection,
41+
validate_explicit_tcp_additional_fields, validate_l7_endpoint_semantics,
4242
};
4343
pub use merge::{
4444
PolicyMergeError, PolicyMergeOp, PolicyMergeResult, PolicyMergeWarning,

‎crates/openshell-server/src/grpc/policy.rs‎

Lines changed: 104 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,6 +4306,18 @@ pub(super) async fn handle_submit_policy_analysis(
43064306
}
43074307

43084308
let rule_ref = chunk.proposed_rule.as_ref().expect("checked above");
4309+
if req.analysis_mode == "agent_authored"
4310+
&& let Some(reason) = rule_ref.endpoints.iter().find_map(|endpoint| {
4311+
openshell_policy::agent_authored_transport_rejection(
4312+
&endpoint.protocol,
4313+
&endpoint.tls,
4314+
)
4315+
})
4316+
{
4317+
rejected += 1;
4318+
rejection_reasons.push(format!("chunk '{}': {reason}", chunk.rule_name));
4319+
continue;
4320+
}
43094321
let incoming_observation_key = rule_ref.endpoints.first().and_then(|endpoint| {
43104322
rule_ref.binaries.first().map(|binary| {
43114323
(
@@ -11298,7 +11310,7 @@ mod tests {
1129811310
}
1129911311

1130011312
#[tokio::test]
11301-
async fn approve_all_skips_later_tls_conflict_and_applies_compatible_prefix() {
11313+
async fn approve_all_skips_later_endpoint_conflict_and_applies_compatible_prefix() {
1130211314
let state = test_server_state().await;
1130311315
let sandbox_id = "sb-approve-all-conflict";
1130411316
let sandbox_name = "approve-all-conflict";
@@ -11339,13 +11351,15 @@ mod tests {
1133911351
..Default::default()
1134011352
},
1134111353
PolicyChunk {
11342-
rule_name: "passthrough".to_string(),
11354+
rule_name: "conflicting".to_string(),
1134311355
proposed_rule: Some(NetworkPolicyRule {
11344-
name: "passthrough".to_string(),
11356+
name: "conflicting".to_string(),
1134511357
endpoints: vec![NetworkEndpoint {
1134611358
host: "shared.example.com".to_string(),
1134711359
port: 443,
11348-
tls: "skip".to_string(),
11360+
protocol: "graphql".to_string(),
11361+
enforcement: "enforce".to_string(),
11362+
access: "read-only".to_string(),
1134911363
advisor_proposed: true,
1135011364
..Default::default()
1135111365
}],
@@ -11423,7 +11437,7 @@ mod tests {
1142311437
.unwrap();
1142411438
let policy = ProtoSandboxPolicy::decode(revision.policy_payload.as_slice()).unwrap();
1142511439
assert!(policy.network_policies.contains_key("inspected"));
11426-
assert!(!policy.network_policies.contains_key("passthrough"));
11440+
assert!(!policy.network_policies.contains_key("conflicting"));
1142711441
}
1142811442

1142911443
#[tokio::test]
@@ -13886,6 +13900,91 @@ mod tests {
1388613900
);
1388713901
}
1388813902

13903+
#[tokio::test]
13904+
async fn agent_authored_submit_rejects_native_tcp_and_tls_skip_but_allows_explicit_proxy() {
13905+
use openshell_core::proto::{NetworkBinary, NetworkEndpoint, NetworkPolicyRule};
13906+
13907+
let state = test_server_state().await;
13908+
let sandbox_name = "reject-agent-raw-transports";
13909+
state
13910+
.store
13911+
.put_message(&test_sandbox(
13912+
"sb-reject-agent-raw-transports",
13913+
sandbox_name,
13914+
ProtoSandboxPolicy::default(),
13915+
vec![],
13916+
))
13917+
.await
13918+
.unwrap();
13919+
13920+
let endpoint = |protocol: &str, tls: &str| NetworkEndpoint {
13921+
host: "api.example.com".to_string(),
13922+
port: 443,
13923+
protocol: protocol.to_string(),
13924+
tls: tls.to_string(),
13925+
..Default::default()
13926+
};
13927+
let chunk = |name: &str, endpoint: NetworkEndpoint| PolicyChunk {
13928+
rule_name: name.to_string(),
13929+
proposed_rule: Some(NetworkPolicyRule {
13930+
name: name.to_string(),
13931+
endpoints: vec![endpoint],
13932+
binaries: vec![NetworkBinary {
13933+
path: "/usr/bin/curl".to_string(),
13934+
..Default::default()
13935+
}],
13936+
}),
13937+
..Default::default()
13938+
};
13939+
13940+
let response = handle_submit_policy_analysis(
13941+
&state,
13942+
with_user(Request::new(SubmitPolicyAnalysisRequest {
13943+
name: sandbox_name.to_string(),
13944+
analysis_mode: "agent_authored".to_string(),
13945+
proposed_chunks: vec![
13946+
chunk("native_tcp", endpoint("tcp", "")),
13947+
chunk("raw_tls", endpoint("", "skip")),
13948+
chunk("explicit_proxy", endpoint("", "")),
13949+
],
13950+
..Default::default()
13951+
})),
13952+
)
13953+
.await
13954+
.unwrap()
13955+
.into_inner();
13956+
13957+
assert_eq!(response.accepted_chunks, 1);
13958+
assert_eq!(response.rejected_chunks, 2);
13959+
assert_eq!(response.rejection_reasons.len(), 2);
13960+
assert!(
13961+
response
13962+
.rejection_reasons
13963+
.iter()
13964+
.any(|reason| reason.contains("protocol tcp"))
13965+
);
13966+
assert!(
13967+
response
13968+
.rejection_reasons
13969+
.iter()
13970+
.any(|reason| reason.contains("tls: skip"))
13971+
);
13972+
13973+
let draft = handle_get_draft_policy(
13974+
&state,
13975+
with_user(Request::new(GetDraftPolicyRequest {
13976+
name: sandbox_name.to_string(),
13977+
status_filter: String::new(),
13978+
workspace: "default".to_string(),
13979+
})),
13980+
)
13981+
.await
13982+
.unwrap()
13983+
.into_inner();
13984+
assert_eq!(draft.chunks.len(), 1);
13985+
assert_eq!(draft.chunks[0].rule_name, "explicit_proxy");
13986+
}
13987+
1388913988
#[tokio::test]
1389013989
async fn approve_draft_chunk_rejects_stored_reserved_provider_rule_name() {
1389113990
use openshell_core::proto::{NetworkBinary, NetworkEndpoint, NetworkPolicyRule};

‎crates/openshell-supervisor-network/src/policy_local.rs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,11 @@ fn network_endpoint_from_json(
11121112
if endpoint.host.trim().is_empty() {
11131113
return Err("endpoint.host is required".to_string());
11141114
}
1115+
if let Some(reason) =
1116+
openshell_policy::agent_authored_transport_rejection(&endpoint.protocol, &endpoint.tls)
1117+
{
1118+
return Err(reason.to_string());
1119+
}
11151120

11161121
let mut ports = endpoint.ports;
11171122
if ports.is_empty() && endpoint.port > 0 {
@@ -1484,6 +1489,47 @@ mod tests {
14841489
assert!(!error.contains("secret"));
14851490
}
14861491

1492+
#[test]
1493+
fn proposal_chunks_from_body_rejects_native_tcp_and_tls_skip() {
1494+
for endpoint in [
1495+
r#"{"host":"db.example.com","port":5432,"protocol":"tcp"}"#,
1496+
r#"{"host":"api.example.com","port":443,"tls":"skip"}"#,
1497+
] {
1498+
let body = format!(
1499+
r#"{{
1500+
"operations": [{{
1501+
"addRule": {{
1502+
"ruleName": "raw_transport",
1503+
"rule": {{"endpoints": [{endpoint}]}}
1504+
}}
1505+
}}]
1506+
}}"#
1507+
);
1508+
1509+
let error = proposal_chunks_from_body(body.as_bytes()).unwrap_err();
1510+
assert!(error.contains("administrator"), "unexpected error: {error}");
1511+
}
1512+
}
1513+
1514+
#[test]
1515+
fn proposal_chunks_from_body_accepts_omitted_protocol_with_default_tls() {
1516+
let body = br#"{
1517+
"operations": [{
1518+
"addRule": {
1519+
"ruleName": "explicit_proxy",
1520+
"rule": {
1521+
"endpoints": [{"host":"api.example.com","port":443}]
1522+
}
1523+
}
1524+
}]
1525+
}"#;
1526+
1527+
let chunks = proposal_chunks_from_body(body).unwrap();
1528+
let endpoint = &chunks[0].proposed_rule.as_ref().unwrap().endpoints[0];
1529+
assert!(endpoint.protocol.is_empty());
1530+
assert!(endpoint.tls.is_empty());
1531+
}
1532+
14871533
#[test]
14881534
fn parse_last_query_clamps_to_max() {
14891535
assert_eq!(parse_last_query("last=5"), Some(5));

‎crates/openshell-supervisor-process/src/skills/policy_advisor.md‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ operations. Each `addRule` carries a complete narrow `NetworkPolicyRule`.
5050
credential is in scope auto-approve** (see Auto-approval below). Any
5151
credentialed reach or capability change goes to human review — that is
5252
the design. L7 is still the agent-speed path because the prover can
53-
precisely describe the change (which method was added on which path);
54-
L4 to a credentialed host loses that precision. Use L4 only when the
55-
binary's wire protocol is opaque to L7 inspection (`ssh`, `nc`,
56-
`git-remote-http`) or the host has no documented REST surface.
53+
precisely describe the change (which method was added on which path).
54+
When a client can use the explicit proxy but its wire protocol is opaque
55+
to L7 inspection (`ssh`, `nc`, `git-remote-http`), omit `protocol` and
56+
retain default TLS handling. Never propose `protocol: tcp` or `tls: skip`:
57+
those choices bypass application-authority inspection and require an
58+
administrator to add the rule explicitly.
5759
5. Draft the narrowest rule: exact host, exact port, exact binary when known,
5860
exact method, and the smallest safe path.
5961
6. Submit the proposal, save `accepted_chunk_ids` from the response, and
@@ -207,6 +209,8 @@ The new submission wins by structural overlap.
207209
`fe80::/10`) or known metadata hostnames such as
208210
`metadata.google.internal`. Cloud-metadata endpoints there can hand out
209211
the host's credentials.
212+
- Do not propose `protocol: tcp` or `tls: skip`. If the task requires native
213+
TCP or a raw TLS tunnel, ask an administrator to add that access explicitly.
210214
- Do not include query strings, tokens, credentials, or secret values in
211215
paths.
212216
- Explain uncertainty in `intent_summary` instead of widening the rule.

‎docs/reference/policy-schema.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ Each endpoint defines a reachable destination and optional inspection rules.
193193
- `access` and `rules` are mutually exclusive; setting both is rejected.
194194
- `protocol: tcp` requires a valid DNS hostname. Hostless `allowed_ips`, IP-literal hosts, trailing-dot names, and malformed DNS selectors are rejected with a policy-validation error.
195195
- `protocol: tcp` requires at least one port and rejects L7-only fields, including `path`, `enforcement`, `access`, `rules`, `deny_rules`, request rewriting and credential signing fields, and GraphQL, JSON-RPC, or MCP options.
196+
- A `protocol: tcp` hostname constrains connection routing, not application authority. OpenShell does not inspect TLS SNI, HTTP `Host`, or another protocol-level destination in the stream. Compatible shared infrastructure can therefore expose other tenants, virtual hosts, or services behind an allowed hostname.
196197
- A sandbox runtime must support policy DNS and transparent TCP capture before it can activate a policy containing `protocol: tcp`. Docker and Podman provide this runtime support.
197198
- Adding the first `protocol: tcp` endpoint to a running sandbox that started without one is rejected atomically because its DNS and capture substrate is startup infrastructure. Recreate the sandbox with a TCP endpoint. A sandbox that started with the substrate can remove and re-add TCP endpoints dynamically.
199+
- Policy-advisor agent proposals cannot request `protocol: tcp` or `tls: skip`. Add native TCP or raw TLS access through an administrator-authored policy. Agent proposals may omit `protocol` to use the explicit proxy with its default TLS termination and HTTP authority checks.
198200
- When `protocol` is set, at least one of `access` or `rules` is required for `rest`, `websocket`, `graphql`, and `sql`.
199201
- `mcp` and `json-rpc` reject `access` presets; use explicit `rules`.
200202
- `json-rpc` requires explicit `rules` with `allow.method`.

‎docs/sandboxes/policies.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ network_policies:
131131

132132
OpenShell answers DNS only for hostnames eligible under an active `protocol: tcp` endpoint. It validates upstream answers against destination and SSRF controls, returns a supervisor-owned synthetic address, and records the validated real addresses. When the application connects to the synthetic address, OpenShell recovers the hostname and port, evaluates the calling process against the current policy generation, and dials only an address pinned by that DNS result.
133133

134+
Treat that hostname as a connection-routing constraint, not an application-authority boundary. OpenShell does not inspect TLS SNI, HTTP `Host`, or another protocol-level destination inside a `protocol: tcp` stream. If the approved hostname reaches compatible shared infrastructure, a client may be able to select another tenant, virtual host, or service behind the same front door. Use native TCP only when the trust boundary includes every destination that the shared infrastructure can expose; use an inspected protocol when application authority must remain constrained.
135+
134136
Applications must honor the returned DNS TTL and resolve the hostname again before reconnecting after that TTL expires. A client that caches the synthetic address indefinitely can receive a connection failure after the mapping expires. Docker and Podman currently advertise only IPv4 egress for this feature, so OpenShell returns an empty successful answer for AAAA queries and lets dual-stack clients use the working A record.
135137

136138
DNS resolution does not authorize a connection by itself. Unknown names, wrong ports, stale mappings, disallowed destination addresses, and binaries outside the matching policy fail closed. Applications cannot inherit access by connecting directly to a real IP returned by an upstream resolver.

‎docs/sandboxes/policy-advisor.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ OpenShell has two proposal paths:
129129
| Path | Source | Typical rule shape |
130130
|---|---|---|
131131
| Mechanistic mapper | Aggregated denial summaries from the sandbox. | Groups by host, port, and binary. If L7 request samples are available, it can draft REST method and path rules. Otherwise it drafts an L4 endpoint. |
132-
| Agent-authored proposal | The in-sandbox agent, using `policy.local`. | Usually a REST `addRule` with exact host, port, binary, method, and path from the structured denial. It can also propose L4 rules for opaque protocols. |
132+
| Agent-authored proposal | The in-sandbox agent, using `policy.local`. | Usually a REST `addRule` with exact host, port, binary, method, and path from the structured denial. It can also omit `protocol` for endpoint-only access through the explicit proxy. |
133133

134134
### How proposal provenance works
135135

@@ -189,7 +189,7 @@ For REST APIs, prefer L7 rules over broad L4 access. A good proposal allows one
189189
}
190190
```
191191

192-
The current `policy.local` JSON shape covers L4 endpoints and REST method or path rules. Use [Customize Sandbox Policies](/sandboxes/policies) or [Policy Schema Reference](/reference/policy-schema) for policy fields that are not part of the agent-authored proposal surface, such as WebSocket credential rewrite, GraphQL operation matching, endpoint path scoping, and provider-owned policy bundles.
192+
The current `policy.local` JSON shape covers explicit-proxy endpoints and REST method or path rules. Agent-authored proposals cannot set `protocol: tcp` or `tls: skip`, because those modes bypass application-authority inspection. When a task requires native TCP or a raw TLS tunnel, a developer must add the rule through the normal policy-authoring workflow. Omitting `protocol` remains supported and retains the explicit proxy's default TLS termination and HTTP authority checks. Use [Customize Sandbox Policies](/sandboxes/policies) or [Policy Schema Reference](/reference/policy-schema) for policy fields that are not part of the agent-authored proposal surface, such as WebSocket credential rewrite, GraphQL operation matching, endpoint path scoping, and provider-owned policy bundles.
193193

194194
Policy advisor proposals do not add `allowed_ips` automatically. If an advisor-proposed hostname resolves to an internal or private address, OpenShell's SSRF protections still block the connection until a developer explicitly adds the required `allowed_ips` entry.
195195

0 commit comments

Comments
 (0)