-
Notifications
You must be signed in to change notification settings - Fork 18
/
trivial-validate
executable file
·184 lines (164 loc) · 3.63 KB
/
trivial-validate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/sh
# This is a shell script that looks for common problems and typos in rules.
set -o errexit -o noclobber -o nounset
[ -n "$1" ] && cd "$1"
if [ "$(echo *.xml)" = '*.xml' ]
then
echo There are no readable XML files in the current or specified directory.
echo Run this script inside a directory containing XML rule files or else
echo specify such a directory on the command line. E.g.,
echo
echo "$0" src/chrome/content/rules
exit 2
fi
echo "-- Rules not anchored to beginning of a line:"
if grep from= *.xml | cut -d\" -f2 | grep '^[^^]'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules with unescaped dots outside of brackets and left of a slash:"
if grep from= *.xml | cut -d\" -f2 | sed 's/\[[^]]*\]//g' | egrep 'http.?.?://[^/]*[^\]\.[^*]'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules with backslashes in to pattern:"
if grep 'to="' *xml | sed 's/^.*to="//' | sed 's/\".*$//' | grep '\\'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules not containing trailing slash in from pattern:"
if grep from= *.xml | cut -d\" -f2 | grep -v '//.*/'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules not containing trailing slash in to pattern:"
if grep 'to="' *xml | sed 's/^.*to="//' | sed 's/\".*$//' | grep -v '//.*/'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules with missing closing slash in rule XML tag:"
if grep to= *xml | grep '[^/]>'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules with multiple wildcards in a single target rule:"
if grep 'target host="' *xml | grep '\*.*\*'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules with targets containing URLs/paths instead of hostnames:"
if grep 'target host="' *xml | egrep '="[^"]*/'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules redirecting to http in to pattern:"
if grep 'to="' *xml | sed 's/^.*to="//' | sed 's/\".*$//' | grep '^http:'
then
fail=1
else
echo "(None.)"
fi
echo
echo "-- Rules containing space before to pattern:"
if grep 'to="' *xml | sed 's/^.*to="//' | grep '^ '
then
fail=1
else
echo "(None.)"
fi
none=true
echo
echo "-- Rules with syntactically invalid regular expressions:"
for f in *.xml
do
grep from= "$f" | cut -d\" -f2 | egrep -f - 2>/dev/null || {
if [ $? -eq 2 ]
then
echo "$f"
fail=1
none=false
fi
}
done
$none && echo "(None.)"
echo
if which xmllint >/dev/null
then
echo "-- Rules with syntatically invalid XML:"
none=true
for rule in *.xml
do
xmllint "$rule" >/dev/null 2>&1 || { echo $rule; none=false; fail=1; }
done
$none && echo "(None.)"
else
echo "-- Could not check XML validity because xmllint not found."
fi
echo
echo "-- Rules containing non-ASCII characters (possible homoglyph attacks):"
none=true
for i in *.xml
do
if egrep '(from|to)=' "$i" | tr -d '[:print:][:space:]' | grep . >/dev/null
then
echo "$i contains non-ASCII character(s)."
none=false
#fail=1
echo "(not exiting)"
fi
done
$none && echo "(None.)"
echo
echo "--- Rules that lack at least one valid <target> tag:"
none=true
for i in *.xml
do
if ! egrep '<target .*host=".*/>' "$i" >/dev/null
then
echo $i
none=false
fail=1
fi
done
$none && echo "(None.)"
echo
echo "--- Duplicated ruleset names:"
if cat *.xml | grep 'ruleset name' | cut -d\" -f2 | sort | uniq -d | grep .
then
fail=1
else
echo "(None.)"
fi
echo
echo "--- Duplicated target hosts:"
if cat *.xml | grep '<target host=' | cut -d\" -f2 | sort | uniq -d | grep .
then
echo "(not exiting)"
else
echo "(None.)"
fi
exit ${fail-0}