aboutsummaryrefslogtreecommitdiff
path: root/tests/tasks
diff options
context:
space:
mode:
authorDennis Fink2026-10-31 19:15:57 +0100
committerDennis Fink2026-10-31 19:15:57 +0100
commit3d6ee99142fbcc7fabaeab770a9c9e9d090ddf0f (patch)
tree52a304df07ef7fcdca1f6a139b94af6c23a15558 /tests/tasks
parent0afc76fb78518bf8185c02e548e83e63c8bb0153 (diff)
downloadwebmentions-ssg-3d6ee99142fbcc7fabaeab770a9c9e9d090ddf0f.tar.gz
webmentions-ssg-3d6ee99142fbcc7fabaeab770a9c9e9d090ddf0f.zip
fix(sender): prevent source scanner path escapes
Reject symlinked source files whose resolved targets leave the configured source directory so scans cannot read unintended files from the host filesystem. Normalize and validate ignored hostname patterns at startup and again at scan time to keep scanner filtering predictable.
Diffstat (limited to '')
-rw-r--r--tests/tasks/test_scanner.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/tests/tasks/test_scanner.py b/tests/tasks/test_scanner.py
index 5a1e5f2..26e81c4 100644
--- a/tests/tasks/test_scanner.py
+++ b/tests/tasks/test_scanner.py
@@ -1979,6 +1979,80 @@ def test_unexpected_persistence_error_is_not_suppressed(
run_scan(app, scanner_module)
+def test_scan_source_rejects_symlink_outside_root_before_read(
+ scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch
+) -> None:
+ root = tmp_path / "site"
+ root.mkdir()
+ external = write_post(tmp_path / "outside", "private", "Sensitive content")
+ linked = root / "escaped" / "index.html"
+ linked.parent.mkdir()
+ linked.symlink_to(external)
+
+ read_bytes = Mock(side_effect=AssertionError("External file must not be read"))
+ monkeypatch.setattr(Path, "read_bytes", read_bytes)
+
+ with pytest.raises(
+ scanner_module.SourceScanError, match="escapes source directory"
+ ):
+ scanner_module.scan_source_file(linked, root=root, base_url=None)
+
+ read_bytes.assert_not_called()
+
+
+def test_scan_source_accepts_symlink_within_root(
+ scanner_module: ModuleType, tmp_path: Path
+) -> None:
+ root = tmp_path / "site"
+ root.mkdir()
+ linked = write_post(
+ root, "example", '<a href="https://example.com/target">Target</a>'
+ )
+ shared = root / "shared.html"
+ linked.rename(shared)
+ linked.symlink_to(shared)
+
+ scanned = scanner_module.scan_source_file(linked, root=root, base_url=None)
+
+ assert scanned.path == "example/index.html"
+ assert scanned.url == SOURCE_URL
+ assert scanned.targets == frozenset({"https://example.com/target"})
+
+
+def test_scan_sources_skips_symlink_outside_root(
+ app: Flask,
+ scanner_module: ModuleType,
+ tmp_path: Path,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ root = tmp_path / "site"
+ root.mkdir()
+ write_post(root, "example", "Normal content")
+ external = write_post(tmp_path / "outside", "private", "Outside content")
+ linked = root / "escaped" / "index.html"
+ linked.parent.mkdir()
+ linked.symlink_to(external)
+ configure_scanner(app, root)
+
+ run_scan(app, scanner_module)
+
+ with app.app_context():
+ sources = list(db.session.scalars(sa.select(Source)))
+ assert len(sources) == 1
+ assert sources[0].path == "example/index.html"
+ assert "escapes source directory" in caplog.text
+
+
+def test_scan_sources_rejects_malformed_runtime_ignored_hostnames(
+ app: Flask, scanner_module: ModuleType, tmp_path: Path
+) -> None:
+ configure_scanner(app, tmp_path)
+ app.config["IGNORED_HOSTNAMES"] = "example.com"
+
+ with pytest.raises(TypeError, match="IGNORED_HOSTNAMES"):
+ run_scan(app, scanner_module)
+
+
def test_scan_sources_accepts_valid_base_url(
app: Flask, scanner_module: ModuleType, tmp_path: Path, monkeypatch: MonkeyPatch
) -> None: