From 91297ca74542c3133799706acf5f52e10edc27dc Mon Sep 17 00:00:00 2001 From: gensokyo-dev Date: Sat, 14 Nov 2026 11:46:09 +0000 Subject: [PATCH] fix(javascript): decide a socket host member by its string type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit net and tls connect through options.host || "localhost" and throw ERR_INVALID_ARG_TYPE from the call when that is not a string, so a truthy non-string host leaves no socket. Read the final host, path and TLS socket members through one option reader shared with the TLS protocol-version check, and drop the acquisition only when the host is statically truthy and not a string while no truthy path or TLS socket, or an Object.prototype write that may supply one, skips the host read. The truthy and falsy host tests come from #9380. Its intrinsic-prototype gating changes are not included. Fixes #10002 Co-Authored-By: 吴佳浩 Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com> --- ...emanticAnalysis.socket_host_member.test.ts | 174 ++++++++++++++++++ .../javascript/javascriptSemanticResources.ts | 137 +++++++++++--- 2 files changed, 284 insertions(+), 27 deletions(-) create mode 100644 src/domain/javascript/javascriptSemanticAnalysis.socket_host_member.test.ts diff --git a/src/domain/javascript/javascriptSemanticAnalysis.socket_host_member.test.ts b/src/domain/javascript/javascriptSemanticAnalysis.socket_host_member.test.ts new file mode 100644 index 0000000000..f3e44ceced --- /dev/null +++ b/src/domain/javascript/javascriptSemanticAnalysis.socket_host_member.test.ts @@ -0,0 +1,174 @@ +import { expect, it } from "vitest"; + +import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js"; + +/** + * `net`/`tls` read `options.host` and throw `ERR_INVALID_ARG_TYPE` from the call itself + * when the member is truthy and not a string, so no socket exists afterwards. Every + * falsy value falls through to the default and is accepted. + * + * Measured on Node 24.22.1 — accepted: `undefined`, `null`, `""`, `0`, `-0`, `false`, + * `0n`, `NaN`, `void 0`, `!1`, `"localhost"`, `String("127.0.0.1")`, `Number(0)`, + * `Number("x")`, `Boolean(0)`, `Boolean("")`, and every value when a non-empty `path` + * selects the Unix overload. Threw: `1`, `1.5`, `true`, `1n`, `{}`, `[]`, `/x/`, + * `new String("x")`, `new Number(1)`, `!0`, `-1`, `+1`, `!!1`, `Infinity`, `Symbol("x")`, + * `Number(1)`, `Boolean(1)`. + * + * The positional second argument is a different slot Node does not validate — + * `net.connect(0, 5)` connects — so only the member is decided. Only shapes with a + * static truthiness are decided; the other rejected shapes above (`/x/`, boxed + * values, `!0`, `Infinity`, intrinsic calls) keep the acquisition. + */ +const acquisitionsFor = (body: string) => + analyzeJavaScriptSemantics( + `import net from "node:net";\nimport tls from "node:tls";\nfunction g() { ${body}; }`, + ).resourceOperations.map(({ kind, method }) => ({ kind, method })); + +it("keeps the socket acquisition when a later argument mutates the prototype", () => { + // JavaScript evaluates the second argument before invoking, so the assignment lands + // before Node reads the options. Measured on Node 24.22.1, this returns a Unix socket. + const operations = analyzeJavaScriptSemantics(` + const net = require("node:net"); + function seed() {} + net.connect({ port: 0, host: 1 }, (Object.prototype.path = "/tmp/a.sock")); + `) + .resourceOperations.filter(({ kind }) => kind === "acquire") + .map(({ kind, method }) => ({ kind, method })); + expect(operations).toEqual([{ kind: "acquire", method: "connect" }]); +}); +it.each([ + ["a numeric host", `net.connect({ port: 0, host: 1 })`], + ["a fractional host", `net.connect({ port: 0, host: 1.5 })`], + ["a boolean host", `net.connect({ port: 0, host: true })`], + ["a BigInt host", `net.connect({ port: 0, host: 1n })`], + ["an object host", `net.connect({ port: 0, host: {} })`], + ["an array host", `net.connect({ port: 0, host: [] })`], + ["a function host", `net.connect({ port: 0, host: () => {} })`], + ["a negated number host", `net.connect({ port: 0, host: -1 })`], + ["a tls numeric host", `tls.connect({ port: 0, host: 5 })`], + ["a tls object host", `tls.connect({ port: 0, host: {} })`], + ["a falsy tls socket", `tls.connect({ port: 0, socket: null, host: 1 })`], + [ + "an undefined tls path", + `tls.connect({ port: 0, path: undefined, host: 1 })`, + ], + [ + "an undefined net path", + `net.connect({ port: 0, path: undefined, host: 1 })`, + ], + ["an empty net path", `net.connect({ port: 0, path: "", host: 1 })`], + [ + "a duplicate path set to undefined", + `net.connect({ port: 0, path: "/tmp/x.sock", path: undefined, host: 1 })`, + ], + [ + "a spread replacing the path with undefined", + `net.connect({ port: 0, path: "/tmp/x.sock", ...{ path: undefined }, host: 1 })`, + ], +] as const)("emits no socket acquisition for %s", (_name, call) => { + expect(acquisitionsFor(call)).toEqual([]); +}); + +it.each([ + ["an omitted host", `net.connect({ port: 0 })`], + ["an undefined host", `net.connect({ port: 0, host: undefined })`], + ["a null host", `net.connect({ port: 0, host: null })`], + ["an empty-string host", `net.connect({ port: 0, host: "" })`], + ["a zero host", `net.connect({ port: 0, host: 0 })`], + ["a negative-zero host", `net.connect({ port: 0, host: -0 })`], + ["a false host", `net.connect({ port: 0, host: false })`], + ["a zero BigInt host", `net.connect({ port: 0, host: 0n })`], + ["a NaN host", `net.connect({ port: 0, host: NaN })`], + ["a void host", `net.connect({ port: 0, host: void 0 })`], + ["a negated host", `net.connect({ port: 0, host: !1 })`], + ["a loopback host", `net.connect({ port: 0, host: "localhost" })`], + ["a template host", "net.connect({ port: 0, host: `x` })"], + ["a String call host", `net.connect({ port: 0, host: String("127.0.0.1") })`], + [ + "an unresolved call host", + `const g = () => "localhost"; net.connect({ port: 0, host: g() })`, + ], + [ + "a later spread host", + `net.connect({ port: 0, host: 1, ...{ host: "127.0.0.1" } })`, + ], + [ + "a later computed host", + `net.connect({ port: 0, host: 1, ["host"]: "127.0.0.1" })`, + ], + [ + "a later duplicate host", + `net.connect({ port: 0, host: 1, host: "localhost" })`, + ], + [ + "a getter in a spread writing the host", + `net.connect({ port: 0, ...{ get x() { this.host = "localhost"; return 0; }, host: 1 } })`, + ], + [ + "an inherited __proto__ path", + `net.connect({ __proto__: { path: "/tmp/a.sock" }, host: 1 })`, + ], + [ + "an inherited Object.prototype.path", + `Object.prototype.path = "/tmp/a.sock"; function f() {} net.connect({ port: 0, host: 1 })`, + ], + [ + "an inherited Object.prototype.host", + `Object.prototype.host = 1; function f() {} net.connect({ port: 0 })`, + ], + [ + "a Unix path overriding a bad host", + `net.connect({ path: "/tmp/x.sock", host: 1 })`, + ], + ["a Number call returning 0", `net.connect({ port: 0, host: Number(0) })`], + [ + "a Number call returning NaN", + `net.connect({ port: 0, host: Number("x") })`, + ], + [ + "a Boolean call returning false", + `net.connect({ port: 0, host: Boolean(0) })`, + ], + [ + "a Boolean call returning ''", + `net.connect({ port: 0, host: Boolean("") })`, + ], + [ + "a non-literal path", + `const p = "/tmp/a.sock"; net.connect({ port: 0, path: p, host: 1 })`, + ], + [ + "a shadowed undefined in a negation", + `const undefined = "x"; net.connect({ port: 0, host: !undefined })`, + ], + [ + "a shadowed Infinity host", + `const Infinity = "localhost"; net.connect({ port: 0, host: Infinity })`, + ], + ["a positional numeric host", `net.connect(0, 5)`], + ["a tls stream socket", `tls.connect({ socket: base, host: 1 })`], + ["a tls string host", `tls.connect({ port: 0, host: "localhost" })`], +] as const)("keeps the socket acquisition for %s", (_name, call) => { + expect(acquisitionsFor(call)).toEqual([ + { kind: "acquire", method: "connect" }, + ]); +}); + +it.each([ + // `tls` merges a later non-null object over this one, and that object can write `host`. + // Measured on Node 24.22.1. + [`tls.connect({ port: 0, host: 1 }, { host: "localhost" })`], + // Global code the analysis cannot see may have patched the intrinsic the value folds through. + [ + `globalThis.eval("Number = String"); net.connect({ port: 0, host: Number(1) })`, + ], +] as const)("keeps the acquisition for a merged or opaque host: %s", (call) => { + expect(acquisitionsFor(call)).toHaveLength(1); +}); + +it.each([ + [`tls.connect({ port: 0, host: "localhost" }, { host: 1 })`], + [`tls.connect({ port: 0, host: 1 }, {})`], +] as const)("emits no acquisition for a decided numeric host: %s", (call) => { + expect(acquisitionsFor(call)).toEqual([]); +}); diff --git a/src/domain/javascript/javascriptSemanticResources.ts b/src/domain/javascript/javascriptSemanticResources.ts index 646023620d..3020878e82 100644 --- a/src/domain/javascript/javascriptSemanticResources.ts +++ b/src/domain/javascript/javascriptSemanticResources.ts @@ -22,7 +22,10 @@ import { } from "./javascriptAstValues.js"; import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference.js"; import { semanticOpaqueBindingReadMayHaveChanged } from "./javascriptSemanticOpaqueEffects.js"; -import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; +import { + semanticBindingAtRead, + semanticEffectTiming, +} from "./javascriptSemanticMutationInitializers.js"; import { resolveSemanticBindingState, type JavaScriptSemanticAnalysisState, @@ -88,6 +91,11 @@ export const collectJavaScriptSemanticResources = ( candidate.node, candidate.specifier, context.state, + ) && + !rejectsSocketHostMember( + candidate.node, + candidate.specifier, + context.state, )), ); return [ @@ -741,36 +749,42 @@ const tlsOptionMerge = ( }; /** - * Whether a `tls` call carries a protocol version Node rejects. + * The value Node reads for one `net`/`tls` option member: a node, `undefined` when + * the member is absent, or `"unknown"` when an accessor, a prototype write or a + * dynamic copy could change it. `tls` merges its option arguments (see + * `tlsOptionMerge`); `net` reads only an options object in argument 0. Returns + * `undefined` when the options themselves are not statically known. * - * `tls.connect` resolves the version before it opens a socket, so a bad literal throws - * from the call itself and no socket exists. Measured on Node 24.22.1: `null` and - * `undefined` are accepted as "not given", every other non-string value throws - * `ERR_TLS_INVALID_PROTOCOL_VERSION` (a BigInt throws `TypeError`), and a string is - * accepted only when it is one of `TLS_PROTOCOL_VERSIONS`. - * - * A truthy `secureContext` bypasses version parsing — Node uses - * `options.secureContext || tls.createSecureContext(options)` — while a falsy one does - * not, so only a truthy context ends the check. - * - * The answer stays undecided, which keeps the acquisition, whenever an accessor could - * have written the member first. Measured on Node 24.22.1, + * The answer stays unknown whenever an accessor could have written the member + * first. Measured on Node 24.22.1, * `{get side() { this.minVersion = "TLSv1.2"; }, minVersion: "nope"}` connects because * the getter runs during Node's own copy, and a `set minVersion` in an earlier argument * absorbs a later write. Both are bodies this reader does not run. - * - * Only `tls` reads these members, so `net` is not consulted. */ -const rejectsTlsProtocolVersionLiteral = ( +const socketOptionReader = ( node: t.CallExpression | t.OptionalCallExpression, specifier: string, state: JavaScriptSemanticAnalysisState, -): boolean => { - if (specifier !== "tls") return false; - const merge = tlsOptionMerge(node, state); - if (merge === undefined || merge.target === "unknown") return false; +): ((member: string) => t.Node | "unknown" | undefined) | undefined => { + let target: t.ObjectExpression | undefined; + let source: t.Node | undefined; + if (specifier === "tls") { + const merge = tlsOptionMerge(node, state); + if (merge === undefined || merge.target === "unknown") return undefined; + target = merge.target === "fresh" ? undefined : merge.target; + source = merge.source; + } else if (specifier === "net") { + const first = node.arguments[0]; + if ( + first === undefined || + node.arguments.some((argument) => t.isSpreadElement(argument)) + ) + return undefined; + const options = unwrapJavaScriptExpression(first as t.Node).node; + if (!t.isObjectExpression(options)) return undefined; + target = options; + } else return undefined; const isNullish = (value: t.Node) => isNullishPlaceholder(value, state); - const target = merge.target === "fresh" ? undefined : merge.target; // A known source value proves the final option only when target writes are // ordinary. Target prototypes and copied __proto__ can absorb later writes. if ( @@ -782,16 +796,16 @@ const rejectsTlsProtocolVersionLiteral = ( primitiveOwnKeys(property.value, isNullish) === null, ) ) - return false; - const sourceReader = staticObjectCopyPropertyReader(merge.source, isNullish); + return undefined; + const sourceReader = staticObjectCopyPropertyReader(source, isNullish); const copiedPrototype = sourceReader("__proto__"); if ( copiedPrototype.status === "dynamic" || (copiedPrototype.status === "explicit" && primitiveOwnKeys(copiedPrototype.value, isNullish) === null) ) - return false; - const objects = [target, merge.source].filter( + return undefined; + const objects = [target, source].filter( (value): value is t.ObjectExpression => t.isObjectExpression(value), ); const accessorWritten = objects.some((options) => @@ -800,7 +814,7 @@ const rejectsTlsProtocolVersionLiteral = ( ), ); const readers = [staticObjectPropertyReader(target, isNullish), sourceReader]; - const effective = (member: string): t.Node | "unknown" | undefined => { + return (member) => { let read: t.Node | "unknown" | undefined; for (const reader of readers) { const property = reader(member); @@ -812,6 +826,32 @@ const rejectsTlsProtocolVersionLiteral = ( } return read; }; +}; + +/** + * Whether a `tls` call carries a protocol version Node rejects. + * + * `tls.connect` resolves the version before it opens a socket, so a bad literal throws + * from the call itself and no socket exists. Measured on Node 24.22.1: `null` and + * `undefined` are accepted as "not given", every other non-string value throws + * `ERR_TLS_INVALID_PROTOCOL_VERSION` (a BigInt throws `TypeError`), and a string is + * accepted only when it is one of `TLS_PROTOCOL_VERSIONS`. + * + * A truthy `secureContext` bypasses version parsing — Node uses + * `options.secureContext || tls.createSecureContext(options)` — while a falsy one does + * not, so only a truthy context ends the check. An unknown member keeps the + * acquisition (see `socketOptionReader`). + * + * Only `tls` reads these members, so `net` is not consulted. + */ +const rejectsTlsProtocolVersionLiteral = ( + node: t.CallExpression | t.OptionalCallExpression, + specifier: string, + state: JavaScriptSemanticAnalysisState, +): boolean => { + if (specifier !== "tls") return false; + const effective = socketOptionReader(node, specifier, state); + if (effective === undefined) return false; // A truthy context means Node never parses the members at all. The shared owner // already treats `-0` as falsy. Measured on Node 24.22.1, // `{secureContext: -0, minVersion: "nope"}` throws while a real context connects. @@ -870,6 +910,49 @@ const rejectsTlsProtocolVersionLiteral = ( return false; }; +/** + * Whether a `net`/`tls` call carries a `host` option Node rejects. + * + * Measured on Node 24.22.1: both connect through `options.host || "localhost"` and + * throw `ERR_INVALID_ARG_TYPE` from the call when that is not a string, so a truthy + * non-string host leaves no socket while a falsy one (`0`, `""`, `false`, `null`) + * connects to localhost. A truthy `path` connects a pipe and a truthy TLS `socket` + * skips the connect, so neither reads `host`, and an absent one may be inherited + * from a patched `Object.prototype`. Anything not statically known keeps the + * acquisition. + */ +const rejectsSocketHostMember = ( + node: t.CallExpression | t.OptionalCallExpression, + specifier: string, + state: JavaScriptSemanticAnalysisState, +): boolean => { + const effective = socketOptionReader(node, specifier, state); + if (effective === undefined) return false; + const falsy = (value: t.Node) => + isNullishPlaceholder(value, state) || + staticJavaScriptTruthiness(value) === false; + // Node reads an absent member through the prototype chain, so a write to + // Object.prototype that may run first can supply a path or socket. + const inheritable = state.prototypeMutationSites.some( + (effect) => + effect.owner === "Object" && + semanticEffectTiming(node, effect.node, state) !== "after", + ); + for (const member of specifier === "tls" ? ["path", "socket"] : ["path"]) { + const value = effective(member); + if (value === "unknown" || (value !== undefined && !falsy(value))) + return false; + if (value === undefined && inheritable) return false; + } + const host = effective("host"); + if (host === undefined || host === "unknown") return false; + // Every statically truthy shape other than a string literal is a non-string. + return ( + staticJavaScriptTruthiness(host) === true && + !t.isStringLiteral(unwrapJavaScriptExpression(host).node) + ); +}; + /** Only `net`/`tls` sockets take a port in argument 0. */ const socketPortInFirstArgument = ( candidate: Pick,