@@ -75,6 +75,50 @@ private static class AuthTestCase {
7575
7676 @ Test
7777 public void testAPIKeySecurityScheme () {
78+ AuthTestCase authTestCase = new AuthTestCase (
79+ "http://agent.com/rpc" ,
80+ "session-id" ,
81+ APIKeySecurityScheme .TYPE ,
82+ "secret-api-key" ,
83+ new APIKeySecurityScheme (APIKeySecurityScheme .Location .HEADER , "X-API-Key" , "API Key authentication" ),
84+ "X-API-Key" ,
85+ "secret-api-key"
86+ );
87+ testSecurityScheme (authTestCase );
88+ }
89+
90+ @ Test
91+ public void testAPIKeySecurityScheme_SafeHeaderName_Authorization () {
92+ AuthTestCase authTestCase = new AuthTestCase (
93+ "http://agent.com/rpc" ,
94+ "session-id" ,
95+ APIKeySecurityScheme .TYPE ,
96+ "secret-api-key" ,
97+ new APIKeySecurityScheme (APIKeySecurityScheme .Location .HEADER , "Authorization" , "API Key authentication" ),
98+ "Authorization" ,
99+ "secret-api-key"
100+ );
101+ testSecurityScheme (authTestCase );
102+ }
103+
104+ @ Test
105+ public void testAPIKeySecurityScheme_SafeHeaderName_XAuthToken () {
106+ AuthTestCase authTestCase = new AuthTestCase (
107+ "http://agent.com/rpc" ,
108+ "session-id" ,
109+ APIKeySecurityScheme .TYPE ,
110+ "secret-api-key" ,
111+ new APIKeySecurityScheme (APIKeySecurityScheme .Location .HEADER , "X-Auth-Token" , "API Key authentication" ),
112+ "X-Auth-Token" ,
113+ "secret-api-key"
114+ );
115+ testSecurityScheme (authTestCase );
116+ }
117+
118+
119+ @ Test
120+ public void testAPIKeySecurityScheme_CaseInsensitiveHeaderName () {
121+ // Test that lowercase header names are accepted (case-insensitive comparison)
78122 AuthTestCase authTestCase = new AuthTestCase (
79123 "http://agent.com/rpc" ,
80124 "session-id" ,
@@ -87,6 +131,124 @@ public void testAPIKeySecurityScheme() {
87131 testSecurityScheme (authTestCase );
88132 }
89133
134+ @ Test
135+ public void testAPIKeySecurityScheme_CaseInsensitiveHeaderName_Authorization () {
136+ // Test that lowercase "authorization" is accepted
137+ AuthTestCase authTestCase = new AuthTestCase (
138+ "http://agent.com/rpc" ,
139+ "session-id" ,
140+ APIKeySecurityScheme .TYPE ,
141+ "secret-api-key" ,
142+ new APIKeySecurityScheme (APIKeySecurityScheme .Location .HEADER , "authorization" , "API Key authentication" ),
143+ "authorization" ,
144+ "secret-api-key"
145+ );
146+ testSecurityScheme (authTestCase );
147+ }
148+
149+
150+ @ Test
151+ public void testAPIKeySecurityScheme_UnsafeHeaderName_Rejected () {
152+ String sessionId = "session-id" ;
153+ String schemeName = APIKeySecurityScheme .TYPE ;
154+ String credential = "secret-api-key" ;
155+
156+ credentialStore .setCredential (sessionId , schemeName , credential );
157+
158+ // Use an unsafe header name that's not in the allowlist
159+ SecurityScheme securityScheme = new APIKeySecurityScheme (
160+ APIKeySecurityScheme .Location .HEADER ,
161+ "X-Malicious-Redirect-Header" ,
162+ "Unsafe header"
163+ );
164+ AgentCard agentCard = createAgentCard (schemeName , securityScheme );
165+
166+ Map <String , Object > requestPayload = Map .of ("test" , "payload" );
167+ Map <String , String > headers = Map .of ();
168+ ClientCallContext context = new ClientCallContext (Map .of ("sessionId" , sessionId ), Map .of ());
169+
170+ PayloadAndHeaders result = authInterceptor .intercept (
171+ "SendMessage" ,
172+ requestPayload ,
173+ headers ,
174+ agentCard ,
175+ context
176+ );
177+
178+ assertEquals (requestPayload , result .getPayload ());
179+ // Credential should NOT be injected for unsafe header name
180+ assertNull (result .getHeaders ().get ("X-Malicious-Redirect-Header" ));
181+ assertEquals (0 , result .getHeaders ().size ());
182+ }
183+
184+ @ Test
185+ public void testAPIKeySecurityScheme_QueryLocation_NotInjectedInHeader () {
186+ String sessionId = "session-id" ;
187+ String schemeName = APIKeySecurityScheme .TYPE ;
188+ String credential = "secret-api-key" ;
189+
190+ credentialStore .setCredential (sessionId , schemeName , credential );
191+
192+ // API key in query parameter location should not be injected as header
193+ SecurityScheme securityScheme = new APIKeySecurityScheme (
194+ APIKeySecurityScheme .Location .QUERY ,
195+ "api_key" ,
196+ "Query parameter API key"
197+ );
198+ AgentCard agentCard = createAgentCard (schemeName , securityScheme );
199+
200+ Map <String , Object > requestPayload = Map .of ("test" , "payload" );
201+ Map <String , String > headers = Map .of ();
202+ ClientCallContext context = new ClientCallContext (Map .of ("sessionId" , sessionId ), Map .of ());
203+
204+ PayloadAndHeaders result = authInterceptor .intercept (
205+ "SendMessage" ,
206+ requestPayload ,
207+ headers ,
208+ agentCard ,
209+ context
210+ );
211+
212+ assertEquals (requestPayload , result .getPayload ());
213+ // Credential should NOT be injected as header for query location
214+ assertNull (result .getHeaders ().get ("api_key" ));
215+ assertEquals (0 , result .getHeaders ().size ());
216+ }
217+
218+ @ Test
219+ public void testAPIKeySecurityScheme_CookieLocation_NotInjectedInHeader () {
220+ String sessionId = "session-id" ;
221+ String schemeName = APIKeySecurityScheme .TYPE ;
222+ String credential = "secret-api-key" ;
223+
224+ credentialStore .setCredential (sessionId , schemeName , credential );
225+
226+ // API key in cookie location should not be injected as header
227+ SecurityScheme securityScheme = new APIKeySecurityScheme (
228+ APIKeySecurityScheme .Location .COOKIE ,
229+ "session_token" ,
230+ "Cookie-based API key"
231+ );
232+ AgentCard agentCard = createAgentCard (schemeName , securityScheme );
233+
234+ Map <String , Object > requestPayload = Map .of ("test" , "payload" );
235+ Map <String , String > headers = Map .of ();
236+ ClientCallContext context = new ClientCallContext (Map .of ("sessionId" , sessionId ), Map .of ());
237+
238+ PayloadAndHeaders result = authInterceptor .intercept (
239+ "SendMessage" ,
240+ requestPayload ,
241+ headers ,
242+ agentCard ,
243+ context
244+ );
245+
246+ assertEquals (requestPayload , result .getPayload ());
247+ // Credential should NOT be injected as header for cookie location
248+ assertNull (result .getHeaders ().get ("session_token" ));
249+ assertEquals (0 , result .getHeaders ().size ());
250+ }
251+
90252 @ Test
91253 public void testOAuth2SecurityScheme () {
92254 AuthTestCase authTestCase = new AuthTestCase (
@@ -238,9 +400,9 @@ void testAvailableSecuritySchemeNotInAgentCardSecuritySchemes() {
238400 String schemeName = "missing" ;
239401 String sessionId = "session-id" ;
240402 String credential = "dummy-token" ;
241-
403+
242404 credentialStore .setCredential (sessionId , schemeName , credential );
243-
405+
244406 // Create agent card with security requirement but no scheme definition
245407 AgentCard agentCard = AgentCard .builder ()
246408 .name ("missing" )
@@ -254,7 +416,7 @@ void testAvailableSecuritySchemeNotInAgentCardSecuritySchemes() {
254416 .securityRequirements (List .of (SecurityRequirement .builder ().scheme (schemeName , List .of ()).build ()))
255417 .securitySchemes (Map .of ()) // no security schemes
256418 .build ();
257-
419+
258420 Map <String , Object > requestPayload = Map .of ("foo" , "bar" );
259421 Map <String , String > headers = Map .of ("fizz" , "buzz" );
260422 ClientCallContext context = new ClientCallContext (Map .of ("sessionId" , sessionId ), Map .of ());
@@ -276,7 +438,7 @@ void testNoCredentialAvailable() {
276438 String schemeName = "apikey" ;
277439 SecurityScheme securityScheme = new APIKeySecurityScheme (APIKeySecurityScheme .Location .HEADER , "X-API-Key" , "API Key authentication" );
278440 AgentCard agentCard = createAgentCard (schemeName , securityScheme );
279-
441+
280442 Map <String , Object > requestPayload = Map .of ("test" , "payload" );
281443 Map <String , String > headers = Map .of ();
282444 ClientCallContext context = new ClientCallContext (Map .of ("sessionId" , "session-id" ), Map .of ());
@@ -307,7 +469,7 @@ void testNoAgentCardSecuritySpecified() {
307469 .skills (List .of ())
308470 .securityRequirements (null ) // no security info
309471 .build ();
310-
472+
311473 Map <String , Object > requestPayload = Map .of ("test" , "payload" );
312474 Map <String , String > headers = Map .of ();
313475 ClientCallContext context = new ClientCallContext (Map .of ("sessionId" , "session-id" ), Map .of ());
0 commit comments