Application Security

Policy-as-Code for AI Developers and Autonomous Software Agents

When agents write code, open pull requests, change infrastructure and call tools, written policies and prompts are not enough. Rules have to be enforced by machines, consistently, at every decision point. This article shows how to apply policy-as-code to AI developers and autonomous agents, from repository rules to runtime tool authorisation.

By EthicalHat Research · · 2 min read
Infographic: Policy decision points for AI agents

A recurring lesson of the past two years is that instructions are not controls. Telling an agent in its instructions file not to touch production, not to modify workflows or not to send data externally reduces how often it does those things. It does not prevent them, especially when the agent reads content an attacker wrote.

The discipline that turns rules into enforcement is policy-as-code: expressing organisational rules in machine-readable form and evaluating them automatically at every decision point. Platform and security teams have used it for years for infrastructure and Kubernetes. It is now essential for AI developers and autonomous agents.

Where policy decisions happen

flowchart TB
  subgraph Build["Software delivery"]
    Repo[Repository<br/>paths, branches] --> PR[Pull request<br/>checks, reviewers]
    PR --> CI[Pipeline<br/>gates, provenance]
    CI --> Dep[Deployment<br/>admission control]
  end
  subgraph Run["Agent runtime"]
    Agent[Agent proposes<br/>tool call] --> PDP{Policy decision point}
    PDP -->|allow| Tool[Execute]
    PDP -->|deny| Deny[Deny + log]
    PDP -->|approve| Human[Human approval]
  end
  Policies[(Versioned policies<br/>Rego / Cedar)] --> PR
  Policies --> CI
  Policies --> Dep
  Policies --> PDP

Policy at the repository and pull request

Rules about what agents may change belong at the pull request, where they cannot be talked around:

  • agent-authored changes to /.github/workflows, /deploy, /infra/prod or security-sensitive modules require a human code owner's approval,
  • agents cannot approve pull requests or merge them,
  • pull requests from agents must include provenance (agent ID and version, task reference) and pass the full check suite, and
  • new dependencies introduced by agents require explicit review.

An illustrative Rego policy evaluated in CI against pull request metadata:

rego
package pr.agents

default allow := false

sensitive_paths := {".github/workflows/", "deploy/", "infra/prod/", "src/auth/"}

touches_sensitive if {
  some f in input.changed_files
  some p in sensitive_paths
  startswith(f, p)
}

human_code_owner_approved if {
  some r in input.reviews
  r.state == "APPROVED"
  r.author.type == "human"
  r.author.is_code_owner
}

allow if {
  input.author.type == "agent"
  not touches_sensitive
  input.checks.all_passed
}

allow if {
  input.author.type == "agent"
  touches_sensitive
  input.checks.all_passed
  human_code_owner_approved
}

allow if input.author.type == "human"   # humans follow the standard branch protection rules

Policy at deployment

Deployment policies ensure that only verified artefacts run: signed by the expected pipeline, with provenance showing all required checks passed, and with no critical vulnerabilities that have fixes available. None of that changes because an agent wrote the code, but provenance lets you apply extra rules to agent-authored changes, such as progressive rollout or mandatory canaries.

Policy at runtime: authorising agent tool calls

For autonomous agents that call tools and APIs, every action should be authorised by a policy engine outside the model, based on:

  • who is acting (agent identity and version),
  • on whose behalf (delegating user or team),
  • what action on which resource,
  • data classification involved, and
  • context (environment, time, recent behaviour, approval status).

Policy languages designed for application authorisation, such as Cedar and Rego, fit this well. An illustrative Cedar policy set:

cedar
// Support agents may read tickets for customers of the user they act for
permit (
  principal in AgentGroup::"support-agents",
  action == Action::"ReadTicket",
  resource
) when {
  resource.customer_id == context.on_behalf_of.customer_id
};

// No agent may send data classified 'restricted' to external recipients
forbid (
  principal in AgentGroup::"all-agents",
  action == Action::"SendMessage",
  resource
) when {
  context.data_classification == "restricted" && !resource.recipient_is_internal
};

// Refunds above a threshold need human approval
forbid (
  principal in AgentGroup::"all-agents",
  action == Action::"IssueRefund",
  resource
) unless {
  context.amount <= 50 || context.human_approval == true
};

The agent runtime calls the policy engine before every tool invocation, and denies by default.

Writing good policies for agents

  • Deny by default, and add narrow permissions.
  • Separate who from what. Keep agent identities and groups in your identity system; keep rules in policy.
  • Encode blast radius: amounts, volumes, environments and data classifications.
  • Make approvals explicit: policies can require a human approval token in context rather than trusting the agent to ask.
  • Give useful denial reasons so agents (and people) can adjust rather than retry blindly.

Treat policies as code

  • Keep policies in version control, with owners and reviews.
  • Unit-test policies with representative inputs, including adversarial ones.
  • Roll out in audit mode first to see what would be denied, then enforce.
  • Log every decision (inputs, result, policy version) for audit and incident response.
bash
# Policy tests run in CI like any other tests
opa test policies/ -v
cedar validate --policies policies/agents.cedar --schema schema.cedarschema

The bottom line

As AI agents take on more development and operational work, the rules that keep them safe cannot live only in prompts and instruction files. Express them as policy-as-code, enforce them at every decision point from the pull request to the runtime tool call, and test and audit them like any other critical code. Then agents can move fast inside boundaries that actually hold.

Our application security testing and DevSecOps teams design and implement policy-as-code for AI development and agent platforms.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.