Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 0e1d97b

Browse files
author
Simeon Vincent
authored
Expand documentation on webRequest extra_headers (#775)
CSP header change based on crbug/1207136 Expand header restrictions crbug/1208466
1 parent bf4b86b commit 0e1d97b

File tree

1 file changed

+90
-76
lines changed
  • site/en/docs/extensions/reference/webRequest

1 file changed

+90
-76
lines changed

site/en/docs/extensions/reference/webRequest/index.md

+90-76
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ api: webRequest
44

55
## Manifest
66

7-
You must declare the "webRequest" permission in the [extension manifest][1] to use the web request
8-
API, along with the necessary [host permissions][2]. To intercept a sub-resource request, the
9-
extension needs to have access to both the requested URL and its initiator. If you want to use the
10-
web request API in a blocking fashion, you need to request the "webRequestBlocking" permission in
11-
addition. For example:
7+
You must declare the "webRequest" permission in the [extension manifest][manifest-json] to use the
8+
web request API, along with the necessary [host permissions][declare-perms]. To intercept a
9+
sub-resource request, the extension needs to have access to both the requested URL and its
10+
initiator. If you want to use the web request API in a blocking fashion, you need to request the
11+
"webRequestBlocking" permission in addition. For example:
1212

1313
```json
1414
{
@@ -34,37 +34,40 @@ The event life cycle for successful requests is illustrated here, followed by ev
3434

3535
`onBeforeRequest` (optionally synchronous)
3636

37-
: Fires when a request is about to occur. This event is sent before any TCP connection is made and can
38-
be used to cancel or redirect requests.
37+
: Fires when a request is about to occur. This event is sent before any TCP connection is made and
38+
can be used to cancel or redirect requests.
3939

4040
`onBeforeSendHeaders` (optionally synchronous)
4141

4242
: Fires when a request is about to occur and the initial headers have been prepared. The event is
43-
intended to allow extensions to add, modify, and delete request headers [(\*)][3]. The
44-
`onBeforeSendHeaders` event is passed to all subscribers, so different subscribers may attempt to
45-
modify the request; see the [Implementation details][4] section for how this is handled. This event
46-
can be used to cancel the request.
43+
intended to allow extensions to add, modify, and delete request headers
44+
[(\*)][footnote-lifecycle]. The `onBeforeSendHeaders` event is passed to all subscribers, so
45+
different subscribers may attempt to modify the request; see the [Implementation
46+
details][impl-details] section for how this is handled. This event can be used to cancel the
47+
request.
4748

4849
`onSendHeaders`
4950

5051
: Fires after all extensions have had a chance to modify the request headers, and presents the final
51-
[(\*)][5] version. The event is triggered before the headers are sent to the network. This event is
52-
informational and handled asynchronously. It does not allow modifying or cancelling the request.
52+
[(\*)][footnote-lifecycle] version. The event is triggered before the headers are sent to the
53+
network. This event is informational and handled asynchronously. It does not allow modifying or
54+
cancelling the request.
5355

5456
`onHeadersReceived` (optionally synchronous)
5557

5658
: Fires each time that an HTTP(S) response header is received. Due to redirects and authentication
5759
requests this can happen multiple times per request. This event is intended to allow extensions to
5860
add, modify, and delete response headers, such as incoming Content-Type headers. The caching
59-
directives are processed before this event is triggered, so modifying headers such as Cache-Control
60-
has no influence on the browser's cache. It also allows you to cancel or redirect the request.
61+
directives are processed before this event is triggered, so modifying headers such as
62+
`Cache-Control` has no influence on the browser's cache. It also allows you to cancel or redirect
63+
the request.
6164

6265
`onAuthRequired` (optionally synchronous)
6366

64-
: Fires when a request requires authentication of the user. This event can be handled synchronously to
65-
provide authentication credentials. Note that extensions may provide invalid credentials. Take care
66-
not to enter an infinite loop by repeatedly providing invalid credentials. This can also be used to
67-
cancel the request.
67+
: Fires when a request requires authentication of the user. This event can be handled synchronously
68+
to provide authentication credentials. Note that extensions may provide invalid credentials. Take
69+
care not to enter an infinite loop by repeatedly providing invalid credentials. This can also be
70+
used to cancel the request.
6871

6972
`onBeforeRedirect`
7073

@@ -90,7 +93,7 @@ The web request API guarantees that for each request either `onCompleted` or `on
9093
fired as the final event with one exception: If a request is redirected to a `data://` URL,
9194
`onBeforeRedirect` is the last reported event.
9295

93-
<a id="#life_cycle_footnote">*</a>
96+
<a id="life-cycle-footnote">*</a>
9497
Note that the web request API presents an abstraction of the network stack to the extension.
9598
Internally, one URL request can be split into several HTTP requests (for example to fetch individual
9699
byte ranges from a large file) or can be handled by the network stack without communicating with the
@@ -135,32 +138,50 @@ removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`:
135138
{% Aside %}
136139

137140
**Note:** Modifying the `Origin` request header might not work as intended and may result in
138-
unexpected errors in the response's [CORS checks][6]. This is because while extensions can only
139-
modify the [Origin][7] request header, they can't change the `request origin` or initiator, which is
140-
a concept defined in the Fetch spec to represent who initiates the request. In such a scenario, the
141-
server may allow the CORS access for the modified request and put the header's `Origin` into the
142-
`Access-Control-Allow-Origin` header in the response. But it won't match the immutable
143-
`request origin` and result in a CORS failure.
141+
unexpected errors in the response's [CORS checks][cors-checks]. This is because while extensions can
142+
only modify the [Origin][origin-header] request header, they can't change the `request origin` or
143+
initiator, which is a concept defined in the Fetch spec to represent who initiates the request. In
144+
such a scenario, the server may allow the CORS access for the modified request and put the header's
145+
`Origin` into the `Access-Control-Allow-Origin` header in the response. But it won't match the
146+
immutable `request origin` and result in a CORS failure.
144147

145148
{% endAside %}
146149

147150
Starting from Chrome 72, if you need to modify responses before [Cross Origin Read Blocking
148-
(CORB)][8] can block the response, you need to specify `'extraHeaders'` in `opt_extraInfpSpec`.
151+
(CORB)][corb] can block the response, you need to specify `'extraHeaders'` in `opt_extraInfpSpec`.
149152

150153
Starting from Chrome 72, the following request headers are **not provided** and cannot be modified
151154
or removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`:
152155

153-
- Accept-Language
154-
- Accept-Encoding
155-
- Referer
156-
- Cookie
156+
- `Accept-Language`
157+
- `Accept-Encoding`
158+
- `Referer`
159+
- `Cookie`
157160

158161
Starting from Chrome 72, the `Set-Cookie` response header is **not provided** and cannot be modified
159162
or removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`.
160163

161-
Starting from Chrome 89, the `X-Frame-Options` response header cannot be effectively modified
164+
Starting from Chrome 84, the `Accept-CH` response header is **not provided** and cannot be modified
162165
or removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`.
163166

167+
Starting from Chrome 86, the `Accept-CH-Lifetime` response header is **not provided** and cannot be
168+
modified or removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`.
169+
170+
Starting from Chrome 88, the `Critical-CH` response header is **not provided** and cannot be
171+
modified or removed without specifying `'extraHeaders'` in `opt_extraInfoSpec`.
172+
173+
Starting from Chrome 89, the `Origin-Agent-Cluster` request header and `X-Frame-Options` response
174+
header are **not provided** and cannot be effectively modified or removed without specifying
175+
`'extraHeaders'` in `opt_extraInfoSpec`.
176+
177+
Starting from Chrome 91, the `Content-Security-Policy`, `Content-Security-Policy-Report-Only`, and
178+
`Link` response headers are **not provided** and cannot be modified or removed without specifying
179+
`'extraHeaders'` in `opt_extraInfoSpec`.
180+
181+
Starting from Chrome 92, the `Timing-Allow-Origin` and `BFCache-Opt-In` response headers are **not
182+
provided** and cannot be modified or removed without specifying `'extraHeaders'` in
183+
`opt_extraInfoSpec`.
184+
164185
{% Aside %}
165186

166187
**Note:** Specifying `'extraHeaders'` in `opt_extraInfoSpec` may have a negative impact on
@@ -169,16 +190,16 @@ performance, hence it should only be used when really necessary.
169190
{% endAside %}
170191

171192
The webRequest API only exposes requests that the extension has permission to see, given its [host
172-
permissions][9]. Moreover, only the following schemes are accessible: `http://`, `https://`,
173-
`ftp://`, `file://`, `ws://` (since Chrome 58), `wss://` (since Chrome 58), `urn:` (since Chrome 91), or
174-
`chrome-extension://`. In addition, even certain requests with URLs using one of the above schemes
175-
are hidden. These include `chrome-extension://other_extension_id` where `other_extension_id` is not
176-
the ID of the extension to handle the request, `https://www.google.com/chrome`, and other sensitive
177-
requests core to browser functionality. Also synchronous XMLHttpRequests from your extension are
178-
hidden from blocking event handlers in order to prevent deadlocks. Note that for some of the
179-
supported schemes the set of available events might be limited due to the nature of the
180-
corresponding protocol. For example, for the file: scheme, only `onBeforeRequest`,
181-
`onResponseStarted`, `onCompleted`, and `onErrorOccurred` may be dispatched.
193+
permissions][declare-perms]. Moreover, only the following schemes are accessible: `http://`,
194+
`https://`, `ftp://`, `file://`, `ws://` (since Chrome 58), `wss://` (since Chrome 58), `urn:`
195+
(since Chrome 91), or `chrome-extension://`. In addition, even certain requests with URLs using one
196+
of the above schemes are hidden. These include `chrome-extension://other_extension_id` where
197+
`other_extension_id` is not the ID of the extension to handle the request,
198+
`https://www.google.com/chrome`, and other sensitive requests core to browser functionality. Also
199+
synchronous XMLHttpRequests from your extension are hidden from blocking event handlers in order to
200+
prevent deadlocks. Note that for some of the supported schemes the set of available events might be
201+
limited due to the nature of the corresponding protocol. For example, for the file: scheme, only
202+
`onBeforeRequest`, `onResponseStarted`, `onCompleted`, and `onErrorOccurred` may be dispatched.
182203

