Skip to content

JavaScript: Recognize Angular @HostListener('window:message') as a postMessage handler#22036

Open
felickz wants to merge 2 commits into
github:mainfrom
forks-felickz:felickz/js-angular-hostlistener-postmessage
Open

JavaScript: Recognize Angular @HostListener('window:message') as a postMessage handler#22036
felickz wants to merge 2 commits into
github:mainfrom
forks-felickz:felickz/js-angular-hostlistener-postmessage

Conversation

@felickz

@felickz felickz commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Angular applications commonly receive cross-window postMessage data through a handler registered with the @HostListener('window:message', ['$event']) decorator rather than window.addEventListener('message', ...).

The PostMessageEventHandler class (in DOM.qll) only modeled the addEventListener('message', ...) and window.onmessage = ... forms. As a result, the decorated handler's event parameter was not recognized as a message-event source, which meant:

  • js/missing-origin-check produced no alert for the missing event.origin check, and
  • the event was not a ClientSideRemoteFlowSource, so downstream taint queries (e.g. js/client-side-unvalidated-url-redirection) did not flag flows from the message payload to sinks such as window.open / window.location.href.

Change

Extend PostMessageEventHandler to also recognize a method decorated with @HostListener from @angular/core when the event name is 'window:message' or 'document:message'. The decorated method's first parameter is the MessageEvent, equivalent to the addEventListener('message', ...) form.

Impact

On a sample Angular app where a @HostListener('window:message') handler forwards the payload to navigation / URL-opening logic without an origin check:

  • js/missing-origin-check now reports the handler (previously missed).
  • js/client-side-unvalidated-url-redirection now reports two flows from the message event to window.open(msg.url, ...) and window.location.href = msg.url (previously missed).

Test

Added Angular.ts to the MissingOriginCheck query test covering:

  • @HostListener('window:message', ...) without an origin check: alert
  • @HostListener('document:message', ...) without an origin check: alert
  • @HostListener('window:message', ...) with an event.origin equality check: no alert
  • @HostListener('window:resize', ...) (not a message event): no alert

…stMessage handler

Angular registers window message handlers via the
@HostListener('window:message', ['\']) decorator rather than
window.addEventListener('message', ...). The PostMessageEventHandler class
only modeled the addEventListener and window.onmessage forms, so the decorated
handler's event parameter was never treated as a message source. As a result,
js/missing-origin-check produced no alert and the event was not a client-side
remote flow source for downstream queries (e.g. client-side URL redirection).

Extend PostMessageEventHandler to also recognize methods decorated with
@HostListener for 'window:message', 'document:message', or 'message'.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 23, 2026 01:22
@felickz felickz requested a review from a team as a code owner June 23, 2026 01:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the JavaScript DOM dataflow modeling to recognize Angular @HostListener(...message...)-decorated methods as postMessage event handlers, ensuring the handler’s event parameter is treated as a message-event source for security queries like js/missing-origin-check.

Changes:

  • Updated the PostMessageEventHandler model to match Angular @HostListener-decorated message handlers.
  • Added an Angular-specific regression test case for missing-origin-check behavior and updated expected results.
  • Added a change note documenting the new modeling support.
Show a summary per file
File Description
javascript/ql/lib/semmle/javascript/security/dataflow/DOM.qll Extends postMessage handler recognition to include Angular @HostListener(...message...) decorated methods.
javascript/ql/test/query-tests/Security/CWE-020/MissingOriginCheck/Angular.ts Adds Angular HostListener examples to exercise the missing-origin-check query.
javascript/ql/test/query-tests/Security/CWE-020/MissingOriginCheck/MissingOriginCheck.expected Updates expected alert locations to include the new Angular test cases.
javascript/ql/lib/change-notes/2026-06-22-angular-hostlistener-postmessage.md Documents the added Angular HostListener postMessage handler support.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment thread javascript/ql/lib/semmle/javascript/security/dataflow/DOM.qll
…sage targets

Drop the plain 'message' event name from the @HostListener matcher. The
postMessage 'message' event is dispatched on window and does not bubble, so an
element-level @HostListener('message') does not receive cross-window messages.
Keeping only 'window:message' and 'document:message' makes the model more
precise and matches the accompanying comment and change note.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants