Skip to content

Commit bcb1b7e

Browse files
SkyZeroZxatscott
authored andcommitted
fix(http): preserve empty referrer option in HttpRequest
Preserve `referrer: ''` when constructing and cloning HttpRequest. An empty string is a valid Fetch referrer value and is documented by Angular as the way to omit referrer information for sensitive requests. The previous truthy checks treated it as if the option was not provided, causing requests to fall back to the browser default referrer behavior. (cherry picked from commit 1e54b8f)
1 parent b9d2938 commit bcb1b7e

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

packages/common/http/src/request.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ export class HttpRequest<T> {
523523
this.integrity = options.integrity;
524524
}
525525

526-
if (options.referrer) {
526+
if (options.referrer !== undefined) {
527527
this.referrer = options.referrer;
528528
}
529529

@@ -734,7 +734,8 @@ export class HttpRequest<T> {
734734
const mode = update.mode || this.mode;
735735
const redirect = update.redirect || this.redirect;
736736
const credentials = update.credentials || this.credentials;
737-
const referrer = update.referrer || this.referrer;
737+
738+
const referrer = update.referrer ?? this.referrer;
738739
const integrity = update.integrity || this.integrity;
739740
const referrerPolicy = update.referrerPolicy || this.referrerPolicy;
740741
// Carefully handle the transferCache to differentiate between

packages/common/http/test/request_spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ describe('HttpRequest', () => {
9595
const req = new HttpRequest('GET', '/test', {referrer: 'about:client'});
9696
expect(req.referrer).toBe('about:client');
9797
});
98+
it('should preserve an empty string referrer (used to suppress the Referer header)', () => {
99+
const req = new HttpRequest('GET', '/test', {referrer: ''});
100+
expect(req.referrer).toBe('');
101+
});
98102
it('should allow setting referrerPolicy option', () => {
99103
const req = new HttpRequest('GET', '/test', {referrerPolicy: 'no-referrer'});
100104
expect(req.referrerPolicy).toBe('no-referrer');
@@ -175,6 +179,13 @@ describe('HttpRequest', () => {
175179
it('and updates the referrer', () => {
176180
expect(req.clone({referrer: 'https://example.com'}).referrer).toBe('https://example.com');
177181
});
182+
it('and preserves an existing empty string referrer when the update omits it', () => {
183+
const emptyReferrerReq = new HttpRequest('GET', '/test', {referrer: ''});
184+
expect(emptyReferrerReq.clone().referrer).toBe('');
185+
});
186+
it('and can update the referrer to an empty string', () => {
187+
expect(req.clone({referrer: ''}).referrer).toBe('');
188+
});
178189
it('and updates the integrity', () => {
179190
expect(req.clone({integrity: 'sha512-...'}).integrity).toBe('sha512-...');
180191
});

0 commit comments

Comments
 (0)