---
title: "Check the sender of every window message · Agent Wranglers"
url: https://agentwranglers.se/playbook/application-security/browser-security-headers/postmessage-origin
id: APS-39
level: Recommended
updated: 2026-10-27
build: a2a6bdfed830
tokens: 786
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Check the sender of every window message

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-27.

- ID: APS-39
- Level: Recommended
- Chapter: [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md)
- Rule: When a page listens for messages from other windows or frames, accept only those from a fixed list of origins. Never send private data to the target origin `*`.
- Check: The agent loads the page from a test page on another origin. It posts a message holding `<img src=x onerror=alert('PENTEST-7a8b')>` and one holding a fake action. The page ignores both. A search of the code finds no `postMessage` call with `'*'` as its target.
- How: Pages use `postMessage` to talk to pop-ups, embedded widgets and login or payment frames. Any site can open your page and post to it, so compare `event.origin` with your list before reading `event.data`. Treat the data as untrusted input, and never put it into `innerHTML` or `eval`. Name the exact receiving origin when you send. Framing of your own pages is covered in [Refuse to be framed](https://agentwranglers.se/playbook/application-security/browser-security-headers/frame-ancestors.md).

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### HTML example

```html
<script>
  const TRUSTED_ORIGINS = new Set(['https://pay.example.com']);
  window.addEventListener('message', event => {
    if (!TRUSTED_ORIGINS.has(event.origin)) return;
    showPaymentStatus(String(event.data.status));
  });
  popup.postMessage({ orderId }, 'https://pay.example.com');
</script>
```

## Sources

- [HTML Standard on web messaging security](https://html.spec.whatwg.org/multipage/web-messaging.html#authors)

## Related rules

- APS-28 [Refuse to be framed](https://agentwranglers.se/playbook/application-security/browser-security-headers/frame-ancestors.md) in Application security

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/application-security/browser-security-headers/postmessage-origin">Agent Wranglers application security playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Check the origin of WebSocket connections](https://agentwranglers.se/playbook/application-security/browser-security-headers/websocket-origin.md)
- Next: [Redirect only to your own pages](https://agentwranglers.se/playbook/application-security/browser-security-headers/block-open-redirects.md)
- [Browser security headers](https://agentwranglers.se/playbook/application-security/browser-security-headers.md)
