test: Guard every custom lookup and drop the internal export

Apply the lookup wrapper whenever the caller passes a lookup function,
also for a loopback hostname. A custom lookup can resolve that name to
another address, so the block moves to the resolved address in every
case.

Remove the package export for the guard file. The configs load it by
path and nothing imports it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Charlie Kolb 2026-09-14 17:19:11 +02:00
parent 54e12f2156
commit a146ef2c67
No known key found for this signature in database
2 changed files with 8 additions and 11 deletions

View File

@ -45,11 +45,6 @@
"require": "./dist/setup/frontend.js",
"types": "./dist/setup/frontend.d.ts"
},
"./setup/network-guard": {
"import": "./dist/setup/network-guard.js",
"require": "./dist/setup/network-guard.js",
"types": "./dist/setup/network-guard.d.ts"
},
"./node": {
"import": "./dist/node.js",
"require": "./dist/node.js",

View File

@ -75,17 +75,19 @@ if (!allowNetwork && !Reflect.has(net.Socket.prototype, installedFlag)) {
Reflect.set(net.Socket.prototype, installedFlag, true);
net.Socket.prototype.connect = function connect(this: net.Socket, ...args: unknown[]) {
const target = connectTarget(args);
if (target.path) return Reflect.apply(originalConnect, this, args) as net.Socket;
const host = target.host ?? 'localhost';
if (!target.path && !LOOPBACK.test(host)) {
if (typeof target.lookup !== 'function') {
// Async, like a real connect failure. A sync throw breaks `tls.connect`.
process.nextTick(() => this.destroy(blockedError(host, target.port)));
return this;
}
if (typeof target.lookup === 'function') {
// A custom lookup can resolve a loopback name to another address, so
// the block always moves to the resolved address here.
const guarded = { ...target, lookup: guardLookup(target, target.lookup) };
// Node owns the normalized array, the caller owns a plain options object.
if (Array.isArray(args[0])) args[0][0] = guarded;
else args[0] = guarded;
} else if (!LOOPBACK.test(host)) {
// Async, like a real connect failure. A sync throw breaks `tls.connect`.
process.nextTick(() => this.destroy(blockedError(host, target.port)));
return this;
}
return Reflect.apply(originalConnect, this, args) as net.Socket;
} as typeof net.Socket.prototype.connect;