microsoft / microsoft/reflect-metadata
Exception when used in Firefox webextension content script
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 3.4k
- Forks
- 190
- Avg merge
- 13h 52m
- Merged PRs (30d)
- 1
Description
Using this in a Firefox webextension content script will cause an error on the 4th line of makeExporter "Not allowed to define cross-origin object as property on [Object] or [Array] XrayWrapper". The exception is not displayed by default. You can only see it if you "pause on exceptions" in the debugger.
The problem is to do with trying to install an object on window. I think it's to do with the particular pattern here. Simple patching of window should work as desired with Firefox's X-Ray vision (visible to the content script, but not with the web page). Possibly it's the use of defineProperty which causes problems?
Here's some background information:
https://github.com/ricardoquesada/Spidermonkey/blob/master/js/xpconnect/wrappers/XrayWrapper.cpp
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/Xray_vision
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Sharing_objects_with_page_scripts
https://stackoverflow.com/questions/41704263/error-not-allowed-to-define-cross-origin-object-as-property-on-object-or-arr
The specific issue here seems to relate to whether Reflect is modified on the actual window object, or whether a Sandbox object is used. It's not very well documented, but someone has tried to discover the different behaviours here:
https://stackoverflow.com/questions/44671610/sandboxed-this-in-firefox-webextension-content-script
I have created a pull request with a workaround at https://github.com/rbuckton/reflect-metadata/pull/93 . This fixes it, but I'm not sure how good a solution it is.
Since it's quite a niche issue, I also tried seeing if it was possible to work around it in another way. I tried to use a shim in webpack like so:
{
test: require.resolve("reflect-metadata"),
use: "imports-loader?global=>require('global-shim').global"
}
With global-shim.js being
var global = (function(win) {
win.__make_sandbox = true;
return win;
})(window);
export { global };
Or
var global = (function() {
return this !== window && this.wrappedJSObject === window.wrappedJSObject
? this
: window;
}).call(window);
Neither works. Any input on this slightly mysterious issue is very welcome.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at makeExporter, especially the fourth line where the issue reports the Firefox XrayWrapper exception. Compare the proposed workaround in pull request #93 and investigate the linked Firefox Xray and sandbox references. Done means reflect-metadata can be used in a Firefox webextension content script without the exception while preserving the intended window behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100