Skip to content

Commit 16a4718

Browse files
committed
Chrome: Streamline redirect counter
(For issue EFForg#12) This refactors & optimizes the old redirect counter. The redirect counter exits to catch infinite HTTPS <-> HTTP loops. The old counter was messy, constantly churning a small array of requestIds. I think it was written before the new API call: onBeforeRedirect. (The old counter also generated a lot of orphan entries, e.g. when another extension or the browser itself aborted requests / redirected to about:blank.) Signed-off-by: Nick Semenkovich <semenko@alum.mit.edu>
1 parent 75cb897 commit 16a4718

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

chromium/background.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,13 @@ function onBeforeRequest(details) {
115115
// If no rulesets could apply, let's get out of here!
116116
if (rs.length === 0) { return; }
117117

118-
if (details.requestId in redirectCounter) {
119-
redirectCounter[details.requestId] += 1;
120-
log(DBUG, "Got redirect id "+details.requestId+
121-
": "+redirectCounter[details.requestId]);
122-
123-
if (redirectCounter[details.requestId] > 9) {
124-
log(NOTE, "Redirect counter hit for "+canonical_url);
125-
urlBlacklist[canonical_url] = true;
126-
var hostname = uri.hostname();
127-
domainBlacklist[hostname] = true;
128-
log(WARN, "Domain blacklisted " + hostname);
129-
return;
130-
}
131-
} else {
132-
redirectCounter[details.requestId] = 0;
118+
if (redirectCounter[details.requestId] >= 8) {
119+
log(NOTE, "Redirect counter hit for " + canonical_url);
120+
urlBlacklist[canonical_url] = true;
121+
var hostname = uri.hostname();
122+
domainBlacklist[hostname] = true;
123+
log(WARN, "Domain blacklisted " + hostname);
124+
return null;
133125
}
134126

135127
var newuristr = null;
@@ -383,13 +375,17 @@ function onBeforeSendHeaders(details) {
383375
return {requestHeaders:details.requestHeaders};
384376
}
385377

386-
function onResponseStarted(details) {
387-
388-
// redirect counter workaround
389-
// TODO: Remove this code if they ever give us a real counter
390-
if (details.requestId in redirectCounter) {
391-
delete redirectCounter[details.requestId];
392-
}
378+
function onBeforeRedirect(details) {
379+
// Catch HTTPs -> HTTP redirect loops, ignoring about:blank, HTTPS 302s, etc.
380+
if (details.redirectUrl.substring(0, 7) === "http://") {
381+
if (details.requestId in redirectCounter) {
382+
redirectCounter[details.requestId] += 1;
383+
log(DBUG, "Got redirect id "+details.requestId+
384+
": "+redirectCounter[details.requestId]);
385+
} else {
386+
redirectCounter[details.requestId] = 1;
387+
}
388+
}
393389
}
394390

395391
wr.onBeforeRequest.addListener(onBeforeRequest, {urls: ["https://*/*", "http://*/*"]}, ["blocking"]);
@@ -399,8 +395,8 @@ wr.onBeforeRequest.addListener(onBeforeRequest, {urls: ["https://*/*", "http://*
399395
wr.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: ["http://*/*"]},
400396
["requestHeaders", "blocking"]);
401397

402-
wr.onResponseStarted.addListener(onResponseStarted,
403-
{urls: ["https://*/*", "http://*/*"]});
398+
// Try to catch redirect loops on URLs we've redirected to HTTPS.
399+
wr.onBeforeRedirect.addListener(onBeforeRedirect, {urls: ["https://*/*"]});
404400

405401

406402
// Add the small HTTPS Everywhere icon in the address bar.

0 commit comments

Comments
 (0)