@barric/next
v0.1.2
Published
barric firewall middleware for Next.js Route Handlers
Downloads
27
Readme
@barric/next
barric firewall middleware for Next.js Route Handlers. Scans inbound prompts, sanitizes input before your handler runs, and scans outbound LLM responses.
Install
npm install @barric/core @barric/nextQuick Start
// app/api/chat/route.ts
import { createFirewall } from '@barric/core'
import { withBarric } from '@barric/next'
const firewall = createFirewall({
rules: ['prompt-injection', 'pii-redaction'],
injection: { threshold: 0.7, action: 'block' },
})
export const POST = withBarric(firewall)(async (req, sanitizedBody) => {
const prompt = sanitizedBody.prompt as string
// prompt is already sanitized — PII redacted, injection checked
const result = await llm.complete(prompt)
return Response.json({ reply: result })
})How It Works
withBarric is a higher-order function that wraps your Route Handler:
- Reads
promptormessagefrom the request body - Runs inbound scanners (injection detection, PII redaction, encoding detection, rate limiting, input limits)
- Your handler receives
sanitizedBodywith the sanitized string - Outbound scanners run on the response (system prompt leak detection, output limits, PII re-injection)
- If any scanner triggers
block, returns a403JSON response — your handler is never called
Automatic Context
The middleware extracts context from request headers:
| Header | Maps to |
|--------|---------|
| x-forwarded-for / x-real-ip | context.ip (used for rate limiting) |
| x-session-id | context.sessionId |
Shared Firewall Instance
Define the firewall once and reuse across routes:
// lib/firewall.ts
import { createFirewall } from '@barric/core'
export const firewall = createFirewall({
rules: ['prompt-injection', 'pii-redaction', 'rate-limit'],
injection: { threshold: 0.7, action: 'block' },
pii: { mode: 'redact', types: ['email', 'phone'] },
rateLimit: { maxRequests: 20, windowMs: 60_000 },
})// app/api/chat/route.ts
import { withBarric } from '@barric/next'
import { firewall } from '@/lib/firewall'
export const POST = withBarric(firewall)(async (req, sanitizedBody) => {
// ...
})Blocked Requests
When blocked, the middleware returns:
HTTP 403
{ "error": "Request blocked by barric firewall" }License
MIT
