Skip to content

Commit 0ccc6b9

Browse files
fix(core): preserve issued identity in credential aliases (#4390)
Signed-off-by: Steven Lee <stevenlee@openai.com>
1 parent d789ec6 commit 0ccc6b9

3 files changed

Lines changed: 125 additions & 7 deletions

File tree

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,15 @@ mod tests {
10471047
use super::*;
10481048
use crate::google_cloud;
10491049

1050+
fn jwt_alias(placeholder: &str) -> String {
1051+
// Claims are irrelevant to injection authority: retain the exact issued
1052+
// identity in the signature while giving clients a parseable JWT envelope.
1053+
let suffix = placeholder
1054+
.strip_prefix("openshell:resolve:env:")
1055+
.expect("issued placeholder");
1056+
format!("e30.e30.OPENSHELL-RESOLVE-ENV-{suffix}")
1057+
}
1058+
10501059
#[test]
10511060
fn body_classification_distinguishes_literal_foreign_bound_and_revoked() {
10521061
use crate::secrets::body::BodyCredentialError;
@@ -1721,6 +1730,14 @@ mod tests {
17211730
None,
17221731
"the expired access token must fail closed"
17231732
);
1733+
assert!(
1734+
state
1735+
.resolver_for_endpoint("api.example.com", 443, "/v1")
1736+
.expect("resolver")
1737+
.rewrite_header_value(&format!("Bearer {}", jwt_alias(&workload_placeholder)))
1738+
.is_err(),
1739+
"wrapping a placeholder must not bypass access-token expiry"
1740+
);
17241741

17251742
state
17261743
.install_bound_environment(
@@ -1741,6 +1758,12 @@ mod tests {
17411758
resolver.resolve_placeholder(&workload_placeholder),
17421759
Some("current")
17431760
);
1761+
assert_eq!(
1762+
resolver
1763+
.rewrite_header_value(&format!("Bearer {}", jwt_alias(&workload_placeholder)))
1764+
.expect("refresh updates the JWT alias"),
1765+
Some("Bearer current".to_string())
1766+
);
17441767
assert_eq!(
17451768
resolver.resolve_placeholder("openshell:resolve:env:v2_API_KEY"),
17461769
None,
@@ -1762,6 +1785,10 @@ mod tests {
17621785
)
17631786
.expect("initial stable binding");
17641787
let workload_placeholder = state.snapshot().child_env["API_KEY"].clone();
1788+
let request = format!(
1789+
"GET /v1 HTTP/1.1\r\nAuthorization: Bearer {}\r\n\r\n",
1790+
jwt_alias(&workload_placeholder)
1791+
);
17651792

17661793
for revision in 2..=13 {
17671794
let token = format!("token-{revision}");
@@ -1783,6 +1810,16 @@ mod tests {
17831810
.resolve_placeholder(&workload_placeholder),
17841811
Some(token.as_str())
17851812
);
1813+
let resolver = state
1814+
.resolver_for_endpoint("api.example.com", 443, "/v1")
1815+
.expect("resolver");
1816+
let rewritten =
1817+
crate::secrets::rewrite_http_header_block(request.as_bytes(), Some(&resolver))
1818+
.expect("the unchanged JWT alias uses the current token");
1819+
assert_eq!(
1820+
String::from_utf8(rewritten.rewritten).expect("HTTP header"),
1821+
format!("GET /v1 HTTP/1.1\r\nAuthorization: Bearer {token}\r\n\r\n")
1822+
);
17861823
}
17871824

17881825
let reconstructed = ProviderCredentialState::from_bound_environment(
@@ -1805,6 +1842,14 @@ mod tests {
18051842
.resolve_placeholder(&workload_placeholder),
18061843
Some("token-14")
18071844
);
1845+
assert_eq!(
1846+
reconstructed
1847+
.resolver_for_endpoint("api.example.com", 443, "/v1")
1848+
.expect("resolver")
1849+
.rewrite_header_value(&format!("Bearer {}", jwt_alias(&workload_placeholder)))
1850+
.expect("the JWT alias survives supervisor reconstruction"),
1851+
Some("Bearer token-14".to_string())
1852+
);
18081853
}
18091854

18101855
#[test]
@@ -1843,6 +1888,24 @@ mod tests {
18431888
.resolver_for_endpoint("new.example.com", 443, "/v2/messages")
18441889
.expect("current resolver");
18451890
assert_eq!(current.resolve_placeholder(&old_placeholder), None);
1891+
assert!(
1892+
current
1893+
.rewrite_header_value(&format!("Bearer {}", jwt_alias(&old_placeholder)))
1894+
.is_err(),
1895+
"a JWT alias must not carry a revoked identity into the new epoch"
1896+
);
1897+
for alias in [
1898+
"e30.e30.OPENSHELL-RESOLVE-ENV-API_KEY",
1899+
"e30.e30.OPENSHELL-RESOLVE-ENV-sshort_API_KEY",
1900+
"e30.e30.OPENSHELL-RESOLVE-ENV-v18446744073709551616_API_KEY",
1901+
] {
1902+
assert!(
1903+
current
1904+
.rewrite_header_value(&format!("Bearer {alias}"))
1905+
.is_err(),
1906+
"identityless and malformed JWT aliases must fail closed"
1907+
);
1908+
}
18461909
assert_eq!(
18471910
current.resolve_placeholder(&state.snapshot().child_env["API_KEY"]),
18481911
Some("new")
@@ -1854,6 +1917,13 @@ mod tests {
18541917
.rewrite_header_value(&state.snapshot().child_env["API_KEY"])
18551918
.expect_err("new handle must not resolve at the old endpoint");
18561919
assert!(error.is_endpoint_mismatch());
1920+
let error = wrong_endpoint
1921+
.rewrite_header_value(&format!(
1922+
"Bearer {}",
1923+
jwt_alias(&state.snapshot().child_env["API_KEY"])
1924+
))
1925+
.expect_err("JWT wrapping preserves endpoint authorization");
1926+
assert!(error.is_endpoint_mismatch());
18571927
}
18581928

18591929
#[test]
@@ -1870,6 +1940,11 @@ mod tests {
18701940
Vec::new(),
18711941
)
18721942
.expect("initial bindings");
1943+
let workload_placeholder = state.snapshot().child_env["API_KEY"].clone();
1944+
let request = format!(
1945+
"POST /v2/inference HTTP/1.1\r\nHost: api.example.com\r\nAuthorization: Bearer {}\r\nContent-Length: 2\r\n\r\n{{}}",
1946+
jwt_alias(&workload_placeholder)
1947+
);
18731948

18741949
for revision in [10, 100, 9, 101, 8, 102, 7, 103, 6] {
18751950
state
@@ -1888,13 +1963,20 @@ mod tests {
18881963
}
18891964

18901965
let resolver = state
1891-
.resolver_for_endpoint("api.example.com", 443, "/v1")
1966+
.resolver_for_endpoint("api.example.com", 443, "/v2/inference")
18921967
.expect("resolver");
18931968
assert_eq!(
18941969
resolver.resolve_placeholder("openshell:resolve:env:v50_API_KEY"),
18951970
Some("secret-6"),
18961971
"an aged-out placeholder may use the current secret across revisions in both numeric directions while its provider identity is unchanged"
18971972
);
1973+
let rewritten =
1974+
crate::secrets::rewrite_http_header_block(request.as_bytes(), Some(&resolver))
1975+
.expect("an aged-out JWT alias retains the issued provider identity");
1976+
assert_eq!(
1977+
String::from_utf8(rewritten.rewritten).expect("HTTP request"),
1978+
"POST /v2/inference HTTP/1.1\r\nHost: api.example.com\r\nAuthorization: Bearer secret-6\r\nContent-Length: 2\r\n\r\n{}"
1979+
);
18981980
}
18991981

19001982
#[test]
@@ -1930,6 +2012,15 @@ mod tests {
19302012
None,
19312013
"an opaque revision from another provider identity must fail closed even when it is numerically greater"
19322014
);
2015+
assert!(
2016+
resolver
2017+
.rewrite_header_value(&format!(
2018+
"Bearer {}",
2019+
jwt_alias(&format!("openshell:resolve:env:v{}_API_KEY", u64::MAX))
2020+
))
2021+
.is_err(),
2022+
"a JWT alias must not resolve a replacement provider's credential"
2023+
);
19332024
assert_eq!(
19342025
resolver.resolve_placeholder("openshell:resolve:env:API_KEY"),
19352026
None,

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -431,14 +431,17 @@ impl SecretResolver {
431431
/// Returns `None` if the placeholder is unknown or the resolved value
432432
/// contains prohibited control characters (CRLF, null byte).
433433
pub fn resolve_placeholder(&self, value: &str) -> Option<&str> {
434+
// Normalize aliases before identity checks so a wrapped revision or stable
435+
// handle retains the original endpoint, epoch, and expiration constraints.
436+
let canonical_alias = alias_env_key(value).map(placeholder_for_env_key);
437+
let value = canonical_alias.as_deref().unwrap_or(value);
434438
if placeholder_env_key(value).is_some_and(|key| self.identity_bound_env_keys.contains(key))
435439
&& revisioned_placeholder_parts(value).is_none()
436440
&& stable_placeholder_parts(value).is_none()
437441
{
438-
// Canonical placeholders and provider-shaped aliases carry no
439-
// credential identity. Endpoint-bound request input must use the
440-
// revision-scoped placeholder issued to the workload so a stale
441-
// process cannot resolve a replacement provider's credential.
442+
// Unversioned placeholders carry no credential identity. Endpoint-bound
443+
// request input must retain the issued revision or stable handle so a
444+
// stale process cannot resolve a replacement provider's credential.
442445
return None;
443446
}
444447
let secret = if let Some(secret) = self.by_placeholder.get(value) {
@@ -449,7 +452,7 @@ impl SecretResolver {
449452
// credential refresh. For endpoint-bound credentials, permit that
450453
// fallback only when the exact opaque revision belongs to the
451454
// current provider-identity epoch.
452-
let key = revisioned_placeholder_env_key(value).or_else(|| alias_env_key(value))?;
455+
let key = revisioned_placeholder_env_key(value)?;
453456
if let Some((revision, key)) = revisioned_placeholder_parts(value)
454457
&& self.identity_bound_env_keys.contains(key)
455458
&& self
@@ -782,7 +785,13 @@ fn placeholder_env_key(token: &str) -> Option<&str> {
782785
.map(|(_, key)| key)
783786
.or_else(|| revisioned_placeholder_env_key(token))
784787
.or_else(|| token.strip_prefix(PLACEHOLDER_PREFIX))
785-
.or_else(|| alias_env_key(token))
788+
.or_else(|| {
789+
alias_env_key(token).map(|suffix| {
790+
split_stable_env_key(suffix)
791+
.or_else(|| split_revisioned_env_key(suffix))
792+
.map_or(suffix, |(_, key)| key)
793+
})
794+
})
786795
}
787796

788797
pub fn uses_reserved_revision_namespace(key: &str) -> bool {

‎docs/how-it-works/providers/overview.mdx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,24 @@ The proxy resolves credential placeholders in the following parts of an HTTP req
447447
The proxy does not rewrite cookies, response content, unsupported request
448448
bodies, or WebSocket binary frames.
449449

450+
### Clients that parse JWT claims
451+
452+
A trusted launcher can wrap an issued placeholder in a JWT-shaped alias when a
453+
client needs local account metadata. Keep the original placeholder suffix in
454+
the signature segment:
455+
456+
```text
457+
base64url(header).base64url(claims).OPENSHELL-RESOLVE-ENV-<issued-placeholder-suffix>
458+
```
459+
460+
For example, a placeholder beginning `openshell:resolve:env:s` becomes a JWT
461+
whose signature begins `OPENSHELL-RESOLVE-ENV-s`, retaining the entire handle and
462+
environment key. Use the placeholder supplied to the running workload; do not
463+
substitute a bare environment key or manufacture a handle. OpenShell replaces
464+
the entire alias with the resolved credential and preserves its endpoint,
465+
authorization-epoch, and expiry checks. The claims supply local metadata only;
466+
the alias is not a signed identity token or an upstream credential.
467+
450468
### Fail-closed behavior
451469

452470
If policy allows a request but the credential binding does not include the

0 commit comments

Comments
 (0)