Commit 9aed1496 authored by Yutaka Hirano's avatar Yutaka Hirano Committed by Commit Bot

[Extension] Allow "extraHeaders" for onBeforeRequest and onErrorOccured

We dispatch CORS preflight events only when there is an event listener
with "extraHeaders" specified. Hence some extension developers may
want to specify that option for onBeforeReuqest and onErrorOcurred.

Bug: 1002884
Change-Id: I7006f9b30c94858427b66aa2fd93d82e4f8f4c37
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866374
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Auto-Submit: Yutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/master@{#707642}
parent eea83424
...@@ -517,11 +517,6 @@ function registerPreflightBlockingListener() { ...@@ -517,11 +517,6 @@ function registerPreflightBlockingListener() {
function registerPreflightRedirectingListener() { function registerPreflightRedirectingListener() {
const url = getServerURL(BASE + 'accept', 'cors.example.com'); const url = getServerURL(BASE + 'accept', 'cors.example.com');
// This is needed to observe preflight requests when OOR-CORS is enabled.
chrome.webRequest.onHeadersReceived.addListener(
() => {},
{urls: [url]}, ['extraHeaders']);
const onBeforeRequestCalledForPreflight = callbackPass(() => {}); const onBeforeRequestCalledForPreflight = callbackPass(() => {});
chrome.webRequest.onBeforeRequest.addListener( chrome.webRequest.onBeforeRequest.addListener(
function onBeforeRequest(details) { function onBeforeRequest(details) {
...@@ -533,7 +528,7 @@ function registerPreflightRedirectingListener() { ...@@ -533,7 +528,7 @@ function registerPreflightRedirectingListener() {
}, 0); }, 0);
return {redirectUrl: url + '?redirected'}; return {redirectUrl: url + '?redirected'};
} }
}, {urls: [url]}, ['blocking']); }, {urls: [url]}, ['blocking', 'extraHeaders']);
if (getCorsMode() == 'network_service') { if (getCorsMode() == 'network_service') {
// When CORS is implemented in the network service, we see failures on both // When CORS is implemented in the network service, we see failures on both
...@@ -560,6 +555,23 @@ function registerPreflightRedirectingListener() { ...@@ -560,6 +555,23 @@ function registerPreflightRedirectingListener() {
} }
} }
function registerOnBeforeRequestAndOnErrorOcurredListeners() {
const url = getServerURL(BASE + 'accept', 'cors.example.com');
const onBeforeRequestCalledForPreflight = callbackPass(() => {});
// onBeforeRequest doesn't have "extraHeaders", but it sees a preflight
// even when OOR-CORS is enabled, because onErrorOccurred has "extraHeaders".
chrome.webRequest.onBeforeRequest.addListener((details) => {
if (details.method === 'OPTIONS') {
onBeforeRequestCalledForPreflight();
}
}, {urls: [url]});
chrome.webRequest.onErrorOccurred.addListener(() => {
}, {urls: [url]}, ['extraHeaders']);
}
runTests([ runTests([
function testOriginHeader() { function testOriginHeader() {
// Register two sets of listener. One with extraHeaders and the second one // Register two sets of listener. One with extraHeaders and the second one
...@@ -636,4 +648,9 @@ runTests([ ...@@ -636,4 +648,9 @@ runTests([
navigateAndWait(getServerURL( navigateAndWait(getServerURL(
BASE + 'fetch.html?path=accept&with-preflight')); BASE + 'fetch.html?path=accept&with-preflight'));
}, },
function testCorsPreflightIsObservableWhenAnyListenerHasExtraHeaders() {
registerOnBeforeRequestAndOnErrorOcurredListeners();
navigateAndWait(getServerURL(
BASE + 'fetch.html?path=accept&with-preflight'));
}
]); ]);
...@@ -148,14 +148,6 @@ const char* const kWebRequestEvents[] = { ...@@ -148,14 +148,6 @@ const char* const kWebRequestEvents[] = {
keys::kOnHeadersReceivedEvent, keys::kOnHeadersReceivedEvent,
}; };
// List of all webRequest events that support extraHeaders in the extraInfoSpec.
const char* const kWebRequestExtraHeadersEventNames[] = {
keys::kOnBeforeSendHeadersEvent, keys::kOnSendHeadersEvent,
keys::kOnHeadersReceivedEvent, keys::kOnAuthRequiredEvent,
keys::kOnResponseStartedEvent, keys::kOnBeforeRedirectEvent,
keys::kOnCompletedEvent,
};
const char* GetRequestStageAsString( const char* GetRequestStageAsString(
ExtensionWebRequestEventRouter::EventTypes type) { ExtensionWebRequestEventRouter::EventTypes type) {
switch (type) { switch (type) {
...@@ -1834,7 +1826,7 @@ bool ExtensionWebRequestEventRouter::HasExtraHeadersListenerForRequest( ...@@ -1834,7 +1826,7 @@ bool ExtensionWebRequestEventRouter::HasExtraHeadersListenerForRequest(
return false; return false;
int extra_info_spec = 0; int extra_info_spec = 0;
for (const char* name : kWebRequestExtraHeadersEventNames) { for (const char* name : kWebRequestEvents) {
GetMatchingListeners(browser_context, name, request, &extra_info_spec); GetMatchingListeners(browser_context, name, request, &extra_info_spec);
if (extra_info_spec & ExtraInfoSpec::EXTRA_HEADERS) if (extra_info_spec & ExtraInfoSpec::EXTRA_HEADERS)
return true; return true;
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
{ {
"id": "OnBeforeRequestOptions", "id": "OnBeforeRequestOptions",
"type": "string", "type": "string",
"enum": ["blocking", "requestBody"] "enum": ["blocking", "requestBody", "extraHeaders"]
}, },
{ {
"id": "OnBeforeSendHeadersOptions", "id": "OnBeforeSendHeadersOptions",
...@@ -58,6 +58,11 @@ ...@@ -58,6 +58,11 @@
"type": "string", "type": "string",
"enum": ["responseHeaders", "extraHeaders"] "enum": ["responseHeaders", "extraHeaders"]
}, },
{
"id": "OnErrorOccurredOptions",
"type": "string",
"enum": ["extraHeaders"]
},
{ {
"id": "RequestFilter", "id": "RequestFilter",
"type": "object", "type": "object",
...@@ -598,6 +603,15 @@ ...@@ -598,6 +603,15 @@
"$ref": "RequestFilter", "$ref": "RequestFilter",
"name": "filter", "name": "filter",
"description": "A set of filters that restricts the events that will be sent to this listener." "description": "A set of filters that restricts the events that will be sent to this listener."
},
{
"type": "array",
"optional": true,
"name": "extraInfoSpec",
"description": "Array of extra information that should be passed to the listener function.",
"items": {
"$ref": "OnErrorOccurredOptions"
}
} }
] ]
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment