Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit c1e138b

Browse files
authored
https_everywhere_checker syntax fixes (#18987)
* https_everywhere_checker: Fix regex syntax Fixes #18985 * check_rules.py: Fix unclosed files Fixes #18986
1 parent e393fcc commit c1e138b

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

test/rules/src/https_everywhere_checker/check_rules.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def disableRuleset(ruleset, problemRules, urlCount):
237237
disableMessage = "The following targets have been disabled at {}:\n".format(timestamp)
238238
host = urllib.parse.urlparse(rule)
239239
logging.info("Disabling target {}".format(host.netloc))
240-
contents = re.sub('<[ \n]*target[ \n]+host[ \n]*=[ \n]*"{}"[ \n]*\/?[ \n]*>'.format(host.netloc),
240+
contents = re.sub('<[ \n]*target[ \n]+host[ \n]*=[ \n]*"{}"[ \n]*/?[ \n]*>'.format(host.netloc),
241241
'<!-- target host="{}" /-->'.format(host.netloc), contents);
242242

243243
# Since the problems are going to be inserted into an XML comment, they cannot
@@ -266,7 +266,8 @@ def disableRuleset(ruleset, problemRules, urlCount):
266266

267267
def skipFile(filename):
268268
hasher = hashlib.new('sha256')
269-
hasher.update(open(filename, 'rb').read())
269+
with open(filename, 'rb') as f:
270+
hasher.update(f.read())
270271
if hasher.digest() in skipdict:
271272
return True
272273
else:
@@ -388,7 +389,8 @@ def cli():
388389
"Skipping rule file '{}', matches skiplist.".format(xmlFname))
389390
continue
390391

391-
ruleset = Ruleset(etree.parse(open(xmlFname, "rb")).getroot(), xmlFname)
392+
with open(xmlFname, "rb") as f:
393+
ruleset = Ruleset(etree.parse(f).getroot(), xmlFname)
392394
if ruleset.defaultOff and not includeDefaultOff:
393395
logging.debug("Skipping rule '{}', reason: {}".format(
394396
ruleset.name, ruleset.defaultOff))

test/rules/src/https_everywhere_checker/rules.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,18 @@ def getCoverageProblems(self):
324324
# According to the logic in rules.js available at
325325
# EFForg/https-everywhere/blob/07fe9bd51456cc963c2d99e327f3183e032374ee/chromium/rules.js#L404
326326
#
327-
pattern = target.replace('.', '\.') # .replace('*', '.+')
327+
pattern = target.replace('.', r'\.') # .replace('*', '.+')
328328

329329
# `*.example.com` matches `bar.example.com` and `foo.bar.example.com` etc.
330330
if pattern[0] == '*':
331331
pattern = pattern.replace('*', '.+')
332332

333333
# however, `example.*` match `example.com` but not `example.co.uk`
334334
if pattern[-1] == '*':
335-
pattern = pattern.replace('*', '[^\.]+')
335+
pattern = pattern.replace('*', '[^.]+')
336336

337337
# `www.*.example.com` match `www.image.example.com` but not `www.ssl.image.example.com`
338-
pattern = pattern.replace('*', '[^\.]+')
338+
pattern = pattern.replace('*', '[^.]+')
339339

340340
pattern = '^' + pattern + '$'
341341

@@ -356,15 +356,15 @@ def getCoverageProblems(self):
356356
# Don't treat the question mark in non-capturing and lookahead groups as increasing the
357357
# number of required tests.
358358
needed_count = needed_count - \
359-
len(regex.findall("\(\?:", rule.fromPattern))
359+
len(regex.findall(r"\(\?:", rule.fromPattern))
360360
needed_count = needed_count - \
361-
len(regex.findall("\(\?!", rule.fromPattern))
361+
len(regex.findall(r"\(\?!", rule.fromPattern))
362362
needed_count = needed_count - \
363-
len(regex.findall("\(\?=", rule.fromPattern))
363+
len(regex.findall(r"\(\?=", rule.fromPattern))
364364
# Don't treat escaped questions marks as increasing the number of required
365365
# tests.
366366
needed_count = needed_count - \
367-
len(regex.findall("\\?", rule.fromPattern))
367+
len(regex.findall(r"\?", rule.fromPattern))
368368
actual_count = len(rule.tests)
369369
if actual_count < needed_count:
370370
problems.append("{}: Not enough tests ({} vs {}) for {}".format(
@@ -374,9 +374,9 @@ def getCoverageProblems(self):
374374
needed_count = 1 + \
375375
len(regex.findall("[+*?|]", exclusion.exclusionPattern))
376376
needed_count = needed_count - \
377-
len(regex.findall("\(\?:", exclusion.exclusionPattern))
377+
len(regex.findall(r"\(\?:", exclusion.exclusionPattern))
378378
needed_count = needed_count - \
379-
len(regex.findall("\\?", rule.fromPattern))
379+
len(regex.findall(r"\?", rule.fromPattern))
380380
actual_count = len(exclusion.tests)
381381
if actual_count < needed_count:
382382
problems.append("{}: Not enough tests ({} vs {}) for {}".format(

0 commit comments

Comments
 (0)