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-09T12:00Z
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.
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.
__all__ = ['LOCALHOST', 'Action', 'PolicyEntry', 'domain_targets', 'ip_targets', 'is_ip', 'localhost_ports', 'parse_policy', 'render_policy']
module-attribute
¶
PolicyEntry(action, target, port=None, meta=dict())
dataclass
¶
One parsed policy line.
Attributes:
| Name | Type | Description |
|---|---|---|
action |
Action
|
|
target |
str
|
a domain (optionally |
port |
int | None
|
the optional |
meta |
dict[str, str]
|
the |
parse_policy(text)
¶
Parse policy text into entries, failing fast on a malformed line.
Raises:
| Type | Description |
|---|---|
ValueError
|
on a missing |
Source code in src/terok_shield/policy.py
render_policy(entries)
¶
Render entries back to canonical text (round-trips parse_policy).
Source code in src/terok_shield/policy.py
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
is_ip(target)
¶
True when target is an IP literal or CIDR (a domain or localhost is False).
ip_targets(entries)
¶
The IP/CIDR targets among entries (domains and localhost excluded), order-preserving.
domain_targets(entries)
¶
The domain targets among entries (IPs and localhost excluded), order-preserving.