@@ -19,13 +19,20 @@ function CookieRule(host, cookiename) {
1919 //this.name_c = new RegExp(cookiename);
2020}
2121
22- function RuleSet ( id , name , default_off , platform ) {
22+ function RuleSet ( id , name , xmlName , match_rule , default_off , platform ) {
23+ if ( xmlName == "WordPress.xml" || xmlName == "Github.xml" ) {
24+ this . log ( NOTE , "RuleSet( name=" + name + ", xmlName=" + xmlName + ", match_rule=" + match_rule + ", default_off=" + default_off + ", platform=" + platform + " )" ) ;
25+ }
26+
2327 this . id = id ;
2428 this . on_by_default = true ;
2529 this . compiled = false ;
2630 this . name = name ;
31+ this . xmlName = xmlName ;
2732 this . notes = "" ;
2833
34+ if ( match_rule ) this . ruleset_match_c = new RegExp ( match_rule ) ;
35+ else this . ruleset_match_c = null ;
2936 if ( default_off ) {
3037 // Perhaps problematically, this currently ignores the actual content of
3138 // the default_off XML attribute. Ideally we'd like this attribute to be
@@ -86,7 +93,12 @@ RuleSet.prototype = {
8693 var i ;
8794 var returl = null ;
8895 this . ensureCompiled ( ) ;
89- // If we're covered by an exclusion, go home
96+ // If a rulset has a match_rule and it fails, go no further
97+ if ( this . ruleset_match_c && ! this . ruleset_match_c . test ( urispec ) ) {
98+ this . log ( VERB , "ruleset_match_c excluded " + urispec ) ;
99+ return null ;
100+ }
101+ // Even so, if we're covered by an exclusion, go home
90102 for ( i = 0 ; i < this . exclusions . length ; ++ i ) {
91103 if ( this . exclusions [ i ] . pattern_c . test ( urispec ) ) {
92104 this . log ( DBUG , "excluded uri " + urispec ) ;
@@ -99,7 +111,7 @@ RuleSet.prototype = {
99111 returl = urispec . replace ( this . rules [ i ] . from_c , this . rules [ i ] . to ) ;
100112 if ( returl != urispec ) {
101113 // we rewrote the uri
102- this . log ( DBUG , "Rewrote " + urispec + " -> " + returl + " using " + this . name + ": " + this . rules [ i ] . from_c + " -> " + this . rules [ i ] . to ) ;
114+ this . log ( DBUG , "Rewrote " + urispec + " -> " + returl + " using " + this . xmlName + ": " + this . rules [ i ] . from_c + " -> " + this . rules [ i ] . to ) ;
103115 return returl ;
104116 }
105117 }
@@ -109,6 +121,35 @@ RuleSet.prototype = {
109121 log : function ( level , msg ) {
110122 https_everywhereLog ( level , msg ) ;
111123 } ,
124+
125+ wouldMatch : function ( hypothetical_uri , alist ) {
126+ // return true if this ruleset would match the uri, assuming it were http
127+ // used for judging moot / inactive rulesets
128+ // alist is optional
129+
130+ // if the ruleset is already somewhere in this applicable list, we don't
131+ // care about hypothetical wouldMatch questions
132+ if ( alist && ( this . name in alist . all ) ) return false ;
133+
134+ this . log ( DBUG , "Would " + this . name + " match " + hypothetical_uri . spec +
135+ "? serial " + ( alist && alist . serial ) ) ;
136+
137+ var uri = hypothetical_uri . clone ( ) ;
138+ if ( uri . scheme == "https" ) uri . scheme = "http" ;
139+ var urispec = uri . spec ;
140+
141+ this . ensureCompiled ( ) ;
142+
143+ if ( this . ruleset_match_c && ! this . ruleset_match_c . test ( urispec ) )
144+ return false ;
145+
146+ for ( var i = 0 ; i < this . exclusions . length ; ++ i )
147+ if ( this . exclusions [ i ] . pattern_c . test ( urispec ) ) return false ;
148+
149+ for ( var i = 0 ; i < this . rules . length ; ++ i )
150+ if ( this . rules [ i ] . from_c . test ( urispec ) ) return true ;
151+ return false ;
152+ } ,
112153
113154 transformURI : function ( uri ) {
114155 // If no rule applies, return null; if a rule would have applied but was
@@ -268,9 +309,10 @@ const RuleWriter = {
268309
269310 this . log ( DBUG , "Parsing " + xmlruleset . getAttribute ( "name" ) ) ;
270311
312+ var match_rl = xmlruleset . getAttribute ( "match_rule" ) ;
271313 var dflt_off = xmlruleset . getAttribute ( "default_off" ) ;
272314 var platform = xmlruleset . getAttribute ( "platform" ) ;
273- var rs = new RuleSet ( ruleset_id , xmlruleset . getAttribute ( "name" ) , dflt_off , platform ) ;
315+ var rs = new RuleSet ( ruleset_id , xmlruleset . getAttribute ( "name" ) , xmlruleset . getAttribute ( "f" ) , match_rl , dflt_off , platform ) ;
274316
275317 // see if this ruleset has the same name as an existing ruleset;
276318 // if so, this ruleset is ignored; DON'T add or return it.
@@ -456,26 +498,31 @@ const HTTPSRules = {
456498 }
457499
458500 // ponder each potentially applicable ruleset, working out if it applies
459- // and recording it as active/inactive/breaking in the applicable list
501+ // and recording it as active/inactive/moot/ breaking in the applicable list
460502 for ( i = 0 ; i < rs . length ; ++ i ) {
461503 if ( ! rs [ i ] . active ) {
462- alist . inactive_rule ( rs [ i ] ) ;
463- }
504+ if ( alist && rs [ i ] . wouldMatch ( uri , alist ) )
505+ alist . inactive_rule ( rs [ i ] ) ;
506+ continue ;
507+ }
464508 blob . newuri = rs [ i ] . transformURI ( uri ) ;
465509 if ( blob . newuri ) {
466510 if ( alist ) {
467- if ( uri . spec in https_everywhere_blacklist ) {
511+ if ( uri . spec in https_everywhere_blacklist )
468512 alist . breaking_rule ( rs [ i ] ) ;
469- } else {
513+ else
470514 alist . active_rule ( rs [ i ] ) ;
471- }
472- }
473- if ( userpass_present ) {
474- blob . newuri . userPass = input_uri . userPass ;
475- }
515+ }
516+ if ( userpass_present ) blob . newuri . userPass = input_uri . userPass ;
476517 blob . applied_ruleset = rs [ i ] ;
477518 return blob ;
478519 }
520+ if ( uri . scheme == "https" && alist ) {
521+ // we didn't rewrite but the rule applies to this domain and the
522+ // requests are going over https
523+ if ( rs [ i ] . wouldMatch ( uri , alist ) ) alist . moot_rule ( rs [ i ] ) ;
524+ continue ;
525+ }
479526 }
480527 return null ;
481528 } ,
@@ -662,6 +709,9 @@ const HTTPSRules = {
662709 return true ;
663710 }
664711 }
712+ if ( ruleset . cookierules . length > 0 && applicable_list ) {
713+ applicable_list . moot_rule ( ruleset ) ;
714+ }
665715 } else if ( ruleset . cookierules . length > 0 ) {
666716 if ( applicable_list ) {
667717 applicable_list . inactive_rule ( ruleset ) ;
0 commit comments