Skip to content

policy

policy

The unified +/- policy line format — one grammar for every tier.

A policy file is one entry per line::

+pypi.org                          # allow a domain
+*.pythonhosted.org                # allow every subdomain
+192.168.1.50:8080                 # allow one host:port
+localhost:8000                    # reach a service on the host's localhost
-telemetry.vendor.com              # deny
+api.anthropic.com  %reason=harness-test %expires=2026-06-09

The leading + (allow) or - (deny) is mandatory. %key=value markers carry optional metadata (reason, expires, from, first/last, hits); # starts a free-text comment that the parser ignores entirely. Blank and comment-only lines are skipped.

%expires is the one marker the shield enforces, on the t10 override tier: a date (2026-06-09) keeps the entry valid through that day, until the host's local midnight that ends it; a full ISO-8601 timestamp (2026-06-09T12:00Z) until that instant (expiry_deadline).

The reserved target localhost is a host-service grant: +localhost:PORT opens the host machine's own localhost:PORT to the container (the loader routes it to the backend host-loopback address, accepted above the deny tiers). It requires an explicit port and the + action.

This module is the single parser for shipped, generated, and authored policy alike. Stdlib-only, so it is cheap to audit and safe to import from anywhere.

Action = Literal['+', '-'] module-attribute

A policy verdict prefix: "+" (allow) or "-" (deny).

LOCALHOST = 'localhost' module-attribute

Reserved target: +localhost:PORT grants the container access to the host's localhost.

EXPIRES_MARKER = 'expires' module-attribute

The %expires= marker key: when a t10 override stops being valid.

REASON_MARKER = 'reason' module-attribute

The %reason= marker key: why a t10 override was granted, in the operator's words.

Recorded, never enforced — a break-glass carries its reason with it, so the tier file says why each hole in the deny exists. Free text rides through marker_value.

__all__ = ['EXPIRES_MARKER', 'LOCALHOST', 'REASON_MARKER', 'Action', 'PolicyEntry', 'domain_targets', 'entry_deadline', 'expiry_deadline', 'ip_targets', 'is_ip', 'localhost_ports', 'marker_value', 'parse_policy', 'render_policy', 'render_target', 'validate_target'] module-attribute

PolicyEntry(action, target, port=None, meta=dict()) dataclass

One parsed policy line.

Attributes:

Name Type Description
action Action

"+" (allow) or "-" (deny).

target str

a domain (optionally *.-prefixed), an IP literal, or a CIDR.

port int | None

the optional :port (None when unspecified).

meta dict[str, str]

the %key=value markers parsed off the line (empty when none).

action instance-attribute

target instance-attribute

port = None class-attribute instance-attribute

meta = field(default_factory=dict) class-attribute instance-attribute

parse_policy(text)

Parse policy text into entries, failing fast on a malformed line.

Raises:

Type Description
ValueError

on a missing +/- prefix, an empty or invalid target, an out-of-range port, or a malformed %key=value marker — annotated with the line number.

Source code in src/terok_shield/policy.py
def parse_policy(text: str) -> list[PolicyEntry]:
    """Parse policy text into entries, failing fast on a malformed line.

    Raises:
        ValueError: on a missing ``+``/``-`` prefix, an empty or invalid
            target, an out-of-range port, or a malformed ``%key=value``
            marker — annotated with the line number.
    """
    entries: list[PolicyEntry] = []
    for lineno, raw in enumerate(text.splitlines(), start=1):
        line = raw.strip()
        if not line or line.startswith("#"):
            continue
        body, _, _comment = line.partition("#")  # comments bear zero load
        try:
            entries.append(_parse_line(body.strip()))
        except ValueError as exc:
            raise ValueError(f"line {lineno}: {exc} ({raw!r})") from exc
    return entries

render_target(entry)

Render an entry's target[:port] — the line minus its action and markers.

An IPv6 literal carrying a port is bracketed, else addr:port is indistinguishable from a longer IPv6 address on re-parse.

Source code in src/terok_shield/policy.py
def render_target(entry: PolicyEntry) -> str:
    """Render an entry's ``target[:port]`` — the line minus its action and markers.

    An IPv6 literal carrying a port is bracketed, else ``addr:port`` is
    indistinguishable from a longer IPv6 address on re-parse.
    """
    if entry.port is None:
        return entry.target
    host = f"[{entry.target}]" if ":" in entry.target else entry.target
    return f"{host}:{entry.port}"

render_policy(entries)

Render entries back to canonical text (round-trips parse_policy).

Source code in src/terok_shield/policy.py
def render_policy(entries: list[PolicyEntry]) -> str:
    """Render entries back to canonical text (round-trips [`parse_policy`][terok_shield.policy.parse_policy])."""
    lines = [
        f"{e.action}{render_target(e)}"
        + "".join(f" %{key}={value}" for key, value in sorted(e.meta.items()))
        for e in entries
    ]
    return "\n".join(lines) + "\n" if lines else ""

localhost_ports(entries)

Ports from the +localhost:PORT host-service grants, fed to the builder's loopback_ports.

Only admitting (+) entries grant a port — a programmatically built -localhost entry (which parse_policy would reject) is never a grant.

Source code in src/terok_shield/policy.py
def localhost_ports(entries: list[PolicyEntry]) -> tuple[int, ...]:
    """Ports from the ``+localhost:PORT`` host-service grants, fed to the builder's ``loopback_ports``.

    Only admitting (``+``) entries grant a port — a programmatically built
    ``-localhost`` entry (which ``parse_policy`` would reject) is never a grant.
    """
    return tuple(
        e.port for e in entries if e.action == "+" and e.target == LOCALHOST and e.port is not None
    )

