http: use objects with null prototype in Agent

Fixes: https://github.com/nodejs/node/issues/36364

PR-URL: https://github.com/nodejs/node/pull/36409
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Michaël Zasso 2020-12-06 14:52:08 +01:00
parent c6c8337402
commit 2ef9a76ece
No known key found for this signature in database
GPG Key ID: 770F7A9A5AE15600
9 changed files with 33 additions and 19 deletions

View File

@ -262,6 +262,10 @@ terminates them.
### `agent.freeSockets`
<!-- YAML
added: v0.11.4
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/36409
description: The property now has a `null` prototype.
-->
* {Object}
@ -328,6 +332,10 @@ can have open. Unlike `maxSockets`, this parameter applies across all origins.
### `agent.requests`
<!-- YAML
added: v0.5.9
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/36409
description: The property now has a `null` prototype.
-->
* {Object}
@ -338,6 +346,10 @@ sockets. Do not modify.
### `agent.sockets`
<!-- YAML
added: v0.3.6
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/36409
description: The property now has a `null` prototype.
-->
* {Object}

View File

@ -23,6 +23,7 @@
const {
NumberIsNaN,
ObjectCreate,
ObjectKeys,
ObjectSetPrototypeOf,
ObjectValues,
@ -83,13 +84,13 @@ function Agent(options) {
this.defaultPort = 80;
this.protocol = 'http:';
this.options = { ...options };
this.options = { __proto__: null, ...options };
// Don't confuse net and make it think that we're connecting to a pipe
this.options.path = null;
this.requests = {};
this.sockets = {};
this.freeSockets = {};
this.requests = ObjectCreate(null);
this.sockets = ObjectCreate(null);
this.freeSockets = ObjectCreate(null);
this.keepAliveMsecs = this.options.keepAliveMsecs || 1000;
this.keepAlive = this.options.keepAlive || false;
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
@ -227,13 +228,14 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
// Legacy API: addRequest(req, host, port, localAddress)
if (typeof options === 'string') {
options = {
__proto__: null,
host: options,
port,
localAddress
};
}
options = { ...options, ...this.options };
options = { __proto__: null, ...options, ...this.options };
if (options.socketPath)
options.path = options.socketPath;
@ -294,7 +296,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
};
Agent.prototype.createSocket = function createSocket(req, options, cb) {
options = { ...options, ...this.options };
options = { __proto__: null, ...options, ...this.options };
if (options.socketPath)
options.path = options.socketPath;
@ -435,7 +437,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
// There might be older requests in a different origin, but
// if the origin which releases the socket has pending requests
// that will be prioritized.
for (const prop in this.requests) {
for (const prop of ObjectKeys(this.requests)) {
// Check whether this specific origin is already at maxSockets
if (this.sockets[prop] && this.sockets[prop].length) break;
debug('removeSocket, have a request with different origin,' +

View File

@ -46,8 +46,8 @@ server.listen(0, common.mustCall(() => {
}));
const countdown = new Countdown(max, () => {
assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!http.globalAgent.requests.hasOwnProperty(name));
assert(!(name in http.globalAgent.sockets));
assert(!(name in http.globalAgent.requests));
server.close();
});

View File

@ -14,7 +14,7 @@ server.listen(0, common.mustCall(() => {
http.globalAgent = agent;
makeRequest();
assert(agent.sockets.hasOwnProperty(name)); // Agent has indeed been used
assert(name in agent.sockets); // Agent has indeed been used
}));
function makeRequest() {

View File

@ -43,8 +43,8 @@ server.listen(0, common.mustCall(function() {
// Make sure this request got removed from the pool.
const name = `localhost:${server.address().port}`;
assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!http.globalAgent.requests.hasOwnProperty(name));
assert(!(name in http.globalAgent.sockets));
assert(!(name in http.globalAgent.requests));
// Make sure this socket has detached.
assert(!socket.ondata);

View File

@ -70,8 +70,8 @@ server.listen(0, common.mustCall(() => {
req.on('connect', common.mustCall((res, socket, firstBodyChunk) => {
// Make sure this request got removed from the pool.
const name = `localhost:${server.address().port}`;
assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!http.globalAgent.requests.hasOwnProperty(name));
assert(!(name in http.globalAgent.sockets));
assert(!(name in http.globalAgent.requests));
// Make sure this socket has detached.
assert(!socket.ondata);

View File

@ -59,7 +59,7 @@ server.listen(0, common.mustCall(function() {
}, common.mustCall((response) => {
response.on('end', common.mustCall(() => {
assert.strictEqual(agent.sockets[name].length, 1);
assert(!agent.requests.hasOwnProperty(name));
assert(!(name in agent.requests));
server.close();
}));
response.resume();
@ -67,6 +67,6 @@ server.listen(0, common.mustCall(function() {
}));
process.on('exit', () => {
assert(!agent.sockets.hasOwnProperty(name));
assert(!agent.requests.hasOwnProperty(name));
assert(!(name in agent.sockets));
assert(!(name in agent.requests));
});

View File

@ -78,7 +78,7 @@ server.listen(0, '127.0.0.1', common.mustCall(function() {
assert.deepStrictEqual(expectedHeaders, res.headers);
// Make sure this request got removed from the pool.
assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!(name in http.globalAgent.sockets));
req.on('close', common.mustCall(function() {
socket.end();

View File

@ -25,7 +25,7 @@ server.listen(0, common.mustCall(() => {
https.globalAgent = agent;
makeRequest();
assert(agent.sockets.hasOwnProperty(name)); // Agent has indeed been used
assert(name in agent.sockets); // Agent has indeed been used
}));
function makeRequest() {