fix(history): reject invalid inspection targets (#209)

This commit is contained in:
Vito Sansevero
2026-10-18 11:45:09 -07:00
parent aa7741000a
commit b070d5455f
4 changed files with 227 additions and 29 deletions
+9 -22
View File
@@ -20,7 +20,13 @@ from PIL import Image, UnidentifiedImageError
from .prompts import bad_request, parse_page_params, publish_image_paths
from .prompts import thumbnail_url_for
from .prompts import safe_error_message as _safe_error
from .inspection_service import CurrentFileChanged, inspect_current_file
from .inspection_service import (
CurrentFileChanged,
IMAGE_EXTENSIONS as _IMAGE_EXTENSIONS,
SERVABLE_EXTENSIONS,
VIDEO_EXTENSIONS,
inspect_current_file,
)
try:
from ...utils.parallel import map_parallel
@@ -29,23 +35,8 @@ except ImportError:
from utils.parallel import map_parallel
from utils.inspection_response import encode_inspection_response
# Only these file types are ever served by the image routes, regardless of
# what sits inside an allowed directory (defence in depth for the gallery).
IMAGE_EXTENSIONS = frozenset(
{
".png",
".jpg",
".jpeg",
".webp",
".gif",
".bmp",
".tiff",
".tif",
".mp4",
".webm",
".mov",
}
)
IMAGE_EXTENSIONS = _IMAGE_EXTENSIONS
def _public_path(path):
@@ -97,11 +88,7 @@ def _media_access_error(path, allowed_dirs):
# Media types the gallery scans and thumbnails (a superset is never served).
GALLERY_IMAGE_EXTENSIONS = frozenset({".png", ".jpg", ".jpeg", ".webp", ".gif"})
VIDEO_EXTENSIONS = frozenset({".mp4", ".webm", ".avi", ".mov", ".mkv", ".m4v", ".wmv"})
MEDIA_EXTENSIONS = GALLERY_IMAGE_EXTENSIONS | VIDEO_EXTENSIONS
# What the serve routes hand out: every image type above plus every video type
# the gallery lists, so a card never points at a 403.
SERVABLE_EXTENSIONS = IMAGE_EXTENSIONS | VIDEO_EXTENSIONS
# Caps on request-triggered filesystem work.
MAX_SCAN_DEPTH = 12
+42 -7
View File
@@ -1,5 +1,7 @@
"""One bounded current-file inspection worker shared by physical identities."""
import os
import stat
from pathlib import Path
try:
@@ -18,23 +20,56 @@ except ImportError:
from utils.video_metadata import is_video_path
# Only these file types are served or inspected within an authorized directory.
IMAGE_EXTENSIONS = frozenset(
{
".png",
".jpg",
".jpeg",
".webp",
".gif",
".bmp",
".tiff",
".tif",
".mp4",
".webm",
".mov",
}
)
VIDEO_EXTENSIONS = frozenset({".mp4", ".webm", ".avi", ".mov", ".mkv", ".m4v", ".wmv"})
SERVABLE_EXTENSIONS = IMAGE_EXTENSIONS | VIDEO_EXTENSIONS
class CurrentFileChanged(Exception):
"""File facts changed across the bounded metadata observation."""
"""File facts are invalid or changed across the bounded metadata observation."""
def _current_file_facts(path: Path) -> os.stat_result:
if path.suffix.lower() not in SERVABLE_EXTENSIONS:
raise CurrentFileChanged
try:
observed = path.stat()
except (FileNotFoundError, NotADirectoryError, PermissionError):
raise CurrentFileChanged from None
if (
not stat.S_ISREG(observed.st_mode)
or not observed.st_mode & 0o444
or not os.access(path, os.R_OK)
):
raise CurrentFileChanged
return observed
def inspect_current_file(
path: Path, *, source_label: str, saved: dict | None, library: dict
) -> dict:
before = path.stat()
before = _current_file_facts(path)
try:
inspection = read_inspection_file(path)
except MetadataReadError:
inspection = None
try:
after = path.stat()
except FileNotFoundError:
raise CurrentFileChanged from None
fields = ("st_dev", "st_ino", "st_size", "st_mtime_ns", "st_ctime_ns")
after = _current_file_facts(path)
fields = ("st_dev", "st_ino", "st_mode", "st_size", "st_mtime_ns", "st_ctime_ns")
if any(getattr(before, key) != getattr(after, key) for key in fields):
raise CurrentFileChanged
if inspection is None: