Repository navigation
fix(javascript): emit no event operation for a missing listener argument #8663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
041a506
f1d30fe
4d36250
b6917aa
9617f1a
cb36de8
e9e604a
0bd6c11
c964e98
87b7be8
c9427ea
f038fdc
47e3b9b
ad13261
94dc27a
98316a1
c719fb1
88d5732
514a306
9a7b483
3c3684c
65d74b0
b6cc762
5944b69
676c01d
25cc26b
95cd68c
e118fc9
ba119f3
6b18b70
9b39943
09001af
be61d5c
048274c
486aade
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,14 +15,19 @@ import { | |
| import { | ||
| resolveSemanticBindingState, | ||
| semanticResolutionBlocked, | ||
| semanticStableInitializer, | ||
| type JavaScriptSemanticAnalysisState, | ||
| type JavaScriptSemanticBindingState, | ||
| } from "./javascriptSemanticState.js"; | ||
| import { semanticBindingAtRead } from "./javascriptSemanticMutationInitializers.js"; | ||
| import { | ||
| semanticBindingAtRead, | ||
| semanticTopLevelEffectFollowsCapture, | ||
| } from "./javascriptSemanticMutationInitializers.js"; | ||
| import { traverseJavaScriptValueAst } from "./javascriptSemanticTraversal.js"; | ||
| import { isUnmodifiedGlobalReference } from "./javascriptSemanticAmbientWrites.js"; | ||
| import { range } from "./javascriptStaticAnalysisHelpers.js"; | ||
| import { | ||
| affectsBuiltinPath, | ||
| builtinDataEffectCallablePath, | ||
| builtinDataEffectMethod, | ||
| dataEffectArgumentBindingId, | ||
| dataEffectLiteralString, | ||
|
|
@@ -161,6 +166,119 @@ const rejectsEventListener = (listener: t.Node): boolean => { | |
| ); | ||
| }; | ||
|
|
||
| /** | ||
| * Node's `events` module, whose `EventEmitter` validates every listener | ||
| * argument before it registers or removes anything. | ||
| */ | ||
| const NODE_EVENTS_SPECIFIERS = ["events", "node:events"] as const; | ||
|
|
||
| /** | ||
| * Report whether a value is `new <events>.EventEmitter()`. Named and default | ||
| * imports, destructured requires, and member requires all denote that class. | ||
| * The construction is returned so the caller reads `callee` from the unwrapped | ||
| * `NewExpression` rather than from a wrapper around it. | ||
| */ | ||
| const nodeEventEmitterConstruction = ( | ||
| node: t.Node, | ||
| state: JavaScriptSemanticAnalysisState, | ||
| ): t.NewExpression | null => { | ||
| const value = unwrapJavaScriptExpression(node).node; | ||
| if (!t.isNewExpression(value)) return null; | ||
| const selected = builtinDataEffectCallablePath( | ||
| value.callee, | ||
| state, | ||
| NODE_EVENTS_SPECIFIERS, | ||
| ); | ||
| if (selected === null) return null; | ||
| // An empty selected path is the module's default export, which `events` | ||
| // defines as `EventEmitter`. | ||
| return selected.length === 0 || selected.at(-1) === "EventEmitter" | ||
| ? value | ||
| : null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If code replaces AGENTS.md reference: AGENTS.md:L21-L21 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid, and it shows why the proof should not be about the constructor alone: native construction does not establish the instance's method once the hook Node calls during it can install one. A member write on the class now withdraws the proof, Validation: replacing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
With separate default imports AGENTS.md reference: AGENTS.md:L21-L21 Useful? React with 👍 / 👎. |
||
| }; | ||
|
|
||
| /** | ||
| * Report whether any binding in this module writes a `prototype.<method>` slot. | ||
| * A receiver cannot be held to the native contract once the method that answers | ||
| * the call may have been replaced on a shared prototype. The write is recorded | ||
| * on whichever binding owns the object, so a scan covers every spelling of it: | ||
| * the class itself, a namespace import, a member require, or an alias of any of | ||
| * them. A file that reassigns `prototype.<method>` for an unrelated class loses | ||
| * the proof as well, which keeps the guard narrow instead of guessing which | ||
| * definition answers the call. | ||
| */ | ||
| const writesPrototypeMethod = ( | ||
| method: string, | ||
| read: t.Node, | ||
| state: JavaScriptSemanticAnalysisState, | ||
| ): boolean => { | ||
| for (const binding of state.bindingsById.values()) | ||
| if ( | ||
| binding.memberWrites.some( | ||
| ({ path, node: write }) => | ||
| path.at(-1) === method && | ||
| path.at(-2) === "prototype" && | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a Node-constructed AGENTS.md reference: AGENTS.md:L21-L21 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid, and it is the same gap in a second place. A write through an inline Both sources are scanned now: every binding's member writes, and the module records for Validation: an inline |
||
| !semanticTopLevelEffectFollowsCapture( | ||
| read, | ||
| write, | ||
| state.parentsByNode, | ||
| ), | ||
| ) | ||
| ) | ||
| return true; | ||
| return false; | ||
| }; | ||
|
|
||
| /** | ||
| * Report whether the receiver is a value this module constructs from Node's | ||
| * `EventEmitter` and still calls the native listener method. Only that class is | ||
| * bound by `checkListener`; other EventEmitter-style APIs define one-argument | ||
| * forms, such as Socket.IO's `socket.off("x")`, which removes every listener | ||
| * for the event. Any receiver whose selected method may have been replaced | ||
| * keeps its operation as an unresolved candidate. | ||
| */ | ||
| const nodeEventEmitterReceiver = ( | ||
| node: t.Expression | t.Super, | ||
| method: string, | ||
| state: JavaScriptSemanticAnalysisState, | ||
| call: t.Node, | ||
| ): boolean => { | ||
| if (!t.isIdentifier(node)) return false; | ||
| const binding = resolveSemanticBindingState(state, node, node.name); | ||
| if (binding === undefined) return false; | ||
| const initializer = semanticStableInitializer(binding, state); | ||
| if (initializer === undefined || initializer.projection.length !== 0) | ||
| return false; | ||
| if (nodeEventEmitterConstruction(initializer.node, state) === null) | ||
| return false; | ||
|
Comment on lines
+609
to
+610
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a module assigns an imported forwarding wrapper to AGENTS.md reference: AGENTS.md:L19-L19 Useful? React with 👍 / 👎. |
||
| // Constructing the class proves where the receiver came from, not which | ||
| // definition answers the call. Passing the instance to another call lets that | ||
| // call replace the method first, so the proof no longer holds. Reading | ||
| // `emitter.on` in order to call it passes the function value rather than the | ||
| // instance, and the collector records the operation's own call as one of | ||
| // these escapes, so that call is not evidence against the receiver. | ||
| if ( | ||
| binding.escapedPaths.some( | ||
| (escape) => escape.path.length === 0 && escape.node !== call, | ||
| ) | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| ) | ||
| return false; | ||
| // A member write replaces the selected method, so it no longer keeps the | ||
| // native contract. An unrelated write such as `emitter.tag = 1` leaves the | ||
| // proof intact. | ||
| if ( | ||
| binding.memberWrites.some( | ||
| ({ path, node: write }) => | ||
| affectsBuiltinPath(path, [method]) && | ||
| !semanticTopLevelEffectFollowsCapture(node, write, state.parentsByNode), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After native construction, AGENTS.md reference: AGENTS.md:L21-L21 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Valid. The receiver's own prototype chain is the other way to select a different method, and it is recorded as a A Validation: |
||
| ) | ||
| ) | ||
| return false; | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| // Every instance shares the constructor's prototype, so a write there | ||
| // replaces the method for this receiver as well. | ||
| return !writesPrototypeMethod(method, node, state); | ||
| }; | ||
|
|
||
| const collectEvent = ( | ||
| node: t.CallExpression | t.OptionalCallExpression, | ||
| ownerCallableId: string | null, | ||
|
|
@@ -177,9 +295,19 @@ const collectEvent = ( | |
| if (method === undefined) return; | ||
| const kind = eventKind(method); | ||
| const eventArgument = node.arguments[0]; | ||
| const listenerArgument = EVENT_LISTENER_METHODS.includes(method) | ||
| ? node.arguments[1] | ||
| : undefined; | ||
| const listenerMethod = EVENT_LISTENER_METHODS.includes(method); | ||
| const listenerArgument = listenerMethod ? node.arguments[1] : undefined; | ||
| // A listener method called without a listener argument passes `undefined` to | ||
| // Node's `checkListener`, which throws before it registers or removes | ||
| // anything. Only a spread argument can still reach that position, and only a | ||
| // receiver proven to be Node's `EventEmitter` is held to that contract. | ||
| if ( | ||
| listenerMethod && | ||
| listenerArgument === undefined && | ||
| !node.arguments.some((argument) => t.isSpreadElement(argument)) && | ||
| nodeEventEmitterReceiver(member.object, method, state, node) | ||
| ) | ||
| return; | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment on lines
+682
to
+687
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For an EventEmitter subclass defining AGENTS.md reference: AGENTS.md:L21-L21 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed on the same root cause as the other thread, and the subclass case is now a regression test. The guard keyed on the property name, so it applied Node's I also kept the uncertainty explicit rather than resolving it toward absence: a receiver that cannot be proven keeps its operation as an unresolved candidate, matching the behavior before this PR. Measured on Node 22.23.3 — the subclass defaults the listener, registers |
||
| // A leading spread shifts the runtime argument positions, so argument 1 is not | ||
| // provably the listener: `e.on(...["x", fn], 1)` registers `fn` and ignores the | ||
| // trailing `1`. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
import A from "events"; import B from "node:events", both bindings reference the same mutable constructor. AfterA.EventEmitter = class extends A { on(name, fn = () => {}) { return super.on(name, fn); } },const e = new B.EventEmitter(); e.on("ready")registers successfully (verified on Node 24.15.0). However,semanticMutableBuiltinOriginexcludeseventsfromMUTABLE_BUILTIN_DEFAULTS, so the write never enters shared module effects and this lookup throughBstill reports["EventEmitter"]. The prototype scan also ignores the constructor-slot write, allowing the new guard to discard the real registration. Register the shared mutable identity at the builtin-mutation owner before using this resolution to prove absence.AGENTS.md reference: AGENTS.md:L19-L19
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid. Both specifiers resolve to one mutable module at runtime, so the proof cannot be scoped to the binding the construction happens to use.
Writes are now matched by what they name rather than by which binding owns them: any write whose path selects
EventEmitterwithdraws the proof, whichever import performed it, and the module records are read the same way for the inlinerequireform.Validation:
first.EventEmitter = class extends first.EventEmitter { ... }followed bynew second.EventEmitter()keeps the registration. Measured on Node 22.22.2:e.on("ready")does not throw afterwards. Reverting the class-surface check fails the new case.