Skip to content

Commit b9b193d

Browse files
committed
Restore duplicate host testing to trvial-validate.
1 parent 95db03b commit b9b193d

File tree

2 files changed

+33
-41
lines changed

2 files changed

+33
-41
lines changed

utils/make-sqlite.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def nomes_all(where=sys.argv[1:]):
4949
etree.strip_tags(tree,etree.Comment)
5050

5151
targets = xpath_host(tree)
52+
if not targets:
53+
print 'File %s has no targets' % fi
54+
sys.exit(1)
55+
5256
# Strip out the target tags. These aren't necessary in the DB because
5357
# targets are looked up in the target table, which has a foreign key
5458
# pointing into the ruleset table.

utils/trivial-validate.py

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def warn(s):
3535
def fail(s):
3636
sys.stdout.write("failure: %s\n" % s)
3737

38-
def test_not_anchored(tree, fi, host, fm, to):
38+
def test_not_anchored(tree, fi, fm, to):
3939
# Rules not anchored to the beginning of a line.
4040
"""The 'from' rule is not anchored to beginning of line using the ^ symbol."""
4141
for f in fm:
@@ -47,7 +47,7 @@ def test_not_anchored(tree, fi, host, fm, to):
4747
xpath_exlusion_pattern = etree.XPath("/ruleset/exclusion/@pattern")
4848
xpath_cookie_pattern = etree.XPath("/ruleset/securecookie/@host")
4949

50-
def test_bad_regexp(tree, fi, host, fm, to):
50+
def test_bad_regexp(tree, fi, fm, to):
5151
# Rules with invalid regular expressions.
5252
"""The 'from' rule contains an invalid extended regular expression."""
5353
patterns = fm + xpath_exlusion_pattern(tree) + xpath_cookie_pattern(tree)
@@ -59,7 +59,7 @@ def test_bad_regexp(tree, fi, host, fm, to):
5959
return True
6060

6161
xpath_rule = etree.XPath("/ruleset/rule")
62-
def test_missing_to(tree, fi, host, fm, to):
62+
def test_missing_to(tree, fi, fm, to):
6363
# Rules that are terminated before setting 'to'.
6464
# These cases are probably either due to a misplaced
6565
# rule end or intended to be different elements.
@@ -71,7 +71,7 @@ def test_missing_to(tree, fi, host, fm, to):
7171
return False
7272
return True
7373

74-
def test_unescaped_dots(tree, fi, host, fm, to):
74+
def test_unescaped_dots(tree, fi, fm, to):
7575
# Rules containing unescaped dots outside of brackets and before slash.
7676
# Note: this is meant to require example\.com instead of example.com,
7777
# but it also forbids things like .* which usually ought to be replaced
@@ -96,15 +96,15 @@ def test_unescaped_dots(tree, fi, host, fm, to):
9696
escaped = False
9797
return True
9898

99-
def test_space_in_to(tree, fi, host, fm, to):
99+
def test_space_in_to(tree, fi, fm, to):
100100
# Rules where the to pattern contains a space.
101101
"""The 'to' rule contains a space."""
102102
for t in to:
103103
if ' ' in t:
104104
return False
105105
return True
106106

107-
def test_unencrypted_to(tree, fi, host, fm, to):
107+
def test_unencrypted_to(tree, fi, fm, to):
108108
# Rules that redirect to something other than https or http.
109109
# This used to test for http: but testing for lack of https: will
110110
# catch more kinds of mistakes.
@@ -122,7 +122,7 @@ def test_unencrypted_to(tree, fi, host, fm, to):
122122
return False
123123
return True
124124

125-
def test_backslash_in_to(tree, fi, host, fm, to):
125+
def test_backslash_in_to(tree, fi, fm, to):
126126
# Rules containing backslashes in to pattern.
127127
"""The 'to' rule contains a backslash."""
128128
for t in to:
@@ -132,7 +132,7 @@ def test_backslash_in_to(tree, fi, host, fm, to):
132132

