-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
require-post-message-target-origin
rule (#1326)
- Loading branch information
Showing
11 changed files
with
336 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Enforce using the `targetOrigin` argument with `window.postMessage()` | ||
|
||
When calling [`window.postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage) without the `targetOrigin` argument, the message cannot be received by any window. | ||
|
||
## Fail | ||
|
||
```js | ||
window.postMessage(message); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
window.postMessage(message, 'https://example.com'); | ||
``` | ||
|
||
```js | ||
window.postMessage(message, '*'); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const {methodCallSelector} = require('./selectors'); | ||
const {appendArgument} = require('./fix'); | ||
|
||
const ERROR = 'error'; | ||
const SUGGESTION_TARGET_LOCATION_ORIGIN = 'target-location-origin'; | ||
const SUGGESTION_SELF_LOCATION_ORIGIN = 'self-location-origin'; | ||
const SUGGESTION_STAR = 'star'; | ||
const messages = { | ||
[ERROR]: 'Missing the `targetOrigin` argument.', | ||
[SUGGESTION_TARGET_LOCATION_ORIGIN]: 'Use `{{target}}.location.origin`.', | ||
[SUGGESTION_SELF_LOCATION_ORIGIN]: 'Use `self.location.origin`.', | ||
[SUGGESTION_STAR]: 'Use `"*"`.' | ||
}; | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
function create(context) { | ||
const sourceCode = context.getSourceCode(); | ||
return { | ||
[methodCallSelector({name: 'postMessage', length: 1})](node) { | ||
const [penultimateToken, lastToken] = sourceCode.getLastTokens(node, 2); | ||
const suggestions = []; | ||
const target = node.callee.object; | ||
if (target.type === 'Identifier') { | ||
const {name} = target; | ||
|
||
suggestions.push({ | ||
messageId: SUGGESTION_TARGET_LOCATION_ORIGIN, | ||
data: {target: name}, | ||
code: `${target.name}.location.origin` | ||
}); | ||
|
||
if (name !== 'self' && name !== 'window' && name !== 'globalThis') { | ||
suggestions.push({messageId: SUGGESTION_SELF_LOCATION_ORIGIN, code: 'self.location.origin'}); | ||
} | ||
} else { | ||
suggestions.push({messageId: SUGGESTION_SELF_LOCATION_ORIGIN, code: 'self.location.origin'}); | ||
} | ||
|
||
suggestions.push({messageId: SUGGESTION_STAR, code: '\'*\''}); | ||
|
||
context.report({ | ||
loc: { | ||
start: penultimateToken.loc.end, | ||
end: lastToken.loc.end | ||
}, | ||
messageId: ERROR, | ||
suggest: suggestions.map(({messageId, data, code}) => ({ | ||
messageId, | ||
data, | ||
/** @param {import('eslint').Rule.RuleFixer} fixer */ | ||
fix: fixer => appendArgument(fixer, node, code, sourceCode) | ||
})) | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Enforce using the `targetOrigin` argument with `window.postMessage()`.', | ||
url: getDocumentationUrl(__filename), | ||
suggestion: true | ||
}, | ||
schema: [], | ||
messages | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'window.postMessage(message, targetOrigin)', | ||
'postMessage(message)', | ||
'window.postMessage', | ||
'window.postMessage()', | ||
'window.postMessage(message, targetOrigin, transfer)', | ||
'window.postMessage(...message)', | ||
'window[postMessage](message)', | ||
'window["postMessage"](message)', | ||
'window.notPostMessage(message)', | ||
'window.postMessage?.(message)', | ||
'window?.postMessage(message)' | ||
], | ||
invalid: [ | ||
'window.postMessage(message)', | ||
'self.postMessage(message)', | ||
'globalThis.postMessage(message)', | ||
'foo.postMessage(message )', | ||
'foo.postMessage( ((message)) )', | ||
'foo.postMessage(message,)', | ||
'foo.postMessage(message , )', | ||
'foo.window.postMessage(message)', | ||
'document.defaultView.postMessage(message)', | ||
'getWindow().postMessage(message)' | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.