Skip to content

Commit

Permalink
fix(http): exclude caching for authenticated HTTP requests (#54746)
Browse files Browse the repository at this point in the history
This update modifies the transfer cache logic to prevent caching of HTTP requests that require authorization.

Closes: #54745

PR Close #54746
  • Loading branch information
alan-agius4 authored and atscott committed Mar 7, 2024
1 parent 3659553 commit 8d37ed0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/common/http/src/transfer_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export function transferCacheInterceptorFn(
// POST requests are allowed either globally or at request level
(requestMethod === 'POST' && !globalOptions.includePostRequests && !requestOptions) ||
(requestMethod !== 'POST' && !ALLOWED_METHODS.includes(requestMethod)) ||
requestOptions === false || //
// Do not cache request that require authorization
req.headers.has('authorization') ||
req.headers.has('proxy-authorization') ||
requestOptions === false ||
globalOptions.filter?.(req) === false
) {
return next(req);
Expand Down
16 changes: 16 additions & 0 deletions packages/common/http/test/transfer_cache_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ describe('TransferCache', () => {
makeRequestAndExpectNone('/test-2?foo=1', 'POST', {transferCache: true});
});

it('should not cache request that requires authorization', async () => {
makeRequestAndExpectOne('/test-auth', 'foo', {
headers: {Authorization: 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'},
});

makeRequestAndExpectOne('/test-auth', 'foo');
});

it('should not cache request that requires proxy authorization', async () => {
makeRequestAndExpectOne('/test-auth', 'foo', {
headers: {'Proxy-Authorization': 'Basic YWxhZGRpbjpvcGVuc2VzYW1l'},
});

makeRequestAndExpectOne('/test-auth', 'foo');
});

describe('caching with global setting', () => {
beforeEach(
withBody('<test-app-http></test-app-http>', () => {
Expand Down

0 comments on commit 8d37ed0

Please sign in to comment.