"""Isolated E72 security target, remediated."""
from __future__ import annotations
import shlex
import subprocess
from pathlib import Path

ROOT = (Path(__file__).resolve().parent / "files").resolve()


def run_report(name: str) -> str:
    # fixed: no shell, argv list, bounded input
    safe = "".join(ch for ch in str(name) if ch.isalnum() or ch in " ._-")[:80]
    return subprocess.run(["/bin/echo", "report:", safe], capture_output=True, text=True, timeout=5,
                          check=False).stdout


def read_file(rel: str) -> str:
    # fixed: canonicalise and confine to ROOT
    candidate = (ROOT / rel).resolve()
    if ROOT not in candidate.parents and candidate != ROOT:
        raise PermissionError("path escapes target root")
    return candidate.read_text(errors="replace")


def handle(query: dict) -> str:
    if "file" in query:
        return read_file(str(query["file"]))
    return run_report(str(query.get("name", "")))
