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() {
function registerPreflightRedirectingListener() {
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(() => {});
chrome.webRequest.onBeforeRequest.addListener(
function onBeforeRequest(details) {
......@@ -533,7 +528,7 @@ function registerPreflightRedirectingListener() {
}, 0);
return {redirectUrl: url + '?redirected'};
}
}, {urls: [url]}, ['blocking']);
}, {urls: [url]}, ['blocking', 'extraHeaders']);
if (getCorsMode() == 'network_service') {
// When CORS is implemented in the network service, we see failures on both
......@@ -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([
function testOriginHeader() {
// Register two sets of listener. One with extraHeaders and the second one
......@@ -636,4 +648,9 @@ runTests([
navigateAndWait(getServerURL(
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[] = {
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(
ExtensionWebRequestEventRouter::EventTypes type) {
switch (type) {
......@@ -1834,7 +1826,7 @@ bool ExtensionWebRequestEventRouter::HasExtraHeadersListenerForRequest(
return false;
int extra_info_spec = 0;
for (const char* name : kWebRequestExtraHeadersEventNames) {
for (const char* name : kWebRequestEvents) {
GetMatchingListeners(browser_context, name, request, &extra_info_spec);
if (extra_info_spec & ExtraInfoSpec::EXTRA_HEADERS)
return true;
......
......@@ -21,7 +21,7 @@
{
"id": "OnBeforeRequestOptions",
"type": "string",
"enum": ["blocking", "requestBody"]
"enum": ["blocking", "requestBody", "extraHeaders"]
},
{
"id": "OnBeforeSendHeadersOptions",
......@@ -58,6 +58,11 @@
"type": "string",
"enum": ["responseHeaders", "extraHeaders"]
},
{
"id": "OnErrorOccurredOptions",
"type": "string",
"enum": ["extraHeaders"]
},
{
"id": "RequestFilter",
"type": "object",
......@@ -598,6 +603,15 @@
"$ref": "RequestFilter",
"name": "filter",
"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