security crate provides RBAC capability enforcement, tamper-proof audit logging with Merkle chain verification, and prompt injection detection.
security::check_capability
Enforce role-based access control for agent tool usage and resource access.Unique identifier for the agent requesting access
Resource identifier being accessed (e.g., “file::read”, “memory::store”)
Optional capability namespace extracted from resource (first segment before
::)Returns true if access is granted
Access Control Rules
-
Tool Capability Matching:
- Checks agent capabilities from
state::getwith scope “capabilities” - Allows access if tools array contains
"*"(wildcard) - Allows access if resource starts with any allowed tool prefix
- Example:
["file::", "memory::"]allows “file::read” and “memory::store”
- Checks agent capabilities from
-
Token Quota Enforcement:
- If
max_tokens_per_hour> 0, checks metering usage - Denies access if
totalTokensexceeds quota - Logs quota exceeded event to audit log
- If
-
Audit on Denial:
- Automatically logs denial with type
capability_denied - Includes resource and reason in audit detail
- Automatically logs denial with type
security::set_capabilities
Update or set capabilities for an agent.Agent ID to update capabilities for
Returns true if update was successful
Set Capabilities
security::audit
Append an entry to the tamper-proof Merkle audit chain.Event type (e.g., “capability_denied”, “capabilities_updated”, “tool_executed”)
Optional agent ID associated with the event
Additional event details as JSON object
UUID of the created audit entry
HMAC-SHA256 hash of the entry (64 hex characters)
Merkle Chain Structure
Each audit entry contains:id: UUID v4timestamp: Unix timestamp in millisecondstype: Event type stringagentId: Optional agent identifierdetail: Event-specific JSON datahash: HMAC-SHA256(entry_data + prev_hash)prevHash: Hash of previous entry (or 64 zeros for genesis)
state::set with scope “audit” and maintains a __latest pointer.
HMAC Key Configuration
The audit chain uses HMAC-SHA256 with a key from:- Environment variable:
AUDIT_HMAC_KEY - Default (dev only):
"dev-default-hmac-key-change-in-prod"
security::verify_audit
Verify the integrity of the entire audit chain by recomputing and validating all hashes.True if chain is valid with no violations
Total number of audit entries verified
Array of violation messages (empty if valid)
Verify Audit Chain
Verification Checks
- Chain Continuity: Each entry’s
prevHashmatches the previous entry’shash - Hash Integrity: Recomputes HMAC-SHA256 and compares with stored hash
- Genesis Entry: First entry should have
prevHash= 64 zeros
"Chain break at {id}: expected {hash}, got {hash}""Tampered entry {id}: hash mismatch""HMAC key error"(if key is invalid)
security::scan_injection
Scan text for prompt injection patterns using regex-based detection.Text content to scan for injection patterns
True if no injection patterns detected
Array of matched regex patterns (empty if safe)
Risk score from 0.0 to 1.0 (matches * 0.25, capped at 1.0)
Injection Patterns Detected
The scanner detects 9 common prompt injection patterns (case-insensitive):ignore (all )?(previous|above|prior) (instructions|prompts)you are nowsystem:DAN.*modepretend you areact as if youdisregard (your|all)override (your|system)jailbreak
Risk Score Calculation
- Each match adds 0.25 to the risk score
- Maximum risk score is 1.0 (capped)
- Recommended threshold: 0.5 (used by agent::chat)
HTTP Endpoints
The security worker exposes HTTP endpoints:GET /security/audit/verify
Verify audit chain integrity via HTTP.POST /security/scan
Scan text for injection patterns via HTTP.Event Subscriptions
The security worker subscribes to theaudit topic and automatically appends events to the Merkle chain.
Additional Modules
The security crate includes additional modules (registered separately):- taint: Data flow tracking and taint propagation
- signing: Cryptographic signature verification
- tool_policy: Fine-grained tool execution policies
- docker_sandbox: Sandboxed code execution in Docker containers