Skip to content

Commit 629c0e2

Browse files
committed
fix(server): per-handler sandbox_id equality check (closes #1354)
Adds the IDOR guard that closes the second half of the per-sandbox identity series. Every sandbox-class handler now verifies that the calling Principal::Sandbox.sandbox_id matches the canonical UUID the request body operates on. User principals bypass the check because RBAC was their gate at the router layer; anonymous callers are rejected outright. New module crates/openshell-server/src/auth/guard.rs exposes ensure_sandbox_scope / enforce_sandbox_scope. Applied at the top of: - handle_get_sandbox_config (id-keyed) - handle_get_sandbox_provider_environment (id-keyed) - handle_report_policy_status (id-keyed) - handle_push_sandbox_logs (id-keyed, first frame only — principal is stable across the stream) - handle_submit_policy_analysis (name-keyed: resolve to id, then check) - handle_get_draft_policy (name-keyed) - handle_update_config (dual-auth: enforce only when Principal::Sandbox; CLI / TUI user paths are unaffected) - handle_get_inference_bundle (no sandbox_id in body; accept any authenticated principal, reject anonymous) Existing policy.rs tests are updated to wrap their requests with a test-helper user principal so the new guard treats them as CLI calls; six new tests cover the cross-sandbox-denied / same-sandbox-allowed / user-bypasses-guard matrix.
1 parent 9d241f5 commit 629c0e2

4 files changed

Lines changed: 469 additions & 60 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
//! Per-handler sandbox-scope guards.
5+
//!
6+
//! Closes the IDOR half of issue #1354: a sandbox principal may only
7+
//! reference its own sandbox, identified by its [`Principal::Sandbox`]'s
8+
//! `sandbox_id`. User principals retain the broad scope the RBAC layer
9+
//! already evaluated.
10+
11+
use super::principal::Principal;
12+
use tonic::Status;
13+
use tracing::info;
14+
15+
/// Reject a sandbox-class request whose body references a sandbox other
16+
/// than the one the calling principal was authenticated against.
17+
///
18+
/// - [`Principal::User`] passes through (RBAC has already evaluated user
19+
/// scope at the router level).
20+
/// - [`Principal::Sandbox`] must reference the same canonical UUID it
21+
/// was authenticated with.
22+
/// - [`Principal::Anonymous`] is rejected — sandbox-class methods are
23+
/// never anonymously callable.
24+
///
25+
/// `claimed_sandbox_id` is the canonical UUID the request is operating
26+
/// on. Name-keyed handlers must resolve the name to a UUID via the
27+
/// store before calling this guard.
28+
#[allow(clippy::result_large_err)]
29+
pub fn ensure_sandbox_scope(principal: &Principal, claimed_sandbox_id: &str) -> Result<(), Status> {
30+
match principal {
31+
Principal::User(_) => Ok(()),
32+
Principal::Sandbox(p) => {
33+
if p.sandbox_id == claimed_sandbox_id {
34+
Ok(())
35+
} else {
36+
info!(
37+
principal_sandbox_id = %p.sandbox_id,
38+
requested_sandbox_id = %claimed_sandbox_id,
39+
"cross-sandbox access denied"
40+
);
41+
Err(Status::permission_denied(
42+
"cross-sandbox access denied: principal does not own this sandbox",
43+
))
44+
}
45+
}
46+
Principal::Anonymous => Err(Status::unauthenticated(
47+
"sandbox-scoped methods require an authenticated caller",
48+
)),
49+
}
50+
}
51+
52+
/// Convenience: read the `Principal` out of a request and apply
53+
/// [`ensure_sandbox_scope`]. Returns the principal so callers can read it
54+
/// further (e.g. for audit logging).
55+
#[allow(clippy::result_large_err)]
56+
pub fn enforce_sandbox_scope<T>(
57+
request: &tonic::Request<T>,
58+
claimed_sandbox_id: &str,
59+
) -> Result<Principal, Status> {
60+
let principal = request
61+
.extensions()
62+
.get::<Principal>()
63+
.cloned()
64+
.ok_or_else(|| Status::unauthenticated("missing principal"))?;
65+
ensure_sandbox_scope(&principal, claimed_sandbox_id)?;
66+
Ok(principal)
67+
}
68+
69+
#[cfg(test)]
70+
mod tests {
71+
use super::*;
72+
use crate::auth::identity::{Identity, IdentityProvider};
73+
use crate::auth::principal::{SandboxIdentitySource, SandboxPrincipal, UserPrincipal};
74+
75+
fn user(subject: &str) -> Principal {
76+
Principal::User(UserPrincipal {
77+
identity: Identity {
78+
subject: subject.to_string(),
79+
display_name: None,
80+
roles: vec![],
81+
scopes: vec![],
82+
provider: IdentityProvider::Oidc,
83+
},
84+
})
85+
}
86+
87+
fn sandbox(id: &str) -> Principal {
88+
Principal::Sandbox(SandboxPrincipal {
89+
sandbox_id: id.to_string(),
90+
source: SandboxIdentitySource::BootstrapJwt {
91+
issuer: "openshell-gateway:test".to_string(),
92+
jti: "j-1".to_string(),
93+
},
94+
trust_domain: Some("openshell".to_string()),
95+
})
96+
}
97+
98+
#[test]
99+
fn user_principal_bypasses_equality_check() {
100+
// RBAC was the user's gate at the router layer.
101+
assert!(ensure_sandbox_scope(&user("alice"), "any-sandbox").is_ok());
102+
}
103+
104+
#[test]
105+
fn sandbox_principal_matching_id_is_allowed() {
106+
assert!(ensure_sandbox_scope(&sandbox("sbx-1"), "sbx-1").is_ok());
107+
}
108+
109+
#[test]
110+
fn sandbox_principal_mismatched_id_is_denied() {
111+
let err =
112+
ensure_sandbox_scope(&sandbox("sbx-1"), "sbx-2").expect_err("must deny cross-sandbox");
113+
assert_eq!(err.code(), tonic::Code::PermissionDenied);
114+
}
115+
116+
#[test]
117+
fn anonymous_principal_is_rejected() {
118+
let err =
119+
ensure_sandbox_scope(&Principal::Anonymous, "sbx-1").expect_err("must reject anon");
120+
assert_eq!(err.code(), tonic::Code::Unauthenticated);
121+
}
122+
123+
#[test]
124+
fn enforce_reads_from_request_extensions() {
125+
let mut req = tonic::Request::new(());
126+
req.extensions_mut().insert(sandbox("sbx-1"));
127+
let result = enforce_sandbox_scope(&req, "sbx-1").expect("scope OK");
128+
assert!(matches!(result, Principal::Sandbox(_)));
129+
}
130+
131+
#[test]
132+
fn enforce_rejects_request_without_principal() {
133+
let req = tonic::Request::new(());
134+
let err = enforce_sandbox_scope(&req, "sbx-1").expect_err("must require principal");
135+
assert_eq!(err.code(), tonic::Code::Unauthenticated);
136+
}
137+
}

‎crates/openshell-server/src/auth/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
pub mod authenticator;
1212
pub mod authz;
13+
pub mod guard;
1314
mod http;
1415
pub mod identity;
1516
pub mod k8s_sa;

0 commit comments

Comments
 (0)