Skip to content

Commit d8f54e0

Browse files
fix(auth): address third gator review findings
- Cap label selector pairs at 64 (CWE-400) to bound SQLite dynamic SQL - Add SCOPE_ONLY_METHODS allowlist for scope-without-role RPCs (CWE-863) - Normalize ID-based data-plane handlers to return NOT_FOUND for unauthorized sandboxes, closing the cross-workspace oracle (CWE-203) - Fix TUI provider profile cache lookup key mismatch for legacy providers with empty profile_workspace - Add whoami to CLI skill reference command tree - Update TUI skill doc with workspace, provider, and settings coverage - Document scope/workspace orthogonality on GetGatewayConfig proto Signed-off-by: Derek Carr <[email protected]>
1 parent 3b5a656 commit d8f54e0

8 files changed

Lines changed: 336 additions & 121 deletions

File tree

‎.agents/skills/openshell-cli/cli-reference.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ openshell
3939
│ ├── list
4040
│ └── select [name]
4141
├── status
42+
├── whoami [--output <format>]
4243
├── inference
4344
│ ├── set --provider --model
4445
│ ├── update [--provider] [--model]
@@ -186,6 +187,15 @@ gateway. Connectivity uses the public health RPC; authentication is checked
186187
with the protected gateway-info capability query and can fail while the gateway
187188
remains connected.
188189

190+
### `openshell whoami`
191+
192+
Show the authenticated user identity: subject, display name, roles, scopes, and
193+
identity provider. Requires an authenticated gateway connection.
194+
195+
| Flag | Description |
196+
|------|-------------|
197+
| `--output <FORMAT>` | Output format: `text` (default) or `json` |
198+
189199
---
190200

191201
## Sandbox Commands

‎.agents/skills/tui-development/SKILL.md‎

Lines changed: 157 additions & 37 deletions
Large diffs are not rendered by default.

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const GATEWAY_PACKAGES: &[&str] = &["openshell.v1", "openshell.inference.v1"];
2525
/// silently turn a future RPC into an authentication-only endpoint.
2626
const AUTH_ONLY_METHODS: &[&str] = &["/openshell.v1.OpenShell/GetCurrentUser"];
2727

28+
/// Bearer-authenticated methods that require a scope but deliberately no role.
29+
///
30+
/// Keep this list explicit so an incomplete authorization annotation cannot
31+
/// silently turn a future mutating RPC into a scope-only endpoint.
32+
const SCOPE_ONLY_METHODS: &[&str] = &["/openshell.v1.OpenShell/GetGatewayConfig"];
33+
2834
/// Per-method authorization entry decoded from proto annotations.
2935
#[derive(Debug, Clone)]
3036
pub struct DescriptorAuthEntry {
@@ -200,12 +206,16 @@ fn validate_entry(
200206
));
201207
}
202208

203-
// scope-only methods (bearer + scope, no role) are valid: any
204-
// authenticated user with the required scope may call them.
205209
if scope.is_none() {
206210
return Err(format!("method {path}: bearer method declares no scope"));
207211
}
208212

213+
if role_missing && !SCOPE_ONLY_METHODS.contains(&path) {
214+
return Err(format!(
215+
"method {path}: scope-only bearer method must be listed in SCOPE_ONLY_METHODS"
216+
));
217+
}
218+
209219
Ok(())
210220
}
211221

