# require-post-message-target-origin

Rule `unicorn/require-post-message-target-origin`. Enforce using the `targetOrigin` argument with `window.postMessage()`.

- Source: unicorn
- Technologies: JavaScript
- Kind: suspicious
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 2.1M weekly downloads of presets that enable it
- Trend: +151.7% last quarter
- Rank: #1525 by reach, #29 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/require-post-message-target-origin` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/require-post-message-target-origin.md) |
| Oxlint | `unicorn/require-post-message-target-origin` | no | safe | plugin `unicorn`, category suspicious | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/require-post-message-target-origin.html) |

## Code examples

### Incorrect: the rule reports this

```js
window.postMessage(data);
```

```js
iframe.contentWindow.postMessage(data, {transfer: [port]});
```

### Correct: the rule accepts this

```js
window.postMessage(data, '/');
```

```js
iframe.contentWindow.postMessage(data, {targetOrigin: 'https://example.com', transfer: [port]});
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/require-post-message-target-origin": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/require-post-message-target-origin": "error"
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |

## Related rules

- [unicorn/prefer-import-meta-properties](https://lintradar.dev/rules/unicorn/prefer-import-meta-properties.md): Prefer `import.meta.{dirname,filename}` over legacy techniques for getting file paths.
- [unicorn/custom-error-definition](https://lintradar.dev/rules/unicorn/custom-error-definition.md): Enforce correct `Error` subclassing.
- [unicorn/no-array-for-each](https://lintradar.dev/rules/unicorn/no-array-for-each.md): Forbids the use of `Array#forEach` in favor of a for loop.
- [unicorn/no-invalid-file-input-accept](https://lintradar.dev/rules/unicorn/no-invalid-file-input-accept.md): Disallow invalid `accept` values on file inputs.
- [unicorn/no-empty-link-text](https://lintradar.dev/rules/unicorn/no-empty-link-text.md): Disallow empty link text in Markdown.
- [unicorn/no-javascript-url](https://lintradar.dev/rules/unicorn/no-javascript-url.md): Disallow `javascript:` URLs in Markdown.

---

Web page: https://lintradar.dev/rules/unicorn/require-post-message-target-origin. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
