# VNX-940: Cross-window message handler does not verify the sender's origin

Reports a browser window 'message' listener (addEventListener, onmessage, jQuery .on) whose handler never reads event.origin or event.source.

| Field | Value |
|---|---|
| Rule ID | `VNX-940` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | JavaScript / TypeScript |
| CWE | [CWE-940](https://cwe.mitre.org/data/definitions/940.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `postmessage` `origin` `cwe-940` |
| Source | [`internal/sast/rules/vnx-940.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-940.rego) |

## Overview

VNX-940 maps to [CWE-940: Improper Verification of Source of a Communication Channel](https://cwe.mitre.org/data/definitions/940.html). A window `message` event can be sent by any page that frames or opens this one. A handler that acts on `event.data` without checking `event.origin` takes commands from any site.

## What gets flagged

```javascript
window.addEventListener('message', function (event) {   // flagged
  document.getElementById('out').innerHTML = event.data.html;
});

window.addEventListener("message", receive, false);      // flagged when receive() never reads origin
window.onmessage = function (e) { ... };                 // flagged
```

Not flagged: a handler that reads `event.origin` (or a destructured `{ origin }`) or compares `event.source`; a named handler the file does not define; message events of Workers, WebSockets, MessagePorts, BroadcastChannels, Node child processes and worker scripts (`self.addEventListener`); comments and string literals. VNX-NODE-024 reports the same listener shape alongside wildcard `postMessage` targets.

## Remediation

```javascript
window.addEventListener('message', (event) => {
  if (event.origin !== 'https://app.example.com') return;
  handle(event.data);
});
```

## References

- [CWE-940: Improper Verification of Source of a Communication Channel](https://cwe.mitre.org/data/definitions/940.html)
- [MDN: Window postMessage security concerns](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#security_concerns)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-940
// nosec vnx-940
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-940 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
