From 3d6ee99142fbcc7fabaeab770a9c9e9d090ddf0f Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sat, 31 Oct 2026 19:15:57 +0100 Subject: 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. --- webmentions_ssg/tasks/scanner.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'webmentions_ssg/tasks') diff --git a/webmentions_ssg/tasks/scanner.py b/webmentions_ssg/tasks/scanner.py index e45085d..666eba7 100644 --- a/webmentions_ssg/tasks/scanner.py +++ b/webmentions_ssg/tasks/scanner.py @@ -25,6 +25,7 @@ from sqlalchemy.orm import selectinload from .. import DATABASE as db from .. import HUEY as huey +from ..config import normalize_ignored_hostnames from ..models import SentWebmention, Source from ..url_security import is_http_url from .sender import send_webmention @@ -348,9 +349,16 @@ def scan_source_file( Webmention source. :raises OSError: If the source document cannot be read. """ - document = BeautifulSoup(path.read_bytes(), "html5lib") + # Use the path as published for its URL, but read only files whose + # resolved target stays inside the configured source directory. Resolving + # before reading also protects against symlinked index.html files. + resolved_root = root.resolve() + resolved_path = path.resolve() + if not resolved_path.is_relative_to(resolved_root): + raise SourceScanError(f"Source document escapes source directory: {path}") relative_path = path.relative_to(root) + document = BeautifulSoup(resolved_path.read_bytes(), "html5lib") source_url = canonical_url(document, relative_path=relative_path, base_url=base_url) @@ -515,9 +523,8 @@ def scan_sources_task() -> None: raise RuntimeError("SOURCE_BASE_URL must be an absolute HTTP or HTTPS URL") base_url = f"{base_url.rstrip('/')}/" - ignored_hostnames = tuple( - pattern.lower().rstrip(".") - for pattern in current_app.config.get("IGNORED_HOSTNAMES", ()) + ignored_hostnames = normalize_ignored_hostnames( + current_app.config["IGNORED_HOSTNAMES"] ) sources_by_path = { -- cgit v1.3.1