Skip to content

Commit e64b035

Browse files
authored
feat(middleware): broaden HTTP header mutation authority (#3072)
* feat(middleware): broaden HTTP header mutation authority Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com> * fix(middleware): protect response credential headers from mutation Response middleware could write or remove Set-Cookie, WWW-Authenticate, Authentication-Info, and Proxy-Authentication-Info, letting a stage plant or strip credentials the sandbox client acts on. Protect them in both directions, matching the request profile's treatment of Authorization and Cookie, and reserve the x-openshell-credential prefix for responses too. Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com> * fix(middleware): reject credential placeholder writes Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com> --------- Signed-off-by: Piotr Mlocek <pmlocek@nvidia.com>
1 parent 5457905 commit e64b035

6 files changed

Lines changed: 454 additions & 41 deletions

File tree

‎architecture/sandbox.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,9 @@ polling runs far more frequently than credentials expire, so the loop rotates
204204
only when a credential is missing or has passed four fifths of its lifetime,
205205
and bounds its sleep by the soonest rotation deadline.
206206

207-
Middleware cannot observe injected credentials or mutate supervisor-owned
208-
credential, routing, or framing headers. Body transformations are re-evaluated
207+
Middleware cannot observe injected credentials, introduce credential
208+
placeholders, or mutate supervisor-owned credential, routing, or framing
209+
headers. Body transformations are re-evaluated
209210
against body-aware L7 policy before later stages or the upstream can observe
210211
them. Requests, results, chain length, execution time, and diagnostics are
211212
bounded; external free-form diagnostic text is not exposed in responses or

‎crates/openshell-core/src/secrets.rs‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ pub fn contains_reserved_credential_marker(value: &str) -> bool {
5454
contains_raw_reserved_marker(&decoded)
5555
}
5656

57+
/// Return whether an HTTP header value contains syntax reserved for credential
58+
/// placeholder rewriting, including the decoded value of Basic authentication.
59+
pub fn header_value_contains_reserved_credential_marker(value: &str) -> bool {
60+
let trimmed = value.trim();
61+
if contains_reserved_credential_marker(trimmed) {
62+
return true;
63+
}
64+
65+
let Some(decoded) = decode_basic_auth_value(trimmed) else {
66+
return false;
67+
};
68+
contains_raw_reserved_marker(&decoded)
69+
}
70+
71+
fn basic_auth_token(value: &str) -> Option<&str> {
72+
value
73+
.strip_prefix("Basic ")
74+
.or_else(|| value.strip_prefix("basic "))
75+
.map(str::trim)
76+
}
77+
78+
fn decode_basic_auth_value(value: &str) -> Option<String> {
79+
decode_basic_auth_token(basic_auth_token(value)?)
80+
}
81+
82+
fn decode_basic_auth_token(encoded: &str) -> Option<String> {
83+
let decoded = base64::engine::general_purpose::STANDARD
84+
.decode(encoded)
85+
.ok()?;
86+
String::from_utf8(decoded).ok()
87+
}
88+
5789
pub fn contains_reserved_credential_marker_bytes(value: &[u8]) -> bool {
5890
if value.is_empty() {
5991
return false;
@@ -494,10 +526,7 @@ impl SecretResolver {
494526

495527
// Basic auth decoding: `Basic <base64>` where the decoded content
496528
// contains a placeholder (e.g. `user:openshell:resolve:env:PASS`).
497-
if let Some(encoded) = trimmed
498-
.strip_prefix("Basic ")
499-
.or_else(|| trimmed.strip_prefix("basic "))
500-
.map(str::trim)
529+
if let Some(encoded) = basic_auth_token(trimmed)
501530
&& let Some(rewritten) = self.rewrite_basic_auth_token(encoded)?
502531
{
503532
return Ok(Some(format!("Basic {rewritten}")));
@@ -633,18 +662,15 @@ impl SecretResolver {
633662
encoded: &str,
634663
) -> Result<Option<String>, UnresolvedPlaceholderError> {
635664
let b64 = base64::engine::general_purpose::STANDARD;
636-
let Some(decoded_bytes) = b64.decode(encoded.trim()).ok() else {
637-
return Ok(None);
638-
};
639-
let Some(decoded) = std::str::from_utf8(&decoded_bytes).ok() else {
665+
let Some(decoded) = decode_basic_auth_token(encoded.trim()) else {
640666
return Ok(None);
641667
};
642668

643-
if !contains_raw_reserved_marker(decoded) {
669+
if !contains_raw_reserved_marker(&decoded) {
644670
return Ok(None);
645671
}
646672

647-
let mut rewritten = decoded.to_string();
673+
let mut rewritten = decoded;
648674
let replacements = self.rewrite_text_placeholders(&mut rewritten, "header")?;
649675

650676
if replacements == 0 {
@@ -1413,6 +1439,28 @@ mod tests {
14131439
]));
14141440
}
14151441

1442+
#[test]
1443+
fn header_value_marker_detection_covers_rewrite_forms() {
1444+
let basic =
1445+
base64::engine::general_purpose::STANDARD.encode("user:openshell:resolve:env:API_KEY");
1446+
1447+
for value in [
1448+
"openshell:resolve:env:API_KEY".to_string(),
1449+
"Bearer openshell:resolve:env:API_KEY".to_string(),
1450+
"provider-OPENSHELL-RESOLVE-ENV-API_KEY".to_string(),
1451+
"openshell%3Aresolve%3Aenv%3AAPI_KEY".to_string(),
1452+
format!("Basic {basic}"),
1453+
] {
1454+
assert!(
1455+
header_value_contains_reserved_credential_marker(&value),
1456+
"reserved credential marker in {value:?}"
1457+
);
1458+
}
1459+
assert!(!header_value_contains_reserved_credential_marker(
1460+
"Bearer ordinary-token"
1461+
));
1462+
}
1463+
14161464
fn fully_percent_encoded(marker: &str) -> String {
14171465
const HEX: &[u8; 16] = b"0123456789ABCDEF";
14181466
let mut encoded = String::with_capacity(marker.len() * 3);

0 commit comments

Comments
 (0)