Skip to content

Commit b8bd493

Browse files
SkyZeroZxalxhub
authored andcommitted
fix(service-worker): Preserves explicit 'credentials: omit' in asset requests
Ensures that explicitly provided `credentials: 'omit'` options are preserved when creating new requests, preventing unintended credential inclusion. (cherry picked from commit 5b0e966)
1 parent 251c8f2 commit b8bd493

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

packages/service-worker/worker/src/assets.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,27 @@ export abstract class AssetGroup {
501501
* Create a new `Request` based on the specified URL and `RequestInit` options, preserving only
502502
* metadata that are known to be safe.
503503
*
504-
* Currently, only headers and redirect policy are preserved.
504+
* Currently, headers, redirect policy, and an explicit `credentials: 'omit'` are preserved.
505505
*
506506
* NOTE:
507-
* Things like credential inclusion are intentionally omitted to avoid issues with opaque
508-
* responses.
509-
*
507+
* `credentials: 'same-origin'` and `credentials: 'include'` are intentionally not preserved.
508+
* Forwarding `'include'` could leak cookies to cross-origin asset hosts, and forwarding
509+
* `'same-origin'` matches the default `fetch()` behavior so there is nothing to preserve.
510510
* TODO(gkalpak):
511511
* Investigate preserving more metadata. See, also, discussion on preserving `mode`:
512-
* https://github.com/angular/angular/issues/41931#issuecomment-1227601347
512+
* https://github.com/angular/angular/issues/41931#issuecomment-1227601347.
513513
*/
514514
private newRequestWithMetadata(url: string, options: RequestInit): Request {
515-
return this.adapter.newRequest(url, {
515+
const init: RequestInit = {
516516
headers: options.headers,
517517
redirect: options.redirect,
518-
});
518+
};
519+
520+
if (options.credentials === 'omit') {
521+
init.credentials = 'omit';
522+
}
523+
524+
return this.adapter.newRequest(url, init);
519525
}
520526

521527
/**

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,18 @@ import {envIsSupported} from '../testing/utils';
16551655
expect((bazReq as any).unknownOption).toBeUndefined();
16561656
});
16571657

1658+
it(`passes 'credentials: omit' through to the server`, async () => {
1659+
// Request a lazy-cached asset (so that it is fetched from the network) and provide an
1660+
// explicit anonymous credentials mode.
1661+
const reqInit = {credentials: 'omit'};
1662+
expect(await makeRequest(scope, '/baz.txt', undefined, reqInit)).toBe('this is baz');
1663+
1664+
// Verify that the explicit `'omit'` value was preserved (instead of being replaced by the
1665+
// default `'same-origin'`).
1666+
const [bazReq] = server.getRequestsFor('/baz.txt');
1667+
expect(bazReq.credentials).toBe('omit');
1668+
});
1669+
16581670
describe('for redirect requests', () => {
16591671
it('passes headers through to the server', async () => {
16601672
// Request a redirected, lazy-cached asset (so that it is fetched from the network) and
@@ -1695,6 +1707,20 @@ import {envIsSupported} from '../testing/utils';
16951707
makeRequest(scope, '/lazy/redirected.txt', undefined, {redirect: 'error'}),
16961708
).toBeRejected();
16971709
});
1710+
1711+
it(`passes 'credentials: omit' through to the server`, async () => {
1712+
// Request a redirected, lazy-cached asset (so that it is fetched from the network) and
1713+
// provide an explicit anonymous credentials mode.
1714+
const reqInit = {credentials: 'omit'};
1715+
expect(await makeRequest(scope, '/lazy/redirected.txt', undefined, reqInit)).toBe(
1716+
'this was a redirect too',
1717+
);
1718+
1719+
// Verify that the explicit `'omit'` value was preserved across the redirect
1720+
// reconstruction (instead of being replaced by the default `'same-origin'`).
1721+
const [redirectReq] = server.getRequestsFor('/lazy/redirect-target.txt');
1722+
expect(redirectReq.credentials).toBe('omit');
1723+
});
16981724
});
16991725
});
17001726

0 commit comments

Comments
 (0)