Docs/Predicate Secure/Writing Policies

Writing Policies

Policies are YAML files that define what actions your agent can perform.


Basic Structure

rules:
  - action: "<action_pattern>"
    resource: "<resource_pattern>"
    effect: allow | deny
    require_verification:  # optional
      - <predicate>

Action Patterns

Actions represent what the agent is trying to do:

PatternMatchesExample
browser.clickSpecific actionOnly click events
browser.*Action prefixAll browser actions
tool.searchTool callSearch tool invocation
api.callAPI requestHTTP API calls
*EverythingCatch-all rule

Resource Patterns

Resources represent what the agent is acting on:

PatternMatchesExample
https://example.com/*URL prefixAll pages on domain
checkoutContains textAny checkout URL
button#submitCSS selectorSpecific element
/safe/path/*File path prefixSafe directory
*EverythingCatch-all

Verification Predicates

Predicates ensure the action had the expected effect:

require_verification:
  # URL checks
  - url_contains: "/checkout"
  - url_matches: "^https://.*\\.example\\.com/.*"

  # DOM checks
  - element_exists: "#cart-items"
  - element_text_contains:
      selector: ".total"
      text: "$"

  # Custom predicates
  - predicate: "cart_not_empty"

Available Predicates

PredicateDescription
url_containsCheck if current URL contains a string
url_matchesCheck if current URL matches a regex pattern
element_existsCheck if an element exists on the page
element_text_containsCheck if an element's text contains a string
text_matchesCheck if page text matches a pattern
snapshot_changedCheck if page snapshot changed after action

Rule Order

Rules are evaluated top-to-bottom. The first matching rule wins:

rules:
  # Specific rules first
  - action: "browser.click"
    resource: "*checkout*"
    effect: allow

  # General rules after
  - action: "browser.*"
    resource: "https://example.com/*"
    effect: allow

  # Default deny last
  - action: "*"
    resource: "*"
    effect: deny

Best practices:

  1. Put specific rules before general rules
  2. Always end with a default deny rule
  3. Group related rules together with comments

Complete Policy Example

# policies/shopping.yaml
#
# Policy for an e-commerce shopping agent

rules:
  # Allow browsing the store
  - action: "browser.navigate"
    resource: "https://*.amazon.com/*"
    effect: allow

  - action: "browser.click"
    resource: "https://*.amazon.com/*"
    effect: allow

  - action: "browser.fill"
    resource: "https://*.amazon.com/*"
    effect: allow

  # Allow checkout with verification
  - action: "browser.click"
    resource: "*place-order*"
    effect: allow
    require_verification:
      - url_contains: "/checkout"
      - element_exists: "#cart-items"

  # Block navigation to external sites
  - action: "browser.navigate"
    resource: "https://malicious.com/*"
    effect: deny

  # Block sensitive actions
  - action: "browser.fill"
    resource: "*password*"
    effect: deny

  # Default: deny everything else
  - action: "*"
    resource: "*"
    effect: deny

Tool Authorization Policy

For LangChain or other tool-based agents:

# policies/tools.yaml
rules:
  # Allow search and calculator
  - action: "tool.search"
    resource: "*"
    effect: allow

  - action: "tool.calculator"
    resource: "*"
    effect: allow

  # Block file operations
  - action: "tool.file_write"
    resource: "*"
    effect: deny

  # Block shell commands
  - action: "tool.shell"
    resource: "*"
    effect: deny

  # Default deny
  - action: "*"
    resource: "*"
    effect: deny

Policy Loading

# Load from file path
secure_agent = SecureAgent(
    agent=agent,
    policy="policies/shopping.yaml",
)

# Or use environment variable
# Set PREDICATE_AUTHORITY_POLICY_FILE=policies/shopping.yaml
secure_agent = SecureAgent(agent=agent)