Skip to content

Commit 2460abb

Browse files
committed
Rewrite HTTP Nowhere using http-on-modify-request
1 parent 27119a1 commit 2460abb

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

src/chrome/content/code/HTTPS.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,23 @@ const HTTPS = {
3030
httpsForcedExceptions: null,
3131
httpsRewrite: null,
3232

33-
replaceChannel: function(applicable_list, channel) {
33+
replaceChannel: function(applicable_list, channel, httpNowhereEnabled) {
3434
var blob = HTTPSRules.rewrittenURI(applicable_list, channel.URI.clone());
35-
if (null == blob) return false; // no rewrite
35+
if (blob === null) {
36+
// Abort insecure requests if HTTP Nowhere is on
37+
if (httpNowhereEnabled && channel.URI.schemeIs("http")) {
38+
IOUtil.abort(channel);
39+
}
40+
return false; // no rewrite
41+
}
3642
var uri = blob.newuri;
3743
if (!uri) this.log(WARN, "OH NO BAD ARGH\nARGH");
3844

45+
// Abort downgrading if HTTP Nowhere is on
46+
if (httpNowhereEnabled && uri.schemeIs("http")) {
47+
IOUtil.abort(channel);
48+
}
49+
3950
var c2 = channel.QueryInterface(CI.nsIHttpChannel);
4051
this.log(DBUG, channel.URI.spec+": Redirection limit is " + c2.redirectionLimit);
4152
// XXX This used to be (c2.redirectionLimit == 1), but that's very

src/components/https-everywhere.js

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ function HTTPSEverywhere() {
190190

191191
this.prefs = this.get_prefs();
192192
this.rule_toggle_prefs = this.get_prefs(PREFBRANCH_RULE_TOGGLE);
193+
194+
this.httpNowhereEnabled = this.prefs.getBoolPref("http_nowhere.enabled");
193195

194196
// We need to use observers instead of categories for FF3.0 for these:
195197
// https://developer.mozilla.org/en/Observer_Notifications
@@ -475,16 +477,21 @@ HTTPSEverywhere.prototype = {
475477

476478
if (topic == "http-on-modify-request") {
477479
if (!(channel instanceof CI.nsIHttpChannel)) return;
478-
480+
479481
this.log(DBUG,"Got http-on-modify-request: "+channel.URI.spec);
480-
var lst = this.getApplicableListForChannel(channel); // null if no window is associated (ex: xhr)
482+
// lst is null if no window is associated (ex: some XHR)
483+
var lst = this.getApplicableListForChannel(channel);
481484
if (channel.URI.spec in https_everywhere_blacklist) {
482485
this.log(DBUG, "Avoiding blacklisted " + channel.URI.spec);
483-
if (lst) lst.breaking_rule(https_everywhere_blacklist[channel.URI.spec]);
484-
else this.log(NOTE,"Failed to indicate breakage in content menu");
486+
if (lst) {
487+
lst.breaking_rule(https_everywhere_blacklist[channel.URI.spec]);
488+
}
489+
else {
490+
this.log(NOTE,"Failed to indicate breakage in content menu");
491+
}
485492
return;
486493
}
487-
HTTPS.replaceChannel(lst, channel);
494+
HTTPS.replaceChannel(lst, channel, this.httpNowhereEnabled);
488495
} else if (topic == "http-on-examine-response") {
489496
this.log(DBUG, "Got http-on-examine-response @ "+ (channel.URI ? channel.URI.spec : '') );
490497
HTTPS.handleSecureCookies(channel);
@@ -520,13 +527,13 @@ HTTPSEverywhere.prototype = {
520527
Thread.hostRunning = false;
521528
} else if (topic == "profile-after-change") {
522529
this.log(DBUG, "Got profile-after-change");
523-
530+
524531
if(this.prefs.getBoolPref("globalEnabled")){
525532
OS.addObserver(this, "cookie-changed", false);
526533
OS.addObserver(this, "http-on-modify-request", false);
527534
OS.addObserver(this, "http-on-examine-merged-response", false);
528535
OS.addObserver(this, "http-on-examine-response", false);
529-
536+
530537
var dls = CC['@mozilla.org/docloaderservice;1']
531538
.getService(CI.nsIWebProgress);
532539
dls.addProgressListener(this, CI.nsIWebProgress.NOTIFY_LOCATION);
@@ -804,66 +811,33 @@ HTTPSEverywhere.prototype = {
804811
let prefService = Services.prefs;
805812
let thisBranch =
806813
prefService.getBranch("extensions.https_everywhere.http_nowhere.");
807-
let networkBranch = prefService.getBranch("network.");
808814
let securityBranch = prefService.getBranch("security.");
809815

810-
// Proxy type. 0: none, 1: manual, 2: autoconfig by URL, 3: same as 0,
811-
// 4: autodetect proxy settings, 5: use system proxy settings (default)
812-
let PROXY_TYPE = "proxy.type";
813-
// HTTP proxy host
814-
let PROXY_HTTP = "proxy.http";
815-
// HTTP proxy port
816-
let PROXY_PORT = "proxy.http_port";
817-
818816
// Whether cert is treated as invalid when OCSP connection fails
819817
let OCSP_REQUIRED = "OCSP.require";
820818

821-
// Original settings
822-
let ORIG_PROXY_TYPE = "orig.proxy.type";
823-
let ORIG_PROXY_HTTP = "orig.proxy.http";
824-
let ORIG_PROXY_PORT = "orig.proxy.http_port";
819+
// Branch to save original settings
825820
let ORIG_OCSP_REQUIRED = "orig.ocsp.required";
826821

827822

828823
if (thisBranch.getBoolPref("enabled")) {
829-
// Restore original proxy/OCSP settings. TODO: What if user manually edits
824+
// Restore original OCSP settings. TODO: What if user manually edits
830825
// these while HTTP Nowhere is enabled?
831-
let origProxyType = thisBranch.getIntPref(ORIG_PROXY_TYPE);
832-
networkBranch.setIntPref(PROXY_TYPE, origProxyType);
833-
834-
let origProxyHttp = thisBranch.getCharPref(ORIG_PROXY_HTTP);
835-
networkBranch.setCharPref(PROXY_HTTP, origProxyHttp);
836-
837-
let origProxyPort = thisBranch.getIntPref(ORIG_PROXY_PORT);
838-
networkBranch.setIntPref(PROXY_PORT, origProxyPort);
839-
840826
let origOcspRequired = thisBranch.getBoolPref(ORIG_OCSP_REQUIRED);
841827
securityBranch.setBoolPref(OCSP_REQUIRED, origOcspRequired);
842828

843829
thisBranch.setBoolPref("enabled", false);
830+
this.httpNowhereEnabled = false;
844831
} else {
845-
// Save original proxy settings in HTTP Nowhere preferences branch.
846-
let origProxyType = networkBranch.getIntPref(PROXY_TYPE);
847-
thisBranch.setIntPref(ORIG_PROXY_TYPE, origProxyType);
848-
849-
let origProxyHttp = networkBranch.getCharPref(PROXY_HTTP);
850-
thisBranch.setCharPref(ORIG_PROXY_HTTP, origProxyHttp);
851-
852-
let origProxyPort = networkBranch.getIntPref(PROXY_PORT);
853-
thisBranch.setIntPref(ORIG_PROXY_PORT, origProxyPort);
854-
832+
// Save original OCSP settings in HTTP Nowhere preferences branch.
855833
let origOcspRequired = securityBranch.getBoolPref(OCSP_REQUIRED);
856834
thisBranch.setBoolPref(ORIG_OCSP_REQUIRED, origOcspRequired);
857835

858-
// Set a null proxy for HTTP requests
859-
networkBranch.setIntPref(PROXY_TYPE, 1); // manual
860-
networkBranch.setCharPref(PROXY_HTTP, "localhost");
861-
networkBranch.setIntPref(PROXY_PORT, 4); // any arbitrary unused port
862-
863836
// Disable OCSP enforcement
864837
securityBranch.setBoolPref(OCSP_REQUIRED, false);
865838

866839
thisBranch.setBoolPref("enabled", true);
840+
this.httpNowhereEnabled = true;
867841
}
868842
}
869843
};

0 commit comments

Comments
 (0)