183204
Starting from Chrome 58, the webRequest API supports intercepting the WebSocket handshake request.
184205
Since the handshake is done by means of an HTTP upgrade request, its flow fits into HTTP-oriented
@@ -207,8 +228,8 @@ in case of HTTP redirection or HTTP authentication.
207228
### Registering event listeners
208229

209230
To register an event listener for a web request, you use a variation on the [usual `addListener()`
210-
function][10]. In addition to specifying a callback function, you have to specify a filter argument
211-
and you may specify an optional extra info argument.
231+
function][api-events]. In addition to specifying a callback function, you have to specify a filter
232+
argument and you may specify an optional extra info argument.
212233

213234
The three arguments to the web request API's `addListener()` have the following definitions:
214235

@@ -233,27 +254,28 @@ information in this dictionary depends on the specific event type as well as the
233254
If the optional `opt_extraInfoSpec` array contains the string `'blocking'` (only allowed for
234255
specific events), the callback function is handled synchronously. That means that the request is
235256
blocked until the callback function returns. In this case, the callback can return a
236-
[`webRequest.BlockingResponse`][11] that determines the further life cycle of the request. Depending
237-
on the context, this response allows cancelling or redirecting a request (`onBeforeRequest`),
238-
cancelling a request or modifying headers (`onBeforeSendHeaders`, `onHeadersReceived`), and
239-
cancelling a request or providing authentication credentials (`onAuthRequired`).
257+
[`webRequest.BlockingResponse`][blocking-response] that determines the further life cycle of the
258+
request. Depending on the context, this response allows cancelling or redirecting a request
259+
(`onBeforeRequest`), cancelling a request or modifying headers (`onBeforeSendHeaders`,
260+
`onHeadersReceived`), and cancelling a request or providing authentication credentials
261+
(`onAuthRequired`).
240262

