DEV Community

Mayuresh Pandit
Mayuresh Pandit

Posted on

Safeguard Your AI Apps: Introducing ai-prompt-firewall

Building AI-powered applications is exciting, but transmitting unfiltered user input directly to Large Language Model (LLM) providers creates significant security and compliance risks.


From accidental API key leaks (sk-proj-...) to exposed PII (emails, phone numbers, internal IPs), unmonitored prompts can violate privacy regulations and expose sensitive system credentials.

To solve this, I built and published ai-prompt-firewall — a lightweight, zero-dependency Node.js and TypeScript library designed to intercept, sanitize, and secure prompt streams before they hit your LLM provider.


🚀 Key Features

  • Zero External Dependencies: Ultra-lightweight footprint with fast execution times.
  • 🛡️ Built-in Pattern Detection: Automatically identifies critical tokens (OpenAI, Anthropic, AWS, Stripe, GitHub, Google API, Slack, Private Keys, Bearer Tokens) and PII (Emails, Phone Numbers, IPv4 Addresses).
  • 🎯 Three Operational Modes:
    • redact: Replaces detected secrets with safe placehold tags (e.g., [OPENAI_KEY_REDACTED]).
    • block: Triggers a zero-trust block state when sensitive tokens are detected.
    • warn: Passes the prompt while capturing findings for background auditing.
  • 📊 SIEM-Ready Auditing: Returns exact character offsets (startIndex, endIndex, length) for security compliance logging.
  • 🏢 Multi-Tenant Support: Pre-compile isolated firewall instances with custom regex patterns per tenant.

📦 Quick Start

1. Installation

Install directly via the npm registry:

npm install ai-prompt-firewall
Enter fullscreen mode Exit fullscreen mode

2. Basic Usage

import { scan } from 'ai-prompt-firewall';

const userPrompt = "Here is my secret key: sk-proj-1234567890abcdef1234567890abcdefABCDEF";

// Scan and redact in real time
const result = scan(userPrompt, 'redact');

console.log(result.safePrompt);
// Output: "Here is my secret key: [OPENAI_KEY_REDACTED]"

console.log(result.findings);
// Output: Detailed array containing token type, severity, and exact character offsets
Enter fullscreen mode Exit fullscreen mode

3. Advanced Features

Custom Regex & Runtime Options
You can extend the firewall with custom domain-specific pattern checks at runtime:

const customPatterns = {
  INTERNAL_PROJECT_ID: /PROJ-[A-Z]{3}-\d{4}/g
};

const result = scan("Access details for PROJ-XYZ-9876", "redact", { customPatterns });
Enter fullscreen mode Exit fullscreen mode

4. Multi-Tenant Pre-Compiled Firewall

For multi-tenant SaaS platforms, pre-compile dedicated firewall instances to optimize regex compiling performance:

import { createFirewall } from 'ai-prompt-firewall';

const tenantFirewall = createFirewall({
  customPatterns: {
    TENANT_BADGE: /BADGE-\d{5}/g
  }
});

const tenantResult = tenantFirewall.scan("Badge code BADGE-99887", "block");
Enter fullscreen mode Exit fullscreen mode

🔗 Try It Out!
npm Package: https://npmjs.com/package/ai-prompt-firewall

GitHub Repository: https://github.com/GamersStop/ai-prompt-firewall

Feedback, issues, and contributions are welcome! If you find this package useful for your AI tech stack, give it a ⭐️ on GitHub!

Top comments (0)