|
| 1 | +"""Security regression tests for the source-linked briefing example.""" |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import sys |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +EXAMPLES_DIR = Path(__file__).resolve().parent.parent / "examples" |
| 8 | +sys.path.insert(0, str(EXAMPLES_DIR)) |
| 9 | + |
| 10 | +from _example_utils import escape_markdown_text, safe_markdown_url # noqa: E402 |
| 11 | + |
| 12 | +_spec = importlib.util.spec_from_file_location( |
| 13 | + "briefing", EXAMPLES_DIR / "source_linked_briefing" / "briefing.py" |
| 14 | +) |
| 15 | +briefing = importlib.util.module_from_spec(_spec) |
| 16 | +_spec.loader.exec_module(briefing) |
| 17 | + |
| 18 | + |
| 19 | +class TestSafeMarkdownUrl: |
| 20 | + def test_javascript_scheme_rejected(self): |
| 21 | + assert safe_markdown_url("javascript:alert(1)") is None |
| 22 | + |
| 23 | + def test_data_scheme_rejected(self): |
| 24 | + assert safe_markdown_url("data:text/html,<script>alert(1)</script>") is None |
| 25 | + |
| 26 | + def test_scheme_relative_rejected(self): |
| 27 | + assert safe_markdown_url("//attacker.example/x") is None |
| 28 | + |
| 29 | + def test_missing_netloc_rejected(self): |
| 30 | + assert safe_markdown_url("https://") is None |
| 31 | + |
| 32 | + def test_angle_brackets_percent_encoded(self): |
| 33 | + out = safe_markdown_url("https://example.com><script>x</script>") |
| 34 | + assert "<" not in out and ">" not in out |
| 35 | + assert out == "https://example.com%3E%3Cscript%3Ex%3C/script%3E" |
| 36 | + |
| 37 | + def test_parens_percent_encoded(self): |
| 38 | + assert safe_markdown_url("https://example.com/x(1)") == "https://example.com/x%281%29" |
| 39 | + |
| 40 | + def test_normal_url_unchanged(self): |
| 41 | + assert safe_markdown_url("https://example.com/a?b=1&c=2") == "https://example.com/a?b=1&c=2" |
| 42 | + |
| 43 | + def test_non_string_rejected(self): |
| 44 | + assert safe_markdown_url(None) is None |
| 45 | + assert safe_markdown_url(123) is None |
| 46 | + |
| 47 | + |
| 48 | +class TestBriefingOutputSanitization: |
| 49 | + def test_hostile_title_cannot_form_link(self): |
| 50 | + art = { |
| 51 | + "title": "[click here](javascript:alert(1))", |
| 52 | + "description": "", |
| 53 | + "url": "", |
| 54 | + "published": "2026-01-01", |
| 55 | + } |
| 56 | + md, _ = briefing.build_output( |
| 57 | + {"status": "ok", "news": [art]}, generated_at="2026-01-01T00:00:00Z" |
| 58 | + ) |
| 59 | + assert "](javascript:" not in md |
| 60 | + |
| 61 | + def test_hostile_url_cannot_break_out(self): |
| 62 | + art = { |
| 63 | + "title": "t", |
| 64 | + "description": "", |
| 65 | + "url": "https://example.com><script>location.href='//attacker.example'</script>", |
| 66 | + "published": "2026-01-01", |
| 67 | + } |
| 68 | + md, _ = briefing.build_output( |
| 69 | + {"status": "ok", "news": [art]}, generated_at="2026-01-01T00:00:00Z" |
| 70 | + ) |
| 71 | + assert "<script>" not in md |
| 72 | + |
| 73 | + def test_clean_url_rendered(self): |
| 74 | + art = { |
| 75 | + "title": "t", |
| 76 | + "description": "", |
| 77 | + "url": "https://example.com/story", |
| 78 | + "published": "2026-01-01", |
| 79 | + } |
| 80 | + md, _ = briefing.build_output( |
| 81 | + {"status": "ok", "news": [art]}, generated_at="2026-01-01T00:00:00Z" |
| 82 | + ) |
| 83 | + assert "<https://example.com/story>" in md |
| 84 | + |
| 85 | + def test_escape_markdown_text_neutralizes_link_syntax(self): |
| 86 | + escaped = escape_markdown_text("[x](javascript:alert(1))") |
| 87 | + assert "\\[x\\]" in escaped |
| 88 | + assert "\\(javascript:" in escaped |
0 commit comments