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

When host_whitelist is empty, don't bother parsing the URLs in allow_embedded_url #18

Merged
merged 2 commits into from
Oct 9, 2024
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
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ lxml_html_clean changelog
Unreleased
==========

0.3.1 (2024-10-09)
==================

Features added
--------------

* Do not parse URL addresses when it is not necessary.

0.3.0 (2024-10-09)
==================

Expand Down
2 changes: 2 additions & 0 deletions lxml_html_clean/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ def allow_embedded_url(self, el, url):
"""
if self.whitelist_tags is not None and el.tag not in self.whitelist_tags:
return False
if not self.host_whitelist:
return False
parts = urlsplit(url)
if parts.scheme not in ('http', 'https'):
return False
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = lxml_html_clean
version = 0.3.0
version = 0.3.1
description = HTML cleaner from lxml project
long_description = file:README.md
long_description_content_type = text/markdown
Expand Down
11 changes: 10 additions & 1 deletion tests/test_clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_memory_usage_many_elements_with_long_tails(self):

self.assertTrue(mem < 10, f"Used {mem} MiB memory, expected at most 10 MiB")

def test_possibly_invalid_url(self):
def test_possibly_invalid_url_with_whitelist(self):
cleaner = Cleaner(host_whitelist=['google.com'])
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
Expand All @@ -359,3 +359,12 @@ def test_possibly_invalid_url(self):
self.assertIn("impossible to parse the hostname", str(w[-1].message))
self.assertNotIn("google.com", result)
self.assertNotIn("example.com", result)

def test_possibly_invalid_url_without_whitelist(self):
cleaner = Cleaner()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
result = cleaner.clean_html(r"<p><iframe src='http://example.com:\@google.com'> </iframe></p>")
self.assertEqual(len(w), 0)
self.assertNotIn("google.com", result)
self.assertNotIn("example.com", result)
Loading