@amanbhawsar/prompt-firewall 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -4
- package/src/index.js +194 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amanbhawsar/prompt-firewall",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local, zero-dependency guardrail for LLM apps: blocks PII (Luhn cards, Verhoeff Aadhaar), API-key/secret leaks, and prompt injection before text leaves your process. Sub-millisecond, no network.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"files": ["src/"],
|
|
7
|
+
"scripts": { "test": "node test/test.js" },
|
|
8
|
+
"keywords": ["llm", "prompt-injection", "pii", "redaction", "security", "guardrail", "ai", "firewall", "secrets", "zero-dependency"],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"engines": { "node": ">=12" },
|
|
11
|
+
"repository": { "type": "git", "url": "https://github.com/Amanbhaw/prompt-firewall" }
|
|
12
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* prompt-firewall — local, zero-dependency guardrail for AI chatbots / LLM apps.
|
|
4
|
+
*
|
|
5
|
+
* Scans text BEFORE it leaves your process for PII (emails, phones, credit cards with
|
|
6
|
+
* Luhn validation, US SSN, India Aadhaar with Verhoeff validation, IPv4), secrets
|
|
7
|
+
* (OpenAI/Anthropic/AWS/GitHub/Slack/Google keys, private-key blocks, JWTs), and prompt
|
|
8
|
+
* injection. Pure regex + checksum math: no network, sub-millisecond, nothing leaves the host.
|
|
9
|
+
*
|
|
10
|
+
* Node >= 12 (lookbehind regex). MIT core; Pro threat-signatures via makeSignature().
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const VERSION = '0.1.0';
|
|
14
|
+
|
|
15
|
+
// ----------------------------- checksum validators -----------------------------
|
|
16
|
+
function luhnOk(digits) {
|
|
17
|
+
const d = String(digits).split('').filter((c) => c >= '0' && c <= '9').map(Number);
|
|
18
|
+
if (d.length < 13 || d.length > 19) return false;
|
|
19
|
+
let total = 0;
|
|
20
|
+
for (let i = 0; i < d.length; i++) {
|
|
21
|
+
let n = d[d.length - 1 - i];
|
|
22
|
+
if (i % 2 === 1) {
|
|
23
|
+
n *= 2;
|
|
24
|
+
if (n > 9) n -= 9;
|
|
25
|
+
}
|
|
26
|
+
total += n;
|
|
27
|
+
}
|
|
28
|
+
return total % 10 === 0;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const _D = [
|
|
32
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
33
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6], [3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
34
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8], [5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
35
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2], [7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
36
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4], [9, 8, 7, 6, 5, 4, 3, 2, 1, 0],
|
|
37
|
+
];
|
|
38
|
+
const _P = [
|
|
39
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
40
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2], [8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
41
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0], [4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
42
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5], [7, 0, 4, 6, 9, 1, 3, 2, 5, 8],
|
|
43
|
+
];
|
|
44
|
+
function verhoeffOk(number) {
|
|
45
|
+
const digits = String(number).split('').filter((c) => c >= '0' && c <= '9').map(Number);
|
|
46
|
+
if (digits.length !== 12) return false;
|
|
47
|
+
let c = 0;
|
|
48
|
+
for (let i = 0; i < digits.length; i++) {
|
|
49
|
+
c = _D[c][_P[i % 8][digits[digits.length - 1 - i]]];
|
|
50
|
+
}
|
|
51
|
+
return c === 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function shannonEntropy(s) {
|
|
55
|
+
if (!s) return 0;
|
|
56
|
+
const counts = {};
|
|
57
|
+
for (const ch of s) counts[ch] = (counts[ch] || 0) + 1;
|
|
58
|
+
const n = s.length;
|
|
59
|
+
let e = 0;
|
|
60
|
+
for (const k in counts) {
|
|
61
|
+
const p = counts[k] / n;
|
|
62
|
+
e -= p * Math.log2(p);
|
|
63
|
+
}
|
|
64
|
+
return e;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ----------------------------- rule tables -----------------------------
|
|
68
|
+
const EMAIL = /[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}/g;
|
|
69
|
+
const PHONE = /(?<!\d)(?:\+?\d{1,3}[\s\-]?)?(?:\(?\d{3}\)?[\s\-]?)\d{3}[\s\-]?\d{4}(?!\d)/g;
|
|
70
|
+
const CARD = /(?<!\d)(?:\d[ \-]?){13,19}(?<!\d)/g;
|
|
71
|
+
const CARD2 = /(?<!\d)\d(?:[ \-]?\d){12,18}(?!\d)/g;
|
|
72
|
+
const SSN = /(?<!\d)\d{3}-\d{2}-\d{4}(?!\d)/g;
|
|
73
|
+
const AADHAAR = /(?<!\d)\d{4}\s?\d{4}\s?\d{4}(?!\d)/g;
|
|
74
|
+
const IPV4 = /(?<!\d)(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)(?!\d)/g;
|
|
75
|
+
|
|
76
|
+
const SECRET_RULES = [
|
|
77
|
+
['secret', 'openai_key', /sk-(?:proj-)?[A-Za-z0-9_\-]{20,}/g, 'critical', null],
|
|
78
|
+
['secret', 'anthropic_key', /sk-ant-[A-Za-z0-9_\-]{20,}/g, 'critical', null],
|
|
79
|
+
['secret', 'aws_access_key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g, 'critical', null],
|
|
80
|
+
['secret', 'github_token', /\bgh[pousr]_[A-Za-z0-9]{36,}\b/g, 'critical', null],
|
|
81
|
+
['secret', 'slack_token', /\bxox[baprs]-[A-Za-z0-9\-]{10,}\b/g, 'high', null],
|
|
82
|
+
['secret', 'google_api_key', /\bAIza[0-9A-Za-z_\-]{35}\b/g, 'high', null],
|
|
83
|
+
['secret', 'private_key_block', /-----BEGIN (?:RSA |EC |OPENSSH |PGP )?PRIVATE KEY-----/g, 'critical', null],
|
|
84
|
+
['secret', 'jwt', /\beyJ[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\.[A-Za-z0-9_\-]{10,}\b/g, 'high', null],
|
|
85
|
+
];
|
|
86
|
+
|
|
87
|
+
const INJECTION = [
|
|
88
|
+
[/ignore (?:all |the )?(?:previous|prior|above) (?:instructions|prompts?|rules)/gi, 'high'],
|
|
89
|
+
[/disregard (?:all |the )?(?:previous|prior|above)/gi, 'high'],
|
|
90
|
+
[/reveal (?:your |the )?(?:system|initial|hidden) prompt/gi, 'high'],
|
|
91
|
+
[/(?:what (?:is|are)|show|print|repeat|output) (?:your |the )?system prompt/gi, 'high'],
|
|
92
|
+
[/you are now (?:a |an )?(?:dan|developer mode|unrestricted|jailbroken)/gi, 'critical'],
|
|
93
|
+
[/pretend (?:you are|to be) (?:not |an )?(?:ai|unrestricted|without (?:rules|restrictions))/gi, 'medium'],
|
|
94
|
+
[/(?:forget|override) (?:all |your )?(?:safety |previous )?(?:rules|guidelines|instructions)/gi, 'high'],
|
|
95
|
+
[/act as (?:if you have|an unrestricted|a jailbroken)/gi, 'medium'],
|
|
96
|
+
[/repeat (?:the words |everything )?above/gi, 'medium'],
|
|
97
|
+
];
|
|
98
|
+
|
|
99
|
+
const SEV_ORDER = { low: 0, medium: 1, high: 2, critical: 3 };
|
|
100
|
+
const DEFAULT_RULES = ['pii', 'secret', 'injection'];
|
|
101
|
+
|
|
102
|
+
function pushAll(findings, rx, kind, rule, severity, text, validator) {
|
|
103
|
+
rx.lastIndex = 0;
|
|
104
|
+
let m;
|
|
105
|
+
while ((m = rx.exec(text)) !== null) {
|
|
106
|
+
if (m[0].length === 0) { rx.lastIndex++; continue; }
|
|
107
|
+
if (!validator || validator(m[0])) {
|
|
108
|
+
findings.push({ kind, rule, match: m[0], start: m.index, end: m.index + m[0].length, severity });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function dedupe(findings) {
|
|
114
|
+
const best = new Map();
|
|
115
|
+
for (const f of findings) {
|
|
116
|
+
const key = f.start + ':' + f.end;
|
|
117
|
+
const cur = best.get(key);
|
|
118
|
+
if (!cur || SEV_ORDER[f.severity] > SEV_ORDER[cur.severity]) best.set(key, f);
|
|
119
|
+
}
|
|
120
|
+
return Array.from(best.values()).sort((a, b) => a.start - b.start);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function scan(text, opts = {}) {
|
|
124
|
+
const rules = opts.rules || DEFAULT_RULES;
|
|
125
|
+
const extra = opts.extraSignatures || null;
|
|
126
|
+
const findings = [];
|
|
127
|
+
if (!text) return result(findings);
|
|
128
|
+
|
|
129
|
+
if (rules.includes('secret')) {
|
|
130
|
+
for (const [kind, rule, rx, sev] of SECRET_RULES) pushAll(findings, rx, kind, rule, sev, text, null);
|
|
131
|
+
}
|
|
132
|
+
if (rules.includes('pii')) {
|
|
133
|
+
pushAll(findings, EMAIL, 'pii', 'email', 'medium', text, null);
|
|
134
|
+
pushAll(findings, SSN, 'pii', 'us_ssn', 'high', text, null);
|
|
135
|
+
pushAll(findings, CARD, 'pii', 'credit_card', 'high', text, luhnOk);
|
|
136
|
+
pushAll(findings, CARD2, 'pii', 'credit_card', 'high', text, luhnOk);
|
|
137
|
+
pushAll(findings, AADHAAR, 'pii', 'aadhaar', 'high', text, verhoeffOk);
|
|
138
|
+
pushAll(findings, PHONE, 'pii', 'phone', 'low', text, null);
|
|
139
|
+
pushAll(findings, IPV4, 'pii', 'ipv4', 'low', text, null);
|
|
140
|
+
}
|
|
141
|
+
if (rules.includes('injection')) {
|
|
142
|
+
for (const [rx, sev] of INJECTION) pushAll(findings, rx, 'injection', 'prompt_injection', sev, text, null);
|
|
143
|
+
}
|
|
144
|
+
if (extra) {
|
|
145
|
+
for (const [kind, rule, rx, sev, validator] of extra) pushAll(findings, rx, kind, rule, sev, text, validator);
|
|
146
|
+
}
|
|
147
|
+
return result(dedupe(findings));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function result(findings) {
|
|
151
|
+
return {
|
|
152
|
+
findings,
|
|
153
|
+
get blocked() { return findings.some((f) => f.severity === 'high' || f.severity === 'critical'); },
|
|
154
|
+
get safe() { return findings.length === 0; },
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function redact(text, opts = {}) {
|
|
159
|
+
const mask = opts.mask || '[REDACTED:{rule}]';
|
|
160
|
+
const res = scan(text, opts);
|
|
161
|
+
let out = text;
|
|
162
|
+
const ordered = res.findings.slice().sort((a, b) => b.start - a.start);
|
|
163
|
+
for (const f of ordered) {
|
|
164
|
+
const token = mask.replace('{rule}', f.rule).replace('{kind}', f.kind);
|
|
165
|
+
out = out.slice(0, f.start) + token + out.slice(f.end);
|
|
166
|
+
}
|
|
167
|
+
return out;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
class FirewallBlocked extends Error {
|
|
171
|
+
constructor(message, result) {
|
|
172
|
+
super(message);
|
|
173
|
+
this.name = 'FirewallBlocked';
|
|
174
|
+
this.result = result;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function guard(text, opts = {}) {
|
|
179
|
+
const res = scan(text, opts);
|
|
180
|
+
if (res.blocked) {
|
|
181
|
+
const kinds = Array.from(new Set(res.findings.filter((f) => f.severity === 'high' || f.severity === 'critical').map((f) => f.rule))).sort();
|
|
182
|
+
throw new FirewallBlocked('prompt-firewall blocked: ' + kinds.join(', '), res);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function makeSignature(kind, rule, pattern, severity = 'high', validator = null) {
|
|
187
|
+
const rx = pattern instanceof RegExp ? new RegExp(pattern.source, pattern.flags.includes('g') ? pattern.flags : pattern.flags + 'g') : new RegExp(pattern, 'g');
|
|
188
|
+
return [kind, rule, rx, severity, validator];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
module.exports = {
|
|
192
|
+
VERSION, scan, redact, guard, FirewallBlocked,
|
|
193
|
+
luhnOk, verhoeffOk, shannonEntropy, makeSignature,
|
|
194
|
+
};
|
package/README.md
DELETED