Skip to content

Commit 4e56287

Browse files
committed
fix(audit): keep target attribution on denied credential exports
Signed-off-by: Eric Curtin <[email protected]>
1 parent b0ae628 commit 4e56287

3 files changed

Lines changed: 75 additions & 5 deletions

File tree

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ async fn handle_audited(
5151
request: Request<GetProviderCredentialsRequest>,
5252
audit: &mut DisclosureAudit,
5353
) -> Result<Response<GetProviderCredentialsResponse>, Status> {
54+
attribute_target(audit, request.get_ref());
5455
let operator = crate::auth::guard::require_operator(&request)?;
5556
let subject = operator.identity.subject.clone();
5657
let fingerprint = operator.certificate_sha256.clone();
@@ -67,9 +68,6 @@ async fn handle_audited(
6768
return Err(error);
6869
}
6970
};
70-
audit.resource("workspace", workspace);
71-
audit.resource("provider", request.name.clone());
72-
audit.resource("credential_keys", request.credential_keys.join(","));
7371
// Admission survives RPC cancellation, while completion records the refresh
7472
// effects even if the final all-or-nothing delivery fails.
7573
tracing::info!(
@@ -97,6 +95,36 @@ async fn handle_audited(
9795
result.map(Response::new)
9896
}
9997

98+
/// Name the target before authorization so denied and invalid attempts stay
99+
/// attributable. Each identifier is checked on its own; malformed input is
100+
/// never recorded.
101+
fn attribute_target(audit: &mut DisclosureAudit, request: &GetProviderCredentialsRequest) {
102+
if let Ok(workspace) =
103+
crate::auth::workspace_authz::selected_workspace_name(request.workspace_scope.as_ref())
104+
{
105+
audit.resource("workspace", workspace);
106+
}
107+
let name = &request.name;
108+
if !name.is_empty()
109+
&& name.len() <= 253
110+
&& name
111+
.bytes()
112+
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'))
113+
{
114+
audit.resource("provider", name.clone());
115+
}
116+
let keys: Vec<&str> = request
117+
.credential_keys
118+
.iter()
119+
.take(MAX_KEYS)
120+
.map(String::as_str)
121+
.filter(|key| key.len() <= 256 && super::provider::is_valid_env_key(key))
122+
.collect();
123+
if !keys.is_empty() {
124+
audit.resource("credential_keys", keys.join(","));
125+
}
126+
}
127+
100128
fn validate(request: &GetProviderCredentialsRequest) -> Result<Duration, Status> {
101129
if request.name.is_empty() || request.name.trim() != request.name || request.name.len() > 253 {
102130
return Err(Status::invalid_argument(

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ async fn emits_one_disclosure_event_per_attempt_without_values() {
166166
assert_eq!(delivered["workspace"], "other");
167167
assert_eq!(delivered["provider"], "provider");
168168
assert_eq!(delivered["credential_keys"], "STATIC_KEY");
169-
assert!(events[2]["unmapped"].get("provider").is_none());
169+
assert_eq!(events[2]["unmapped"]["provider"], "provider");
170+
assert!(events[2]["unmapped"].get("credential_keys").is_none());
170171
let recorded = serde_json::to_string(&events).unwrap();
171172
for secret in [
172173
"old-access-token",
@@ -181,6 +182,47 @@ async fn emits_one_disclosure_event_per_attempt_without_values() {
181182
}
182183
}
183184

185+
#[tokio::test]
186+
async fn denied_and_invalid_attempts_keep_safe_target_attribution() {
187+
let (state, _) = fixture().await;
188+
let capture = openshell_ocsf::test_support::OcsfCapture::default();
189+
let _guard = capture.install();
190+
handle(
191+
&state,
192+
crate::grpc::test_support::authed_request(request(&["STATIC_KEY"])),
193+
)
194+
.await
195+
.unwrap_err();
196+
let mut mixed = request(&["STATIC_KEY", "API_KEY=secret-from-invalid-input"]);
197+
mixed.name = "provider secret-from-invalid-input".into();
198+
handle(&state, authenticated(mixed)).await.unwrap_err();
199+
let mut mixed = request(&["STATIC_KEY", "API_KEY=secret-from-invalid-input"]);
200+
mixed.workspace_scope = Some(workspace_selector("Bad secret-from-invalid-input"));
201+
handle(&state, authenticated(mixed)).await.unwrap_err();
202+
203+
let events = crate::auth::audit::test_support::disclosure_events(&capture);
204+
assert_eq!(events.len(), 3, "{events:?}");
205+
let denied = &events[0]["unmapped"];
206+
assert_eq!(denied["outcome"], "denied");
207+
assert_eq!(denied["workspace"], "other");
208+
assert_eq!(denied["provider"], "provider");
209+
assert_eq!(denied["credential_keys"], "STATIC_KEY");
210+
let bad_name = &events[1]["unmapped"];
211+
assert_eq!(bad_name["outcome"], "failed");
212+
assert_eq!(bad_name["workspace"], "other");
213+
assert_eq!(bad_name["credential_keys"], "STATIC_KEY");
214+
assert!(bad_name.get("provider").is_none());
215+
let bad_workspace = &events[2]["unmapped"];
216+
assert_eq!(bad_workspace["provider"], "provider");
217+
assert_eq!(bad_workspace["credential_keys"], "STATIC_KEY");
218+
assert!(bad_workspace.get("workspace").is_none());
219+
let recorded = serde_json::to_string(&events).unwrap();
220+
assert!(
221+
!recorded.contains("secret-from-invalid-input"),
222+
"leaked into {recorded}"
223+
);
224+
}
225+
184226
#[tokio::test]
185227
async fn exports_only_selected_runtime_keys_across_workspaces() {
186228
let (state, _) = fixture().await;

‎docs/observability/ocsf-json-export.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ The gateway emits an API Activity (`class_uid: 6003`) event with Medium severity
5858

5959
When a sandbox principal is refused with `method_not_permitted`, `cross_sandbox`, or `platform_admin_required`, the gateway also emits a High-severity Detection Finding (`class_uid: 2004`) with `finding_info.uid` `sandbox-authorization-boundary`. Supervisors never make these requests, so the sandbox's credential may be compromised. Both events carry the same `metadata.correlation_uid`. Some methods answer a refused request with "not found" so callers cannot probe for resources; the event still records the denial.
6060

61-
The gateway also emits a Device Config State Change (`class_uid: 5019`) event for each attempt to read secret-bearing material through `GetSandboxProviderEnvironment`, which a sandbox supervisor calls to load its provider environment, or `GetProviderCredentials`, the operator credential export. The state is `disclosed`, `denied`, `failed`, or `cancelled`. `unmapped` holds `operation`, `actor` (the authenticated principal as `type:id`, or `unknown`), `outcome`, and `status_code` for denied or failed attempts. It also names what was requested: `requested_sandbox_id` and `providers` for the sandbox environment, or `workspace`, `provider`, and `credential_keys` for the export. Events about a sandbox carry it in `container`, and `metadata.correlation_uid` holds the request ID as described above. These events never include credential values or error text.
61+
The gateway also emits a Device Config State Change (`class_uid: 5019`) event for each attempt to read secret-bearing material through `GetSandboxProviderEnvironment`, which a sandbox supervisor calls to load its provider environment, or `GetProviderCredentials`, the operator credential export. The state is `disclosed`, `denied`, `failed`, or `cancelled`. `unmapped` holds `operation`, `actor` (the authenticated principal as `type:id`, or `unknown`), `outcome`, and `status_code` for denied or failed attempts. It also names what was requested: `requested_sandbox_id` and `providers` for the sandbox environment, or `workspace`, `provider`, and `credential_keys` for the export. The export records each target identifier that is valid on its own, even when the attempt is denied or the request is otherwise invalid. Events about a sandbox carry it in `container`, and `metadata.correlation_uid` holds the request ID as described above. These events never include credential values or error text.
6262

6363
Without JSONL enabled, gateway OCSF events still appear as shorthand in console output, subject to the diagnostic log filter. Events associated with a sandbox also appear in its gateway log stream; gateway-wide events have no sandbox association.
6464

0 commit comments

Comments
 (0)