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:
- An agent does something unexpected in staging
- A developer adds an if-statement to block that specific action
- Another incident leads to another if-statement
- Over time, the middleware becomes a tangled web of special cases, regex patterns, and string matching
- Has no formal test coverage. Custom middleware is typically written under pressure and rarely tested systematically.
- Uses allow-by-default logic. Most custom implementations try to block known bad actions rather than allowing only known good ones.
- Has inconsistent audit logging. Some actions are logged, others are not. Logs are plain text with no tamper evidence.
- Is coupled to one provider. Middleware written for OpenAI function calls will not work with Claude tool use, and vice versa.
- Is maintained by tribal knowledge. Only the developer who wrote it understands it, and they might not be on the team anymore.
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:
- Actions your middleware allows that SafeClaw would deny: these are the gaps in your custom approach
- Actions both systems agree on: these validate your SafeClaw policy
- Actions SafeClaw would allow that your middleware blocks: review these to ensure your SafeClaw policy is not too permissive
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
- 446 tests validating the policy engine, replacing your untested custom code
- Deny-by-default replacing your allow-by-default with block-list approach
- Hash-chained audit trails replacing inconsistent plain-text logs
- Provider agnosticism replacing provider-specific middleware
- Declarative policies replacing imperative spaghetti code
- Zero dependencies replacing whatever your custom middleware pulled in
- Community maintenance replacing single-developer tribal knowledge
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:
- Migration Guide: Adding SafeClaw to an Existing AI Agent
- SafeClaw Compared: How It Stacks Up Against Every Alternative
- How to Switch from Allow-by-Default to Deny-by-Default
- SafeClaw Features: Everything You Get Out of the Box
Try SafeClaw
Action-level gating for AI agents. Set it up in your browser in 60 seconds.
$ npx @authensor/safeclaw