JS-2205 USER-2389 Support Window onmessage in S2819 - #7701
JS-2205 USER-2389 Support Window onmessage in S2819#7701francois-mora-sonarsource wants to merge 12 commits into
Conversation
Ruling ReportNo changes to ruling expected issues in this PR |
This comment has been minimized.
This comment has been minimized.
497d706 to
f464fd0
Compare
Drop the eslint-env worker opt-out: the comment form is ignored under flat config and becomes an error in ESLint 10, and the lib.dom.d.ts check already suppresses worker and Node projects. Revert the unused isWindowObject parameter, drop a no-op unique-write lookup, and document the receiver restriction and the `through` invariant. Cover the operator and computed-property guards, non-function right-hand sides, and listener resolution through a const arrow function. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
The `window = self` Worker shim is not distinguishable by type once the DOM lib
is loaded, since `self` and `window` are both `Window & typeof globalThis`. The
scope-based detection only worked while `window` was an unresolved reference,
and it guarded the new `onmessage` path only, leaving the pre-existing
`addEventListener("message", ...)` behaviour inconsistent with it.
Report the shim on both paths instead and document the limitation. Worker and
Node projects are still left alone by the `lib.dom.d.ts` check.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Ruling showed the exclusion is load-bearing: its only real-world hit, ace/lib/ace/worker/worker_v2.js, aliases `window = self` at the top of a module factory and then registers `window.onmessage`. Messages there come from the parent page, so there is no origin to verify. Look the write up on the `window` variable when the configuration declares it as a global, and among the enclosing scope's unresolved references otherwise, so the check no longer depends on whether `window` resolves. Drop the enclosing function comparison: a shim mutates the global, so it disqualifies every receiver in the file. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Two known gaps, left deliberately out of this PR and recorded here rather than as tickets: 1. Compound assignments are not detected. 2. The For context on why the shim exclusion exists at all: dropping it made ruling fail with exactly one new issue, |
Reject on the receiver name before consulting the type checker or the scope chain, and look the Worker shim up last, so that an `onmessage` assignment on an unrelated transport no longer pays for a scope walk. Behaviour is unchanged; the predicate now also returns a plain boolean instead of `boolean | undefined`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Code Review ✅ Approved 2 resolved / 2 findingsAdds S2819 support for unverified Window.onmessage registrations while refining receiver validation and alias resolution, addressing the non-Window false positives and alias suppression findings. ✅ 2 resolved✅ Edge Case: onmessage check may false-positive on non-Window objects
✅ Edge Case: window=self alias suppresses S2819 for entire file
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|




Part of USER-2389
What changed
S2819 now detects unverified
Window.onmessageregistrations alongside the existingaddEventListener("message", ...)support.Covered receivers are non-computed
onmessageassignments onwindowandglobalThis, including local aliases whose unique write resolves to one of them (const target = window). The receiver'sonmessageproperty must be declared inlib.dom.d.ts, which leaves Worker and Node projects alone since they compile againstlib.webworker.d.tsor no DOM lib at all.Files that alias the global with a
window = self/window = globalshim are excluded: they run in a Worker, where messages come from the parent and there is no origin to verify. Types cannot tell this apart, sinceselfandwindoware bothWindow & typeof globalThisonce the DOM lib is loaded, so the shim is detected syntactically.ace/lib/ace/worker/worker_v2.jsin the ruling sources is exactly this case.Listener resolution is shared with the
addEventListenerpath, which as a side effect also resolves handlers held inconstvariables (const handler = event => {...}) — previously only function declarations were followed, soaddEventListenergains a few true positives too.Out of scope
window["onmessage"]).??=,||=), which do register a handler and are a known false negative.window/globalThis: parameters,iframe.contentWindow,document.defaultView.MessagePortorBroadcastChannel.addEventListenerpath, which reports it today. Harmonising the two paths would remove issues from the ruling baseline and belongs in its own change.Why
Assignments to
Window.onmessagereceivepostMessageevents and require the same origin verification asaddEventListener("message", ...).Validation
npx tsx --tsconfig packages/tsconfig.test.json --test packages/analysis/src/jsts/rules/S2819/unit.test.tsnpx tsc --noEmit -p packages/tsconfig.test.jsonnpm run bbfgit diff --check origin/master...HEAD