|
| 1 | +"""Regression tests for issue #13186 (follow-up to PR #12985's Follow.html fix). |
| 2 | +
|
| 3 | +Templetor's bare `$var` substitution HTML-escapes `<`, `>`, `&`, `"` but not |
| 4 | +whitespace. Before this fix, onboarding_card.html, header_dropdown.html, |
| 5 | +modal_links.html, and title_and_author.html each built the tracking attribute |
| 6 | +as a single pre-formatted "attr=value" string spliced into the tag unquoted -- |
| 7 | +so a value containing a space (e.g. "x onmouseover=alert(1)//") injects a real |
| 8 | +new HTML attribute, because the escaped `"` characters become inert `"` |
| 9 | +text rather than real quote delimiters. The fix places the value inside a |
| 10 | +*static*, template-source quote character instead, which can't be broken out |
| 11 | +of regardless of what the value contains. |
| 12 | +""" |
| 13 | + |
| 14 | +from bs4 import BeautifulSoup |
| 15 | + |
| 16 | +INJECTION_PAYLOAD = "x onmouseover=alert(document.cookie)//" |
| 17 | + |
| 18 | + |
| 19 | +def test_onboarding_card_blocks_attribute_injection(render_template, request_context_fixture): |
| 20 | + request_context_fixture(lang="en") |
| 21 | + html = str( |
| 22 | + render_template( |
| 23 | + "home/onboarding_card", |
| 24 | + "Title", |
| 25 | + "Body", |
| 26 | + "/img.png", |
| 27 | + "/href", |
| 28 | + ol_link_track=INJECTION_PAYLOAD, |
| 29 | + ) |
| 30 | + ) |
| 31 | + a = BeautifulSoup(html, "lxml").find("a") |
| 32 | + assert a.get("onmouseover") is None |
| 33 | + assert a["data-ol-link-track"] == INJECTION_PAYLOAD |
| 34 | + |
| 35 | + |
| 36 | +def test_header_dropdown_singleton_blocks_attribute_injection(render_template, request_context_fixture): |
| 37 | + request_context_fixture(lang="en") |
| 38 | + props = {"name": "test-dropdown", "label": "Test", "links": [{"href": "/x", "track": INJECTION_PAYLOAD}]} |
| 39 | + html = str(render_template("lib/header_dropdown", props, track_prefix="Test")) |
| 40 | + a = BeautifulSoup(html, "lxml").find("a") |
| 41 | + assert a.get("onmouseover") is None |
| 42 | + assert a["data-ol-link-track"] == f"Test|{INJECTION_PAYLOAD}" |
| 43 | + |
| 44 | + |
| 45 | +def test_header_dropdown_menu_item_blocks_attribute_injection(render_template, request_context_fixture): |
| 46 | + request_context_fixture(lang="en") |
| 47 | + props = { |
| 48 | + "name": "test-dropdown", |
| 49 | + "label": "Test", |
| 50 | + "links": [ |
| 51 | + {"href": "/x", "track": INJECTION_PAYLOAD, "text": "Item one"}, |
| 52 | + {"href": "/y", "track": INJECTION_PAYLOAD, "text": "Item two"}, |
| 53 | + ], |
| 54 | + } |
| 55 | + html = str(render_template("lib/header_dropdown", props, track_prefix="Test")) |
| 56 | + links = BeautifulSoup(html, "lxml").find_all("a", href=True) |
| 57 | + menu_links = [a for a in links if a["href"] in ("/x", "/y")] |
| 58 | + assert len(menu_links) == 2 |
| 59 | + for a in menu_links: |
| 60 | + assert a.get("onmouseover") is None |
| 61 | + assert a["data-ol-link-track"] == f"Test|{INJECTION_PAYLOAD}" |
| 62 | + |
| 63 | + |
| 64 | +def test_icon_link_pattern_blocks_attribute_injection(render_template, request_context_fixture): |
| 65 | + """Covers the icon_link() pattern duplicated in modal_links.html and |
| 66 | + title_and_author.html -- see openlibrary/templates/tests/icon_link_pattern_check.html.""" |
| 67 | + request_context_fixture(lang="en") |
| 68 | + html = str(render_template("tests/icon_link_pattern_check", ga_data=INJECTION_PAYLOAD)) |
| 69 | + a = BeautifulSoup(html, "lxml").find("a") |
| 70 | + assert a.get("onmouseover") is None |
| 71 | + assert a["data-ol-link-track"] == INJECTION_PAYLOAD |
0 commit comments