# post-message

Rule `sonarjs/post-message`. Origins should be verified during cross-origin communications

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: yes
- Autofix: no
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1340 by reach, #641 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/post-message` | yes | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2819/) |

## Code examples

### Incorrect: the rule reports this

```js
var iframe = document.getElementById("testiframe");
iframe.contentWindow.postMessage("secret", "*"); // Noncompliant: * is used
```

```js
window.addEventListener("message", function(event) { // Noncompliant: no checks are done on the origin property.
      console.log(event.data);
 });
```

### Correct: the rule accepts this

```js
var iframe = document.getElementById("testsecureiframe");
iframe.contentWindow.postMessage("hello", "https://secure.example.com"); // Compliant
```

```js
window.addEventListener("message", function(event) {

  if (event.origin !== "http://example.org") // Compliant
    return;

  console.log(event.data)
});
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/post-message": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/parameterized-tests](https://lintradar.dev/rules/sonarjs/parameterized-tests.md): Similar tests should be grouped in a single Parameterized test
- [sonarjs/prefer-cypress-should](https://lintradar.dev/rules/sonarjs/prefer-cypress-should.md): Cypress assertions should be retryable
- [sonarjs/object-alt-content](https://lintradar.dev/rules/sonarjs/object-alt-content.md): "<object>" tags should provide an alternative content
- [sonarjs/prefer-default-last](https://lintradar.dev/rules/sonarjs/prefer-default-last.md): "default" clauses should be last
- [sonarjs/null-dereference](https://lintradar.dev/rules/sonarjs/null-dereference.md): Properties of variables with "null" or "undefined" values should not be accessed
- [sonarjs/prefer-native-axios-alternative](https://lintradar.dev/rules/sonarjs/prefer-native-axios-alternative.md): Native APIs should be preferred over Axios utility methods

---

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