I would like to know how to access and modify the contents of a cross domain iframe in JavaScript within a Firefox WebExtension. I understand the limitations of normal JavaScript and that modifying a cross domain iframe would be an XSS vulnerability, but I believe there is some way to do it in a WebExtension that I cannot find. I believe this because the legacy extension manifests had options for allowing cross domain content in the permissions section.
When viewing old code for the legacy versions of FireFox extensions, there seems to be options for cross domain content for certain websites in the fashion below. Although for the new FireFox WebExtension, this is not a feature listed in the documentation.
"cross-domain-content": ["https://www.example.com"]
Here is my manifest.json
file.
{
"manifest_version": 2,
"name": "Test Extension",
"version": "1.0",
"content_scripts": [
{
"matches": ["*://*/*"],
"all_frames": true,
"js": [
"js/main.js"
]
}
],
"permissions": [
"*://*/*",
"<all_urls>",
]
}
Here is my main.js
file.
// Code is being run within an iframe ("all_frames": true)
if (window != window.top) {
// Attempt to log the source of the parent iframe
// If cross domain, met throws - Error: Permission denied to access property "frameElement"
console.log(window.parent.frameElement.src);
}
As you can see in the main.js
file, when attempting to print the source for the parent iframe an error is thrown as below.
Error: Permission denied to access property "frameElement"
I want to know how it would be possible to allow a FireFox WebExtension to access and modify the contents of a cross domain iframe. I'm not sure if it is a problem of not putting down the right permission in the manifest, or that I have to use the WebExtension API or something, I just can't find anything on this.
Additionally, if anyone could refer or provide me some examples of modifying the contents of an iframe in this fashion, it would be much appreciated.
allow-top-navigation
parameter doesn't seem to work on cross-origin frames whatever we do, but you can still usewindow.parent.postMessage
to communicate between the iframe and the main document. The script you injected in the frame has the same restrictions as the framed document. – Hydrostat