Skip to content

Commit

Permalink
Fix protocol filtering of net.request
Browse files Browse the repository at this point in the history
net::URLRequest inherits from base::SupportsUserData, which allows
associating arbitrary data with the request. Use this mechanism as a
condition for filtering requests from custom protocols.

Close electron#11657
  • Loading branch information
tarruda authored and John Kleinschmidt committed Feb 16, 2018
1 parent 78ccfa0 commit bc76f35
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 0 additions & 4 deletions atom/browser/api/atom_api_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ class Protocol : public mate::TrackableObject<Protocol> {
net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
if (!request->initiator().has_value()) {
// Don't intercept this request as it was created by `net.request`.
return nullptr;
}
RequestJob* request_job = new RequestJob(request, network_delegate);
request_job->SetHandlerInfo(isolate_, request_context_.get(), handler_);
return request_job;
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/net/atom_url_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include "atom/browser/api/atom_api_url_request.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/net/atom_url_request_job_factory.h"
#include "base/callback.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/elements_upload_data_stream.h"
Expand Down Expand Up @@ -121,6 +122,9 @@ void AtomURLRequest::DoInitialize(
request_->set_method(method);
// Do not send cookies from the cookie store.
DoSetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES);
// Set a flag to stop custom protocol from intercepting this request.
request_->SetUserData(DisableProtocolInterceptFlagKey(),
base::WrapUnique(new base::SupportsUserData::Data()));
}

void AtomURLRequest::DoTerminate() {
Expand Down
12 changes: 12 additions & 0 deletions atom/browser/net/atom_url_request_job_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ using content::BrowserThread;

namespace atom {

namespace {

int disable_protocol_intercept_flag_key = 0;

} // namespace

typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;

const void* DisableProtocolInterceptFlagKey() {
return &disable_protocol_intercept_flag_key;
}

AtomURLRequestJobFactory::AtomURLRequestJobFactory() {}

AtomURLRequestJobFactory::~AtomURLRequestJobFactory() {
Expand Down Expand Up @@ -93,6 +103,8 @@ net::URLRequestJob* AtomURLRequestJobFactory::MaybeCreateJobWithProtocolHandler(
auto it = protocol_handler_map_.find(scheme);
if (it == protocol_handler_map_.end())
return nullptr;
if (request->GetUserData(DisableProtocolInterceptFlagKey()))
return nullptr;
return it->second->MaybeCreateJob(request, network_delegate);
}

Expand Down
2 changes: 2 additions & 0 deletions atom/browser/net/atom_url_request_job_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace atom {

const void* DisableProtocolInterceptFlagKey();

class AtomURLRequestJobFactory : public net::URLRequestJobFactory {
public:
AtomURLRequestJobFactory();
Expand Down

0 comments on commit bc76f35

Please sign in to comment.