Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Allow multiple inlined image data links in html clean #5

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
2 changes: 1 addition & 1 deletion lxml_html_clean/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
# All kinds of schemes besides just javascript: that can cause
# execution:
_find_image_dataurls = re.compile(
r'data:image/(.+);base64,', re.I).findall
r'data:image/(.+?);base64,', re.I).findall
_possibly_malicious_schemes = re.compile(
r'(javascript|jscript|livescript|vbscript|data|about|mocha):',
re.I).findall
Expand Down
25 changes: 25 additions & 0 deletions tests/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,31 @@ def test_image_data_links_in_style(self):
cleaned,
"%s -> %s" % (url, cleaned))

def test_image_data_links_in_inline_style(self):
safe_attrs = set(lxml.html.defs.safe_attrs)
safe_attrs.add('style')

cleaner = Cleaner(
safe_attrs_only=True,
safe_attrs=safe_attrs)

data = b'123'
data_b64 = base64.b64encode(data).decode('ASCII')
url = "url(data:image/jpeg;base64,%s)" % data_b64
styles = [
"background: %s" % url,
"background: %s; background-image: %s" % (url, url),
]
for style in styles:
html = '<div style="%s"></div>' % style
s = lxml.html.fragment_fromstring(html)

cleaned = lxml.html.tostring(cleaner.clean_html(s))
self.assertEqual(
html.encode("UTF-8"),
cleaned,
"%s -> %s" % (style, cleaned))

def test_formaction_attribute_in_button_input(self):
# The formaction attribute overrides the form's action and should be
# treated as a malicious link attribute
Expand Down
Loading