Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require-post-message-target-origin shouldn't trigger for incompatible APIs #1396

Closed
GauBen opened this issue Jul 2, 2021 · 20 comments · Fixed by #1415 or #1581
Closed

require-post-message-target-origin shouldn't trigger for incompatible APIs #1396

GauBen opened this issue Jul 2, 2021 · 20 comments · Fixed by #1415 or #1581
Labels

Comments

@GauBen
Copy link

GauBen commented Jul 2, 2021

What's wrong

I'm currently developing a browser extension that communicates with native software:

import { browser, Runtime } from 'webextension-polyfill-ts'
const nativePort: Runtime.Port = browser.runtime.connectNative(APPLICATION_ID)
nativePort.postMessage({ cmd: 'Version' }) // (x) Missing the `targetOrigin` argument.

Which rule is buggy

require-post-message-target-origin is triggered but the method only accepts one argument (see runtime.Port).

Ideas

  1. Enforce the name of the variable to be window, document or the likes.
  2. Check the type of the variable or the existence of the import 'webextension-polyfill'/'webextension-polyfill-ts'.
  3. Add a warning for web extension developers here: https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v34.0.1/docs/rules/require-post-message-target-origin.md
@GauBen GauBen added the bug label Jul 2, 2021
@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

Sorry, didn't know this.

As I understand, runtime.Port don't have to come from 'webextension-polyfill'/'webextension-polyfill-ts', right?

@GauBen
Copy link
Author

GauBen commented Jul 2, 2021

You understood right, 'webextension-polyfill' (https://github.com/mozilla/webextension-polyfill) is just something that helps erasing differences between chromium and gecko. Especially, chrome.runtime becomes browser.runtime, as asked by the web extension standard.

@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

Do you think disable this rule in your file or project is a good idea? If you are developing a browser extension, you probably don't need call window.postMessage.

@GauBen
Copy link
Author

GauBen commented Jul 2, 2021

Yep, you're right, I shouldn't be using window.postMessage (https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#using_window.postmessage_in_extensions_non-standard_inline)
I'll disable the rule in my eslintrc.

Thanks for the quick response!

@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

But is it common to use worker in extension? In that case people may need self.postMessage?

@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

Wait, worker.postMessage second parameter is not targetOrigin....

@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

I think we should turn this rule off...

@GauBen
Copy link
Author

GauBen commented Jul 2, 2021

I'm not sure why this rule exists, the parameter targetOrigin is not optionnal, and the error is properly caught by tsc:

Expected 2-3 arguments, but got 1. ts(2554)
lib.dom.d.ts(18289, 31): An argument for 'targetOrigin' was not provided.

@fisker
Copy link
Collaborator

fisker commented Jul 2, 2021

Our rule should support JavasScript,

When calling window.postMessage() without targetOrigin argument, browser won't throw any error, but message can't receive by any window. I remember the first time I got into this, really drive me crazy.

#1307 (comment)

@GauBen
Copy link
Author

GauBen commented Jul 2, 2021

Ok got it, the problem is more that browsers fail silently instead of throwing an exception. What if this rule only triggers for window.postMessage by default in unicorn/recommended, and add an option to trigger on any variable like foo.postMessage?

rules: {
  'unicorn/require-post-message-target-origin': ['error', 'window' | 'any'],
}

@JounQin
Copy link
Contributor

JounQin commented Jul 9, 2021

This rule should not trigger on worker_threads, either.

const msg: MainToWorkerMessage = { sharedBuffer, id, args }
new Worker('', {}).postMessage(msg)

@fregante
Copy link
Collaborator

I think this rule is safe only if types are available:

  • Does the postMessage function have a targetOrigin argument? Then report the error

Can that be done?

@fisker
Copy link
Collaborator

fisker commented Jul 12, 2021

Can that be done?

I don't think so, without type info, we can't know where the postMessage from.

@fregante
Copy link
Collaborator

fregante commented Jul 12, 2021

I think this rule is safe only if types are available:

👆 Yes I'm talking about a TypeScript-only rule. My question was about looking into the types of the to ensure that the matches our expectation.

@fisker
Copy link
Collaborator

fisker commented Jul 12, 2021

That will be meaningless if this #1396 (comment) is true.

@fregante
Copy link
Collaborator

fregante commented Jul 12, 2021

That will be meaningless if this #1396 (comment) is true.

Indeed it is! So this rule can be dropped altogether as far as I'm concerned.

Screen Shot

@JounQin
Copy link
Contributor

JounQin commented Jul 12, 2021

That will be meaningless if this #1396 (comment) is true.

So require-post-message-target-origin should be disabled by default for .ts files in the recommended config.

@fisker
Copy link
Collaborator

fisker commented Aug 20, 2021

Reopen this since {Worker,MessagePort,Client,BroadcastChannel}#postMessage() and runtime.Port#postMessage() still false positive in pure JS parsers.

@fisker
Copy link
Collaborator

fisker commented Aug 20, 2021

@2000yeshu What's your solution for this?

@fregante fregante changed the title require-post-message-target-origin shouldn't trigger in web extension context require-post-message-target-origin shouldn't trigger for incompatible APIs Oct 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants