Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions test/rules/src/https_everywhere_checker/check_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def disableRuleset(ruleset, problemRules, urlCount):
disableMessage = "The following targets have been disabled at {}:\n".format(timestamp)
host = urllib.parse.urlparse(rule)
logging.info("Disabling target {}".format(host.netloc))
contents = re.sub('<[ \n]*target[ \n]+host[ \n]*=[ \n]*"{}"[ \n]*\/?[ \n]*>'.format(host.netloc),
contents = re.sub('<[ \n]*target[ \n]+host[ \n]*=[ \n]*"{}"[ \n]*/?[ \n]*>'.format(host.netloc),
'<!-- target host="{}" /-->'.format(host.netloc), contents);

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

def skipFile(filename):
hasher = hashlib.new('sha256')
hasher.update(open(filename, 'rb').read())
with open(filename, 'rb') as f:
hasher.update(f.read())
if hasher.digest() in skipdict:
return True
else:
Expand Down Expand Up @@ -388,7 +389,8 @@ def cli():
"Skipping rule file '{}', matches skiplist.".format(xmlFname))
continue

ruleset = Ruleset(etree.parse(open(xmlFname, "rb")).getroot(), xmlFname)
with open(xmlFname, "rb") as f:
ruleset = Ruleset(etree.parse(f).getroot(), xmlFname)
if ruleset.defaultOff and not includeDefaultOff:
logging.debug("Skipping rule '{}', reason: {}".format(
ruleset.name, ruleset.defaultOff))
Expand Down
18 changes: 9 additions & 9 deletions test/rules/src/https_everywhere_checker/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,18 +324,18 @@ def getCoverageProblems(self):
# According to the logic in rules.js available at
# EFForg/https-everywhere/blob/07fe9bd51456cc963c2d99e327f3183e032374ee/chromium/rules.js#L404
#
pattern = target.replace('.', '\.') # .replace('*', '.+')
pattern = target.replace('.', r'\.') # .replace('*', '.+')

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

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

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

pattern = '^' + pattern + '$'

Expand All @@ -356,15 +356,15 @@ def getCoverageProblems(self):
# Don't treat the question mark in non-capturing and lookahead groups as increasing the
# number of required tests.
needed_count = needed_count - \
len(regex.findall("\(\?:", rule.fromPattern))
len(regex.findall(r"\(\?:", rule.fromPattern))
needed_count = needed_count - \
len(regex.findall("\(\?!", rule.fromPattern))
len(regex.findall(r"\(\?!", rule.fromPattern))
needed_count = needed_count - \
len(regex.findall("\(\?=", rule.fromPattern))
len(regex.findall(r"\(\?=", rule.fromPattern))
# Don't treat escaped questions marks as increasing the number of required
# tests.
needed_count = needed_count - \
len(regex.findall("\\?", rule.fromPattern))
len(regex.findall(r"\?", rule.fromPattern))
actual_count = len(rule.tests)
if actual_count < needed_count:
problems.append("{}: Not enough tests ({} vs {}) for {}".format(
Expand All @@ -374,9 +374,9 @@ def getCoverageProblems(self):
needed_count = 1 + \
len(regex.findall("[+*?|]", exclusion.exclusionPattern))
needed_count = needed_count - \
len(regex.findall("\(\?:", exclusion.exclusionPattern))
len(regex.findall(r"\(\?:", exclusion.exclusionPattern))
needed_count = needed_count - \
len(regex.findall("\\?", rule.fromPattern))
len(regex.findall(r"\?", rule.fromPattern))
actual_count = len(exclusion.tests)
if actual_count < needed_count:
problems.append("{}: Not enough tests ({} vs {}) for {}".format(
Expand Down