@ct801/leak-shield 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/README.md +132 -2
- package/dist/index.cjs +276 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +93 -0
- package/dist/index.d.ts +93 -0
- package/dist/index.js +265 -0
- package/dist/index.js.map +1 -0
- package/package.json +56 -4
package/README.md
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
-
#
|
|
1
|
+
# leak-shield
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency, high-speed redaction tool for secrets, API tokens, and sensitive data in strings, objects, and Node.js streams.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Zero dependencies**: Pure TypeScript/JavaScript with minimal footprint.
|
|
8
|
+
- **Multi-target protection**: Redacts plain text, nested JavaScript objects/arrays, Node.js streams, and intercepts `console` methods.
|
|
9
|
+
- **Built-in patterns**: Covers OpenAI, Anthropic, GitHub, AWS, Stripe, Slack, Bearer headers, JWTs, private keys, and credit cards.
|
|
10
|
+
- **Customizable**: Add custom regex rules, adjust mask format (`simple`, `named`, `partial`), or disable specific rules.
|
|
11
|
+
- **Safe object handling**: Recursively sanitizes objects while safely handling circular references, Buffers, Dates, and Errors.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install leak-shield
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### 1. Basic String Redaction
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { leakShield } from 'leak-shield';
|
|
25
|
+
|
|
26
|
+
const rawLog = 'User initiated session with key sk-proj-abcdef123456789012345678';
|
|
27
|
+
const safeLog = leakShield.redact(rawLog);
|
|
28
|
+
|
|
29
|
+
console.log(safeLog);
|
|
30
|
+
// => "User initiated session with key [REDACTED]"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 2. Deep Object Sanitization
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { leakShield } from 'leak-shield';
|
|
37
|
+
|
|
38
|
+
const payload = {
|
|
39
|
+
id: 101,
|
|
40
|
+
username: 'alex',
|
|
41
|
+
password: 'superSecretPassword!',
|
|
42
|
+
apiKey: 'sk-proj-abcdef123456789012345678',
|
|
43
|
+
headers: {
|
|
44
|
+
authorization: 'Bearer tokenValue123'
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const sanitized = leakShield.redactObject(payload);
|
|
49
|
+
console.log(sanitized);
|
|
50
|
+
// Password, apiKey, and authorization headers are safely masked.
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 3. Node.js Transform Stream
|
|
54
|
+
|
|
55
|
+
Pipe stdout or file streams through `leak-shield`:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { createLeakShieldStream } from 'leak-shield';
|
|
59
|
+
|
|
60
|
+
// Pipe any readable stream through the shield
|
|
61
|
+
readableStream
|
|
62
|
+
.pipe(createLeakShieldStream())
|
|
63
|
+
.pipe(process.stdout);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 4. Console Interceptor
|
|
67
|
+
|
|
68
|
+
Scrub sensitive tokens from all outgoing `console.log` calls:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { leakShield, patchConsole } from 'leak-shield';
|
|
72
|
+
|
|
73
|
+
// Automatically redacts strings and objects passed to console
|
|
74
|
+
const restore = patchConsole(leakShield);
|
|
75
|
+
|
|
76
|
+
console.log('API key is:', 'sk-proj-1234567890abcdef1234567890');
|
|
77
|
+
// Output: API key is: [REDACTED]
|
|
78
|
+
|
|
79
|
+
// Restore original console behavior when needed
|
|
80
|
+
restore();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### 5. Formatting Options
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { LeakShield } from 'leak-shield';
|
|
87
|
+
|
|
88
|
+
// Named rule labels
|
|
89
|
+
const namedShield = new LeakShield({ formatMode: 'named' });
|
|
90
|
+
namedShield.redact('sk-proj-abcdef123456789012345678');
|
|
91
|
+
// => "[REDACTED: OpenAI API Key]"
|
|
92
|
+
|
|
93
|
+
// Partial masking (preserves prefix and tail)
|
|
94
|
+
const partialShield = new LeakShield({ formatMode: 'partial' });
|
|
95
|
+
partialShield.redact('sk-proj-abcdef123456789012345678');
|
|
96
|
+
// => "sk-p...5678"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Adding Custom Rules
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { LeakShield } from 'leak-shield';
|
|
103
|
+
|
|
104
|
+
const customShield = new LeakShield({
|
|
105
|
+
customRules: [
|
|
106
|
+
{
|
|
107
|
+
id: 'customer-id',
|
|
108
|
+
name: 'Customer ID',
|
|
109
|
+
pattern: /CUST-[0-9]{8}/g,
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Publishing to npm
|
|
116
|
+
|
|
117
|
+
1. Authenticate with npm:
|
|
118
|
+
```bash
|
|
119
|
+
npm login
|
|
120
|
+
```
|
|
121
|
+
2. Build and verify tests:
|
|
122
|
+
```bash
|
|
123
|
+
npm test
|
|
124
|
+
npm run build
|
|
125
|
+
```
|
|
126
|
+
3. Publish:
|
|
127
|
+
```bash
|
|
128
|
+
npm publish --access public
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## License
|
|
132
|
+
|
|
133
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var stream = require('stream');
|
|
6
|
+
|
|
7
|
+
// src/patterns.ts
|
|
8
|
+
function createPartialMask(input) {
|
|
9
|
+
if (input.length <= 8) {
|
|
10
|
+
return "***";
|
|
11
|
+
}
|
|
12
|
+
const headLength = Math.min(4, Math.floor(input.length / 4));
|
|
13
|
+
const tailLength = Math.min(3, Math.floor(input.length / 4));
|
|
14
|
+
const head = input.slice(0, headLength);
|
|
15
|
+
const tail = input.slice(input.length - tailLength);
|
|
16
|
+
return `${head}...${tail}`;
|
|
17
|
+
}
|
|
18
|
+
var DEFAULT_RULES = [
|
|
19
|
+
{
|
|
20
|
+
id: "openai-key",
|
|
21
|
+
name: "OpenAI API Key",
|
|
22
|
+
pattern: /\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\b/g,
|
|
23
|
+
description: "Matches standard and project-scoped OpenAI API keys"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "anthropic-key",
|
|
27
|
+
name: "Anthropic API Key",
|
|
28
|
+
pattern: /\bsk-ant-[A-Za-z0-9_-]{20,}\b/g,
|
|
29
|
+
description: "Matches Anthropic Claude API keys"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
id: "github-token",
|
|
33
|
+
name: "GitHub Token",
|
|
34
|
+
pattern: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\b|\bgithub_pat_[A-Za-z0-9_]{22,}\b/g,
|
|
35
|
+
description: "Matches GitHub personal access and fine-grained tokens"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: "aws-access-key",
|
|
39
|
+
name: "AWS Access Key ID",
|
|
40
|
+
pattern: /\b(?:AKIA|ASIA|AROA)[A-Z0-9]{16}\b/g,
|
|
41
|
+
description: "Matches AWS access key IDs"
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
id: "aws-secret-key",
|
|
45
|
+
name: "AWS Secret Access Key",
|
|
46
|
+
pattern: /(?<=\b(?:aws_secret_access_key|aws_secret_key)\s*[:=]\s*["']?)[A-Za-z0-9/+=]{40}(?=["']?)/gi,
|
|
47
|
+
description: "Matches AWS secret access key assignments"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: "stripe-key",
|
|
51
|
+
name: "Stripe API Key",
|
|
52
|
+
pattern: /\b(?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,}\b/g,
|
|
53
|
+
description: "Matches Stripe standard and restricted API keys"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: "slack-token",
|
|
57
|
+
name: "Slack Token",
|
|
58
|
+
pattern: /\bxox[baprs]-[0-9a-zA-Z]{10,48}\b/g,
|
|
59
|
+
description: "Matches Slack bot, app, and user tokens"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
id: "bearer-token",
|
|
63
|
+
name: "Bearer Token",
|
|
64
|
+
pattern: /(?<=\bBearer\s+)[A-Za-z0-9\-._~+/]+=*/g,
|
|
65
|
+
description: "Matches HTTP Bearer authorization tokens"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
id: "jwt-token",
|
|
69
|
+
name: "JSON Web Token",
|
|
70
|
+
pattern: /\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g,
|
|
71
|
+
description: "Matches standard 3-segment JSON Web Tokens"
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: "private-key",
|
|
75
|
+
name: "Private Key Block",
|
|
76
|
+
pattern: /-----BEGIN (?:[A-Z ]+)?PRIVATE KEY-----[\s\S]*?-----END (?:[A-Z ]+)?PRIVATE KEY-----/g,
|
|
77
|
+
description: "Matches PEM formatted private key certificates"
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "credit-card",
|
|
81
|
+
name: "Credit Card Number",
|
|
82
|
+
pattern: /\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})\b/g,
|
|
83
|
+
description: "Matches major credit card number patterns"
|
|
84
|
+
}
|
|
85
|
+
];
|
|
86
|
+
var DEFAULT_SENSITIVE_KEYS = [
|
|
87
|
+
"password",
|
|
88
|
+
"passwd",
|
|
89
|
+
"pwd",
|
|
90
|
+
"secret",
|
|
91
|
+
"token",
|
|
92
|
+
"apikey",
|
|
93
|
+
"api_key",
|
|
94
|
+
"access_token",
|
|
95
|
+
"auth_token",
|
|
96
|
+
"authorization",
|
|
97
|
+
"private_key",
|
|
98
|
+
"privatekey",
|
|
99
|
+
"credit_card",
|
|
100
|
+
"creditcard",
|
|
101
|
+
"card_number",
|
|
102
|
+
"cvv",
|
|
103
|
+
"cvc",
|
|
104
|
+
"ssn"
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// src/redactor.ts
|
|
108
|
+
var LeakShield = class {
|
|
109
|
+
rules;
|
|
110
|
+
sensitiveKeys;
|
|
111
|
+
formatMode;
|
|
112
|
+
customMask;
|
|
113
|
+
constructor(options = {}) {
|
|
114
|
+
const disabled = new Set(options.disabledRules || []);
|
|
115
|
+
const baseRules = DEFAULT_RULES.filter((rule) => !disabled.has(rule.id));
|
|
116
|
+
const custom = options.customRules || [];
|
|
117
|
+
this.rules = [...baseRules, ...custom];
|
|
118
|
+
this.formatMode = options.formatMode || "simple";
|
|
119
|
+
this.customMask = options.mask;
|
|
120
|
+
const keys = options.sensitiveKeys || DEFAULT_SENSITIVE_KEYS;
|
|
121
|
+
this.sensitiveKeys = new Set(keys.map((k) => k.toLowerCase()));
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Resolves the masked string for a matched secret rule
|
|
125
|
+
*/
|
|
126
|
+
formatMatch(matched, rule) {
|
|
127
|
+
if (typeof this.customMask === "function") {
|
|
128
|
+
return this.customMask(matched, rule);
|
|
129
|
+
}
|
|
130
|
+
if (typeof this.customMask === "string") {
|
|
131
|
+
return this.customMask;
|
|
132
|
+
}
|
|
133
|
+
if (rule.mask) {
|
|
134
|
+
return typeof rule.mask === "function" ? rule.mask(matched) : rule.mask;
|
|
135
|
+
}
|
|
136
|
+
switch (this.formatMode) {
|
|
137
|
+
case "named":
|
|
138
|
+
return `[REDACTED: ${rule.name}]`;
|
|
139
|
+
case "partial":
|
|
140
|
+
return createPartialMask(matched);
|
|
141
|
+
case "simple":
|
|
142
|
+
default:
|
|
143
|
+
return "[REDACTED]";
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Redacts secrets and sensitive tokens from a plain string
|
|
148
|
+
*/
|
|
149
|
+
redact(input) {
|
|
150
|
+
if (!input || typeof input !== "string") {
|
|
151
|
+
return input;
|
|
152
|
+
}
|
|
153
|
+
let result = input;
|
|
154
|
+
for (const rule of this.rules) {
|
|
155
|
+
rule.pattern.lastIndex = 0;
|
|
156
|
+
result = result.replace(rule.pattern, (matched) => this.formatMatch(matched, rule));
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Deeply redacts sensitive keys and values inside an object, array, or error.
|
|
162
|
+
* Circular references are handled safely.
|
|
163
|
+
*/
|
|
164
|
+
redactObject(input, seen = /* @__PURE__ */ new WeakSet()) {
|
|
165
|
+
if (input === null || typeof input !== "object") {
|
|
166
|
+
if (typeof input === "string") {
|
|
167
|
+
return this.redact(input);
|
|
168
|
+
}
|
|
169
|
+
return input;
|
|
170
|
+
}
|
|
171
|
+
if (input instanceof Date || input instanceof RegExp || typeof Buffer !== "undefined" && Buffer.isBuffer(input)) {
|
|
172
|
+
return input;
|
|
173
|
+
}
|
|
174
|
+
if (seen.has(input)) {
|
|
175
|
+
return "[Circular]";
|
|
176
|
+
}
|
|
177
|
+
seen.add(input);
|
|
178
|
+
if (Array.isArray(input)) {
|
|
179
|
+
return input.map((item) => this.redactObject(item, seen));
|
|
180
|
+
}
|
|
181
|
+
if (input instanceof Error) {
|
|
182
|
+
const copy = new input.constructor(
|
|
183
|
+
this.redact(input.message)
|
|
184
|
+
);
|
|
185
|
+
if (input.stack) {
|
|
186
|
+
copy.stack = this.redact(input.stack);
|
|
187
|
+
}
|
|
188
|
+
return copy;
|
|
189
|
+
}
|
|
190
|
+
const output = {};
|
|
191
|
+
for (const [key, value] of Object.entries(input)) {
|
|
192
|
+
const normalizedKey = key.toLowerCase().replace(/[-_]/g, "");
|
|
193
|
+
const isSensitiveKey = Array.from(this.sensitiveKeys).some(
|
|
194
|
+
(sensitive) => normalizedKey.includes(sensitive.replace(/[-_]/g, ""))
|
|
195
|
+
);
|
|
196
|
+
if (isSensitiveKey) {
|
|
197
|
+
if (typeof value === "string") {
|
|
198
|
+
output[key] = this.formatMode === "partial" ? createPartialMask(value) : "[REDACTED]";
|
|
199
|
+
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
200
|
+
output[key] = "[REDACTED]";
|
|
201
|
+
} else {
|
|
202
|
+
output[key] = this.redactObject(value, seen);
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
output[key] = this.redactObject(value, seen);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return output;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Returns list of configured active rules
|
|
212
|
+
*/
|
|
213
|
+
getRules() {
|
|
214
|
+
return this.rules;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
function createLeakShieldStream(options = {}) {
|
|
218
|
+
const shield = new LeakShield(options);
|
|
219
|
+
const encoding = options.encoding || "utf8";
|
|
220
|
+
return new stream.Transform({
|
|
221
|
+
transform(chunk, _enc, callback) {
|
|
222
|
+
try {
|
|
223
|
+
const text = typeof chunk === "string" ? chunk : chunk.toString(encoding);
|
|
224
|
+
const redacted = shield.redact(text);
|
|
225
|
+
callback(null, Buffer.from(redacted, encoding));
|
|
226
|
+
} catch (err) {
|
|
227
|
+
callback(err);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/console.ts
|
|
234
|
+
var DEFAULT_METHODS = ["log", "info", "warn", "error", "debug"];
|
|
235
|
+
function patchConsole(shield, methods = DEFAULT_METHODS) {
|
|
236
|
+
const originalMethods = /* @__PURE__ */ new Map();
|
|
237
|
+
for (const method of methods) {
|
|
238
|
+
if (typeof console[method] === "function") {
|
|
239
|
+
const original = console[method].bind(console);
|
|
240
|
+
originalMethods.set(method, original);
|
|
241
|
+
console[method] = (...args) => {
|
|
242
|
+
const sanitizedArgs = args.map((arg) => {
|
|
243
|
+
if (typeof arg === "string") {
|
|
244
|
+
return shield.redact(arg);
|
|
245
|
+
}
|
|
246
|
+
if (arg !== null && typeof arg === "object") {
|
|
247
|
+
return shield.redactObject(arg);
|
|
248
|
+
}
|
|
249
|
+
return arg;
|
|
250
|
+
});
|
|
251
|
+
original(...sanitizedArgs);
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return function unpatch() {
|
|
256
|
+
for (const [method, original] of originalMethods.entries()) {
|
|
257
|
+
console[method] = original;
|
|
258
|
+
}
|
|
259
|
+
originalMethods.clear();
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/index.ts
|
|
264
|
+
var leakShield = new LeakShield();
|
|
265
|
+
var index_default = leakShield;
|
|
266
|
+
|
|
267
|
+
exports.DEFAULT_RULES = DEFAULT_RULES;
|
|
268
|
+
exports.DEFAULT_SENSITIVE_KEYS = DEFAULT_SENSITIVE_KEYS;
|
|
269
|
+
exports.LeakShield = LeakShield;
|
|
270
|
+
exports.createLeakShieldStream = createLeakShieldStream;
|
|
271
|
+
exports.createPartialMask = createPartialMask;
|
|
272
|
+
exports.default = index_default;
|
|
273
|
+
exports.leakShield = leakShield;
|
|
274
|
+
exports.patchConsole = patchConsole;
|
|
275
|
+
//# sourceMappingURL=index.cjs.map
|
|
276
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/patterns.ts","../src/redactor.ts","../src/stream.ts","../src/console.ts","../src/index.ts"],"names":["Transform"],"mappings":";;;;;;;AAMO,SAAS,kBAAkB,KAAA,EAAuB;AACvD,EAAA,IAAI,KAAA,CAAM,UAAU,CAAA,EAAG;AACrB,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,MAAM,UAAA,GAAa,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,KAAA,CAAM,KAAA,CAAM,MAAA,GAAS,CAAC,CAAC,CAAA;AAC3D,EAAA,MAAM,UAAA,GAAa,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,KAAA,CAAM,KAAA,CAAM,MAAA,GAAS,CAAC,CAAC,CAAA;AAC3D,EAAA,MAAM,IAAA,GAAO,KAAA,CAAM,KAAA,CAAM,CAAA,EAAG,UAAU,CAAA;AACtC,EAAA,MAAM,IAAA,GAAO,KAAA,CAAM,KAAA,CAAM,KAAA,CAAM,SAAS,UAAU,CAAA;AAClD,EAAA,OAAO,CAAA,EAAG,IAAI,CAAA,GAAA,EAAM,IAAI,CAAA,CAAA;AAC1B;AAEO,IAAM,aAAA,GAAwB;AAAA,EACnC;AAAA,IACE,EAAA,EAAI,YAAA;AAAA,IACJ,IAAA,EAAM,gBAAA;AAAA,IACN,OAAA,EAAS,sCAAA;AAAA,IACT,WAAA,EAAa;AAAA,GACf;AAAA,EACA;AAAA,IACE,EAAA,EAAI,eAAA;AAAA,IACJ,IAAA,EAAM,mBAAA;AAAA,IACN,OAAA,EAAS,gCAAA;AAAA,IACT,WAAA,EAAa;AAAA,GACf;AAAA,EACA;AAAA,IACE,EAAA,EAAI,cAAA;AAAA,IACJ,IAAA,EAAM,cAAA;AAAA,IACN,OAAA,EAAS,gFAAA;AAAA,IACT,WAAA,EAAa;AAAA,GACf;AAAA,EACA;AAAA,IACE,EAAA,EAAI,gBAAA;AAAA,IACJ,IAAA,EAAM,mBAAA;AAAA,IACN,OAAA,EAAS,qCAAA;AAAA,IACT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AA,GAAa,IAAI,UAAA;AAQ9B,IAAO,aAAA,GAAQ","file":"index.cjs","sourcesContent":["import { Rule } from './types.js';\n\n/**\n * Creates a partial mask preserving the head and tail of the token\n * e.g. \"sk-abcdef123456\" -> \"sk-a...3456\"\n */\nexport function createPartialMask(input: string): string {\n if (input.length <= 8) {\n return '***';\n }\n const headLength = Math.min(4, Math.floor(input.length / 4));\n const tailLength = Math.min(3, Math.floor(input.length / 4));\n const head = input.slice(0, headLength);\n const tail = input.slice(input.length - tailLength);\n return `${head}...${tail}`;\n}\n\nexport const DEFAULT_RULES: Rule[] = [\n {\n id: 'openai-key',\n name: 'OpenAI API Key',\n pattern: /\\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\\b/g,\n description: 'Matches standard and project-scoped OpenAI API keys',\n },\n {\n id: 'anthropic-key',\n name: 'Anthropic API Key',\n pattern: /\\bsk-ant-[A-Za-z0-9_-]{20,}\\b/g,\n description: 'Matches Anthropic Claude API keys',\n },\n {\n id: 'github-token',\n name: 'GitHub Token',\n pattern: /\\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\\b|\\bgithub_pat_[A-Za-z0-9_]{22,}\\b/g,\n description: 'Matches GitHub personal access and fine-grained tokens',\n },\n {\n id: 'aws-access-key',\n name: 'AWS Access Key ID',\n pattern: /\\b(?:AKIA|ASIA|AROA)[A-Z0-9]{16}\\b/g,\n description: 'Matches AWS access key IDs',\n },\n {\n id: 'aws-secret-key',\n name: 'AWS Secret Access Key',\n pattern: /(?<=\\b(?:aws_secret_access_key|aws_secret_key)\\s*[:=]\\s*[\"']?)[A-Za-z0-9/+=]{40}(?=[\"']?)/gi,\n description: 'Matches AWS secret access key assignments',\n },\n {\n id: 'stripe-key',\n name: 'Stripe API Key',\n pattern: /\\b(?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,}\\b/g,\n description: 'Matches Stripe standard and restricted API keys',\n },\n {\n id: 'slack-token',\n name: 'Slack Token',\n pattern: /\\bxox[baprs]-[0-9a-zA-Z]{10,48}\\b/g,\n description: 'Matches Slack bot, app, and user tokens',\n },\n {\n id: 'bearer-token',\n name: 'Bearer Token',\n pattern: /(?<=\\bBearer\\s+)[A-Za-z0-9\\-._~+/]+=*/g,\n description: 'Matches HTTP Bearer authorization tokens',\n },\n {\n id: 'jwt-token',\n name: 'JSON Web Token',\n pattern: /\\beyJ[A-Za-z0-9_-]{10,}\\.eyJ[A-Za-z0-9_-]{10,}\\.[A-Za-z0-9_-]{10,}\\b/g,\n description: 'Matches standard 3-segment JSON Web Tokens',\n },\n {\n id: 'private-key',\n name: 'Private Key Block',\n pattern: /-----BEGIN (?:[A-Z ]+)?PRIVATE KEY-----[\\s\\S]*?-----END (?:[A-Z ]+)?PRIVATE KEY-----/g,\n description: 'Matches PEM formatted private key certificates',\n },\n {\n id: 'credit-card',\n name: 'Credit Card Number',\n pattern: /\\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\\d{3})\\d{11})\\b/g,\n description: 'Matches major credit card number patterns',\n },\n];\n\nexport const DEFAULT_SENSITIVE_KEYS: string[] = [\n 'password',\n 'passwd',\n 'pwd',\n 'secret',\n 'token',\n 'apikey',\n 'api_key',\n 'access_token',\n 'auth_token',\n 'authorization',\n 'private_key',\n 'privatekey',\n 'credit_card',\n 'creditcard',\n 'card_number',\n 'cvv',\n 'cvc',\n 'ssn',\n];\n","import { LeakShieldOptions, Rule, MaskFormatter } from './types.js';\nimport { DEFAULT_RULES, DEFAULT_SENSITIVE_KEYS, createPartialMask } from './patterns.js';\n\nexport class LeakShield {\n private rules: Rule[];\n private sensitiveKeys: Set<string>;\n private formatMode: 'simple' | 'named' | 'partial';\n private customMask?: string | MaskFormatter;\n\n constructor(options: LeakShieldOptions = {}) {\n const disabled = new Set(options.disabledRules || []);\n const baseRules = DEFAULT_RULES.filter((rule) => !disabled.has(rule.id));\n const custom = options.customRules || [];\n\n this.rules = [...baseRules, ...custom];\n this.formatMode = options.formatMode || 'simple';\n this.customMask = options.mask;\n\n const keys = options.sensitiveKeys || DEFAULT_SENSITIVE_KEYS;\n this.sensitiveKeys = new Set(keys.map((k) => k.toLowerCase()));\n }\n\n /**\n * Resolves the masked string for a matched secret rule\n */\n private formatMatch(matched: string, rule: Rule): string {\n if (typeof this.customMask === 'function') {\n return this.customMask(matched, rule);\n }\n if (typeof this.customMask === 'string') {\n return this.customMask;\n }\n if (rule.mask) {\n return typeof rule.mask === 'function' ? rule.mask(matched) : rule.mask;\n }\n\n switch (this.formatMode) {\n case 'named':\n return `[REDACTED: ${rule.name}]`;\n case 'partial':\n return createPartialMask(matched);\n case 'simple':\n default:\n return '[REDACTED]';\n }\n }\n\n /**\n * Redacts secrets and sensitive tokens from a plain string\n */\n redact(input: string): string {\n if (!input || typeof input !== 'string') {\n return input;\n }\n\n let result = input;\n for (const rule of this.rules) {\n // Reset lastIndex for global regexes\n rule.pattern.lastIndex = 0;\n result = result.replace(rule.pattern, (matched) => this.formatMatch(matched, rule));\n }\n\n return result;\n }\n\n /**\n * Deeply redacts sensitive keys and values inside an object, array, or error.\n * Circular references are handled safely.\n */\n redactObject<T>(input: T, seen = new WeakSet()): T {\n if (input === null || typeof input !== 'object') {\n if (typeof input === 'string') {\n return this.redact(input) as unknown as T;\n }\n return input;\n }\n\n // Pass through primitives, Buffers, Dates, RegExp\n if (\n input instanceof Date ||\n input instanceof RegExp ||\n (typeof Buffer !== 'undefined' && Buffer.isBuffer(input))\n ) {\n return input;\n }\n\n // Handle circular structures\n if (seen.has(input)) {\n return '[Circular]' as unknown as T;\n }\n seen.add(input);\n\n if (Array.isArray(input)) {\n return input.map((item) => this.redactObject(item, seen)) as unknown as T;\n }\n\n if (input instanceof Error) {\n const copy = new (input.constructor as new (message?: string) => Error)(\n this.redact(input.message)\n );\n if (input.stack) {\n copy.stack = this.redact(input.stack);\n }\n return copy as unknown as T;\n }\n\n const output: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(input)) {\n const normalizedKey = key.toLowerCase().replace(/[-_]/g, '');\n const isSensitiveKey = Array.from(this.sensitiveKeys).some((sensitive) =>\n normalizedKey.includes(sensitive.replace(/[-_]/g, ''))\n );\n\n if (isSensitiveKey) {\n if (typeof value === 'string') {\n output[key] = this.formatMode === 'partial' ? createPartialMask(value) : '[REDACTED]';\n } else if (typeof value === 'number' || typeof value === 'boolean') {\n output[key] = '[REDACTED]';\n } else {\n output[key] = this.redactObject(value, seen);\n }\n } else {\n output[key] = this.redactObject(value, seen);\n }\n }\n\n return output as T;\n }\n\n /**\n * Returns list of configured active rules\n */\n getRules(): ReadonlyArray<Rule> {\n return this.rules;\n }\n}\n","import { Transform, TransformCallback } from 'node:stream';\nimport { LeakShield } from './redactor.js';\nimport { StreamShieldOptions } from './types.js';\n\n/**\n * Creates a Node.js Transform Stream that redacts secrets from passing chunks.\n * Perfect for piping to process.stdout or log file write streams.\n */\nexport function createLeakShieldStream(options: StreamShieldOptions = {}): Transform {\n const shield = new LeakShield(options);\n const encoding = options.encoding || 'utf8';\n\n return new Transform({\n transform(chunk: Buffer | string, _enc: BufferEncoding, callback: TransformCallback) {\n try {\n const text = typeof chunk === 'string' ? chunk : chunk.toString(encoding);\n const redacted = shield.redact(text);\n callback(null, Buffer.from(redacted, encoding));\n } catch (err) {\n callback(err as Error);\n }\n },\n });\n}\n","import { LeakShield } from './redactor.js';\n\nexport type ConsoleMethod = 'log' | 'info' | 'warn' | 'error' | 'debug';\n\nconst DEFAULT_METHODS: ConsoleMethod[] = ['log', 'info', 'warn', 'error', 'debug'];\n\n/**\n * Monkey-patches console methods to automatically redact secrets from arguments.\n * Returns an unpatch function to restore original console behavior.\n */\nexport function patchConsole(\n shield: LeakShield,\n methods: ConsoleMethod[] = DEFAULT_METHODS\n): () => void {\n const originalMethods = new Map<ConsoleMethod, (...args: unknown[]) => void>();\n\n for (const method of methods) {\n if (typeof console[method] === 'function') {\n const original = console[method].bind(console);\n originalMethods.set(method, original);\n\n console[method] = (...args: unknown[]) => {\n const sanitizedArgs = args.map((arg) => {\n if (typeof arg === 'string') {\n return shield.redact(arg);\n }\n if (arg !== null && typeof arg === 'object') {\n return shield.redactObject(arg);\n }\n return arg;\n });\n\n original(...sanitizedArgs);\n };\n }\n }\n\n return function unpatch(): void {\n for (const [method, original] of originalMethods.entries()) {\n console[method] = original;\n }\n originalMethods.clear();\n };\n}\n","import { LeakShield } from './redactor.js';\nimport { createLeakShieldStream } from './stream.js';\nimport { patchConsole } from './console.js';\n\n// Default global singleton instance\nexport const leakShield = new LeakShield();\n\nexport * from './types.js';\nexport * from './patterns.js';\nexport * from './redactor.js';\nexport * from './stream.js';\nexport * from './console.js';\n\nexport default leakShield;\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
|
|
3
|
+
interface Rule {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
pattern: RegExp;
|
|
7
|
+
mask?: string | ((matched: string) => string);
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
type MaskFormatter = (matched: string, rule: Rule) => string;
|
|
11
|
+
interface LeakShieldOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Custom replacement string or custom formatter function.
|
|
14
|
+
*/
|
|
15
|
+
mask?: string | MaskFormatter;
|
|
16
|
+
/**
|
|
17
|
+
* Formatting mode for redaction:
|
|
18
|
+
* - 'simple': replaces with "[REDACTED]"
|
|
19
|
+
* - 'named': replaces with "[REDACTED: <RULE_NAME>]"
|
|
20
|
+
* - 'partial': preserves first few and last few characters (e.g. "sk-123***89")
|
|
21
|
+
* Default: 'simple'
|
|
22
|
+
*/
|
|
23
|
+
formatMode?: 'simple' | 'named' | 'partial';
|
|
24
|
+
/**
|
|
25
|
+
* List of rule IDs to disable.
|
|
26
|
+
*/
|
|
27
|
+
disabledRules?: string[];
|
|
28
|
+
/**
|
|
29
|
+
* Additional custom rules to apply.
|
|
30
|
+
*/
|
|
31
|
+
customRules?: Rule[];
|
|
32
|
+
/**
|
|
33
|
+
* Object keys to mask in deep object sanitization.
|
|
34
|
+
* Default includes: ['password', 'secret', 'token', 'apikey', 'authorization', 'creditcard', 'privatekey']
|
|
35
|
+
*/
|
|
36
|
+
sensitiveKeys?: string[];
|
|
37
|
+
}
|
|
38
|
+
interface StreamShieldOptions extends LeakShieldOptions {
|
|
39
|
+
/**
|
|
40
|
+
* Default chunk string encoding. Defaults to 'utf8'.
|
|
41
|
+
*/
|
|
42
|
+
encoding?: BufferEncoding;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare class LeakShield {
|
|
46
|
+
private rules;
|
|
47
|
+
private sensitiveKeys;
|
|
48
|
+
private formatMode;
|
|
49
|
+
private customMask?;
|
|
50
|
+
constructor(options?: LeakShieldOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Resolves the masked string for a matched secret rule
|
|
53
|
+
*/
|
|
54
|
+
private formatMatch;
|
|
55
|
+
/**
|
|
56
|
+
* Redacts secrets and sensitive tokens from a plain string
|
|
57
|
+
*/
|
|
58
|
+
redact(input: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Deeply redacts sensitive keys and values inside an object, array, or error.
|
|
61
|
+
* Circular references are handled safely.
|
|
62
|
+
*/
|
|
63
|
+
redactObject<T>(input: T, seen?: WeakSet<WeakKey>): T;
|
|
64
|
+
/**
|
|
65
|
+
* Returns list of configured active rules
|
|
66
|
+
*/
|
|
67
|
+
getRules(): ReadonlyArray<Rule>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Creates a partial mask preserving the head and tail of the token
|
|
72
|
+
* e.g. "sk-abcdef123456" -> "sk-a...3456"
|
|
73
|
+
*/
|
|
74
|
+
declare function createPartialMask(input: string): string;
|
|
75
|
+
declare const DEFAULT_RULES: Rule[];
|
|
76
|
+
declare const DEFAULT_SENSITIVE_KEYS: string[];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a Node.js Transform Stream that redacts secrets from passing chunks.
|
|
80
|
+
* Perfect for piping to process.stdout or log file write streams.
|
|
81
|
+
*/
|
|
82
|
+
declare function createLeakShieldStream(options?: StreamShieldOptions): Transform;
|
|
83
|
+
|
|
84
|
+
type ConsoleMethod = 'log' | 'info' | 'warn' | 'error' | 'debug';
|
|
85
|
+
/**
|
|
86
|
+
* Monkey-patches console methods to automatically redact secrets from arguments.
|
|
87
|
+
* Returns an unpatch function to restore original console behavior.
|
|
88
|
+
*/
|
|
89
|
+
declare function patchConsole(shield: LeakShield, methods?: ConsoleMethod[]): () => void;
|
|
90
|
+
|
|
91
|
+
declare const leakShield: LeakShield;
|
|
92
|
+
|
|
93
|
+
export { type ConsoleMethod, DEFAULT_RULES, DEFAULT_SENSITIVE_KEYS, LeakShield, type LeakShieldOptions, type MaskFormatter, type Rule, type StreamShieldOptions, createLeakShieldStream, createPartialMask, leakShield as default, leakShield, patchConsole };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Transform } from 'node:stream';
|
|
2
|
+
|
|
3
|
+
interface Rule {
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
pattern: RegExp;
|
|
7
|
+
mask?: string | ((matched: string) => string);
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
type MaskFormatter = (matched: string, rule: Rule) => string;
|
|
11
|
+
interface LeakShieldOptions {
|
|
12
|
+
/**
|
|
13
|
+
* Custom replacement string or custom formatter function.
|
|
14
|
+
*/
|
|
15
|
+
mask?: string | MaskFormatter;
|
|
16
|
+
/**
|
|
17
|
+
* Formatting mode for redaction:
|
|
18
|
+
* - 'simple': replaces with "[REDACTED]"
|
|
19
|
+
* - 'named': replaces with "[REDACTED: <RULE_NAME>]"
|
|
20
|
+
* - 'partial': preserves first few and last few characters (e.g. "sk-123***89")
|
|
21
|
+
* Default: 'simple'
|
|
22
|
+
*/
|
|
23
|
+
formatMode?: 'simple' | 'named' | 'partial';
|
|
24
|
+
/**
|
|
25
|
+
* List of rule IDs to disable.
|
|
26
|
+
*/
|
|
27
|
+
disabledRules?: string[];
|
|
28
|
+
/**
|
|
29
|
+
* Additional custom rules to apply.
|
|
30
|
+
*/
|
|
31
|
+
customRules?: Rule[];
|
|
32
|
+
/**
|
|
33
|
+
* Object keys to mask in deep object sanitization.
|
|
34
|
+
* Default includes: ['password', 'secret', 'token', 'apikey', 'authorization', 'creditcard', 'privatekey']
|
|
35
|
+
*/
|
|
36
|
+
sensitiveKeys?: string[];
|
|
37
|
+
}
|
|
38
|
+
interface StreamShieldOptions extends LeakShieldOptions {
|
|
39
|
+
/**
|
|
40
|
+
* Default chunk string encoding. Defaults to 'utf8'.
|
|
41
|
+
*/
|
|
42
|
+
encoding?: BufferEncoding;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare class LeakShield {
|
|
46
|
+
private rules;
|
|
47
|
+
private sensitiveKeys;
|
|
48
|
+
private formatMode;
|
|
49
|
+
private customMask?;
|
|
50
|
+
constructor(options?: LeakShieldOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Resolves the masked string for a matched secret rule
|
|
53
|
+
*/
|
|
54
|
+
private formatMatch;
|
|
55
|
+
/**
|
|
56
|
+
* Redacts secrets and sensitive tokens from a plain string
|
|
57
|
+
*/
|
|
58
|
+
redact(input: string): string;
|
|
59
|
+
/**
|
|
60
|
+
* Deeply redacts sensitive keys and values inside an object, array, or error.
|
|
61
|
+
* Circular references are handled safely.
|
|
62
|
+
*/
|
|
63
|
+
redactObject<T>(input: T, seen?: WeakSet<WeakKey>): T;
|
|
64
|
+
/**
|
|
65
|
+
* Returns list of configured active rules
|
|
66
|
+
*/
|
|
67
|
+
getRules(): ReadonlyArray<Rule>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Creates a partial mask preserving the head and tail of the token
|
|
72
|
+
* e.g. "sk-abcdef123456" -> "sk-a...3456"
|
|
73
|
+
*/
|
|
74
|
+
declare function createPartialMask(input: string): string;
|
|
75
|
+
declare const DEFAULT_RULES: Rule[];
|
|
76
|
+
declare const DEFAULT_SENSITIVE_KEYS: string[];
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a Node.js Transform Stream that redacts secrets from passing chunks.
|
|
80
|
+
* Perfect for piping to process.stdout or log file write streams.
|
|
81
|
+
*/
|
|
82
|
+
declare function createLeakShieldStream(options?: StreamShieldOptions): Transform;
|
|
83
|
+
|
|
84
|
+
type ConsoleMethod = 'log' | 'info' | 'warn' | 'error' | 'debug';
|
|
85
|
+
/**
|
|
86
|
+
* Monkey-patches console methods to automatically redact secrets from arguments.
|
|
87
|
+
* Returns an unpatch function to restore original console behavior.
|
|
88
|
+
*/
|
|
89
|
+
declare function patchConsole(shield: LeakShield, methods?: ConsoleMethod[]): () => void;
|
|
90
|
+
|
|
91
|
+
declare const leakShield: LeakShield;
|
|
92
|
+
|
|
93
|
+
export { type ConsoleMethod, DEFAULT_RULES, DEFAULT_SENSITIVE_KEYS, LeakShield, type LeakShieldOptions, type MaskFormatter, type Rule, type StreamShieldOptions, createLeakShieldStream, createPartialMask, leakShield as default, leakShield, patchConsole };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { Transform } from 'stream';
|
|
2
|
+
|
|
3
|
+
// src/patterns.ts
|
|
4
|
+
function createPartialMask(input) {
|
|
5
|
+
if (input.length <= 8) {
|
|
6
|
+
return "***";
|
|
7
|
+
}
|
|
8
|
+
const headLength = Math.min(4, Math.floor(input.length / 4));
|
|
9
|
+
const tailLength = Math.min(3, Math.floor(input.length / 4));
|
|
10
|
+
const head = input.slice(0, headLength);
|
|
11
|
+
const tail = input.slice(input.length - tailLength);
|
|
12
|
+
return `${head}...${tail}`;
|
|
13
|
+
}
|
|
14
|
+
var DEFAULT_RULES = [
|
|
15
|
+
{
|
|
16
|
+
id: "openai-key",
|
|
17
|
+
name: "OpenAI API Key",
|
|
18
|
+
pattern: /\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\b/g,
|
|
19
|
+
description: "Matches standard and project-scoped OpenAI API keys"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: "anthropic-key",
|
|
23
|
+
name: "Anthropic API Key",
|
|
24
|
+
pattern: /\bsk-ant-[A-Za-z0-9_-]{20,}\b/g,
|
|
25
|
+
description: "Matches Anthropic Claude API keys"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "github-token",
|
|
29
|
+
name: "GitHub Token",
|
|
30
|
+
pattern: /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\b|\bgithub_pat_[A-Za-z0-9_]{22,}\b/g,
|
|
31
|
+
description: "Matches GitHub personal access and fine-grained tokens"
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: "aws-access-key",
|
|
35
|
+
name: "AWS Access Key ID",
|
|
36
|
+
pattern: /\b(?:AKIA|ASIA|AROA)[A-Z0-9]{16}\b/g,
|
|
37
|
+
description: "Matches AWS access key IDs"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
id: "aws-secret-key",
|
|
41
|
+
name: "AWS Secret Access Key",
|
|
42
|
+
pattern: /(?<=\b(?:aws_secret_access_key|aws_secret_key)\s*[:=]\s*["']?)[A-Za-z0-9/+=]{40}(?=["']?)/gi,
|
|
43
|
+
description: "Matches AWS secret access key assignments"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: "stripe-key",
|
|
47
|
+
name: "Stripe API Key",
|
|
48
|
+
pattern: /\b(?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,}\b/g,
|
|
49
|
+
description: "Matches Stripe standard and restricted API keys"
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: "slack-token",
|
|
53
|
+
name: "Slack Token",
|
|
54
|
+
pattern: /\bxox[baprs]-[0-9a-zA-Z]{10,48}\b/g,
|
|
55
|
+
description: "Matches Slack bot, app, and user tokens"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: "bearer-token",
|
|
59
|
+
name: "Bearer Token",
|
|
60
|
+
pattern: /(?<=\bBearer\s+)[A-Za-z0-9\-._~+/]+=*/g,
|
|
61
|
+
description: "Matches HTTP Bearer authorization tokens"
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
id: "jwt-token",
|
|
65
|
+
name: "JSON Web Token",
|
|
66
|
+
pattern: /\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g,
|
|
67
|
+
description: "Matches standard 3-segment JSON Web Tokens"
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: "private-key",
|
|
71
|
+
name: "Private Key Block",
|
|
72
|
+
pattern: /-----BEGIN (?:[A-Z ]+)?PRIVATE KEY-----[\s\S]*?-----END (?:[A-Z ]+)?PRIVATE KEY-----/g,
|
|
73
|
+
description: "Matches PEM formatted private key certificates"
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: "credit-card",
|
|
77
|
+
name: "Credit Card Number",
|
|
78
|
+
pattern: /\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})\b/g,
|
|
79
|
+
description: "Matches major credit card number patterns"
|
|
80
|
+
}
|
|
81
|
+
];
|
|
82
|
+
var DEFAULT_SENSITIVE_KEYS = [
|
|
83
|
+
"password",
|
|
84
|
+
"passwd",
|
|
85
|
+
"pwd",
|
|
86
|
+
"secret",
|
|
87
|
+
"token",
|
|
88
|
+
"apikey",
|
|
89
|
+
"api_key",
|
|
90
|
+
"access_token",
|
|
91
|
+
"auth_token",
|
|
92
|
+
"authorization",
|
|
93
|
+
"private_key",
|
|
94
|
+
"privatekey",
|
|
95
|
+
"credit_card",
|
|
96
|
+
"creditcard",
|
|
97
|
+
"card_number",
|
|
98
|
+
"cvv",
|
|
99
|
+
"cvc",
|
|
100
|
+
"ssn"
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
// src/redactor.ts
|
|
104
|
+
var LeakShield = class {
|
|
105
|
+
rules;
|
|
106
|
+
sensitiveKeys;
|
|
107
|
+
formatMode;
|
|
108
|
+
customMask;
|
|
109
|
+
constructor(options = {}) {
|
|
110
|
+
const disabled = new Set(options.disabledRules || []);
|
|
111
|
+
const baseRules = DEFAULT_RULES.filter((rule) => !disabled.has(rule.id));
|
|
112
|
+
const custom = options.customRules || [];
|
|
113
|
+
this.rules = [...baseRules, ...custom];
|
|
114
|
+
this.formatMode = options.formatMode || "simple";
|
|
115
|
+
this.customMask = options.mask;
|
|
116
|
+
const keys = options.sensitiveKeys || DEFAULT_SENSITIVE_KEYS;
|
|
117
|
+
this.sensitiveKeys = new Set(keys.map((k) => k.toLowerCase()));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Resolves the masked string for a matched secret rule
|
|
121
|
+
*/
|
|
122
|
+
formatMatch(matched, rule) {
|
|
123
|
+
if (typeof this.customMask === "function") {
|
|
124
|
+
return this.customMask(matched, rule);
|
|
125
|
+
}
|
|
126
|
+
if (typeof this.customMask === "string") {
|
|
127
|
+
return this.customMask;
|
|
128
|
+
}
|
|
129
|
+
if (rule.mask) {
|
|
130
|
+
return typeof rule.mask === "function" ? rule.mask(matched) : rule.mask;
|
|
131
|
+
}
|
|
132
|
+
switch (this.formatMode) {
|
|
133
|
+
case "named":
|
|
134
|
+
return `[REDACTED: ${rule.name}]`;
|
|
135
|
+
case "partial":
|
|
136
|
+
return createPartialMask(matched);
|
|
137
|
+
case "simple":
|
|
138
|
+
default:
|
|
139
|
+
return "[REDACTED]";
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Redacts secrets and sensitive tokens from a plain string
|
|
144
|
+
*/
|
|
145
|
+
redact(input) {
|
|
146
|
+
if (!input || typeof input !== "string") {
|
|
147
|
+
return input;
|
|
148
|
+
}
|
|
149
|
+
let result = input;
|
|
150
|
+
for (const rule of this.rules) {
|
|
151
|
+
rule.pattern.lastIndex = 0;
|
|
152
|
+
result = result.replace(rule.pattern, (matched) => this.formatMatch(matched, rule));
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Deeply redacts sensitive keys and values inside an object, array, or error.
|
|
158
|
+
* Circular references are handled safely.
|
|
159
|
+
*/
|
|
160
|
+
redactObject(input, seen = /* @__PURE__ */ new WeakSet()) {
|
|
161
|
+
if (input === null || typeof input !== "object") {
|
|
162
|
+
if (typeof input === "string") {
|
|
163
|
+
return this.redact(input);
|
|
164
|
+
}
|
|
165
|
+
return input;
|
|
166
|
+
}
|
|
167
|
+
if (input instanceof Date || input instanceof RegExp || typeof Buffer !== "undefined" && Buffer.isBuffer(input)) {
|
|
168
|
+
return input;
|
|
169
|
+
}
|
|
170
|
+
if (seen.has(input)) {
|
|
171
|
+
return "[Circular]";
|
|
172
|
+
}
|
|
173
|
+
seen.add(input);
|
|
174
|
+
if (Array.isArray(input)) {
|
|
175
|
+
return input.map((item) => this.redactObject(item, seen));
|
|
176
|
+
}
|
|
177
|
+
if (input instanceof Error) {
|
|
178
|
+
const copy = new input.constructor(
|
|
179
|
+
this.redact(input.message)
|
|
180
|
+
);
|
|
181
|
+
if (input.stack) {
|
|
182
|
+
copy.stack = this.redact(input.stack);
|
|
183
|
+
}
|
|
184
|
+
return copy;
|
|
185
|
+
}
|
|
186
|
+
const output = {};
|
|
187
|
+
for (const [key, value] of Object.entries(input)) {
|
|
188
|
+
const normalizedKey = key.toLowerCase().replace(/[-_]/g, "");
|
|
189
|
+
const isSensitiveKey = Array.from(this.sensitiveKeys).some(
|
|
190
|
+
(sensitive) => normalizedKey.includes(sensitive.replace(/[-_]/g, ""))
|
|
191
|
+
);
|
|
192
|
+
if (isSensitiveKey) {
|
|
193
|
+
if (typeof value === "string") {
|
|
194
|
+
output[key] = this.formatMode === "partial" ? createPartialMask(value) : "[REDACTED]";
|
|
195
|
+
} else if (typeof value === "number" || typeof value === "boolean") {
|
|
196
|
+
output[key] = "[REDACTED]";
|
|
197
|
+
} else {
|
|
198
|
+
output[key] = this.redactObject(value, seen);
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
output[key] = this.redactObject(value, seen);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return output;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Returns list of configured active rules
|
|
208
|
+
*/
|
|
209
|
+
getRules() {
|
|
210
|
+
return this.rules;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
function createLeakShieldStream(options = {}) {
|
|
214
|
+
const shield = new LeakShield(options);
|
|
215
|
+
const encoding = options.encoding || "utf8";
|
|
216
|
+
return new Transform({
|
|
217
|
+
transform(chunk, _enc, callback) {
|
|
218
|
+
try {
|
|
219
|
+
const text = typeof chunk === "string" ? chunk : chunk.toString(encoding);
|
|
220
|
+
const redacted = shield.redact(text);
|
|
221
|
+
callback(null, Buffer.from(redacted, encoding));
|
|
222
|
+
} catch (err) {
|
|
223
|
+
callback(err);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/console.ts
|
|
230
|
+
var DEFAULT_METHODS = ["log", "info", "warn", "error", "debug"];
|
|
231
|
+
function patchConsole(shield, methods = DEFAULT_METHODS) {
|
|
232
|
+
const originalMethods = /* @__PURE__ */ new Map();
|
|
233
|
+
for (const method of methods) {
|
|
234
|
+
if (typeof console[method] === "function") {
|
|
235
|
+
const original = console[method].bind(console);
|
|
236
|
+
originalMethods.set(method, original);
|
|
237
|
+
console[method] = (...args) => {
|
|
238
|
+
const sanitizedArgs = args.map((arg) => {
|
|
239
|
+
if (typeof arg === "string") {
|
|
240
|
+
return shield.redact(arg);
|
|
241
|
+
}
|
|
242
|
+
if (arg !== null && typeof arg === "object") {
|
|
243
|
+
return shield.redactObject(arg);
|
|
244
|
+
}
|
|
245
|
+
return arg;
|
|
246
|
+
});
|
|
247
|
+
original(...sanitizedArgs);
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return function unpatch() {
|
|
252
|
+
for (const [method, original] of originalMethods.entries()) {
|
|
253
|
+
console[method] = original;
|
|
254
|
+
}
|
|
255
|
+
originalMethods.clear();
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// src/index.ts
|
|
260
|
+
var leakShield = new LeakShield();
|
|
261
|
+
var index_default = leakShield;
|
|
262
|
+
|
|
263
|
+
export { DEFAULT_RULES, DEFAULT_SENSITIVE_KEYS, LeakShield, createLeakShieldStream, createPartialMask, index_default as default, leakShield, patchConsole };
|
|
264
|
+
//# sourceMappingURL=index.js.map
|
|
265
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/patterns.ts","../src/redactor.ts","../src/stream.ts","../src/console.ts","../src/index.ts"],"names":[],"mappings":";;;AAMO,SAAS,kBAAkB,KAAA,EAAuB;AACvD,EAAA,IAAI,KAAA,CAAM,UAAU,CAAA,EAAG;AACrB,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,MAAM,UAAA,GAAa,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,KAAA,CAAM,KAAA,CAAM,MAAA,GAAS,CAAC,CAAC,CAAA;AAC3D,EAAA,MAAM,UAAA,GAAa,KAAK,GAAA,CAAI,CAAA,EAAG,KAAK,KAAA,CAAM,KAAA,CAAM,MAAA,GAAS,CAAC,CAAC,CAAA;AAC3D,EAAA,MAAM,IAAA,GAAO,KAAA,CAAM,KAAA,CAAM,CAA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AAA,KAAoB;AACxC,QAAA,MAAM,aAAA,GAAgB,IAAA,CAAK,GAAA,CAAI,CAAC,GAAA,KAAQ;AACtC,UAAA,IAAI,OAAO,QAAQ,QAAA,EAAU;AAC3B,YAAA,OAAO,MAAA,CAAO,OAAO,GAAG,CAAA;AAAA,UAC1B;AACA,UAAA,IAAI,GAAA,KAAQ,IAAA,IAAQ,OAAO,GAAA,KAAQ,QAAA,EAAU;AAC3C,YAAA,OAAO,MAAA,CAAO,aAAa,GAAG,CAAA;AAAA,UAChC;AACA,UAAA,OAAO,GAAA;AAAA,QACT,CAAC,CAAA;AAED,QAAA,QAAA,CAAS,GAAG,aAAa,CAAA;AAAA,MAC3B,CAAA;AAAA,IACF;AAAA,EACF;AAEA,EAAA,OAAO,SAAS,OAAA,GAAgB;AAC9B,IAAA,KAAA,MAAW,CAAC,MAAA,EAAQ,QAAQ,CAAA,IAAK,eAAA,CAAgB,SAAQ,EAAG;AAC1D,MAAA,OAAA,CAAQ,MAAM,CAAA,GAAI,QAAA;AAAA,IACpB;AACA,IAAA,eAAA,CAAgB,KAAA,EAAM;AAAA,EACxB,CAAA;AACF;;;ACtCO,IAAM,UAAA,GAAa,IAAI,UAAA;AAQ9B,IAAO,aAAA,GAAQ","file":"index.js","sourcesContent":["import { Rule } from './types.js';\n\n/**\n * Creates a partial mask preserving the head and tail of the token\n * e.g. \"sk-abcdef123456\" -> \"sk-a...3456\"\n */\nexport function createPartialMask(input: string): string {\n if (input.length <= 8) {\n return '***';\n }\n const headLength = Math.min(4, Math.floor(input.length / 4));\n const tailLength = Math.min(3, Math.floor(input.length / 4));\n const head = input.slice(0, headLength);\n const tail = input.slice(input.length - tailLength);\n return `${head}...${tail}`;\n}\n\nexport const DEFAULT_RULES: Rule[] = [\n {\n id: 'openai-key',\n name: 'OpenAI API Key',\n pattern: /\\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}\\b/g,\n description: 'Matches standard and project-scoped OpenAI API keys',\n },\n {\n id: 'anthropic-key',\n name: 'Anthropic API Key',\n pattern: /\\bsk-ant-[A-Za-z0-9_-]{20,}\\b/g,\n description: 'Matches Anthropic Claude API keys',\n },\n {\n id: 'github-token',\n name: 'GitHub Token',\n pattern: /\\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,}\\b|\\bgithub_pat_[A-Za-z0-9_]{22,}\\b/g,\n description: 'Matches GitHub personal access and fine-grained tokens',\n },\n {\n id: 'aws-access-key',\n name: 'AWS Access Key ID',\n pattern: /\\b(?:AKIA|ASIA|AROA)[A-Z0-9]{16}\\b/g,\n description: 'Matches AWS access key IDs',\n },\n {\n id: 'aws-secret-key',\n name: 'AWS Secret Access Key',\n pattern: /(?<=\\b(?:aws_secret_access_key|aws_secret_key)\\s*[:=]\\s*[\"']?)[A-Za-z0-9/+=]{40}(?=[\"']?)/gi,\n description: 'Matches AWS secret access key assignments',\n },\n {\n id: 'stripe-key',\n name: 'Stripe API Key',\n pattern: /\\b(?:sk|rk)_(?:live|test)_[0-9a-zA-Z]{24,}\\b/g,\n description: 'Matches Stripe standard and restricted API keys',\n },\n {\n id: 'slack-token',\n name: 'Slack Token',\n pattern: /\\bxox[baprs]-[0-9a-zA-Z]{10,48}\\b/g,\n description: 'Matches Slack bot, app, and user tokens',\n },\n {\n id: 'bearer-token',\n name: 'Bearer Token',\n pattern: /(?<=\\bBearer\\s+)[A-Za-z0-9\\-._~+/]+=*/g,\n description: 'Matches HTTP Bearer authorization tokens',\n },\n {\n id: 'jwt-token',\n name: 'JSON Web Token',\n pattern: /\\beyJ[A-Za-z0-9_-]{10,}\\.eyJ[A-Za-z0-9_-]{10,}\\.[A-Za-z0-9_-]{10,}\\b/g,\n description: 'Matches standard 3-segment JSON Web Tokens',\n },\n {\n id: 'private-key',\n name: 'Private Key Block',\n pattern: /-----BEGIN (?:[A-Z ]+)?PRIVATE KEY-----[\\s\\S]*?-----END (?:[A-Z ]+)?PRIVATE KEY-----/g,\n description: 'Matches PEM formatted private key certificates',\n },\n {\n id: 'credit-card',\n name: 'Credit Card Number',\n pattern: /\\b(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\\d{3})\\d{11})\\b/g,\n description: 'Matches major credit card number patterns',\n },\n];\n\nexport const DEFAULT_SENSITIVE_KEYS: string[] = [\n 'password',\n 'passwd',\n 'pwd',\n 'secret',\n 'token',\n 'apikey',\n 'api_key',\n 'access_token',\n 'auth_token',\n 'authorization',\n 'private_key',\n 'privatekey',\n 'credit_card',\n 'creditcard',\n 'card_number',\n 'cvv',\n 'cvc',\n 'ssn',\n];\n","import { LeakShieldOptions, Rule, MaskFormatter } from './types.js';\nimport { DEFAULT_RULES, DEFAULT_SENSITIVE_KEYS, createPartialMask } from './patterns.js';\n\nexport class LeakShield {\n private rules: Rule[];\n private sensitiveKeys: Set<string>;\n private formatMode: 'simple' | 'named' | 'partial';\n private customMask?: string | MaskFormatter;\n\n constructor(options: LeakShieldOptions = {}) {\n const disabled = new Set(options.disabledRules || []);\n const baseRules = DEFAULT_RULES.filter((rule) => !disabled.has(rule.id));\n const custom = options.customRules || [];\n\n this.rules = [...baseRules, ...custom];\n this.formatMode = options.formatMode || 'simple';\n this.customMask = options.mask;\n\n const keys = options.sensitiveKeys || DEFAULT_SENSITIVE_KEYS;\n this.sensitiveKeys = new Set(keys.map((k) => k.toLowerCase()));\n }\n\n /**\n * Resolves the masked string for a matched secret rule\n */\n private formatMatch(matched: string, rule: Rule): string {\n if (typeof this.customMask === 'function') {\n return this.customMask(matched, rule);\n }\n if (typeof this.customMask === 'string') {\n return this.customMask;\n }\n if (rule.mask) {\n return typeof rule.mask === 'function' ? rule.mask(matched) : rule.mask;\n }\n\n switch (this.formatMode) {\n case 'named':\n return `[REDACTED: ${rule.name}]`;\n case 'partial':\n return createPartialMask(matched);\n case 'simple':\n default:\n return '[REDACTED]';\n }\n }\n\n /**\n * Redacts secrets and sensitive tokens from a plain string\n */\n redact(input: string): string {\n if (!input || typeof input !== 'string') {\n return input;\n }\n\n let result = input;\n for (const rule of this.rules) {\n // Reset lastIndex for global regexes\n rule.pattern.lastIndex = 0;\n result = result.replace(rule.pattern, (matched) => this.formatMatch(matched, rule));\n }\n\n return result;\n }\n\n /**\n * Deeply redacts sensitive keys and values inside an object, array, or error.\n * Circular references are handled safely.\n */\n redactObject<T>(input: T, seen = new WeakSet()): T {\n if (input === null || typeof input !== 'object') {\n if (typeof input === 'string') {\n return this.redact(input) as unknown as T;\n }\n return input;\n }\n\n // Pass through primitives, Buffers, Dates, RegExp\n if (\n input instanceof Date ||\n input instanceof RegExp ||\n (typeof Buffer !== 'undefined' && Buffer.isBuffer(input))\n ) {\n return input;\n }\n\n // Handle circular structures\n if (seen.has(input)) {\n return '[Circular]' as unknown as T;\n }\n seen.add(input);\n\n if (Array.isArray(input)) {\n return input.map((item) => this.redactObject(item, seen)) as unknown as T;\n }\n\n if (input instanceof Error) {\n const copy = new (input.constructor as new (message?: string) => Error)(\n this.redact(input.message)\n );\n if (input.stack) {\n copy.stack = this.redact(input.stack);\n }\n return copy as unknown as T;\n }\n\n const output: Record<string, unknown> = {};\n for (const [key, value] of Object.entries(input)) {\n const normalizedKey = key.toLowerCase().replace(/[-_]/g, '');\n const isSensitiveKey = Array.from(this.sensitiveKeys).some((sensitive) =>\n normalizedKey.includes(sensitive.replace(/[-_]/g, ''))\n );\n\n if (isSensitiveKey) {\n if (typeof value === 'string') {\n output[key] = this.formatMode === 'partial' ? createPartialMask(value) : '[REDACTED]';\n } else if (typeof value === 'number' || typeof value === 'boolean') {\n output[key] = '[REDACTED]';\n } else {\n output[key] = this.redactObject(value, seen);\n }\n } else {\n output[key] = this.redactObject(value, seen);\n }\n }\n\n return output as T;\n }\n\n /**\n * Returns list of configured active rules\n */\n getRules(): ReadonlyArray<Rule> {\n return this.rules;\n }\n}\n","import { Transform, TransformCallback } from 'node:stream';\nimport { LeakShield } from './redactor.js';\nimport { StreamShieldOptions } from './types.js';\n\n/**\n * Creates a Node.js Transform Stream that redacts secrets from passing chunks.\n * Perfect for piping to process.stdout or log file write streams.\n */\nexport function createLeakShieldStream(options: StreamShieldOptions = {}): Transform {\n const shield = new LeakShield(options);\n const encoding = options.encoding || 'utf8';\n\n return new Transform({\n transform(chunk: Buffer | string, _enc: BufferEncoding, callback: TransformCallback) {\n try {\n const text = typeof chunk === 'string' ? chunk : chunk.toString(encoding);\n const redacted = shield.redact(text);\n callback(null, Buffer.from(redacted, encoding));\n } catch (err) {\n callback(err as Error);\n }\n },\n });\n}\n","import { LeakShield } from './redactor.js';\n\nexport type ConsoleMethod = 'log' | 'info' | 'warn' | 'error' | 'debug';\n\nconst DEFAULT_METHODS: ConsoleMethod[] = ['log', 'info', 'warn', 'error', 'debug'];\n\n/**\n * Monkey-patches console methods to automatically redact secrets from arguments.\n * Returns an unpatch function to restore original console behavior.\n */\nexport function patchConsole(\n shield: LeakShield,\n methods: ConsoleMethod[] = DEFAULT_METHODS\n): () => void {\n const originalMethods = new Map<ConsoleMethod, (...args: unknown[]) => void>();\n\n for (const method of methods) {\n if (typeof console[method] === 'function') {\n const original = console[method].bind(console);\n originalMethods.set(method, original);\n\n console[method] = (...args: unknown[]) => {\n const sanitizedArgs = args.map((arg) => {\n if (typeof arg === 'string') {\n return shield.redact(arg);\n }\n if (arg !== null && typeof arg === 'object') {\n return shield.redactObject(arg);\n }\n return arg;\n });\n\n original(...sanitizedArgs);\n };\n }\n }\n\n return function unpatch(): void {\n for (const [method, original] of originalMethods.entries()) {\n console[method] = original;\n }\n originalMethods.clear();\n };\n}\n","import { LeakShield } from './redactor.js';\nimport { createLeakShieldStream } from './stream.js';\nimport { patchConsole } from './console.js';\n\n// Default global singleton instance\nexport const leakShield = new LeakShield();\n\nexport * from './types.js';\nexport * from './patterns.js';\nexport * from './redactor.js';\nexport * from './stream.js';\nexport * from './console.js';\n\nexport default leakShield;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ct801/leak-shield",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-dependency high-speed secret, token, and sensitive data redactor for logs, strings, and Node.js streams.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup",
|
|
26
|
+
"dev": "tsup --watch",
|
|
27
|
+
"typecheck": "tsc --noEmit",
|
|
28
|
+
"test": "node --import tsx --test test/**/*.test.ts",
|
|
29
|
+
"prepublishOnly": "npm run build"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"redact",
|
|
33
|
+
"mask",
|
|
34
|
+
"secrets",
|
|
35
|
+
"api-keys",
|
|
36
|
+
"pii",
|
|
37
|
+
"stream",
|
|
38
|
+
"sanitize",
|
|
39
|
+
"logging",
|
|
40
|
+
"security"
|
|
41
|
+
],
|
|
42
|
+
"author": "tyChirag",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "git+https://github.com/tyChirag/leak-shield.git"
|
|
47
|
+
},
|
|
48
|
+
"homepage": "https://github.com/tyChirag/leak-shield#readme",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/tyChirag/leak-shield/issues"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/node": "^26.6.6",
|
|
54
|
+
"tsup": "^8.3.5",
|
|
55
|
+
"tsx": "^4.19.2",
|
|
56
|
+
"typescript": "^5.7.2"
|
|
57
|
+
}
|
|
58
|
+
}
|