241263
If the optional `opt_extraInfoSpec` array contains the string `'asyncBlocking'` instead (only
242-
allowed for `onAuthRequired`), the extension can generate the [`webRequest.BlockingResponse`][12]
243-
asynchronously.
264+
allowed for `onAuthRequired`), the extension can generate the
265+
[`webRequest.BlockingResponse`][blocking-response] asynchronously.
244266

245-
The [`webRequest.RequestFilter`][13] `filter` allows limiting the requests for which events are
246-
triggered in various dimensions:
267+
The [`webRequest.RequestFilter`][request-filter] `filter` allows limiting the requests for which
268+
events are triggered in various dimensions:
247269

248270
URLs
249271

250-
: [URL patterns][14] such as `*://www.google.com/foo*bar`.
272+
: [URL patterns][match-patterns] such as `*://www.google.com/foo*bar`.
251273

252274
Types
253275

254-
: Request types such as `main_frame` (a document that is loaded for a top-level frame), `sub_frame` (a
255-
document that is loaded for an embedded frame), and `image` (an image on a web site). See
256-
[`webRequest.RequestFilter`][15].
276+
: Request types such as `main_frame` (a document that is loaded for a top-level frame), `sub_frame`
277+
(a document that is loaded for an embedded frame), and `image` (an image on a web site). See
278+
[`webRequest.RequestFilter`][request-filter].
257279

