Skip to content

Commit 2a36530

Browse files
authored
fix: prevent credential leakage via header injection and redirects (a2aproject#1097)
- Add safe header allowlist validation to AuthInterceptor - Disable automatic HTTP redirects in all HTTP clients - Add 14 security regression tests - Add security documentation page Fixes GHSA-9rhm-2h4x-jwmx
1 parent 2de986c commit 2a36530

10 files changed

Lines changed: 571 additions & 38 deletions

File tree

‎client/transport/spi/src/main/java/org/a2aproject/sdk/client/transport/spi/interceptors/auth/AuthInterceptor.java‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Locale;
66
import java.util.Map;
7+
import java.util.Set;
78

89
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallContext;
910
import org.a2aproject.sdk.client.transport.spi.interceptors.ClientCallInterceptor;
@@ -28,6 +29,22 @@ public class AuthInterceptor extends ClientCallInterceptor {
2829
public static final String AUTHORIZATION = "Authorization";
2930
private static final String BEARER = "Bearer ";
3031
private static final String BASIC = "Basic ";
32+
33+
/**
34+
* Allowlist of header names that are safe for API key injection.
35+
* This prevents credential leakage through malicious header names that could
36+
* be forwarded to third-party origins during redirects or other scenarios.
37+
* Only standard authentication-related headers are permitted.
38+
* Header names are stored in lowercase for case-insensitive comparison.
39+
*/
40+
private static final Set<String> SAFE_API_KEY_HEADER_NAMES = Set.of(
41+
"authorization",
42+
"x-api-key",
43+
"api-key",
44+
"x-auth-token",
45+
"x-authentication"
46+
);
47+
3148
private final CredentialService credentialService;
3249

3350
public AuthInterceptor(final CredentialService credentialService) {
@@ -66,8 +83,13 @@ public PayloadAndHeaders intercept(String methodName, @Nullable Object payload,
6683
updatedHeaders.put(AUTHORIZATION, getBearerValue(credential));
6784
return new PayloadAndHeaders(payload, updatedHeaders);
6885
} else if (securityScheme instanceof APIKeySecurityScheme apiKeySecurityScheme) {
69-
updatedHeaders.put(apiKeySecurityScheme.name(), credential);
70-
return new PayloadAndHeaders(payload, updatedHeaders);
86+
// Only inject API key if it's intended for header transport and the header name is safe
87+
if (apiKeySecurityScheme.location() == APIKeySecurityScheme.Location.HEADER
88+
&& isSafeHeaderName(apiKeySecurityScheme.name())) {
89+
updatedHeaders.put(apiKeySecurityScheme.name(), credential);
90+
return new PayloadAndHeaders(payload, updatedHeaders);
91+
}
92+
// Skip credential injection for unsafe header names or non-header locations
7193
}
7294
}
7395
}
@@ -82,4 +104,17 @@ private static String getBearerValue(String credential) {
82104
private static String getBasicValue(String credential) {
83105
return BASIC + credential;
84106
}
107+
108+
/**
109+
* Validates that a header name is safe for API key injection.
110+
* This prevents credential leakage by rejecting header names that could
111+
* be exploited to forward credentials to unintended destinations.
112+
* Header name comparison is case-insensitive per RFC 7230.
113+
*
114+
* @param headerName the header name to validate
115+
* @return true if the header name is in the safe allowlist, false otherwise
116+
*/
117+
private static boolean isSafeHeaderName(String headerName) {
118+
return SAFE_API_KEY_HEADER_NAMES.contains(headerName.toLowerCase(Locale.ROOT));
119+
}
85120
}

‎client/transport/spi/src/test/java/org/a2aproject/sdk/client/transport/spi/interceptors/auth/AuthInterceptorTest.java‎

Lines changed: 167 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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());

‎compat-0.3/client/transport/spi/src/main/java/org/a2aproject/sdk/compat03/client/transport/spi/interceptors/auth/AuthInterceptor_v0_3.java‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Locale;
66
import java.util.Map;
7+
import java.util.Set;
78

89
import org.a2aproject.sdk.compat03.client.transport.spi.interceptors.ClientCallContext_v0_3;
910
import org.a2aproject.sdk.compat03.client.transport.spi.interceptors.ClientCallInterceptor_v0_3;
@@ -27,6 +28,22 @@ public class AuthInterceptor_v0_3 extends ClientCallInterceptor_v0_3 {
2728
public static final String AUTHORIZATION = "Authorization";
2829
private static final String BEARER = "Bearer ";
2930
private static final String BASIC = "Basic ";
31+
32+
/**
33+
* Allowlist of header names that are safe for API key injection.
34+
* This prevents credential leakage through malicious header names that could
35+
* be forwarded to third-party origins during redirects or other scenarios.
36+
* Only standard authentication-related headers are permitted.
37+
* Header names are stored in lowercase for case-insensitive comparison.
38+
*/
39+
private static final Set<String> SAFE_API_KEY_HEADER_NAMES = Set.of(
40+
"authorization",
41+
"x-api-key",
42+
"api-key",
43+
"x-auth-token",
44+
"x-authentication"
45+
);
46+
3047
private final CredentialService_v0_3 credentialService;
3148

3249
public AuthInterceptor_v0_3(final CredentialService_v0_3 credentialService) {
@@ -62,8 +79,13 @@ public PayloadAndHeaders_v0_3 intercept(String methodName, @Nullable Object payl
6279
updatedHeaders.put(AUTHORIZATION, getBearerValue(credential));
6380
return new PayloadAndHeaders_v0_3(payload, updatedHeaders);
6481
} else if (securityScheme instanceof APIKeySecurityScheme_v0_3 apiKeySecurityScheme) {
65-
updatedHeaders.put(apiKeySecurityScheme.name(), credential);
66-
return new PayloadAndHeaders_v0_3(payload, updatedHeaders);
82+
// Only inject API key if it's intended for header transport and the header name is safe
83+
if ("header".equals(apiKeySecurityScheme.in())
84+
&& isSafeHeaderName(apiKeySecurityScheme.name())) {
85+
updatedHeaders.put(apiKeySecurityScheme.name(), credential);
86+
return new PayloadAndHeaders_v0_3(payload, updatedHeaders);
87+
}
88+
// Skip credential injection for unsafe header names or non-header locations
6789
}
6890
}
6991
}
@@ -78,4 +100,17 @@ private static String getBearerValue(String credential) {
78100
private static String getBasicValue(String credential) {
79101
return BASIC + credential;
80102
}
103+
104+
/**
105+
* Validates that a header name is safe for API key injection.
106+
* This prevents credential leakage by rejecting header names that could
107+
* be exploited to forward credentials to unintended destinations.
108+
* Header name comparison is case-insensitive per RFC 7230.
109+
*
110+
* @param headerName the header name to validate
111+
* @return true if the header name is in the safe allowlist, false otherwise
112+
*/
113+
private static boolean isSafeHeaderName(String headerName) {
114+
return SAFE_API_KEY_HEADER_NAMES.contains(headerName.toLowerCase(Locale.ROOT));
115+
}
81116
}

0 commit comments

Comments
 (0)