mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
Between onSocket and onSocketNT, the socket had no error handler, meaning any errors emitted during that window (e.g. from a blocklist check or custom lookup) would be unhandled even if the user had set up a request error handler. Fix this by attaching socketErrorListener synchronously in onSocket, setting socket._httpMessage so the listener can forward errors to the request. The _destroy path in onSocketNT is also guarded to prevent double-firing if socketErrorListener already emitted the error. Fixes: https://github.com/nodejs/node/issues/48771 Refs: https://github.com/nodejs/node/pull/61658 PR-URL: https://github.com/nodejs/node/pull/61770 Refs: https://github.com/nodejs/node/issues/48771 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
48 lines
1.6 KiB
JavaScript
48 lines
1.6 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const http = require('http');
|
|
const net = require('net');
|
|
|
|
// This test verifies that errors occurring synchronously during connection
|
|
// when using http.request with a custom lookup function and blockList
|
|
// can be caught by the error handler.
|
|
// Regression test for https://github.com/nodejs/node/issues/48771
|
|
|
|
// The issue occurs when:
|
|
// 1. http.request() is called with a custom synchronous lookup function
|
|
// 2. The lookup returns an IP that triggers a synchronous error (e.g., blockList)
|
|
// 3. The error is emitted before http's error handler is set up (via nextTick)
|
|
//
|
|
// The fix attaches socketErrorListener synchronously in onSocket so that
|
|
// socket errors are forwarded to the request before onSocketNT runs.
|
|
|
|
const blockList = new net.BlockList();
|
|
blockList.addAddress(common.localhostIPv4);
|
|
|
|
// Synchronous lookup that returns the blocked IP
|
|
const lookup = (_hostname, _options, callback) => {
|
|
callback(null, common.localhostIPv4, 4);
|
|
};
|
|
|
|
const req = http.request({
|
|
host: 'example.com',
|
|
port: 80,
|
|
lookup,
|
|
family: 4, // Force IPv4 to use simple lookup path
|
|
createConnection: (opts) => {
|
|
// Pass blockList to trigger synchronous ERR_IP_BLOCKED error
|
|
return net.createConnection({ ...opts, blockList });
|
|
},
|
|
}, common.mustNotCall());
|
|
|
|
// This error handler must be called.
|
|
// Without the fix, the error would be emitted before http.request()
|
|
// returns, causing an unhandled 'error' event.
|
|
req.on('error', common.mustCall((err) => {
|
|
if (err.code !== 'ERR_IP_BLOCKED') {
|
|
throw new Error(`Expected ERR_IP_BLOCKED but got ${err.code}`);
|
|
}
|
|
}));
|
|
|
|
req.end();
|