Skip to content

Commit 7a44f88

Browse files
committed
test: validate that custom headers are removed on redirect
1 parent c855923 commit 7a44f88

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

components/http/okHttp/src/test/java/com/microsoft/kiota/http/middleware/RedirectHandlerTests.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,48 @@ void customScrubberIsUsed() throws Exception {
301301
assertNotNull(result.header("Authorization")); // KEPT by custom scrubber
302302
assertNotNull(result.header("Cookie")); // KEPT by custom scrubber
303303
}
304+
305+
@Test
306+
void customScrubberRemovesCustomHeaders() throws Exception {
307+
Request original =
308+
new Request.Builder()
309+
.url("http://trusted.example.com/api")
310+
.addHeader("Authorization", "Bearer token")
311+
.addHeader("X-Custom-Secret", "my-secret-value")
312+
.addHeader("X-Api-Key", "key-12345")
313+
.addHeader("X-Safe-Header", "keep-me")
314+
.build();
315+
Response redirect =
316+
new Response.Builder()
317+
.request(original)
318+
.protocol(Protocol.HTTP_1_1)
319+
.code(302)
320+
.message("Found")
321+
.header("Location", "http://other.example.com/api")
322+
.body(ResponseBody.create("", MediaType.parse("text/plain")))
323+
.build();
324+
325+
// Custom scrubber that removes custom headers in addition to the defaults
326+
RedirectHandlerOption.IScrubSensitiveHeaders customScrubber =
327+
(requestBuilder, originalUrl, newUrl, proxyResolver) -> {
328+
// Apply default scrubbing first
329+
RedirectHandlerOption.DEFAULT_SCRUB_SENSITIVE_HEADERS.scrubHeaders(
330+
requestBuilder, originalUrl, newUrl, proxyResolver);
331+
// Also remove application-specific sensitive headers
332+
requestBuilder.removeHeader("X-Custom-Secret");
333+
requestBuilder.removeHeader("X-Api-Key");
334+
};
335+
336+
Interceptor.Chain chain = mock(Interceptor.Chain.class);
337+
338+
RedirectHandlerOption option = new RedirectHandlerOption(5, null, customScrubber);
339+
Request result = new RedirectHandler().getRedirect(original, redirect, option, chain);
340+
341+
assertNotNull(result);
342+
assertEquals("other.example.com", result.url().host());
343+
assertNull(result.header("Authorization")); // stripped by default scrubber
344+
assertNull(result.header("X-Custom-Secret")); // stripped by custom scrubber
345+
assertNull(result.header("X-Api-Key")); // stripped by custom scrubber
346+
assertNotNull(result.header("X-Safe-Header")); // kept (not in scrub list)
347+
}
304348
}

0 commit comments

Comments
 (0)