Skip to content

Commit 06be298

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 9f443bc commit 06be298

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
@@ -516,7 +516,7 @@ export class HttpRequest<T> {
516516
this.integrity = options.integrity;
517517
}
518518

519-
if (options.referrer) {
519+
if (options.referrer !== undefined) {
520520
this.referrer = options.referrer;
521521
}
522522

@@ -720,7 +720,8 @@ export class HttpRequest<T> {
720720
const mode = update.mode || this.mode;
721721
const redirect = update.redirect || this.redirect;
722722
const credentials = update.credentials || this.credentials;
723-
const referrer = update.referrer || this.referrer;
723+
724+
const referrer = update.referrer ?? this.referrer;
724725
const integrity = update.integrity || this.integrity;
725726
// Carefully handle the transferCache to differentiate between
726727
// `false` and `undefined` in the update args.

‎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
});
99103
describe('clone() copies the request', () => {
100104
const headers = new HttpHeaders({
@@ -169,6 +173,13 @@ describe('HttpRequest', () => {
169173
it('and updates the referrer', () => {
170174
expect(req.clone({referrer: 'https://example.com'}).referrer).toBe('https://example.com');
171175
});
176+
it('and preserves an existing empty string referrer when the update omits it', () => {
177+
const emptyReferrerReq = new HttpRequest('GET', '/test', {referrer: ''});
178+
expect(emptyReferrerReq.clone().referrer).toBe('');
179+
});
180+
it('and can update the referrer to an empty string', () => {
181+
expect(req.clone({referrer: ''}).referrer).toBe('');
182+
});
172183
it('and updates the integrity', () => {
173184
expect(req.clone({integrity: 'sha512-...'}).integrity).toBe('sha512-...');
174185
});

0 commit comments

Comments
 (0)