-
Notifications
You must be signed in to change notification settings - Fork 0
/
bleached.py
301 lines (259 loc) · 8.69 KB
/
bleached.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import re
__version__ = '1.0.0'
__all__ = ['UnsafeInput', 'check_html', 'is_html_bleached']
WHITESPACE = ' \t\n\r\v\f'
"""Those are tags that don't need a closing tag (and can't have one).
They can be seen with an slash at the end (ex: "<img/>") but that is not
required.
Other elements cannot use the ending slash syntax (ex: "<h1/>").
"""
VOID_ELEMENTS = {
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta',
'source', 'track', 'wbr',
}
ELEMENT_CHARS = (
'abcdefghijklmnopqrstuvwxyz' +
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'0123456789' +
'-'
)
ELEMENT_CHARS = set(ELEMENT_CHARS)
ATTRIBUTE_CHARS = ELEMENT_CHARS
HTML_ENTITIES = {
'nbsp': '\x20',
'quot': '"',
'amp': '&',
'apos': "'",
'lt': '<',
'gt': '>',
}
DEFAULT_TAGS = {
'p', 'br', 'code', 'blockquote', 'pre', # formatting
'sub', 'sup', 'caption',
'a', 'img', # non-text
'h1', 'h2', 'h3', 'h4', 'h5', # headers
'strong', 'em', 'b', 'u', 'q', 'del', # emphasis
'ul', 'ol', 'li', 'dl', 'dt', 'dd', # lists
'table', 'thead', 'tbody', 'tr', 'th', 'td', # tables
'colgroup', 'col', # columns
}
DEFAULT_ATTRIBUTES = {
'a': {'href', 'title'},
'img': {'src', 'width', 'height'},
}
class EndOfInput(ValueError):
pass
class UnsafeInput(ValueError):
def __init__(self, message, index, line, line_position):
full_message = "Line %d character %d (input index %d): %s" % (
line,
line_position,
index,
message,
)
super(ValueError, self).__init__(full_message)
self.message = message
"""Index in the string, 0-based."""
self.index = index
"""Line number, 1-based."""
self.line = line
"""Character number in the line, 1-based."""
self.line_position = line_position
def check_html(
source,
*,
tags=DEFAULT_TAGS,
attributes=DEFAULT_ATTRIBUTES,
):
Checker(source, tags=tags, attributes=attributes).check()
def is_html_bleached(
source,
*,
tags=DEFAULT_TAGS,
attributes=DEFAULT_ATTRIBUTES,
):
try:
check_html(source, tags=tags, attributes=attributes)
return True
except UnsafeInput:
return False
class Checker(object):
def __init__(self, source, *, tags, attributes):
self.source = source
self.tags = tags
self.attributes = attributes
self.position = 0
self.line = 1
self.line_position = 1
self.element_stack = []
def consume_char(self):
c = self.peek_char()
self.position += 1
if c == '\n':
self.line_position = 1
self.line += 1
else:
self.line_position += 1
return c
def peek_char(self):
if self.position >= len(self.source):
raise EndOfInput
return self.source[self.position:self.position + 1]
def skip_whitespace(self):
while self.peek_char() in WHITESPACE:
self.consume_char()
def check(self):
try:
while True:
try:
c = self.peek_char()
except EndOfInput:
break
if c == '>':
self.fail("Unexpected '>' character")
elif c == '<':
what, tag_name = self.read_tag()
if what == 'open':
if tag_name not in VOID_ELEMENTS:
self.element_stack.append(tag_name)
if len(self.element_stack) >= 1000:
self.fail("Element stack too deep")
elif what == 'close':
if self.element_stack and tag_name == self.element_stack[-1]:
self.element_stack.pop(-1)
else:
self.fail(
"Closing tag for wrong element %r" % tag_name,
)
else:
if tag_name not in VOID_ELEMENTS:
self.fail(
"Self-closing tag for non-void element %r" % (
tag_name,
),
)
elif c == '&':
self.read_entity()
else:
self.consume_char()
except EndOfInput:
self.fail("Unexpected end of input")
if self.element_stack:
self.fail(
"Missing closing tag for element %r" % self.element_stack[-1],
)
def read_entity(self):
"""Reads an HTML entity, e.g. " ".
"""
assert self.consume_char() == '&'
entity = []
while True:
c = self.consume_char()
if c == ';':
break
elif len(entity) >= 10:
self.fail("Entity too long")
else:
entity.append(c)
entity = ''.join(entity).lower()
if entity and entity[0] == '#':
if not re.match('^#[0-9]+$', entity):
self.fail("Invalid numerical entity")
else:
if entity not in HTML_ENTITIES:
self.fail("Unknown HTML entity %r" % entity)
return HTML_ENTITIES[entity]
def read_tag(self):
"""Reads an HTML tag, e.g. either "<h1>" or "</h1>".
This always assumes that tags are terminated, so "<br>" won't
be accepted.
"""
assert self.consume_char() == '<'
self.skip_whitespace()
if self.peek_char() == '/':
closing = True
self.consume_char()
self.skip_whitespace()
else:
closing = False
tag_name = []
while True:
c = self.peek_char()
if c in WHITESPACE or c == '/' or c == '>':
break
elif c in ELEMENT_CHARS:
tag_name.append(self.consume_char())
else:
self.fail("Unexpected character in tag name")
tag_name = ''.join(tag_name).lower()
if tag_name not in self.tags:
self.fail("Found forbidden opening tag %r" % tag_name)
if not closing:
self.read_attributes(tag_name)
self.skip_whitespace()
c = self.consume_char()
if c == '>':
if closing:
return 'close', tag_name
else:
return 'open', tag_name
elif c == '/':
if closing:
self.fail("Saw tag with slashes on both sides")
else:
self.skip_whitespace()
if self.consume_char() != '>':
self.fail("Missing tag close after ending slash")
return 'selfclose', tag_name
else:
self.fail("Unexpected character in tag")
def read_attributes(self, tag_name):
"""Reads the attributes of an HTML tag.
"""
while True:
self.skip_whitespace()
if self.peek_char() in '>/':
break
# Read name
attribute_name = []
while True:
c = self.peek_char()
if c in WHITESPACE or c == '/' or c == '>' or c == '=':
break
elif c in ATTRIBUTE_CHARS:
attribute_name.append(self.consume_char())
else:
self.fail("Unexpected character in attribute")
attribute_name = ''.join(attribute_name).lower()
if attribute_name not in self.attributes.get(tag_name, ()):
self.fail("Forbidden attribute %r in tag %r" % (
attribute_name,
tag_name,
))
self.skip_whitespace()
c = self.consume_char()
if c != '=':
self.fail("Unexpected character %r in attribute" % c)
self.skip_whitespace()
quote = self.consume_char()
if quote != '"' and quote != "'":
self.fail("Missing quote for attribute value")
# Read value
while True:
c = self.peek_char()
if c == quote:
self.consume_char()
break
elif c in '<>':
self.fail("Forbidden character in attribute value")
elif c == '&':
self.read_entity()
else:
self.consume_char()
def fail(self, message):
raise UnsafeInput(
message,
self.position,
self.line,
self.line_position,
)