Skip to content

Commit b02a20e

Browse files
zcbenzminiak
andauthored
fix: webRequest should be able to modify CORS headers (electron#21099)
* fix: always use extraHeaders mode * fix: clear pending callbacks * fix: do not use "extraHeaders" for net module * test: webRequest should be able to modify CROS headers * chore: CROS => CORS Co-Authored-By: Milan Burda <milan.burda@gmail.com> * chore: CROS => CORS Co-Authored-By: Milan Burda <milan.burda@gmail.com>
1 parent af1e8a3 commit b02a20e

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

shell/browser/api/atom_api_web_request.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ void WebRequest::OnCompleted(extensions::WebRequestInfo* info,
332332
HandleSimpleEvent(kOnCompleted, info, request, net_error);
333333
}
334334

335+
void WebRequest::OnRequestWillBeDestroyed(extensions::WebRequestInfo* info) {
336+
callbacks_.erase(info->id);
337+
}
338+
335339
template <WebRequest::SimpleEvent event>
336340
void WebRequest::SetSimpleListener(gin::Arguments* args) {
337341
SetListener<SimpleListener>(event, &simple_listeners_, args);

shell/browser/api/atom_api_web_request.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class WebRequest : public gin::Wrappable<WebRequest>, public WebRequestAPI {
8484
void OnCompleted(extensions::WebRequestInfo* info,
8585
const network::ResourceRequest& request,
8686
int net_error) override;
87+
void OnRequestWillBeDestroyed(extensions::WebRequestInfo* info) override;
8788

8889
enum SimpleEvent {
8990
kOnSendHeaders,

shell/browser/net/proxying_url_loader_factory.cc

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ ProxyingURLLoaderFactory::InProgressRequest::InProgressRequest(
4949
target_client_(std::move(client)),
5050
current_response_(network::mojom::URLResponseHead::New()),
5151
proxied_client_binding_(this),
52-
// TODO(zcbenz): We should always use "extraHeaders" mode to be compatible
53-
// with old APIs.
54-
has_any_extra_headers_listeners_(false) {
52+
// Always use "extraHeaders" mode to be compatible with old APIs, except
53+
// when the |request_id_| is zero, which is not supported in Chromium and
54+
// only happens in Electron when the request is started from net module.
55+
has_any_extra_headers_listeners_(network_service_request_id != 0) {
5556
// If there is a client error, clean up the request.
5657
target_client_.set_connection_error_handler(base::BindOnce(
5758
&ProxyingURLLoaderFactory::InProgressRequest::OnRequestError,
@@ -60,7 +61,19 @@ ProxyingURLLoaderFactory::InProgressRequest::InProgressRequest(
6061
}
6162

6263
ProxyingURLLoaderFactory::InProgressRequest::~InProgressRequest() {
63-
// TODO(zcbenz): Do cleanup here.
64+
// This is important to ensure that no outstanding blocking requests continue
65+
// to reference state owned by this object.
66+
if (info_) {
67+
factory_->web_request_api()->OnRequestWillBeDestroyed(&info_.value());
68+
}
69+
if (on_before_send_headers_callback_) {
70+
std::move(on_before_send_headers_callback_)
71+
.Run(net::ERR_ABORTED, base::nullopt);
72+
}
73+
if (on_headers_received_callback_) {
74+
std::move(on_headers_received_callback_)
75+
.Run(net::ERR_ABORTED, base::nullopt, base::nullopt);
76+
}
6477
}
6578

6679
void ProxyingURLLoaderFactory::InProgressRequest::Restart() {
@@ -84,8 +97,7 @@ void ProxyingURLLoaderFactory::InProgressRequest::UpdateRequestInfo() {
8497

8598
current_request_uses_header_client_ =
8699
factory_->url_loader_header_client_receiver_.is_bound() &&
87-
network_service_request_id_ != 0 &&
88-
false /* TODO(zcbenz): HasExtraHeadersListenerForRequest */;
100+
network_service_request_id_ != 0 && has_any_extra_headers_listeners_;
89101
}
90102

91103
void ProxyingURLLoaderFactory::InProgressRequest::RestartInternal() {
@@ -722,6 +734,10 @@ void ProxyingURLLoaderFactory::CreateLoaderAndStart(
722734
// don't use it for identity here.
723735
const uint64_t web_request_id = ++g_request_id;
724736

737+
// Notes: Chromium assumes that requests with zero-ID would never use the
738+
// "extraHeaders" code path, however in Electron requests started from
739+
// the net module would have zero-ID because they do not have renderer process
740+
// associated.
725741
if (request_id)
726742
network_request_id_to_web_request_id_.emplace(request_id, web_request_id);
727743

shell/browser/net/proxying_url_loader_factory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class WebRequestAPI {
6767
virtual void OnCompleted(extensions::WebRequestInfo* info,
6868
const network::ResourceRequest& request,
6969
int net_error) = 0;
70+
virtual void OnRequestWillBeDestroyed(extensions::WebRequestInfo* info) = 0;
7071
};
7172

7273
// This class is responsible for following tasks when NetworkService is enabled:

spec-main/api-web-request-spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ describe('webRequest module', () => {
2020
if (req.headers.accept === '*/*;test/header') {
2121
content += 'header/received'
2222
}
23+
if (req.headers.origin === 'http://new-origin') {
24+
content += 'new/origin'
25+
}
2326
res.end(content)
2427
}
2528
})
@@ -145,6 +148,16 @@ describe('webRequest module', () => {
145148
expect(data).to.equal('/header/received')
146149
})
147150

151+
it('can change CORS headers', async () => {
152+
ses.webRequest.onBeforeSendHeaders((details, callback) => {
153+
const requestHeaders = details.requestHeaders
154+
requestHeaders.Origin = 'http://new-origin'
155+
callback({ requestHeaders: requestHeaders })
156+
})
157+
const { data } = await ajax(defaultURL)
158+
expect(data).to.equal('/new/origin')
159+
})
160+
148161
it('resets the whole headers', async () => {
149162
const requestHeaders = {
150163
Test: 'header'
@@ -199,6 +212,16 @@ describe('webRequest module', () => {
199212
expect(headers).to.match(/^custom: Changed$/m)
200213
})
201214

215+
it('can change CORS headers', async () => {
216+
ses.webRequest.onHeadersReceived((details, callback) => {
217+
const responseHeaders = details.responseHeaders!
218+
responseHeaders['access-control-allow-origin'] = ['http://new-origin'] as any
219+
callback({ responseHeaders: responseHeaders })
220+
})
221+
const { headers } = await ajax(defaultURL)
222+
expect(headers).to.match(/^access-control-allow-origin: http:\/\/new-origin$/m)
223+
})
224+
202225
it('does not change header by default', async () => {
203226
ses.webRequest.onHeadersReceived((details, callback) => {
204227
callback({})

0 commit comments

Comments
 (0)