expiry_deadline(value)

The instant an %expires value runs out, as a timezone-aware datetime.

A date — the form an operator writes, and the one an orchestrator passes — keeps the entry valid through that day: it runs out at the host's local midnight that ends the day, so expires set to today still holds until tonight. A full ISO-8601 timestamp runs out at that instant; one without an offset is read as host local time, like the date.

Raises:

Type Description
ValueError

When value is neither an ISO date nor an ISO timestamp.

Source code in src/terok_shield/policy.py
def expiry_deadline(value: str) -> datetime:
    """The instant an ``%expires`` value runs out, as a timezone-aware datetime.

    A date — the form an operator writes, and the one an orchestrator passes
    — keeps the entry valid *through* that day: it runs out at the host's
    local midnight that ends the day, so ``expires`` set to today still holds
    until tonight.  A full ISO-8601 timestamp runs out at that instant; one
    without an offset is read as host local time, like the date.

    Raises:
        ValueError: When *value* is neither an ISO date nor an ISO timestamp.
    """
    text = value.strip()
    try:
        day = date.fromisoformat(text)
    except ValueError:
        moment = datetime.fromisoformat(text)  # raises ValueError for anything else
        return moment if moment.tzinfo is not None else moment.astimezone()
    try:
        return datetime.combine(day + timedelta(days=1), time.min).astimezone()
    except (OverflowError, ValueError):  # the calendar's last days: no midnight after them
        return _LAST_INSTANT

marker_value(text)

text as one %key=value marker value — free text kept to a single token.

A marker ends at the first whitespace and a # starts a comment, so a reason written the way an operator types it ("vault outage, direct debug") would break the line it rides on. Percent-encoding keeps it whole: whitespace, # and % itself become %XX, and urllib.parse.unquote reads the text back unchanged.

Source code in src/terok_shield/policy.py
def marker_value(text: str) -> str:
    """*text* as one ``%key=value`` marker value — free text kept to a single token.

    A marker ends at the first whitespace and a ``#`` starts a comment, so a
    reason written the way an operator types it ("vault outage, direct
    debug") would break the line it rides on.  Percent-encoding keeps it
    whole: whitespace, ``#`` and ``%`` itself become ``%XX``, and
    ``urllib.parse.unquote`` reads the text back unchanged.
    """
    return quote(text, safe=_MARKER_SAFE)

entry_deadline(entry)

When entry stops being valid, or None when it carries no %expires marker.

Raises:

Type Description
ValueError

When the marker's value is not a date or a timestamp.

Source code in src/terok_shield/policy.py
def entry_deadline(entry: PolicyEntry) -> datetime | None:
    """When *entry* stops being valid, or ``None`` when it carries no ``%expires`` marker.

    Raises:
        ValueError: When the marker's value is not a date or a timestamp.
    """
    value = entry.meta.get(EXPIRES_MARKER)
    if value is None:
        return None
    try:
        return expiry_deadline(value)
    except ValueError as exc:
        raise ValueError(
            f"invalid %{EXPIRES_MARKER}={value!r} on {entry.target} "
            "(expected a date, e.g. 2026-06-09, or an ISO timestamp)"
        ) from exc

is_ip(target)

True when target is an IP literal or CIDR (a domain or localhost is False).

Source code in src/terok_shield/policy.py
def is_ip(target: str) -> bool:
    """True when *target* is an IP literal or CIDR (a domain or ``localhost`` is False)."""
    try:
        ipaddress.ip_network(target, strict=False)
    except ValueError:
        return False
    return True

ip_targets(entries)

The IP/CIDR targets among entries (domains and localhost excluded), order-preserving.

Source code in src/terok_shield/policy.py
def ip_targets(entries: list[PolicyEntry]) -> list[str]:
    """The IP/CIDR targets among *entries* (domains and ``localhost`` excluded), order-preserving."""
    return [e.target for e in entries if is_ip(e.target)]

domain_targets(entries)

The domain targets among entries (IPs and localhost excluded), order-preserving.

Source code in src/terok_shield/policy.py
def domain_targets(entries: list[PolicyEntry]) -> list[str]:
    """The domain targets among *entries* (IPs and ``localhost`` excluded), order-preserving."""
    return [e.target for e in entries if e.target != LOCALHOST and not is_ip(e.target)]

validate_target(target)

Accept a domain (optionally *.-prefixed), an IP literal, or a CIDR.

The target grammar every policy line obeys, on its own: no action prefix, no :port, no %markers. Public so a caller that keeps host lists of its own (an orchestrator's named egress sets, a promotion target) validates them by the same rule the shield will apply, instead of a copy of the pattern that drifts. A caller that wants a narrower grammar — no wildcards, no CIDRs — layers its own check on top.

Raises:

Type Description
ValueError

When target is neither a domain, an IP literal, nor a CIDR.

Source code in src/terok_shield/policy.py
def validate_target(target: str) -> None:
    """Accept a domain (optionally ``*.``-prefixed), an IP literal, or a CIDR.

    The target grammar every policy line obeys, on its own: no action prefix,
    no ``:port``, no ``%markers``.  Public so a caller that keeps host lists
    of its own (an orchestrator's named egress sets, a promotion target)
    validates them by the same rule the shield will apply, instead of a copy
    of the pattern that drifts.  A caller that wants a narrower grammar — no
    wildcards, no CIDRs — layers its own check on top.

    Raises:
        ValueError: When *target* is neither a domain, an IP literal, nor a CIDR.
    """
    try:
        ipaddress.ip_network(target, strict=False)
        return
    except ValueError:
        pass
    if not _DOMAIN.fullmatch(target):
        raise ValueError(f"invalid target: {target!r}")