Consider this scenario:
A content script wants to upload a file. I would prefer this operation to be done from my service/background script, but passing a File
to that side via messages seems to be somewhat impossible without weird workarounds.
This is a good reference for the workarounds available: Passing FormData/File Object from content script to background script in chrome extension with Manifest V3
Workaround 1 seems bad, since it will require to break up the request which is not compatible with the backend API. Workaround 2 seems insecure, since the host web page will definitely be able to send rogue messages to an embedded iframe as it will share context.
I'm considering having the content script perform the upload directly like this:
- The content script loads the authentication secret from extension storage (
chrome.storage.sync
orchrome.storage.local
depending on login persistence mode). - It calls
fetch
directly to upload the form data.
Is that safe?
One hazard I had in mind was that a host page could hook window.fetch
to intercept the credentials. That doesn't seem possible after my testing (the content script has a separate fetch
defined in its own isolated environment). Is there anything else to watch out for?