Powered by AppSignal & Oban Pro

Policy And The Lethal Trifecta

policy-and-lethal-trifecta.livemd

Policy And The Lethal Trifecta

Run in Livebook

This chapter compiles one policy file, exercises its boundary rules, applies its repository rules, and enforces its outbound data contract. The policy targets the lethal trifecta: an agent that reads private data, consumes untrusted instructions, and communicates externally in the same flow, where a prompt injection can become an exfiltration path. Removing any one edge drops the risk sharply, and SigilGuard expresses each edge as reviewable policy data.

Setup

repo_root = Path.expand("..", __DIR__)
local_checkout? = File.regular?(Path.join(repo_root, "mix.exs"))

{sigil_guard_dep, install_opts} =
  if local_checkout? do
    {
      {:sigil_guard, path: repo_root, env: :prod},
      [
        config_path: Path.join(repo_root, "config/config.exs"),
        lockfile: Path.join(repo_root, "mix.lock")
      ]
    }
  else
    {{:sigil_guard, "~> 1.0"}, []}
  end

Mix.install([sigil_guard_dep], install_opts)

One File, Three Kinds Of Policy

The [rules] section governs runtime boundaries. [repo] governs proposed file changes. [contracts] constrains content that is otherwise allowed to cross an outbound sink. The three sections do separate jobs in one reviewable file with one digest.

policy_text = """
version 3

[rules]
default allow
block sensitivity:private origin:tool,resource,repo zone:untrusted sink:external,network trust:low,medium
confirm sensitivity:private origin:tool,resource,repo zone:untrusted sink:external,network trust:high
allow sensitivity:public sink:model

[repo]
default require_approval
allow agent:spiffe://demo.example/agents/docs action:modify docs/** README.md
block agent:* priv/secrets/**

[contracts]
contract sink:external,network max_size:96 no_raw_credentials:true digest_only_pii:true classes:text credential_transform:mask
"""

{:ok, policy} = SigilGuard.BoundaryPolicy.File.parse(policy_text)

64 = String.length(policy.digest)
3 = length(policy.rules)
true = not is_nil(policy.repo)
true = Map.has_key?(policy.contracts, "external")

%{
  digest: policy.digest,
  boundary_rules: length(policy.rules),
  repo_rules: length(policy.repo.rules),
  contract_sinks: policy.contracts |> Map.keys() |> Enum.sort()
}

The digest is over the raw policy bytes. A comment, newline, or rule edit is a different reviewed artifact even when two parsed structures happen to behave the same.

Make The Three Edges Explicit

In the risky path, a tool supplied private content, the content came from an untrusted zone, and the agent wants to send it outside the system.

digest = String.duplicate("a", 64)

trifecta = [
  phase: :model_egress,
  source: :tool,
  origin: :tool,
  sink: :external,
  source_sensitivity: :private,
  trust_zone: :untrusted,
  trust_level: :medium,
  action_digest: digest,
  payload_digest: digest,
  context_digest: digest
]

blocked = SigilGuard.BoundaryPolicy.evaluate(trifecta, policy: policy)

:block = blocked.action
true = Enum.any?(blocked.matched_rules, &(&1.rule_id == "line_5"))

%{action: blocked.action, reason: blocked.reason, rules: blocked.matched_rules}

On stage: draw the three edges before evaluating. Each edge is a typed policy input.

Trust Changes The Workflow

A high-trust actor does not turn private, untrusted content into public data. The policy changes the workflow from a hard block to exact human approval.

high_trust = Keyword.put(trifecta, :trust_level, :high)
approval_required = SigilGuard.BoundaryPolicy.evaluate(high_trust, policy: policy)

:confirm = approval_required.action

%{action: approval_required.action, reason: approval_required.reason}

Remove one edge and the matching rule no longer applies. This variant sends public content back to the model, not to an external system.

public_model_boundary =
  trifecta
  |> Keyword.put(:sink, :model)
  |> Keyword.put(:source_sensitivity, :public)
  |> Keyword.put(:trust_zone, :semi_trusted)

allowed = SigilGuard.BoundaryPolicy.evaluate(public_model_boundary, policy: policy)

:allow = allowed.action
%{action: allowed.action, reason: allowed.reason}

Repository Changes Use A Path Kernel

Repository governance has a smaller vocabulary: allow, require approval, or block. Paths are normalized and unsafe relative paths fail before rule matching. A documentation agent can edit docs, but it cannot wander into a secret-bearing directory.

docs_change =
  SigilGuard.RepoPolicy.evaluate(policy.repo,
    agent: "spiffe://demo.example/agents/docs",
    action: "modify",
    changed_paths: ["docs/architecture.md", "README.md"]
  )

secret_change =
  SigilGuard.RepoPolicy.evaluate(policy.repo,
    agent: "spiffe://demo.example/agents/docs",
    action: "modify",
    changed_paths: ["priv/secrets/provider.key"]
  )

unmatched_change =
  SigilGuard.RepoPolicy.evaluate(policy.repo,
    agent: "spiffe://demo.example/agents/docs",
    action: "modify",
    changed_paths: ["lib/payment.ex"]
  )

:allow = docs_change.verdict
:block = secret_change.verdict
:require_approval = unmatched_change.verdict

%{
  docs: docs_change.verdict,
  secrets: secret_change.verdict,
  unmatched: unmatched_change.verdict,
  unmatched_paths: unmatched_change.unmatched_paths
}

The repo decision can be folded into a boundary decision without inventing a second policy language at the adapter seam.

repo_facts = SigilGuard.RepoPolicy.policy_facts(policy.repo, unmatched_change)

