Skip to content
reaatechREAATECH

@reaatech/tool-use-firewall-policies

npm v0.1.0

A collection of middleware components—policy engine, rate limiter, cost tracker, argument validators, SQL validator, secret scanner, anomaly detector, and read-only enforcement—each implementing the `Middleware` interface for pluggable use in a tool-use-firewall interceptor pipeline.

@reaatech/tool-use-firewall-policies

npm version License: MIT CI

Status: Pre-1.0 — APIs may change in minor versions. Pin to a specific version in production.

Policy engine, rate limiter, cost tracker, argument validators, SQL validator, and read-only enforcement for tool-use-firewall. All components implement the Middleware interface for pluggable use in the interceptor pipeline.

Installation

terminal
npm install @reaatech/tool-use-firewall-policies
# or
pnpm add @reaatech/tool-use-firewall-policies

Feature Overview

  • Policy engine — Priority-based rule evaluation with glob pattern matching for tool names
  • Token bucket rate limiter — Global, per-tool, and per-session limits with TTL-based memory eviction
  • Session cost tracker — Per-session budget enforcement with tool-level pricing and warn/block actions
  • SQL validator — Blocked patterns, injection detection, WHERE clause enforcement, read-only mode
  • Argument validator — Regex, shell-safety, and SQL-safety validators with custom rules from config
  • Schema validator — Validates tools/call arguments against the upstream’s advertised JSON inputSchema
  • Secret scanner — Blocks tool calls whose arguments contain API keys, tokens, or other secrets
  • Anomaly detector — Flags tool calls that deviate from a session’s recent behavioral baseline
  • Read-only check — Global toggle with per-tool exceptions and timing-safe bypass tokens
  • All implement Middleware — Drop into any InterceptorPipeline

Quick Start

typescript
import { PolicyEngine, RateLimiter, TokenBucket } from "@reaatech/tool-use-firewall-policies";
import { createRequestContext } from "@reaatech/tool-use-firewall-core";
 
// Rate limiting
const limiter = new RateLimiter({
  global: { requests_per_minute: 60, burst_capacity: 10 },
});
 
const ctx = createRequestContext({
  requestId: "1", sessionId: "s1", method: "tools/call", toolName: "db_query",
});
 
// Returns { action: "CONTINUE" } or throws RateLimitError
const result = await limiter.execute(ctx);
 
// Policy evaluation
const engine = new PolicyEngine({
  rules: [{ id: "r1", type: "block", tools: ["dangerous_tool"], priority: 10 }],
  settings: { default_action: "allow", audit_level: "full", read_only: false },
  version: "1.0",
});
 
const evalResult = await engine.evaluate(ctx);
// → { action: "ALLOW" | "BLOCK" | "APPROVAL_REQUIRED", rule?, reason? }

Exports

ExportDescription
PolicyEngineRule evaluation engine: evaluates config rules against request context
EvaluationResult{ action, rule?, reason? }
RateLimiterMiddleware: global, per-tool, per-session token bucket rate limiting
TokenBucketCore token bucket algorithm (used internally, also exported for custom use)
CostTrackerMiddleware: session budget enforcement with tool-level costs
ArgumentValidatorMiddleware: schema and regex validation of tool arguments
SQLValidatorComprehensive SQL query validation (blocked patterns, injection, WHERE)
SQLValidationResult / SQLValidationConfigSQL validator types
SchemaValidatorMiddleware: validates arguments against the upstream’s advertised JSON inputSchema
SecretScannerMiddleware: blocks tool calls whose arguments contain secrets
AnomalyDetectorMiddleware: flags calls that deviate from a session’s behavioral baseline
ReadOnlyCheckMiddleware: blocks write operations when read-only mode is enabled
ValidatorFn / ValidationResultArgument validator function types

License

MIT