258280
Tab ID
259281

@@ -267,7 +289,7 @@ Depending on the event type, you can specify strings in `opt_extraInfoSpec` to a
267289
information about the request. This is used to provide detailed information on request's data only
268290
if explicitly requested.
269291

270-
## Implementation details
292+
## Implementation details {: #implementation-details }
271293

272294
Several implementation details can be important to understand when developing an extension that uses
273295
the web request API:
@@ -352,22 +374,14 @@ chrome.webRequest.onBeforeSendHeaders.addListener(
352374
);
353375
```
354376

355-
For more example code, see the [web request samples][16].
356-
357-
[1]: /docs/extensions/mv2/tabs
358-
[2]: /docs/extensions/mv2/declare_permissions
359-
[3]: #life_cycle_footnote
360-
[4]: #implementation-details
361-
[5]: #life_cycle_footnote
362-
[6]: https://fetch.spec.whatwg.org/#cors-check
363-
[7]: https://fetch.spec.whatwg.org/#origin-header
364-
[8]:
365-
https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md
366-
[9]: /docs/extensions/mv2/declare_permissions
367-
[10]: /docs/extensions/events
368-
[11]: #type-BlockingResponse
369-
[12]: #type-BlockingResponse
370-
[13]: #type-RequestFilter
371-
[14]: /docs/extensions/mv2/match_patterns
372-
[15]: #type-RequestFilter
373-
[16]: /docs/extensions/mv2/samples#search:webrequest
377+
[api-events]: /docs/extensions/events
378+
[manifest-json]: /docs/extensions/mv3/manifest/
379+
[blocking-response]: #type-BlockingResponse
380+
[corb]: https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md
381+
[cors-checks]: https://fetch.spec.whatwg.org/#cors-check
382+
[declare-perms]: /docs/extensions/mv3/declare_permissions
383+
[footnote-lifecycle]: #life-cycle-footnote
384+
[impl-details]: #implementation-details
385+
[match-patterns]: /docs/extensions/mv3/match_patterns
386+
[origin-header]: https://fetch.spec.whatwg.org/#origin-header
387+
[request-filter]: #type-RequestFilter

0 commit comments

Comments
 (0)