Skip to content

Commit cd771d3

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.
1 parent e6ae250 commit cd771d3

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
@@ -379,7 +379,7 @@ export class HttpRequest<T> implements HttpRequestOptions {
379379
this.integrity = options.integrity;
380380
}
381381

382-
if (options.referrer) {
382+
if (options.referrer !== undefined) {
383383
this.referrer = options.referrer;
384384
}
385385

@@ -555,7 +555,8 @@ export class HttpRequest<T> implements HttpRequestOptions {
555555
const mode = update.mode || this.mode;
556556
const redirect = update.redirect || this.redirect;
557557
const credentials = update.credentials || this.credentials;
558-
const referrer = update.referrer || this.referrer;
558+
559+
const referrer = update.referrer ?? this.referrer;
559560
const integrity = update.integrity || this.integrity;
560561
const referrerPolicy = update.referrerPolicy || this.referrerPolicy;
561562
// 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');
@@ -194,6 +198,13 @@ describe('HttpRequest', () => {
194198
it('and updates the referrer', () => {
195199
expect(req.clone({referrer: 'https://example.com'}).referrer).toBe('https://example.com');
196200
});
201+
it('and preserves an existing empty string referrer when the update omits it', () => {
202+
const emptyReferrerReq = new HttpRequest('GET', '/test', {referrer: ''});
203+
expect(emptyReferrerReq.clone().referrer).toBe('');
204+
});
205+
it('and can update the referrer to an empty string', () => {
206+
expect(req.clone({referrer: ''}).referrer).toBe('');
207+
});
197208
it('and updates the integrity', () => {
198209
expect(req.clone({integrity: 'sha512-...'}).integrity).toBe('sha512-...');
199210
});

0 commit comments

Comments
 (0)