aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/tasks/test_scanner.py74
-rw-r--r--tests/test_config.py34
2 files changed, 108 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:
diff --git a/tests/test_config.py b/tests/test_config.py
index 933f437..aae7cce 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -75,6 +75,7 @@ def test_endpoint_limits_are_inherited_from_defaults() -> None:
assert config.REQUEST_TOTAL_TIMEOUT == 30.0
assert DefaultConfig.ALLOWED_HOSTNAMES == set()
+ assert DefaultConfig.IGNORED_HOSTNAMES == ()
def test_allowed_hostnames_are_normalized_at_startup(tmp_path: Path) -> None:
@@ -124,6 +125,39 @@ def test_invalid_allowed_hostnames_fail_at_startup(
create_app(InvalidHostnameConfig)
+def test_ignored_hostnames_are_normalized_at_startup(tmp_path: Path) -> None:
+ class MixedCaseConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "test.log")
+ IGNORED_HOSTNAMES = [" Example.COM. ", "*.EXAMPLE.com.", "example.com"]
+
+ app = create_app(MixedCaseConfig)
+ assert app.config["IGNORED_HOSTNAMES"] == ("*.example.com", "example.com")
+
+
+@pytest.mark.parametrize(
+ "hostnames,expected_exception",
+ [
+ ("example.com", TypeError),
+ (None, TypeError),
+ ({"example.com": True}, TypeError),
+ (123, TypeError),
+ (["example.com", None], ValueError),
+ (["example.com", ""], ValueError),
+ (["example.com", " "], ValueError),
+ (["example.com", "..."], ValueError),
+ ],
+)
+def test_invalid_ignored_hostnames_fail_at_startup(
+ tmp_path: Path, hostnames: object, expected_exception: type[Exception]
+) -> None:
+ class InvalidHostnameConfig(TestingConfig):
+ LOG_FILE = str(tmp_path / "test.log")
+ IGNORED_HOSTNAMES = hostnames
+
+ with pytest.raises(expected_exception, match="IGNORED_HOSTNAMES"):
+ create_app(InvalidHostnameConfig)
+
+
def test_production_requires_nonempty_allowed_hostnames(tmp_path: Path) -> None:
config_path = tmp_path / "production.cfg"
config_path.write_text(