Skip to content

Design and implement a governance-rule sandboxing/resource-limiting execution model - #317

Open
nurudeenheroic wants to merge 1 commit into
Adamantine-guild:mainfrom
nurudeenheroic:feat/issue-155-governance-rule-sandboxing
Open

Design and implement a governance-rule sandboxing/resource-limiting execution model#317
nurudeenheroic wants to merge 1 commit into
Adamantine-guild:mainfrom
nurudeenheroic:feat/issue-155-governance-rule-sandboxing

Conversation

@nurudeenheroic

Copy link
Copy Markdown

Summary

Implements a three-layer resource-limiting execution model for governance rule evaluation, preventing maliciously or accidentally crafted rule ASTs from degrading checkAccess() latency.

Layers of Defence

Layer Mechanism Enforced At Purpose
1. Complexity limit Weighted AST scoring (≤64) Rule creation (validateRuleAST) Rejects overly complex rules before storage
2. Depth limit Recursion guard (≤10 levels) Rule creation (validateRuleAST) Prevents stack overflow
3. Wall-clock timeout Date.now() deadline checks (5ms) Evaluation (evaluateRuleWithBudget) Bounds runtime of any accepted rule

Changes

packages/governance-engine/src/validator.ts

  • Added computeComplexity() — weighted AST complexity scoring
  • Added estimateComplexity() — pre-pass guard protecting recursive validator from pathologically wide×deep ASTs (e.g., 50¹⁰ worst case)
  • Added RESOURCE_LIMITS export
  • Added pre-pass complexity check (depth === 0) that rejects ASTs exceeding MAX_COMPLEXITY=64 before structural validation begins
  • Updated validateRuleAST() to use the pre-pass guard

packages/governance-engine/src/evaluator.ts

  • Added EvaluationOptions interface with timeoutMs
  • Added evaluateRuleWithBudget() — budget-aware entry point with wall-clock deadline
  • Added yield-point deadline check in evaluateNode() at each AST node entry
  • Threaded deadline through all combinator functions (AND, OR, NOT, N_OF_M)
  • evaluateRule() kept for backward compatibility

apps/access-api/src/policy/governanceRuleProvider.ts

  • Switched from evaluateRule() to evaluateRuleWithBudget() with 5ms timeout
  • Returns GOVERNANCE_TIMEOUT DENY code on timeout

tests

  • 9 new unit tests for complexity scoring and budget-aware evaluation
  • New load-test file with max-complexity AST (1000 evaluations bounded)

docs

  • Added full resource-limiting execution model section to GOVERNANCE_ENGINE_IMPLEMENTATION.md

Acceptance Criteria

  • validateRuleAST() rejects ASTs beyond complexity/depth threshold
  • Evaluation is provably bounded in time (most complex valid AST completes in microseconds)
  • Load test confirms max-complexity rule stays within budget (1000 iterations in ~55ms)
  • Resource-limiting mechanism documented in GOVERNANCE_ENGINE_IMPLEMENTATION.md

Fixes #155

…mits and timeout

- Add AST complexity scoring (computeComplexity) with MAX_COMPLEXITY=64
- Add pre-pass complexity guard (estimateComplexity) to protect recursive
  validator from pathologically wide×deep ASTs (50¹⁰ worst case)
- Export RESOURCE_LIMITS for consumers to surface in docs/UI
- Add evaluateRuleWithBudget() with wall-clock timeout (default 5ms)
- Thread deadline through all combinator functions as yield point
- Wire timeout into GovernanceRuleProvider → returns GOVERNANCE_TIMEOUT code
- Add 9 new tests for complexity scoring and budget-aware evaluation
- Add load test with max-complexity AST (1000 evaluations bounded)
- Document resource-limiting model in GOVERNANCE_ENGINE_IMPLEMENTATION.md

Closes Adamantine-guild#155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Design and implement a governance-rule sandboxing/resource-limiting execution model

2 participants