|
|
7b0f79 |
diff --git a/src/lxml/html/clean.py b/src/lxml/html/clean.py
|
|
|
7b0f79 |
index adc3f45..6f3f7de 100644
|
|
|
7b0f79 |
--- a/src/lxml/html/clean.py
|
|
|
7b0f79 |
+++ b/src/lxml/html/clean.py
|
|
|
7b0f79 |
@@ -61,12 +61,15 @@ __all__ = ['clean_html', 'clean', 'Cleaner', 'autolink', 'autolink_html',
|
|
|
7b0f79 |
|
|
|
7b0f79 |
# This is an IE-specific construct you can have in a stylesheet to
|
|
|
7b0f79 |
# run some Javascript:
|
|
|
7b0f79 |
-_css_javascript_re = re.compile(
|
|
|
7b0f79 |
- r'expression\s*\(.*?\)', re.S|re.I)
|
|
|
7b0f79 |
+_replace_css_javascript = re.compile(
|
|
|
7b0f79 |
+ r'expression\s*\(.*?\)', re.S|re.I).sub
|
|
|
7b0f79 |
|
|
|
7b0f79 |
# Do I have to worry about @\nimport?
|
|
|
7b0f79 |
-_css_import_re = re.compile(
|
|
|
7b0f79 |
- r'@\s*import', re.I)
|
|
|
7b0f79 |
+_replace_css_import = re.compile(
|
|
|
7b0f79 |
+ r'@\s*import', re.I).sub
|
|
|
7b0f79 |
+
|
|
|
7b0f79 |
+_looks_like_tag_content = re.compile(
|
|
|
7b0f79 |
+ r'</?[a-zA-Z]+|\son[a-zA-Z]+\s*=', re.ASCII).search
|
|
|
7b0f79 |
|
|
|
7b0f79 |
# All kinds of schemes besides just javascript: that can cause
|
|
|
7b0f79 |
# execution:
|
|
|
7b0f79 |
@@ -292,8 +295,8 @@ class Cleaner(object):
|
|
|
7b0f79 |
if not self.inline_style:
|
|
|
7b0f79 |
for el in _find_styled_elements(doc):
|
|
|
7b0f79 |
old = el.get('style')
|
|
|
7b0f79 |
- new = _css_javascript_re.sub('', old)
|
|
|
7b0f79 |
- new = _css_import_re.sub('', new)
|
|
|
7b0f79 |
+ new = _replace_css_javascript('', old)
|
|
|
7b0f79 |
+ new = _replace_css_import('', new)
|
|
|
7b0f79 |
if self._has_sneaky_javascript(new):
|
|
|
7b0f79 |
# Something tricky is going on...
|
|
|
7b0f79 |
del el.attrib['style']
|
|
|
7b0f79 |
@@ -305,9 +308,9 @@ class Cleaner(object):
|
|
|
7b0f79 |
el.drop_tree()
|
|
|
7b0f79 |
continue
|
|
|
7b0f79 |
old = el.text or ''
|
|
|
7b0f79 |
- new = _css_javascript_re.sub('', old)
|
|
|
7b0f79 |
+ new = _replace_css_javascript('', old)
|
|
|
7b0f79 |
# The imported CSS can do anything; we just can't allow:
|
|
|
7b0f79 |
- new = _css_import_re.sub('', old)
|
|
|
7b0f79 |
+ new = _replace_css_import('', new)
|
|
|
7b0f79 |
if self._has_sneaky_javascript(new):
|
|
|
7b0f79 |
# Something tricky is going on...
|
|
|
7b0f79 |
el.text = '/* deleted */'
|
|
|
7b0f79 |
@@ -509,6 +512,12 @@ class Cleaner(object):
|
|
|
7b0f79 |
return True
|
|
|
7b0f79 |
if 'expression(' in style:
|
|
|
7b0f79 |
return True
|
|
|
7b0f79 |
+ if '
|
|
|
7b0f79 |
+ # e.g. '">'
|
|
|
7b0f79 |
+ return True
|
|
|
7b0f79 |
+ if _looks_like_tag_content(style):
|
|
|
7b0f79 |
+ # e.g. '<math><style></style></math>'
|
|
|
7b0f79 |
+ return True
|
|
|
7b0f79 |
return False
|
|
|
7b0f79 |
|
|
|
7b0f79 |
def clean_html(self, html):
|
|
|
7b0f79 |
diff --git a/src/lxml/html/tests/test_clean.py b/src/lxml/html/tests/test_clean.py
|
|
|
7b0f79 |
index 3bcaaf5..451eec2 100644
|
|
|
7b0f79 |
--- a/src/lxml/html/tests/test_clean.py
|
|
|
7b0f79 |
+++ b/src/lxml/html/tests/test_clean.py
|
|
|
7b0f79 |
@@ -69,6 +69,26 @@ class CleanerTest(unittest.TestCase):
|
|
|
7b0f79 |
s = lxml.html.fromstring('<invalid tag>child</another>')
|
|
|
7b0f79 |
self.assertEqual('child', clean_html(s).text_content())
|
|
|
7b0f79 |
|
|
|
7b0f79 |
+ def test_sneaky_noscript_in_style(self):
|
|
|
7b0f79 |
+ # This gets parsed as ..."</style>
|
|
|
7b0f79 |
+ # thus passing the through into the output.
|
|
|
7b0f79 |
+ html = '">'
|
|
|
7b0f79 |
+ s = lxml.html.fragment_fromstring(html)
|
|
|
7b0f79 |
+
|
|
|
7b0f79 |
+ self.assertEqual(
|
|
|
7b0f79 |
+ b'',
|
|
|
7b0f79 |
+ lxml.html.tostring(clean_html(s)))
|
|
|
7b0f79 |
+
|
|
|
7b0f79 |
+ def test_sneaky_js_in_math_style(self):
|
|
|
7b0f79 |
+ # This gets parsed as <math> -> <style>"..."</style>
|
|
|
7b0f79 |
+ # thus passing any tag/script/whatever content through into the output.
|
|
|
7b0f79 |
+ html = '<math><style></style></math>'
|
|
|
7b0f79 |
+ s = lxml.html.fragment_fromstring(html)
|
|
|
7b0f79 |
+
|
|
|
7b0f79 |
+ self.assertEqual(
|
|
|
7b0f79 |
+ b'<math><style>/* deleted */</style></math>',
|
|
|
7b0f79 |
+ lxml.html.tostring(clean_html(s)))
|
|
|
7b0f79 |
+
|
|
|
7b0f79 |
|
|
|
7b0f79 |
def test_suite():
|
|
|
7b0f79 |
suite = unittest.TestSuite()
|
|
|
7b0f79 |
diff --git a/src/lxml/html/tests/test_clean.txt b/src/lxml/html/tests/test_clean.txt
|
|
|
7b0f79 |
index c78ab4f..c901871 100644
|
|
|
7b0f79 |
--- a/src/lxml/html/tests/test_clean.txt
|
|
|
7b0f79 |
+++ b/src/lxml/html/tests/test_clean.txt
|
|
|
7b0f79 |
@@ -104,7 +104,11 @@
|
|
|
7b0f79 |
>>> print(Cleaner(page_structure=False, safe_attrs_only=False).clean_html(doc))
|
|
|
7b0f79 |
<html>
|
|
|
7b0f79 |
<head>
|
|
|
7b0f79 |
- <style>/* deleted */</style>
|
|
|
7b0f79 |
+ <style>
|
|
|
7b0f79 |
+ body {background-image: url()};
|
|
|
7b0f79 |
+ div {background-image: url()};
|
|
|
7b0f79 |
+ div {color: };
|
|
|
7b0f79 |
+ </style>
|
|
|
7b0f79 |
</head>
|
|
|
7b0f79 |
<body>
|
|
|
7b0f79 |
a link
|
|
|
7b0f79 |
@@ -168,7 +172,11 @@
|
|
|
7b0f79 |
<link rel="alternate" type="text/rss" src="evil-rss">
|
|
|
7b0f79 |
<link rel="alternate" type="text/rss" href="http://example.com">
|
|
|
7b0f79 |
<link rel="stylesheet" type="text/rss" href="http://example.com">
|
|
|
7b0f79 |
- <style>/* deleted */</style>
|
|
|
7b0f79 |
+ <style>
|
|
|
7b0f79 |
+ body {background-image: url()};
|
|
|
7b0f79 |
+ div {background-image: url()};
|
|
|
7b0f79 |
+ div {color: };
|
|
|
7b0f79 |
+ </style>
|
|
|
7b0f79 |
</head>
|
|
|
7b0f79 |
<body>
|
|
|
7b0f79 |
a link
|