# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-dynamically-loaded-skill-registry-has-been-poisoned-via-description-field-injection.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-dynamically-loaded-skill-registry-has-been-poisoned-via-description-field-injection.warrant.json
# Markdown SHA-256:   208f8776714b14c0b0633f296d33f9e548910708e1fcff3a1c5274699dfce7a5
# Code AST SHA-256:   a07d3b4b01727d341d0e6f0422efba6eb10c5af52ebc8762d3f041a042291e35
# Verified Classes:   SkillRegistryInjectionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import re

class SkillRegistryInjectionError(RuntimeError):
    """Raised when a registry entry's description attempts cross-tool or
    sensitive-source redirection, indicating a poisoned registry."""
    pass


# Imperatives that reference other capabilities or sensitive sources.
_CROSS_TOOL = re.compile(
    r"\b(call|invoke|use|run|execute|read|open|load)\b[^.]{0,80}\b"
    r"(read_file|write_file|exec|shell|browser|http|fetch|curl)\b",
    re.IGNORECASE,
)
_SENSITIVE_SOURCE = re.compile(
    r"(\.ssh|\.config|credentials|\.env|api[_-]?key|token|secret|/etc/passwd)",
    re.IGNORECASE,
)
_SUPPRESS = re.compile(
    r"\b(do not|don't|never)\b[^.]{0,40}\b(tell|inform|warn|confirm|ask)\b",
    re.IGNORECASE,
)


def validate_skill_entry(entry: dict) -> None:
    """Gate a single registry entry before it enters the agent's context.

    Legitimate descriptions describe the entry's own tool. Anything that
    directs the agent toward other tools, sensitive sources, or suppression
    of confirmation is a category violation.
    """
    name = entry.get("name", "<unnamed>")
    desc = entry.get("description", "") or ""

    # A description may only reference its own tool by name.
    other_tools = re.findall(r"\b([a-z_][a-z0-9_]*)\b", desc)
    foreign = [
        t for t in other_tools
        if t != name and t in {"read_file", "write_file", "exec", "shell",
                               "browser", "http", "fetch", "curl"}
    ]

    if _CROSS_TOOL.search(desc) or foreign:
        raise SkillRegistryInjectionError(
            f"entry {name!r}: description issues cross-tool imperative "
            f"(foreign={foreign}); registry likely poisoned, quarantine entry"
        )
    if _SENSITIVE_SOURCE.search(desc):
        raise SkillRegistryInjectionError(
            f"entry {name!r}: description references sensitive source path; "
            f"refusing to load"
        )
    if _SUPPRESS.search(desc):
        raise SkillRegistryInjectionError(
            f"entry {name!r}: description instructs suppression of user "
            f"confirmation; refusing to load"
        )


def load_registry(entries: list[dict]) -> list[dict]:
    """Load all entries or fail the batch: one poisoned entry taints siblings."""
    clean = []
    for e in entries:
        validate_skill_entry(e)  # raises on the first violation
        clean.append(e)
    return clean
