node/test/parallel/test-http-client-null-prototype-options.js
Matteo Collina e0200f2d73
http: harden ClientRequest options merge
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/63082
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-05-04 15:22:31 +00:00

62 lines
1.4 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('node:assert');
const http = require('node:http');
const server = http.createServer(common.mustCall((req, res) => {
req.resume();
req.on('end', common.mustCall(() => {
res.end('ok');
}));
}, 2));
function makeRequest(options, callback) {
Object.defineProperty(Object.prototype, 'hostname', {
__proto__: null,
configurable: true,
get: common.mustNotCall('get %Object.prototype%.hostname'),
});
let req;
try {
req = http.request(options, callback);
} finally {
delete Object.prototype.hostname;
}
req.on('error', common.mustNotCall());
req.end();
}
server.listen(0, common.localhostIPv4, common.mustCall(() => {
const { address, port } = server.address();
makeRequest(
{
host: address,
port,
path: '/',
},
common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
res.resume();
res.on('end', common.mustCall());
}),
);
const nullProtoOptions = { __proto__: null, host: address, port, path: '/' };
assert.strictEqual(Object.getPrototypeOf(nullProtoOptions), null);
makeRequest(
nullProtoOptions,
common.mustCall((res) => {
assert.strictEqual(res.statusCode, 200);
res.resume();
res.on('end', common.mustCall(() => {
server.close(common.mustCall());
}));
}),
);
}));