133133
RE_TRAILING_SLASH = re.compile("//.*/")
134134

135-
def test_no_trailing_slash(tree, fi, host, fm, to):
135+
def test_no_trailing_slash(tree, fi, fm, to):
136136
# Rules not containing trailing slash in from or to pattern.
137137
"""Rule omits forward slash after host name."""
138138
for r in xpath_rule(tree):
@@ -143,29 +143,9 @@ def test_no_trailing_slash(tree, fi, host, fm, to):
143143
return False
144144
return True
145145

146-
def test_lacks_target_host(tree, fi, host, fm, to):
147-
# Rules that lack at least one target host (target tag with host attr).
148-
"""Rule fails to specify at least one target host."""
149-
return not not host
150-
151-
def test_bad_target_host(tree, fi, host, fm, to):
152-
# Rules where a target host contains multiple wildcards or a slash.
153-
"""The target host must be a hostname, not URL, and must use at most one wildcard."""
154-
for target in host:
155-
if "/" in target:
156-
return False
157-
if target.count("*") > 1:
158-
return False
159-
return True
160-
161-
def test_duplicated_target_host(tree, fi, host, fm, to):
162-
# Rules where a single target host appears more than once.
163-
"""Rule contains the same target host more than once."""
164-
return len(set(host)) == len(host)
165-
166146
printable_characters = set(map(chr, list(range(32, 127))))
167147

168-
def test_non_ascii(tree, fi, host, fm, to):
148+
def test_non_ascii(tree, fi, fm, to):
169149
# Rules containing non-printable characters.
170150
"""Rule contains non-printable character in 'to' pattern."""
171151
for t in to:
@@ -174,6 +154,15 @@ def test_non_ascii(tree, fi, host, fm, to):
174154
return False
175155
return True
176156

157+
def is_valid_target_host(host):
158+
# Rules where a target host contains multiple wildcards or a slash.
159+
"""The target host must be a hostname, not URL, and must use at most one wildcard."""
160+
if "/" in host:
161+
return False
162+
if host.count("*") > 1:
163+
return False
164+
return True
165+
177166
def nomes_all(where=sys.argv[1:]):
178167
"""Returns generator to extract all files from a list of files/dirs"""
179168
if not where: where=['.']
@@ -193,8 +182,6 @@ def nomes_all(where=sys.argv[1:]):
193182
test_unencrypted_to,
194183
test_backslash_in_to,
195184
test_no_trailing_slash,
196-
test_bad_target_host,
197-
test_duplicated_target_host,
198185
test_non_ascii]
199186

200187
failure = 0
@@ -224,18 +211,19 @@ def nomes_all(where=sys.argv[1:]):
224211
fail("unnamed ruleset")
225212
continue
226213
rf = xpath_ruleset_name(tree)[0]
227-
host = xpath_host(tree)
228214
fm = xpath_from(tree)
229215
to = xpath_to(tree)
230216
for test in tests:
231-
if not test(tree, rf, host=host, fm=fm, to=to):
232-
print("%s failed test: %s" % (rf, test.__doc__))
233-
for target in xpath_host(tree):
234-
print target
235-
if target in all_targets and not any(ign.search(target) for ign in ignoredups):
236-
# suppress warning about duplicate targets if an --ignoredups
237-
# pattern matches target
238-
warn("has duplicate target: %s" % (target))
239-
all_targets.add(target)
217+
if not test(tree, rf, fm=fm, to=to):
218+
failure = 1
219+
fail("%s failed test: %s" % (rf, test.__doc__))
220+
221+
for (host, count) in c.execute('''
222+
select host, count(host) as c from targets group by host;'''):
223+
if count > 1:
224+
warn("Hostname %s shows up in %d different rulesets." % (host, count))
225+
if not is_valid_target_host(host):
226+
failure = 1
227+
fail("%s failed: %s" % (host, is_valid_target_host.__doc__))
240228

241229
sys.exit(failure)

0 commit comments

Comments
 (0)