2026-02-02 · Authensor

Custom safety middleware, the hand-built if-else chains and regex filters that many teams use to control agent behavior, is fragile, untested, and difficult to maintain. SafeClaw by Authensor replaces it with a tested policy engine backed by 446 tests, deny-by-default evaluation, and hash-chained audit trails. Install it with npx @authensor/safeclaw and retire the code your team dreads maintaining.

Why Custom Middleware Falls Short

Most teams that build custom safety middleware follow a predictable pattern:

  1. An agent does something unexpected in staging
  2. A developer adds an if-statement to block that specific action
  3. Another incident leads to another if-statement
  4. Over time, the middleware becomes a tangled web of special cases, regex patterns, and string matching
The result is code that:

Mapping Custom Rules to SafeClaw Policies

The first step is understanding what your current middleware does. Review your custom code and catalog every rule:

| Custom Middleware Rule | SafeClaw Policy Equivalent |
|---|---|
| if (command.includes('rm -rf')) block | action: "shell:execute", pattern: "rm -rf*", effect: "deny" |
| if (path.startsWith('/etc')) block | action: "file:write", path: "/etc/**", effect: "deny" |
| if (url.includes('internal')) block | action: "network:request", host: ".internal.", effect: "deny" |
| Allow everything else | SafeClaw denies everything else by default |

The critical difference: SafeClaw's deny-by-default model means you only need to define what is allowed. Your custom middleware likely tries to enumerate what is blocked, which always has gaps.

Step-by-Step Migration

Step 1: Catalog Existing Rules

Extract every condition from your custom middleware. Document what each rule blocks and why. This becomes your migration checklist.

Step 2: Install SafeClaw

npx @authensor/safeclaw

Step 3: Write Your Deny-by-Default Policy

Instead of porting your block rules, invert the model. Define what your agent should be allowed to do:

rules:
  - action: "file:read"
    path: "/app/**"
    effect: "allow"
  - action: "file:write"
    path: "/app/output/**"
    effect: "allow"
  - action: "shell:execute"
    command: "npm test"
    effect: "allow"
  # Default: deny everything not listed

This is typically fewer rules than your custom middleware has, and it is more secure because it blocks unknown actions by default.

Step 4: Run Both Systems in Parallel

Keep your custom middleware active while running SafeClaw in simulation mode. Compare the decisions:

Step 5: Switch to SafeClaw Enforcement

Once you are confident that SafeClaw's policy correctly captures your intended permissions (and covers the gaps your custom middleware missed), switch SafeClaw to enforcement mode and remove the custom middleware.

Step 6: Enable Audit Logging

Replace whatever logging your custom middleware provided with SafeClaw's hash-chained audit trail. Every action, every decision, tamper-evident and exportable.

What You Gain

Handling Edge Cases

If your custom middleware handles complex scenarios (multi-step approval, conditional permissions based on time or context), SafeClaw's policy engine supports these through its rule configuration. Consult the policy rule syntax documentation for advanced patterns.


Related reading:

Try SafeClaw

Action-level gating for AI agents. Set it up in your browser in 60 seconds.

$ npx @authensor/safeclaw