@@ -312,19 +322,31 @@ mod tests {
312322

313323
#[test]
314324
fn bearer_rpc_requires_scope() {
315-
// scope-only (no role) is valid: any authenticated user with the scope
325+
let missing_scope = validate_entry(FUTURE_RPC, AuthMode::Bearer, Some("user"), None, None)
326+
.expect_err("missing scope must be rejected");
327+
assert!(missing_scope.ends_with("bearer method declares no scope"));
328+
}
329+
330+
#[test]
331+
fn scope_only_rpc_must_be_explicitly_allowlisted() {
316332
validate_entry(
317-
FUTURE_RPC,
333+
SCOPE_ONLY_METHODS[0],
318334
AuthMode::Bearer,
319335
None,
320336
None,
321337
Some("config:read"),
322338
)
323-
.expect("scope-only bearer method should be accepted");
339+
.expect("listed scope-only bearer method should be accepted");
324340

325-
let missing_scope = validate_entry(FUTURE_RPC, AuthMode::Bearer, Some("user"), None, None)
326-
.expect_err("missing scope must be rejected");
327-
assert!(missing_scope.ends_with("bearer method declares no scope"));
341+
let err = validate_entry(
342+
FUTURE_RPC,
343+
AuthMode::Bearer,
344+
None,
345+
None,
346+
Some("config:read"),
347+
)
348+
.expect_err("unlisted scope-only RPC must be rejected");
349+
assert!(err.contains("SCOPE_ONLY_METHODS"));
328350
}
329351

330352
#[test]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ const MAX_PROVIDER_TYPE_LEN: usize = 64;
152152
const MAX_PROVIDER_CREDENTIALS_ENTRIES: usize = 32;
153153
/// Maximum number of entries in the provider `config` map.
154154
const MAX_PROVIDER_CONFIG_ENTRIES: usize = 64;
155+
/// Maximum number of key=value pairs in a label selector query.
156+
const MAX_LABEL_SELECTOR_PAIRS: usize = 64;
155157

156158
// ---------------------------------------------------------------------------
157159
// Shared types (used by the policy/settings submodule)

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

Lines changed: 95 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ use crate::persistence::current_time_ms;
5959

6060
const TCP_FORWARD_CHUNK_SIZE: usize = 64 * 1024;
6161

62+
/// Fetch a sandbox by ID and authorize the caller in one step, returning
63+
/// `NOT_FOUND` for both missing and unauthorized sandboxes so that callers
64+
/// cannot distinguish the two cases (CWE-203).
65+
async fn fetch_and_authorize_sandbox(
66+
state: &Arc<ServerState>,
67+
principal: &crate::auth::principal::Principal,
68+
sandbox_id: &str,
69+
) -> Result<Sandbox, Status> {
70+
let sandbox = state
71+
.store
72+
.get_message::<Sandbox>(sandbox_id)
73+
.await
74+
.map_err(|e| Status::internal(format!("fetch sandbox failed: {e}")))?
75+
.ok_or_else(|| Status::not_found("sandbox not found"))?;
76+
authorize_sandbox_workspace(
77+
&state.store,
78+
&state.admin_role,
79+
principal,
80+
sandbox.object_workspace(),
81+
MinWorkspaceRole::User,
82+
)
83+
.await
84+
.map_err(|_| Status::not_found("sandbox not found"))?;
85+
Ok(sandbox)
86+
}
87+
6288
fn generate_routable_name() -> String {
6389
let name = petname::petname(2, "-").unwrap_or_else(generate_name);
6490
let mut truncated = &name[..name.len().min(MAX_ROUTABLE_NAME_LEN)];
@@ -721,20 +747,7 @@ pub(super) async fn handle_watch_sandbox(
721747
}
722748
let sandbox_id = req.id.clone();
723749

724-
let sandbox = state
725-
.store
726-
.get_message::<Sandbox>(&sandbox_id)
727-
.await
728-
.map_err(|e| Status::internal(format!("fetch sandbox failed: {e}")))?
729-
.ok_or_else(|| Status::not_found("sandbox not found"))?;
730-
authorize_sandbox_workspace(
731-
&state.store,
732-
&state.admin_role,
733-
&principal,
734-
sandbox.object_workspace(),
735-
MinWorkspaceRole::User,
736-
)
737-
.await?;
750+
let _sandbox = fetch_and_authorize_sandbox(state, &principal, &sandbox_id).await?;
738751

739752
let follow_status = req.follow_status;
740753
let follow_logs = req.follow_logs;
@@ -975,20 +988,7 @@ pub(super) async fn handle_exec_sandbox(
975988
}
976989
validate_exec_request_fields(&req)?;
977990

978-
let sandbox = state
979-
.store
980-
.get_message::<Sandbox>(&req.sandbox_id)
981-
.await
982-
.map_err(|e| Status::internal(format!("fetch sandbox failed: {e}")))?
983-
.ok_or_else(|| Status::not_found("sandbox not found"))?;
984-
authorize_sandbox_workspace(
985-
&state.store,
986-
&state.admin_role,
987-
&principal,
988-
sandbox.object_workspace(),
989-
MinWorkspaceRole::User,
990-
)
991-
.await?;
991+
let sandbox = fetch_and_authorize_sandbox(state, &principal, &req.sandbox_id).await?;
992992

993993
if SandboxPhase::try_from(sandbox.phase()).ok() != Some(SandboxPhase::Ready) {
994994
return Err(Status::failed_precondition("sandbox is not ready"));
@@ -1098,20 +1098,7 @@ pub(super) async fn handle_forward_tcp(
10981098

10991099
let target = validate_tcp_forward_init(&init)?;
11001100

1101-
let sandbox = state
1102-
.store
1103-
.get_message::<Sandbox>(&init.sandbox_id)
1104-
.await
1105-
.map_err(|e| Status::internal(format!("fetch sandbox failed: {e}")))?
1106-
.ok_or_else(|| Status::not_found("sandbox not found"))?;
1107-
authorize_sandbox_workspace(
1108-
&state.store,
1109-
&state.admin_role,
1110-
&principal,
1111-
sandbox.object_workspace(),
1112-
MinWorkspaceRole::User,
1113-
)
1114-
.await?;
1101+
let sandbox = fetch_and_authorize_sandbox(state, &principal, &init.sandbox_id).await?;
11151102

11161103
if SandboxPhase::try_from(sandbox.phase()).ok() != Some(SandboxPhase::Ready) {
11171104
return Err(Status::failed_precondition("sandbox is not ready"));
@@ -1433,20 +1420,7 @@ pub(super) async fn handle_exec_sandbox_interactive(
14331420

14341421
let req = validate_interactive_exec_start(first_msg)?;
14351422

1436-
let sandbox = state
1437-
.store
1438-
.get_message::<Sandbox>(&req.sandbox_id)
1439-
.await
1440-
.map_err(|e| Status::internal(format!("fetch sandbox failed: {e}")))?
1441-
.ok_or_else(|| Status::not_found("sandbox not found"))?;
1442-
authorize_sandbox_workspace(
1443-
&state.store,
1444-
&state.admin_role,
1445-
&principal,
1446-
sandbox.object_workspace(),
1447-
MinWorkspaceRole::User,
1448-
)
1449-
.await?;
1423+
let sandbox = fetch_and_authorize_sandbox(state, &principal, &req.sandbox_id).await?;
14501424

14511425
if SandboxPhase::try_from(sandbox.phase()).ok() != Some(SandboxPhase::Ready) {
14521426
return Err(Status::failed_precondition("sandbox is not ready"));
@@ -1517,20 +1491,7 @@ pub(super) async fn handle_create_ssh_session(
15171491
return Err(Status::invalid_argument("sandbox_id is required"));
15181492
}
15191493

1520-
let sandbox = state
1521-
.store
1522-
.get_message::<Sandbox>(&req.sandbox_id)
1523-
.await
1524-
.map_err(|e| Status::internal(format!("fetch sandbox failed: {e}")))?
1525-
.ok_or_else(|| Status::not_found("sandbox not found"))?;
1526-
authorize_sandbox_workspace(
1527-
&state.store,
1528-
&state.admin_role,
1529-
&principal,
1530-
sandbox.object_workspace(),
1531-
MinWorkspaceRole::User,
1532-
)
1533-
.await?;
1494+
let sandbox = fetch_and_authorize_sandbox(state, &principal, &req.sandbox_id).await?;
15341495

15351496
if SandboxPhase::try_from(sandbox.phase()).ok() != Some(SandboxPhase::Ready) {
15361497
return Err(Status::failed_precondition("sandbox is not ready"));
@@ -1631,7 +1592,8 @@ pub(super) async fn handle_revoke_ssh_session(
16311592
session.object_workspace(),
16321593
MinWorkspaceRole::User,
16331594
)
1634-
.await?;
1595+
.await
1596+
.map_err(|_| Status::not_found("sandbox not found"))?;
16351597

16361598
let resource_version = session
16371599
.metadata
@@ -4252,6 +4214,68 @@ mod tests {
42524214
);
42534215
}
42544216

4217+
/// ID-based data-plane handlers must return `NOT_FOUND` — never
4218+
/// `PERMISSION_DENIED` — when the caller lacks workspace access, so that
4219+
/// cross-workspace sandbox existence cannot be inferred (CWE-203).
4220+
#[tokio::test]
4221+
async fn id_based_handlers_hide_cross_workspace_sandboxes() {
4222+
use crate::auth::identity::{Identity, IdentityProvider};
4223+
use crate::auth::principal::{Principal, UserPrincipal};
4224+
use tonic::Code;
4225+
4226+
fn non_member_request<T>(inner: T) -> Request<T> {
4227+
let mut req = Request::new(inner);
4228+
req.extensions_mut().insert(Principal::User(UserPrincipal {
4229+
identity: Identity {
4230+
subject: "non-member".to_string(),
4231+
display_name: None,
4232+
roles: vec![],
4233+
scopes: vec![],
4234+
provider: IdentityProvider::Oidc,
4235+
},
4236+
}));
4237+
req
4238+
}
4239+
4240+
let mut state = test_server_state().await;
4241+
Arc::get_mut(&mut state).unwrap().admin_role = "openshell-admin".to_string();
4242+
4243+
let mut sandbox = test_sandbox("cross-ws", Vec::new());
4244+
sandbox.metadata.as_mut().unwrap().workspace = "other-workspace".to_string();
4245+
state.store.put_message(&sandbox).await.unwrap();
4246+
4247+
// --- handle_watch_sandbox ---
4248+
let err = handle_watch_sandbox(
4249+
&state,
4250+
non_member_request(WatchSandboxRequest {
4251+
id: "sandbox-cross-ws".into(),
4252+
..Default::default()
4253+
}),
4254+
)
4255+
.await
4256+
.unwrap_err();
4257+
assert_eq!(
4258+
err.code(),
4259+
Code::NotFound,
4260+
"handle_watch_sandbox must return NotFound, not PermissionDenied"
4261+
);
4262+
4263+
// --- handle_create_ssh_session ---
4264+
let err = handle_create_ssh_session(
4265+
&state,
4266+
non_member_request(CreateSshSessionRequest {
4267+
sandbox_id: "sandbox-cross-ws".into(),
4268+
}),
4269+
)
4270+
.await
4271+
.unwrap_err();
4272+
assert_eq!(
4273+
err.code(),
4274+
Code::NotFound,
4275+
"handle_create_ssh_session must return NotFound, not PermissionDenied"
4276+
);
4277+
}
4278+
42554279
#[tokio::test]
42564280
async fn revoke_ssh_session_preserves_workspace() {
42574281
let state = test_server_state().await;

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ use prost::Message;
1616
use tonic::Status;
1717

1818
use super::{
19-
MAX_ENVIRONMENT_ENTRIES, MAX_LOG_LEVEL_LEN, MAX_MAP_KEY_LEN, MAX_MAP_VALUE_LEN,
20-
MAX_METADATA_ANNOTATIONS_ENTRIES, MAX_NAME_LEN, MAX_POLICY_SIZE, MAX_PROVIDER_CONFIG_ENTRIES,
21-
MAX_PROVIDER_CREDENTIALS_ENTRIES, MAX_PROVIDER_TYPE_LEN, MAX_PROVIDERS, MAX_ROUTABLE_NAME_LEN,
22-
MAX_TEMPLATE_MAP_ENTRIES, MAX_TEMPLATE_STRING_LEN, MAX_TEMPLATE_STRUCT_SIZE,
19+
MAX_ENVIRONMENT_ENTRIES, MAX_LABEL_SELECTOR_PAIRS, MAX_LOG_LEVEL_LEN, MAX_MAP_KEY_LEN,
20+
MAX_MAP_VALUE_LEN, MAX_METADATA_ANNOTATIONS_ENTRIES, MAX_NAME_LEN, MAX_POLICY_SIZE,
21+
MAX_PROVIDER_CONFIG_ENTRIES, MAX_PROVIDER_CREDENTIALS_ENTRIES, MAX_PROVIDER_TYPE_LEN,
22+
MAX_PROVIDERS, MAX_ROUTABLE_NAME_LEN, MAX_TEMPLATE_MAP_ENTRIES, MAX_TEMPLATE_STRING_LEN,
23+
MAX_TEMPLATE_STRUCT_SIZE,
2324
};
2425

2526
// ---------------------------------------------------------------------------
@@ -637,11 +638,18 @@ pub(super) fn validate_label_selector(selector: &str) -> Result<(), Status> {
637638
return Ok(());
638639
}
639640

641+
let mut count = 0usize;
640642
for pair in selector.split(',') {
641643
let pair = pair.trim();
642644
if pair.is_empty() {
643645
continue;
644646
}
647+
count += 1;
648+
if count > MAX_LABEL_SELECTOR_PAIRS {
649+
return Err(Status::invalid_argument(format!(
650+
"label selector exceeds {MAX_LABEL_SELECTOR_PAIRS} pair limit"
651+
)));
652+
}
645653

646654
let parts: Vec<&str> = pair.splitn(2, '=').collect();
647655
if parts.len() != 2 {
@@ -1652,6 +1660,22 @@ mod tests {
16521660
assert!(err.message().contains("exceeds 63 characters"));
16531661
}
16541662

1663+
#[test]
1664+
fn validate_label_selector_rejects_too_many_pairs() {
1665+
let pairs: Vec<String> = (0..65).map(|i| format!("k{i}=v{i}")).collect();
1666+
let selector = pairs.join(",");
1667+
let err = validate_label_selector(&selector).unwrap_err();
1668+
assert_eq!(err.code(), Code::InvalidArgument);
1669+
assert!(err.message().contains("64 pair limit"));
1670+
}
1671+
1672+
#[test]
1673+
fn validate_label_selector_accepts_max_pairs() {
1674+
let pairs: Vec<String> = (0..64).map(|i| format!("k{i}=v{i}")).collect();
1675+
let selector = pairs.join(",");
1676+
assert!(validate_label_selector(&selector).is_ok());
1677+
}
1678+
16551679
// ---- Policy safety ----
16561680

16571681
#[test]

0 commit comments

Comments
 (0)