net: Don't ignore the referer header in net.request (#23386)
This commit is contained in:
parent
0a481242f0
commit
9d851b8791
4 changed files with 35 additions and 0 deletions
|
@ -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) => {
|
||||
|
|
|
@ -352,6 +352,7 @@ gin::Handle<SimpleURLLoaderWrapper> 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<std::string, std::string> extra_headers;
|
||||
if (opts.Get("extraHeaders", &extra_headers)) {
|
||||
for (const auto& it : extra_headers) {
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
1
typings/internal-ambient.d.ts
vendored
1
typings/internal-ambient.d.ts
vendored
|
@ -58,6 +58,7 @@ declare namespace NodeJS {
|
|||
body: Uint8Array | BodyFunc;
|
||||
session?: Electron.Session;
|
||||
partition?: string;
|
||||
referrer?: string;
|
||||
}
|
||||
type ResponseHead = {
|
||||
statusCode: number;
|
||||
|
|
Loading…
Reference in a new issue