Although the code running on your background page / content script is pretty well isolated, once you enter the script in the page context, you are in the Wild West. Any extension and the page itself has access to this context and can affect how your code is executed.
For example, some extension may override chrome.runtime.sendMessage to send a message AND register it. This must be taken seriously - you may already have lost it.
However, method 1 is harder to crack than 2/3 - as explained, the attacker’s extension will have to directly change the page context in order to intervene, whereas in the case of DOM events, he can just listen to them from the security of his script contents - events are sent to all script context contexts.
Hypothetically, you could use some asymmetric cryptography for the channel, and also - enter the encryption key entered by the script and save the decryption key in the privileged zone. This protects the message if it is the only thing intercepted, but at some point the plaintext data exists in a global context - this may be enough for the attacker to retrieve the script (which you must accept by executing the script before your injection).
source share