diff --git a/lib/browser/api/net.ts b/lib/browser/api/net.ts index 6753850da954..6803968b6391 100644 --- a/lib/browser/api/net.ts +++ b/lib/browser/api/net.ts @@ -386,6 +386,7 @@ class ClientRequest extends Writable implements Electron.ClientRequest { } return ret; }; + this._urlLoaderOptions.referrer = this._urlLoaderOptions.extraHeaders.referer || ''; const opts = { ...this._urlLoaderOptions, extraHeaders: stringifyValues(this._urlLoaderOptions.extraHeaders) }; this._urlLoader = createURLLoader(opts); this._urlLoader.on('response-started', (event, finalUrl, responseHead) => { diff --git a/shell/browser/api/electron_api_url_loader.cc b/shell/browser/api/electron_api_url_loader.cc index b129804e65ad..4b0dabf75798 100644 --- a/shell/browser/api/electron_api_url_loader.cc +++ b/shell/browser/api/electron_api_url_loader.cc @@ -352,6 +352,7 @@ gin::Handle SimpleURLLoaderWrapper::Create( request->force_ignore_site_for_cookies = true; opts.Get("method", &request->method); opts.Get("url", &request->url); + opts.Get("referrer", &request->referrer); std::map extra_headers; if (opts.Get("extraHeaders", &extra_headers)) { for (const auto& it : extra_headers) { diff --git a/spec-main/api-net-spec.ts b/spec-main/api-net-spec.ts index 2316718d8540..5c5a2e1b334c 100644 --- a/spec-main/api-net-spec.ts +++ b/spec-main/api-net-spec.ts @@ -1253,6 +1253,38 @@ describe('net module', () => { setTimeout(resolve, 50); }); }); + + it('should remove the referer header when no referrer url specified', async () => { + const serverUrl = await respondOnce.toSingleURL((request, response) => { + expect(request.headers.referer).to.equal(undefined); + response.statusCode = 200; + response.statusMessage = 'OK'; + response.end(); + }); + const urlRequest = net.request(serverUrl); + urlRequest.end(); + + const response = await getResponse(urlRequest); + expect(response.statusCode).to.equal(200); + await collectStreamBody(response); + }); + + it('should set the referer header when a referrer url specified', async () => { + const referrerURL = 'https://www.electronjs.org/'; + const serverUrl = await respondOnce.toSingleURL((request, response) => { + expect(request.headers.referer).to.equal(referrerURL); + response.statusCode = 200; + response.statusMessage = 'OK'; + response.end(); + }); + const urlRequest = net.request(serverUrl); + urlRequest.setHeader('referer', referrerURL); + urlRequest.end(); + + const response = await getResponse(urlRequest); + expect(response.statusCode).to.equal(200); + await collectStreamBody(response); + }); }); describe('IncomingMessage API', () => { diff --git a/typings/internal-ambient.d.ts b/typings/internal-ambient.d.ts index 24390a177217..6e4f7d59b29a 100644 --- a/typings/internal-ambient.d.ts +++ b/typings/internal-ambient.d.ts @@ -58,6 +58,7 @@ declare namespace NodeJS { body: Uint8Array | BodyFunc; session?: Electron.Session; partition?: string; + referrer?: string; } type ResponseHead = { statusCode: number;