Skip to content

Commit 2869180

Browse files
rebloorgithub-actions[bot]Rob--W
authored
Bug-2011234: remove the ability to dynamically execute code in moz-extension: documents (mdn#43108)
* Bug-2011234: remove the ability to dynamically execute code in `moz-extension:` documents * Apply suggestions from linter Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from review Co-authored-by: Rob Wu <rob@robwu.nl> * Apply suggestions from review Co-authored-by: Rob Wu <rob@robwu.nl> * Apply suggestion from @Rob--W Co-authored-by: Rob Wu <rob@robwu.nl> * Apply suggestion from @Rob--W Co-authored-by: Rob Wu <rob@robwu.nl> * Further clarifications of dynamic code execution in extension documents * Apply suggestions from review Co-authored-by: Rob Wu <rob@robwu.nl> * Further feedback related changes * typo * Further feedback changes * Apply suggestions from @Rob--W review Co-authored-by: Rob Wu <rob@robwu.nl> * Minor corrections * Reapply deletion --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Rob Wu <rob@robwu.nl>
1 parent 887d5ab commit 2869180

File tree

7 files changed

+38
-72
lines changed

7 files changed

+38
-72
lines changed

files/en-us/mozilla/add-ons/webextensions/api/runtime/onmessage/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Some example use cases are:
1414
- **in a background script** to listen for messages from a content script.
1515
- **in an [options page or popup](/en-US/docs/Mozilla/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#sidebars_popups_and_options_pages) script** to listen for messages from a background script.
1616
- **in a background script** to listen for messages from an options page or popup script.
17+
- **in a script in an [extension page](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages)** to listen for messages requesting the execution of code in the page's scripts.
1718

1819
To send a message that is received by the `onMessage()` listener, use {{WebExtAPIRef("runtime.sendMessage()")}} or (to send a message to a content script) {{WebExtAPIRef("tabs.sendMessage()")}}.
1920

files/en-us/mozilla/add-ons/webextensions/api/scripting/executescript/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ Injects a script into a target context. The script is run at `document_idle` by
1111
> [!NOTE]
1212
> This method is available in Manifest V3 or higher in Chrome and Firefox 101. In Safari and Firefox 102+, this method is also available in Manifest V2.
1313
14-
To use this API you must have the `"scripting"` [permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions) and permission for the target's URL, either explicitly as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions) or using the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, and PDF viewer pages.
14+
To use this API you must have the `"scripting"` [permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions) and permission for the target's URL, either explicitly as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions) or using the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, PDF viewer, and other built-in browser UI pages.
1515

1616
In Firefox and Safari, partial lack of host permissions can result in a successful execution (with the partial results in the resolved promise). In Chrome, any missing permission prevents any execution from happening (see [Issue 1325114](https://crbug.com/1325114)).
1717

1818
The scripts you inject are called [content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts).
1919

20-
This is an asynchronous function that returns a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
20+
Extensions cannot run content scripts in [extension pages](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages). If an extension wants to run code in an extension page dynamically, it can include a script in the document. This script contains the code to run and registers a {{WebExtAPIRef("runtime.onMessage")}} listener that implements a way to execute the code. The extension can then send a message to the listener to trigger the code's execution.
2121

2222
## Syntax
2323

files/en-us/mozilla/add-ons/webextensions/api/tabs/executescript/index.md

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,17 @@ Injects JavaScript code into a page.
1111
> [!NOTE]
1212
> When using Manifest V3 or higher, use {{WebExtAPIRef("scripting.executeScript()")}} to execute scripts.
1313
14-
You can inject code into pages whose URL can be expressed using a [match pattern](/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns). To do so, its scheme must be one of: `http`, `https`, or `file`.
14+
You can inject code into pages whose URL you can express using a [match pattern](/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns). To do so, its scheme must be one of: `http`, `https`, or `file`.
1515

16-
You must have the permission for the page's URLeither explicitly, as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions)—or, via the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, and PDF viewer pages.
16+
You must have the permission for the page's URL either explicitly, as a [host permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#host_permissions), or using the [activeTab permission](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/permissions#activetab_permission). Note that some special pages do not allow this permission, including reader view, view-source, PDF viewer, and other built-in browser UI pages.
1717

18-
You can also inject code into pages packaged with your own extension:
18+
Extensions cannot run content scripts in [extension pages](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages). If an extension wants to run code in an extension page dynamically, it can include a script in the document. This script contains the code to run and registers a {{WebExtAPIRef("runtime.onMessage")}} listener that implements a way to execute the code. The extension can then send a message to the listener to trigger the code's execution.
1919

20-
```js
21-
browser.tabs.create({ url: "/my-page.html" }).then(() => {
22-
browser.tabs.executeScript({
23-
code: `console.log('location:', window.location.href);`,
24-
});
25-
});
26-
```
27-
28-
You don't need any special permissions to do this.
29-
30-
You _cannot_ inject code into any of the browser's built-in pages, such as: `about:debugging`, `about:addons`, or the page that opens when you open a new empty tab.
20+
> [!NOTE]
21+
> The ability to inject code into pages packaged with your extension was deprecated in Firefox 149 and removed in Firefox 152.
3122
3223
The scripts you inject are called [content scripts](/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts).
3324

34-
This is an asynchronous function that returns a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
35-
3625
## Syntax
3726

3827
```js-nolint
@@ -54,11 +43,11 @@ let executing = browser.tabs.executeScript(
5443

5544
It contains the following properties:
5645
- `allFrames` {{optional_inline}}
57-
- : `boolean`. If `true`, the code will be injected into all frames of the current page.
46+
- : `boolean`. If `true`, the code is injected into all frames of the current page.
5847

59-
If `true` and `frameId` is set, then it will raise an error. (`frameId` and `allFrames` are mutually exclusive.)
48+
If set to `true` and `frameId` is set, it raises an error. (`frameId` and `allFrames` are mutually exclusive.)
6049

61-
If it is `false`, code is only injected into the top frame.
50+
If it is `false`, the code is injected only into the top frame.
6251

6352
Defaults to `false`.
6453

@@ -81,38 +70,38 @@ let executing = browser.tabs.executeScript(
8170
Defaults to `0` (the top-level frame).
8271

8372
- `matchAboutBlank` {{optional_inline}}
84-
- : `boolean`. If `true`, the code will be injected into embedded `about:blank` and `about:srcdoc` frames if your extension has access to their parent document. The code cannot be inserted in top-level `about:` frames.
73+
- : `boolean`. If `true`, the code is injected into embedded `about:blank` and `about:srcdoc` frames if your extension has access to their parent document. The code cannot be inserted in top-level `about:` frames.
8574

8675
Defaults to `false`.
8776

8877
- `runAt` {{optional_inline}}
89-
- : {{WebExtAPIRef('extensionTypes.RunAt')}}. The soonest that the code will be injected into the tab.
78+
- : {{WebExtAPIRef('extensionTypes.RunAt')}}. The earliest that the code is injected into the tab.
9079

9180
Defaults to `"document_idle"`.
9281

9382
### Return value
9483

95-
A [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that will resolve to an array of objects. The array's values represent the result of the script in every injected frame.
84+
A [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that resolves to an array of objects. The array's values represent the script's result in each injected frame.
9685

97-
The result of the script is the last evaluated statement, which is similar to what would be output (the results, not any `console.log()` output) if you executed the script in the [Web Console](https://firefox-source-docs.mozilla.org/devtools-user/web_console/index.html). For example, consider a script like this:
86+
The result of the script is the last evaluated statement, which is similar to the output (the results, not any `console.log()` output) if you executed the script in the [Web Console](https://firefox-source-docs.mozilla.org/devtools-user/web_console/index.html). For example, consider a script like this:
9887

9988
```js
10089
let foo = "my result";
10190
foo;
10291
```
10392

104-
Here the results array will contain the string `"my result"` as an element.
93+
Here, the results array contains the string `"my result"` as an element.
10594

10695
The result values must be [structured cloneable](/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm) (see [Data cloning algorithm](/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities#data_cloning_algorithm)).
10796

10897
> [!NOTE]
109-
> The last statement may be also a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), but this feature is unsupported by [webextension-polyfill](https://github.com/mozilla/webextension-polyfill#tabsexecutescript) library.
98+
> The last statement can also a [`Promise`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), but this feature is unsupported by [webextension-polyfill](https://github.com/mozilla/webextension-polyfill#tabsexecutescript) library.
11099
111-
If any error occurs, the promise will be rejected with an error message.
100+
If any error occurs, the promise is rejected with an error message.
112101

113102
## Examples
114103

115-
This example executes a one-line code snippet in the currently active tab:
104+
This example executes a one-line code snippet in the active tab:
116105

117106
```js
118107
function onExecuted(result) {
@@ -131,7 +120,7 @@ const executing = browser.tabs.executeScript({
131120
executing.then(onExecuted, onError);
132121
```
133122

134-
This example executes a script from a file (packaged with the extension) called `"content-script.js"`. The script is executed in the currently active tab. The script is executed in subframes as well as the main document:
123+
This example executes a script from a file (packaged with the extension) called `"content-script.js"`. The script is executed in the active tab. The script is executed in subframes as well as the main document:
135124

136125
```js
137126
function onExecuted(result) {

files/en-us/mozilla/add-ons/webextensions/content_scripts/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ You can load a content script into a web page:
2525

2626
There is only one global scope _per frame, per extension_. This means that variables from a content script can be accessed by any other content scripts, regardless of how the content script was loaded.
2727

28-
Using methods (1) and (2), you can only load scripts into pages whose URLs can be represented using a [match pattern](/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns).
29-
30-
Using method (3), you can also load scripts into pages packaged with your extension, but you can't load scripts into privileged browser pages (like `about:debugging` or `about:addons`).
31-
3228
> [!NOTE]
3329
> [Dynamic JS module imports](/en-US/docs/Web/JavaScript/Guide/Modules#dynamic_module_loading) are now working in content scripts. For more details, see [Firefox bug 1536094](https://bugzil.la/1536094).
3430
> Only URLs with the _moz-extension_ scheme are allowed, which excludes data URLs ([Firefox bug 1587336](https://bugzil.la/1587336)).
@@ -79,7 +75,11 @@ The set of domains can be restricted further through enterprise policies: Firefo
7975

8076
### Limitations
8177

82-
Whole tabs or frames may be loaded using [`data:` URI](/en-US/docs/Web/URI/Reference/Schemes/data), {{DOMxRef("URL.createObjectURL_static", "Blob")}} objects, and other similar techniques. Support of content scripts injection into such special documents varies across browsers, see the Firefox [bug #1411641 comment 41](https://bugzil.la/1411641#c41) for some details.
78+
By default, content scripts do not run in `about:blank`, `about:srcdoc`, `data:`, and `blob:` pages. To enable their execution, use the [`match_origin_as_fallback`](/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts#match_origin_as_fallback) option in the `content_scripts` manifest key or the [`matchOriginAsFallback`](/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/RegisteredContentScript#matchoriginasfallback) option in the `scripting` API.
79+
80+
Extensions cannot inject content scripts into privileged browser UI pages (such as `about:debugging`, `about:addons`, reader view, view-source, or the PDF viewer) or [extension pages](/en-US/docs/Mozilla/Add-ons/WebExtensions/user_interface/Extension_pages).
81+
82+
If an extension wants to run code in an extension page dynamically, it can include a script in the page. This script contains the code to run and registers a {{WebExtAPIRef("runtime.onMessage")}} listener that implements a way to execute the code. The extension can then send a message to the listener to trigger the code's execution.
8383

8484
## Content script environment
8585

files/en-us/mozilla/add-ons/webextensions/manifest.json/host_permissions/index.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,6 @@ The extra privileges include:
6565
- the ability to access cookies for that host using the {{webextAPIref("cookies")}} API, as long as the `"cookies"` API permission is also included.
6666
- bypassing tracking protection for extension pages where a host is specified as a full domain or with wildcards.
6767

68-
In Firefox extensions get host permissions for their origin, which is of the form:
69-
70-
```url
71-
moz-extension://60a20a9b-1ad4-af49-9b6c-c64c98c37920/
72-
```
73-
74-
where `60a20a9b-1ad4-af49-9b6c-c64c98c37920` is the extension's internal ID. The extension can get this URL programmatically by calling {{webextAPIref("extension/getURL", "extension.getURL()")}}:
75-
76-
```js
77-
browser.extension.getURL("");
78-
// moz-extension://60a20a9b-1ad4-af49-9b6c-c64c98c37920/
79-
```
80-
8168
## Example
8269

8370
```json

0 commit comments

Comments
 (0)