@@ -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 \n Authorization: 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 \n Authorization: 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 \n Host: api.example.com\r \n Authorization: Bearer {}\r \n Content-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 \n Host: api.example.com\r \n Authorization: Bearer secret-6\r \n Content-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 ,
0 commit comments