"""HTTP endpoint for uploading House Plan manual files. Files (PDF and the like) are uploaded not over WebSocket (its message size limit breaks the connection on a large PDF) but via a plain multipart POST — like media in HA itself. """ from __future__ import annotations import logging import os import tempfile from pathlib import Path from aiohttp import web from homeassistant.components.http import HomeAssistantView try: # KEY_HASS — the modern way to access hass from the aiohttp application from homeassistant.components.http import KEY_HASS except ImportError: # older HA versions KEY_HASS = "hass" # type: ignore[assignment] from homeassistant.core import HomeAssistant from .const import CONF_ADMIN_ONLY, CONTENT_URL, FILES_DIR, FILES_URL, PLANS_DIR from .auth import may_write from .plans import TMP_PREFIX, reserve_filename from .validation import ( FILE_EXTENSIONS, MAX_FILE_BYTES, file_ext, sanitize_filename, sanitize_marker_id, ) _LOGGER = logging.getLogger(__name__) _CHUNK = 64 * 1024 # batch disk writes: one executor job per megabyte instead of per chunk _FLUSH_AT = 1024 * 1024 _MIME = { ".pdf": "application/pdf", ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".svg": "image/svg+xml", ".webp": "image/webp", ".gif": "image/gif", ".txt": "text/plain", } class HouseplanContentView(HomeAssistantView): """Authenticated read access to plans and marker files (audit B1). The directories used to be exposed as unauthenticated static paths, so anyone who could reach the HA endpoint could pull floor plans and uploaded manuals without logging in. This view keeps the same URLs but requires a Home Assistant session (or a signed path, which the frontend uses for inside the SVG). """ url = "/api/houseplan/content/{kind}/{sub}/{name}" name = "api:houseplan:content" requires_auth = True async def get(self, request: web.Request, kind: str, sub: str, name: str) -> web.StreamResponse: hass: HomeAssistant = request.app[KEY_HASS] if kind not in ("plans", "files"): return web.Response(status=404) safe_sub = sanitize_marker_id(sub) safe_name = sanitize_filename(name) if not safe_sub or not safe_name: return web.Response(status=404) base = Path(hass.config.path(PLANS_DIR if kind == "plans" else FILES_DIR)).resolve() # plans live flat in one directory: the sub segment is a placeholder ("_") path = (base / safe_name if kind == "plans" else base / safe_sub / safe_name).resolve() # defence in depth: the sanitizers already strip separators if not str(path).startswith(str(base)): return web.Response(status=404) if not await hass.async_add_executor_job(path.is_file): return web.Response(status=404) suffix = path.suffix.lower() headers = { "Cache-Control": "private, max-age=3600", "Content-Type": _MIME.get(suffix, "application/octet-stream"), } if suffix == ".svg": # An uploaded SVG is user content served from Home Assistant's own # origin. Inside the card it is referenced by , where scripts # never run — but the same url opened as a top-level document is a # live document of this origin, and a