|
| 1 | +"""Only module-level settings configure Django. |
| 2 | +
|
| 3 | +The request context processor is taken as active only from the settings a project |
| 4 | +assigns at module level: a ``TEMPLATES`` assigned inside a function, called or not, |
| 5 | +binds a local name and configures nothing, and so does a class attribute. A function |
| 6 | +that reads or rebinds the module's ``TEMPLATES`` (through ``global``, or by mutating |
| 7 | +it) may change the effective settings, so it leaves the activation uncertain, as any |
| 8 | +other mention does. Uncertain activation leaves the filter reachable, neither |
| 9 | +exploitable through the processor nor declared absent. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from coretrace_python import engine |
| 19 | +from coretrace_python.dependency import Advisory, AdvisoryEntryPoint, dump_advisories |
| 20 | +from coretrace_python.findings import Severity |
| 21 | +from coretrace_python.semantic.symbols import SymbolId |
| 22 | + |
| 23 | +STRIPTAGS = "python.django.template.defaultfilters.striptags" |
| 24 | +ADVISORY = Advisory( |
| 25 | + "CVE-2099-5601", |
| 26 | + "django", |
| 27 | + "<4.2.17", |
| 28 | + "striptags is quadratic in its input", |
| 29 | + Severity.MEDIUM, |
| 30 | + entry_points=(AdvisoryEntryPoint(SymbolId(STRIPTAGS), "a template filter"),), |
| 31 | +) |
| 32 | +LOCK = """version = 1 |
| 33 | +
|
| 34 | +[[package]] |
| 35 | +name = "app" |
| 36 | +version = "0.1.0" |
| 37 | +source = { virtual = "." } |
| 38 | +dependencies = [{ name = "django" }] |
| 39 | +
|
| 40 | +[[package]] |
| 41 | +name = "django" |
| 42 | +version = "4.2.16" |
| 43 | +source = { registry = "https://pypi.org/simple" } |
| 44 | +""" |
| 45 | +ENGINE = ( |
| 46 | + "{'BACKEND': 'django.template.backends.django.DjangoTemplates', 'APP_DIRS': True, " |
| 47 | + "'OPTIONS': {'context_processors': ['django.template.context_processors.request']}}" |
| 48 | +) |
| 49 | +VIEW = ( |
| 50 | + "from django.http import HttpRequest\n" |
| 51 | + "from django.shortcuts import render\n\n" |
| 52 | + "def search(request: HttpRequest):\n" |
| 53 | + " return render(request, 'app/search.html')\n" |
| 54 | +) |
| 55 | +PAGE = "app/templates/app/search.html" |
| 56 | + |
| 57 | + |
| 58 | +def levels(root: Path, settings: str) -> list[str]: |
| 59 | + files = { |
| 60 | + "uv.lock": LOCK, |
| 61 | + "app/__init__.py": "", |
| 62 | + "app/settings.py": settings, |
| 63 | + "app/views.py": VIEW, |
| 64 | + PAGE: "{{ request.GET.q|striptags }}\n", |
| 65 | + } |
| 66 | + for relative, text in files.items(): |
| 67 | + (root / relative).parent.mkdir(parents=True, exist_ok=True) |
| 68 | + (root / relative).write_text(text, encoding="utf-8") |
| 69 | + (root / "advisories.json").write_text(dump_advisories((ADVISORY,)), encoding="utf-8") |
| 70 | + findings = engine.analyze_project(root, [engine.BUNDLED_PLUGINS]).findings |
| 71 | + reached = ("reachable-vulnerability", "exploitable-vulnerability") |
| 72 | + return sorted(f.metadata["level"] for f in findings if f.rule_id in reached and f.metadata["advisory"] == ADVISORY.id) |
| 73 | + |
| 74 | + |
| 75 | +def test_module_level_settings_activate_the_processor(tmp_path: Path) -> None: |
| 76 | + assert levels(tmp_path, f"TEMPLATES = [{ENGINE}]\n") == ["exploitable", "reachable"] |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "settings", |
| 81 | + [ |
| 82 | + f"def never_called():\n TEMPLATES = [{ENGINE}]\n", |
| 83 | + f"def configure():\n TEMPLATES = [{ENGINE}]\n return TEMPLATES\n\nconfigure()\n", |
| 84 | + f"class Settings:\n TEMPLATES = [{ENGINE}]\n", |
| 85 | + f"def build():\n templates = [{ENGINE}]\n return templates\n", |
| 86 | + ], |
| 87 | + ids=["uncalled-function", "called-function", "class-attribute", "other-local-name"], |
| 88 | +) |
| 89 | +def test_settings_bound_below_the_module_do_not_configure_django(tmp_path: Path, settings: str) -> None: |
| 90 | + assert levels(tmp_path, settings) == ["reachable"] |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "settings", |
| 95 | + [ |
| 96 | + f"TEMPLATES = [{ENGINE}]\n\ndef tweak():\n TEMPLATES[0]['OPTIONS']['context_processors'].pop(0)\n", |
| 97 | + f"TEMPLATES = [{ENGINE}]\n\ndef reset():\n global TEMPLATES\n TEMPLATES = []\n", |
| 98 | + f"TEMPLATES = [{ENGINE}]\n\ndef read():\n return TEMPLATES\n", |
| 99 | + ], |
| 100 | + ids=["mutated-in-a-function", "rebound-through-global", "read-in-a-function"], |
| 101 | +) |
| 102 | +def test_a_function_touching_the_module_settings_leaves_activation_uncertain(tmp_path: Path, settings: str) -> None: |
| 103 | + assert levels(tmp_path, settings) == ["reachable"] |
| 104 | + |
| 105 | + |
| 106 | +def test_a_local_name_beside_module_settings_changes_nothing(tmp_path: Path) -> None: |
| 107 | + settings = f"TEMPLATES = [{ENGINE}]\n\ndef helper():\n TEMPLATES = []\n return TEMPLATES\n" |
| 108 | + |
| 109 | + assert levels(tmp_path, settings) == ["exploitable", "reachable"] |
0 commit comments