repo_boundary = [
  phase: :model_egress,
  source: :model,
  origin: :model,
  sink: :repo,
  trust_zone: :trusted,
  trust_level: :high,
  action_digest: digest,
  payload_digest: digest,
  context_digest: digest
]

repo_gate =
  SigilGuard.BoundaryPolicy.evaluate(repo_boundary,
    policy: policy,
    repo_facts: repo_facts
  )

:confirm = repo_gate.action
%{action: repo_gate.action, repo: unmatched_change.verdict}

Allowed Does Not Mean Unconstrained

An output contract runs only after policy permits a crossing. It can restrict the content class, replace raw credentials, hash designated PII, and cap the final UTF-8 byte size. The host supplies the credential and PII spans from its approved detectors.

alias SigilGuard.BoundaryPolicy.Contract

contract = policy.contracts["external"]
credential = "AKIAIOSFODNN7EXAMPLE"
email = "operator@example.test"

outbound =
  "Build owner #{email}; credential #{credential}; " <>
    String.duplicate("diagnostic ", 12)

{:ok, constrained} =
  Contract.enforce(contract, "external", outbound,
    verdict: :allow,
    credentials: [credential],
    pii: [email]
  )

false = String.contains?(constrained, credential)
false = String.contains?(constrained, email)
true = byte_size(constrained) <= 96
true = String.ends_with?(constrained, "[TRUNCATED]")

%{bytes: byte_size(constrained), output: constrained}

A structured payload is blocked here because this contract admits only text. There is no lossy conversion to "make it fit."

{:block, "contract.external.class"} =
  Contract.enforce(contract, "external", %{"status" => "ok"}, verdict: :allow)

The Gate Applies The Policy File

Runtime.Gate applies a sensitivity: rule when the host states source_sensitivity in context.metadata. Without that label the rule does not match and the payload is allowed.

gate_egress = fn metadata ->
  SigilGuard.Runtime.Gate.evaluate(
    "Q3 revenue draft",
    [
      phase: :outbound_model,
      origin: :tool,
      sink: :external,
      trust_zone: :untrusted,
      trust_level: :medium,
      metadata: metadata
    ],
    boundary_policy: policy
  )
end

private_through_gate = gate_egress.(%{source_sensitivity: :private})
unlabelled_through_gate = gate_egress.(%{})

:block = private_through_gate.action
["line_5"] = private_through_gate.audit_metadata.matched_rule_ids
:allow = unlabelled_through_gate.action

%{
  labelled_private: private_through_gate.action,
  rule: private_through_gate.audit_metadata.matched_rule_ids,
  unlabelled: unlabelled_through_gate.action
}

At a :repo_change phase, Runtime.Gate evaluates the file's [repo] section and reports the repo policy facts in audit_metadata.

gate_change = fn paths ->
  SigilGuard.Runtime.Gate.evaluate(
    %{"changed_paths" => paths},
    [
      phase: :repo_change,
      origin: :model,
      sink: :repo,
      actor: "spiffe://demo.example/agents/docs",
      action: "modify",
      trust_zone: :trusted,
      trust_level: :high
    ],
    boundary_policy: policy
  )
end

docs_through_gate = gate_change.(["docs/architecture.md", "README.md"])
unmatched_through_gate = gate_change.(["lib/payment.ex"])
secret_through_gate = gate_change.(["priv/secrets/provider.key"])

:allow = docs_through_gate.action
:confirm = unmatched_through_gate.action
:block = secret_through_gate.action
["repo.default"] = unmatched_through_gate.audit_metadata.matched_rule_ids
["repo.line_3"] = secret_through_gate.audit_metadata.matched_rule_ids
:require_approval = secret_through_gate.audit_metadata.repo_policy_default

true =
  secret_through_gate.audit_metadata.repo_policy_digest ==
    SigilGuard.RepoPolicy.digest(policy.repo)

%{
  docs: docs_through_gate.action,
  unmatched: {unmatched_through_gate.action, unmatched_through_gate.audit_metadata.matched_rule_ids},
  secrets: {secret_through_gate.action, secret_through_gate.audit_metadata.matched_rule_ids},
  repo_policy_digest: secret_through_gate.audit_metadata.repo_policy_digest
}

Runtime.Gate applies the [contracts] section of a :boundary_policy file. Pass output_contracts: false to skip it.

egress_context = [
  phase: :outbound_model,
  origin: :model,
  sink: :external,
  actor: "spiffe://demo.example/agents/release-assistant",
  trust_zone: :trusted,
  trust_level: :high
]

status_line = "Deploy finished. " <> String.duplicate("diagnostic ", 12)

constrained_by_gate =
  SigilGuard.Runtime.Gate.evaluate(status_line, egress_context, boundary_policy: policy)

contract_off =
  SigilGuard.Runtime.Gate.evaluate(status_line, egress_context,
    boundary_policy: policy,
    output_contracts: false
  )

structured_through_gate =
  SigilGuard.Runtime.Gate.evaluate(%{"status" => "ok"}, egress_context, boundary_policy: policy)

:redact = constrained_by_gate.action
[:truncate] = constrained_by_gate.audit_metadata.output_contract_transforms
true = byte_size(constrained_by_gate.sanitized_text) <= 96
true = String.ends_with?(constrained_by_gate.sanitized_text, "[TRUNCATED]")
:allow = contract_off.action
:block = structured_through_gate.action
true = "contract.external.class" in structured_through_gate.audit_metadata.matched_rule_ids

%{
  text: {constrained_by_gate.action, constrained_by_gate.audit_metadata.output_contract_transforms},
  released: constrained_by_gate.sanitized_text,
  contract_off: contract_off.action,
  structured: structured_through_gate.reason
}

Next: An AI Agent Under Attack.