|
5 | 5 | from django.urls import reverse |
6 | 6 | from django.urls.exceptions import NoReverseMatch |
7 | 7 |
|
8 | | -from pontoon.base.utils import get_m2m_changes, get_search_phrases |
| 8 | +from pontoon.base.utils import get_m2m_changes, get_search_phrases, sanitize_html |
9 | 9 | from pontoon.test.factories import ( |
10 | 10 | LocaleCodeHistoryFactory, |
11 | 11 | LocaleFactory, |
@@ -585,3 +585,40 @@ def test_get_m2m_mixed(user_a, user_b, user_c): |
585 | 585 | ) |
586 | 586 | def test_get_search_phrases(search_query, expected_results): |
587 | 587 | assert get_search_phrases(search_query) == expected_results |
| 588 | + |
| 589 | + |
| 590 | +@pytest.mark.parametrize( |
| 591 | + "html,expected", |
| 592 | + ( |
| 593 | + ("", ""), |
| 594 | + ("plain text", "plain text"), |
| 595 | + # Allowed tags and attributes are kept |
| 596 | + ( |
| 597 | + '<p>Hello <b>bold</b> <a href="https://mozilla.org" target="_blank" ' |
| 598 | + 'title="t">link</a></p>', |
| 599 | + '<p>Hello <b>bold</b> <a href="https://mozilla.org" target="_blank" ' |
| 600 | + 'title="t">link</a></p>', |
| 601 | + ), |
| 602 | + ("<ul><li>one</li></ul><br>", "<ul><li>one</li></ul><br>"), |
| 603 | + # Disallowed tags are stripped, but their content is kept |
| 604 | + ('<div class="c">div <em>em</em></div>', "div <em>em</em>"), |
| 605 | + ("<img src=x onerror=alert(1)>text", "text"), |
| 606 | + # Dangerous content is dropped entirely |
| 607 | + ("a<script>alert(1)</script>b", "ab"), |
| 608 | + # Disallowed attributes and URL schemes are removed |
| 609 | + ( |
| 610 | + '<a href="https://mozilla.org" onclick="x()">l</a>', |
| 611 | + '<a href="https://mozilla.org">l</a>', |
| 612 | + ), |
| 613 | + ('<a href="javascript:alert(1)">l</a>', "<a>l</a>"), |
| 614 | + ( |
| 615 | + '<a href="mailto:a@example.com">m</a>', |
| 616 | + '<a href="mailto:a@example.com">m</a>', |
| 617 | + ), |
| 618 | + ('<p style="color:red">styled</p>', "<p>styled</p>"), |
| 619 | + # Text is escaped |
| 620 | + ("Tom & Jerry <3", "Tom & Jerry <3"), |
| 621 | + ), |
| 622 | +) |
| 623 | +def test_sanitize_html(html, expected): |
| 624 | + assert sanitize_html(html) == expected |
0 commit comments