sensi-mask 0.0.0-stage → 1.0.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 +75 -2
- package/dist/index.cjs +207 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +79 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -4
package/README.md
CHANGED
|
@@ -1,3 +1,76 @@
|
|
|
1
|
-
#
|
|
1
|
+
# sensi-mask
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight, zero-dependency data sanitation library designed to mask personally identifiable information (PII) and secret credentials in nested objects, log streams, and strings.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Zero dependencies**: No runtime bloat.
|
|
8
|
+
- **Deep object traversal**: Traverses nested structures, arrays, and properties while guarding against circular references.
|
|
9
|
+
- **Preconfigured detectors**: Handles emails, payment cards, phone numbers, bearer headers, and popular API token formats.
|
|
10
|
+
- **Key-name inference**: Identifies sensitive field names such as `password`, `token`, `secret`, `apiKey`, and `authorization`.
|
|
11
|
+
- **Customizable masking**: Offers partial masking, complete token replacement, and custom redaction handlers.
|
|
12
|
+
- **TypeScript ready**: Full typings included out of the box.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install sensi-mask
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { mask } from 'sensi-mask';
|
|
24
|
+
|
|
25
|
+
const incomingRequest = {
|
|
26
|
+
user: 'alex',
|
|
27
|
+
email: 'alex.smith@example.org',
|
|
28
|
+
security: {
|
|
29
|
+
apiKey: 'sk_live_9876543210abcdefghijklmnop',
|
|
30
|
+
password: 'UnsafePassword!99',
|
|
31
|
+
},
|
|
32
|
+
payment: {
|
|
33
|
+
cardNumber: '4532-7521-9988-1234',
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const safeToLog = mask(incomingRequest);
|
|
38
|
+
console.log(safeToLog);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Result
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"user": "alex",
|
|
46
|
+
"email": "a***h@example.org",
|
|
47
|
+
"security": {
|
|
48
|
+
"apiKey": "sk_l...mnop",
|
|
49
|
+
"password": "Unsa...d!99"
|
|
50
|
+
},
|
|
51
|
+
"payment": {
|
|
52
|
+
"cardNumber": "****-****-****-1234"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Full Redaction Mode
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const fullyRedacted = mask(incomingRequest, { strategy: 'full' });
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Individual Helper Functions
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { maskEmail, maskCreditCard, maskPhone, maskToken } from 'sensi-mask';
|
|
67
|
+
|
|
68
|
+
maskEmail('jordan@domain.com'); // "j***n@domain.com"
|
|
69
|
+
maskCreditCard('4111-2222-3333-4444'); // "****-****-****-4444"
|
|
70
|
+
maskPhone('+1-555-234-5678'); // "***-***-5678"
|
|
71
|
+
maskToken('secret_token_value_here'); // "secr...here"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
// src/patterns.ts
|
|
6
|
+
var DEFAULT_SENSITIVE_KEYS = [
|
|
7
|
+
"password",
|
|
8
|
+
"passwd",
|
|
9
|
+
"secret",
|
|
10
|
+
"token",
|
|
11
|
+
"apikey",
|
|
12
|
+
"api_key",
|
|
13
|
+
"auth",
|
|
14
|
+
"authorization",
|
|
15
|
+
"accesstoken",
|
|
16
|
+
"access_token",
|
|
17
|
+
"refreshtoken",
|
|
18
|
+
"refresh_token",
|
|
19
|
+
"cvv",
|
|
20
|
+
"cvc",
|
|
21
|
+
"creditcard",
|
|
22
|
+
"credit_card",
|
|
23
|
+
"cardnumber",
|
|
24
|
+
"card_number",
|
|
25
|
+
"ssn",
|
|
26
|
+
"privatekey",
|
|
27
|
+
"private_key",
|
|
28
|
+
"client_secret",
|
|
29
|
+
"clientsecret",
|
|
30
|
+
"pin"
|
|
31
|
+
];
|
|
32
|
+
var PATTERNS = {
|
|
33
|
+
jwt: /\beyJ[a-zA-Z0-9_-]{10,}\.[a-zA-Z0-9_-]{10,}\.[a-zA-Z0-9_-]{10,}\b/g,
|
|
34
|
+
bearer: /\bBearer\s+[a-zA-Z0-9._~+/-]{15,}={0,2}\b/gi,
|
|
35
|
+
apiKey: /\b(?:AKIA|ABIA|ACCA|ASIA)[0-9A-Z]{16}\b|\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\b|\bgh[pousr]_[0-9a-zA-Z]{36}\b|\bxox[baprs]-[0-9a-zA-Z-]{10,48}\b/g,
|
|
36
|
+
creditCard: /\b(?:\d{4}[ -]?){3}\d{4}\b/g,
|
|
37
|
+
email: /\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\b/g,
|
|
38
|
+
phone: /\b(?:\+?\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/index.ts
|
|
42
|
+
function maskEmail(email, full = false) {
|
|
43
|
+
if (full) return "[REDACTED_EMAIL]";
|
|
44
|
+
const parts = email.split("@");
|
|
45
|
+
if (parts.length !== 2) return "[REDACTED_EMAIL]";
|
|
46
|
+
const [user, domain] = parts;
|
|
47
|
+
if (!user || user.length <= 2) {
|
|
48
|
+
return `${user[0] ?? "*"}***@${domain}`;
|
|
49
|
+
}
|
|
50
|
+
return `${user[0]}***${user[user.length - 1]}@${domain}`;
|
|
51
|
+
}
|
|
52
|
+
function maskCreditCard(card, full = false) {
|
|
53
|
+
if (full) return "[REDACTED_CREDIT_CARD]";
|
|
54
|
+
const digitsOnly = card.replace(/\D/g, "");
|
|
55
|
+
if (digitsOnly.length < 4) return "****";
|
|
56
|
+
const last4 = digitsOnly.slice(-4);
|
|
57
|
+
return `****-****-****-${last4}`;
|
|
58
|
+
}
|
|
59
|
+
function maskToken(token, full = false, label = "TOKEN") {
|
|
60
|
+
if (full || token.length < 10) return `[REDACTED_${label}]`;
|
|
61
|
+
const prefix = token.slice(0, 4);
|
|
62
|
+
const suffix = token.slice(-4);
|
|
63
|
+
return `${prefix}...${suffix}`;
|
|
64
|
+
}
|
|
65
|
+
function maskPhone(phone, full = false) {
|
|
66
|
+
if (full) return "[REDACTED_PHONE]";
|
|
67
|
+
const digits = phone.replace(/\D/g, "");
|
|
68
|
+
if (digits.length < 4) return "***-***-****";
|
|
69
|
+
const last4 = digits.slice(-4);
|
|
70
|
+
return `***-***-${last4}`;
|
|
71
|
+
}
|
|
72
|
+
function isSensitiveKey(key, customKeys) {
|
|
73
|
+
const normalizedKey = key.toLowerCase().replace(/[-_]/g, "");
|
|
74
|
+
const allKeys = customKeys ? [...DEFAULT_SENSITIVE_KEYS, ...customKeys] : DEFAULT_SENSITIVE_KEYS;
|
|
75
|
+
for (const item of allKeys) {
|
|
76
|
+
if (typeof item === "string") {
|
|
77
|
+
const normalizedItem = item.toLowerCase().replace(/[-_]/g, "");
|
|
78
|
+
if (normalizedKey === normalizedItem || normalizedKey.includes(normalizedItem)) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
} else if (item instanceof RegExp) {
|
|
82
|
+
if (item.test(key)) return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
function maskString(text, options = {}) {
|
|
88
|
+
if (typeof text !== "string" || text.length === 0) return text;
|
|
89
|
+
const strategy = options.strategy ?? "partial";
|
|
90
|
+
const isFull = strategy === "full";
|
|
91
|
+
const customFn = typeof strategy === "function" ? strategy : null;
|
|
92
|
+
const patterns = {
|
|
93
|
+
jwt: options.patterns?.jwt ?? true,
|
|
94
|
+
bearer: options.patterns?.bearer ?? true,
|
|
95
|
+
apiKey: options.patterns?.apiKey ?? true,
|
|
96
|
+
creditCard: options.patterns?.creditCard ?? true,
|
|
97
|
+
email: options.patterns?.email ?? true,
|
|
98
|
+
phone: options.patterns?.phone ?? true
|
|
99
|
+
};
|
|
100
|
+
let result = text;
|
|
101
|
+
if (patterns.bearer) {
|
|
102
|
+
result = result.replace(PATTERNS.bearer, (match) => {
|
|
103
|
+
if (customFn) return customFn(match, "bearer");
|
|
104
|
+
return isFull ? "Bearer [REDACTED_TOKEN]" : `Bearer ${maskToken(match.replace(/^Bearer\s+/i, ""), false, "TOKEN")}`;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (patterns.jwt) {
|
|
108
|
+
result = result.replace(PATTERNS.jwt, (match) => {
|
|
109
|
+
if (customFn) return customFn(match, "jwt");
|
|
110
|
+
return isFull ? "[REDACTED_JWT]" : maskToken(match, false, "JWT");
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (patterns.apiKey) {
|
|
114
|
+
result = result.replace(PATTERNS.apiKey, (match) => {
|
|
115
|
+
if (customFn) return customFn(match, "apiKey");
|
|
116
|
+
return isFull ? "[REDACTED_API_KEY]" : maskToken(match, false, "KEY");
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (patterns.creditCard) {
|
|
120
|
+
result = result.replace(PATTERNS.creditCard, (match) => {
|
|
121
|
+
if (customFn) return customFn(match, "creditCard");
|
|
122
|
+
return maskCreditCard(match, isFull);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
if (patterns.email) {
|
|
126
|
+
result = result.replace(PATTERNS.email, (match) => {
|
|
127
|
+
if (customFn) return customFn(match, "email");
|
|
128
|
+
return maskEmail(match, isFull);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (patterns.phone) {
|
|
132
|
+
result = result.replace(PATTERNS.phone, (match) => {
|
|
133
|
+
if (customFn) return customFn(match, "phone");
|
|
134
|
+
return maskPhone(match, isFull);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
function mask(value, options = {}) {
|
|
140
|
+
const maxDepth = options.maxDepth ?? 20;
|
|
141
|
+
const inPlace = options.inPlace ?? false;
|
|
142
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
143
|
+
function traverse(current, key, depth) {
|
|
144
|
+
if (depth > maxDepth) return "[MAX_DEPTH_EXCEEDED]";
|
|
145
|
+
if (current === null || current === void 0) {
|
|
146
|
+
return current;
|
|
147
|
+
}
|
|
148
|
+
if (key && isSensitiveKey(key, options.sensitiveKeys)) {
|
|
149
|
+
if (typeof current === "string") {
|
|
150
|
+
const strategy = options.strategy ?? "partial";
|
|
151
|
+
if (typeof strategy === "function") return strategy(current, "sensitiveKey");
|
|
152
|
+
return strategy === "full" ? "[REDACTED]" : maskToken(current, false, "SECRET");
|
|
153
|
+
}
|
|
154
|
+
return "[REDACTED]";
|
|
155
|
+
}
|
|
156
|
+
if (typeof current === "string") {
|
|
157
|
+
return maskString(current, options);
|
|
158
|
+
}
|
|
159
|
+
if (typeof current === "number" || typeof current === "boolean" || typeof current === "bigint") {
|
|
160
|
+
return current;
|
|
161
|
+
}
|
|
162
|
+
if (typeof current === "function") {
|
|
163
|
+
return current;
|
|
164
|
+
}
|
|
165
|
+
if (typeof current === "object") {
|
|
166
|
+
if (seen.has(current)) {
|
|
167
|
+
return "[CIRCULAR]";
|
|
168
|
+
}
|
|
169
|
+
seen.add(current);
|
|
170
|
+
if (current instanceof Date) {
|
|
171
|
+
return new Date(current.getTime());
|
|
172
|
+
}
|
|
173
|
+
if (current instanceof RegExp) {
|
|
174
|
+
return new RegExp(current.source, current.flags);
|
|
175
|
+
}
|
|
176
|
+
if (Array.isArray(current)) {
|
|
177
|
+
const arr = inPlace ? current : new Array(current.length);
|
|
178
|
+
for (let i = 0; i < current.length; i++) {
|
|
179
|
+
arr[i] = traverse(current[i], key, depth + 1);
|
|
180
|
+
}
|
|
181
|
+
return arr;
|
|
182
|
+
}
|
|
183
|
+
const obj = current;
|
|
184
|
+
const target = inPlace ? obj : {};
|
|
185
|
+
for (const prop of Object.keys(obj)) {
|
|
186
|
+
target[prop] = traverse(obj[prop], prop, depth + 1);
|
|
187
|
+
}
|
|
188
|
+
return target;
|
|
189
|
+
}
|
|
190
|
+
return current;
|
|
191
|
+
}
|
|
192
|
+
return traverse(value, null, 0);
|
|
193
|
+
}
|
|
194
|
+
var index_default = mask;
|
|
195
|
+
|
|
196
|
+
exports.DEFAULT_SENSITIVE_KEYS = DEFAULT_SENSITIVE_KEYS;
|
|
197
|
+
exports.PATTERNS = PATTERNS;
|
|
198
|
+
exports.default = index_default;
|
|
199
|
+
exports.isSensitiveKey = isSensitiveKey;
|
|
200
|
+
exports.mask = mask;
|
|
201
|
+
exports.maskCreditCard = maskCreditCard;
|
|
202
|
+
exports.maskEmail = maskEmail;
|
|
203
|
+
exports.maskPhone = maskPhone;
|
|
204
|
+
exports.maskString = maskString;
|
|
205
|
+
exports.maskToken = maskToken;
|
|
206
|
+
//# sourceMappingURL=index.cjs.map
|
|
207
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/patterns.ts","../src/index.ts"],"names":[],"mappings":";;;;;AAAO,IAAM,sBAAA,GAA8C;AAAA,EACzD,UAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACA,QAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;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OAAA,GAAwB,IAAI,KAAA,CAAM,QAAQ,MAAM,CAAA;AACvE,QAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,OAAA,CAAQ,QAAQ,CAAA,EAAA,EAAK;AACvC,UAAA,GAAA,CAAI,CAAC,IAAI,QAAA,CAAS,OAAA,CAAQ,CAAC,CAAA,EAAG,GAAA,EAAK,QAAQ,CAAC,CAAA;AAAA,QAC9C;AACA,QAAA,OAAO,GAAA;AAAA,MACT;AAEA,MAAA,MAAM,GAAA,GAAM,OAAA;AACZ,MAAA,MAAM,MAAA,GAAkC,OAAA,GAAU,GAAA,GAAM,EAAC;AAEzD,MAAA,KAAA,MAAW,IAAA,IAAQ,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAG;AACnC,QAAA,MAAA,CAAO,IAAI,IAAI,QAAA,CAAS,GAAA,CAAI,IAAI,CAAA,EAAG,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,MACpD;AAEA,MAAA,OAAO,MAAA;AAAA,IACT;AAEA,IAAA,OAAO,OAAA;AAAA,EACT;AAEA,EAAA,OAAO,QAAA,CAAS,KAAA,EAAO,IAAA,EAAM,CAAC,CAAA;AAChC;AAEA,IAAO,aAAA,GAAQ","file":"index.cjs","sourcesContent":["export const DEFAULT_SENSITIVE_KEYS: (string | RegExp)[] = [\n 'password',\n 'passwd',\n 'secret',\n 'token',\n 'apikey',\n 'api_key',\n 'auth',\n 'authorization',\n 'accesstoken',\n 'access_token',\n 'refreshtoken',\n 'refresh_token',\n 'cvv',\n 'cvc',\n 'creditcard',\n 'credit_card',\n 'cardnumber',\n 'card_number',\n 'ssn',\n 'privatekey',\n 'private_key',\n 'client_secret',\n 'clientsecret',\n 'pin',\n];\n\nexport const PATTERNS = {\n jwt: /\\beyJ[a-zA-Z0-9_-]{10,}\\.[a-zA-Z0-9_-]{10,}\\.[a-zA-Z0-9_-]{10,}\\b/g,\n bearer: /\\bBearer\\s+[a-zA-Z0-9._~+/-]{15,}={0,2}\\b/gi,\n apiKey: /\\b(?:AKIA|ABIA|ACCA|ASIA)[0-9A-Z]{16}\\b|\\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\\b|\\bgh[pousr]_[0-9a-zA-Z]{36}\\b|\\bxox[baprs]-[0-9a-zA-Z-]{10,48}\\b/g,\n creditCard: /\\b(?:\\d{4}[ -]?){3}\\d{4}\\b/g,\n email: /\\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+\\b/g,\n phone: /\\b(?:\\+?\\d{1,3}[-.\\s]?)?\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}\\b/g,\n};\n","import { DEFAULT_SENSITIVE_KEYS, PATTERNS } from './patterns.js';\nimport type { MaskOptions, MaskStrategy } from './types.js';\n\nexport * from './types.js';\nexport { DEFAULT_SENSITIVE_KEYS, PATTERNS };\n\n/**\n * Mask an email address (e.g. alice.smith@example.com -> a***h@example.com)\n */\nexport function maskEmail(email: string, full = false): string {\n if (full) return '[REDACTED_EMAIL]';\n const parts = email.split('@');\n if (parts.length !== 2) return '[REDACTED_EMAIL]';\n const [user, domain] = parts;\n if (!user || user.length <= 2) {\n return `${user[0] ?? '*'}***@${domain}`;\n }\n return `${user[0]}***${user[user.length - 1]}@${domain}`;\n}\n\n/**\n * Mask a credit card number (e.g. 4111-2222-3333-4444 -> ****-****-****-4444)\n */\nexport function maskCreditCard(card: string, full = false): string {\n if (full) return '[REDACTED_CREDIT_CARD]';\n const digitsOnly = card.replace(/\\D/g, '');\n if (digitsOnly.length < 4) return '****';\n const last4 = digitsOnly.slice(-4);\n return `****-****-****-${last4}`;\n}\n\n/**\n * Mask an API token, JWT, or secret key\n */\nexport function maskToken(token: string, full = false, label = 'TOKEN'): string {\n if (full || token.length < 10) return `[REDACTED_${label}]`;\n const prefix = token.slice(0, 4);\n const suffix = token.slice(-4);\n return `${prefix}...${suffix}`;\n}\n\n/**\n * Mask a phone number\n */\nexport function maskPhone(phone: string, full = false): string {\n if (full) return '[REDACTED_PHONE]';\n const digits = phone.replace(/\\D/g, '');\n if (digits.length < 4) return '***-***-****';\n const last4 = digits.slice(-4);\n return `***-***-${last4}`;\n}\n\n/**\n * Determine if an object key name matches known sensitive key terms\n */\nexport function isSensitiveKey(key: string, customKeys?: (string | RegExp)[]): boolean {\n const normalizedKey = key.toLowerCase().replace(/[-_]/g, '');\n const allKeys = customKeys ? [...DEFAULT_SENSITIVE_KEYS, ...customKeys] : DEFAULT_SENSITIVE_KEYS;\n\n for (const item of allKeys) {\n if (typeof item === 'string') {\n const normalizedItem = item.toLowerCase().replace(/[-_]/g, '');\n if (normalizedKey === normalizedItem || normalizedKey.includes(normalizedItem)) {\n return true;\n }\n } else if (item instanceof RegExp) {\n if (item.test(key)) return true;\n }\n }\n return false;\n}\n\n/**\n * Redact sensitive patterns inside a raw string\n */\nexport function maskString(text: string, options: MaskOptions = {}): string {\n if (typeof text !== 'string' || text.length === 0) return text;\n\n const strategy = options.strategy ?? 'partial';\n const isFull = strategy === 'full';\n const customFn = typeof strategy === 'function' ? strategy : null;\n\n const patterns = {\n jwt: options.patterns?.jwt ?? true,\n bearer: options.patterns?.bearer ?? true,\n apiKey: options.patterns?.apiKey ?? true,\n creditCard: options.patterns?.creditCard ?? true,\n email: options.patterns?.email ?? true,\n phone: options.patterns?.phone ?? true,\n };\n\n let result = text;\n\n // Bearer tokens\n if (patterns.bearer) {\n result = result.replace(PATTERNS.bearer, (match) => {\n if (customFn) return customFn(match, 'bearer');\n return isFull ? 'Bearer [REDACTED_TOKEN]' : `Bearer ${maskToken(match.replace(/^Bearer\\s+/i, ''), false, 'TOKEN')}`;\n });\n }\n\n // JWTs\n if (patterns.jwt) {\n result = result.replace(PATTERNS.jwt, (match) => {\n if (customFn) return customFn(match, 'jwt');\n return isFull ? '[REDACTED_JWT]' : maskToken(match, false, 'JWT');\n });\n }\n\n // API Keys (AWS, Stripe, GitHub, Slack)\n if (patterns.apiKey) {\n result = result.replace(PATTERNS.apiKey, (match) => {\n if (customFn) return customFn(match, 'apiKey');\n return isFull ? '[REDACTED_API_KEY]' : maskToken(match, false, 'KEY');\n });\n }\n\n // Credit Cards\n if (patterns.creditCard) {\n result = result.replace(PATTERNS.creditCard, (match) => {\n if (customFn) return customFn(match, 'creditCard');\n return maskCreditCard(match, isFull);\n });\n }\n\n // Emails\n if (patterns.email) {\n result = result.replace(PATTERNS.email, (match) => {\n if (customFn) return customFn(match, 'email');\n return maskEmail(match, isFull);\n });\n }\n\n // Phones\n if (patterns.phone) {\n result = result.replace(PATTERNS.phone, (match) => {\n if (customFn) return customFn(match, 'phone');\n return maskPhone(match, isFull);\n });\n }\n\n return result;\n}\n\n/**\n * Deeply sanitizes objects, arrays, and primitive values.\n * Handles circular references safely using WeakSet.\n */\nexport function mask<T = unknown>(value: T, options: MaskOptions = {}): T {\n const maxDepth = options.maxDepth ?? 20;\n const inPlace = options.inPlace ?? false;\n const seen = new WeakSet<object>();\n\n function traverse(current: unknown, key: string | null, depth: number): unknown {\n if (depth > maxDepth) return '[MAX_DEPTH_EXCEEDED]';\n\n if (current === null || current === undefined) {\n return current;\n }\n\n // Check if parent key itself indicates sensitive content\n if (key && isSensitiveKey(key, options.sensitiveKeys)) {\n if (typeof current === 'string') {\n const strategy = options.strategy ?? 'partial';\n if (typeof strategy === 'function') return strategy(current, 'sensitiveKey');\n return strategy === 'full' ? '[REDACTED]' : maskToken(current, false, 'SECRET');\n }\n return '[REDACTED]';\n }\n\n if (typeof current === 'string') {\n return maskString(current, options);\n }\n\n if (typeof current === 'number' || typeof current === 'boolean' || typeof current === 'bigint') {\n return current;\n }\n\n if (typeof current === 'function') {\n return current;\n }\n\n if (typeof current === 'object') {\n if (seen.has(current)) {\n return '[CIRCULAR]';\n }\n seen.add(current);\n\n if (current instanceof Date) {\n return new Date(current.getTime());\n }\n\n if (current instanceof RegExp) {\n return new RegExp(current.source, current.flags);\n }\n\n if (Array.isArray(current)) {\n const arr = inPlace ? (current as unknown[]) : new Array(current.length);\n for (let i = 0; i < current.length; i++) {\n arr[i] = traverse(current[i], key, depth + 1);\n }\n return arr;\n }\n\n const obj = current as Record<string, unknown>;\n const target: Record<string, unknown> = inPlace ? obj : {};\n\n for (const prop of Object.keys(obj)) {\n target[prop] = traverse(obj[prop], prop, depth + 1);\n }\n\n return target;\n }\n\n return current;\n }\n\n return traverse(value, null, 0) as T;\n}\n\nexport default mask;\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
declare const DEFAULT_SENSITIVE_KEYS: (string | RegExp)[];
|
|
2
|
+
declare const PATTERNS: {
|
|
3
|
+
jwt: RegExp;
|
|
4
|
+
bearer: RegExp;
|
|
5
|
+
apiKey: RegExp;
|
|
6
|
+
creditCard: RegExp;
|
|
7
|
+
email: RegExp;
|
|
8
|
+
phone: RegExp;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type MaskStrategy = 'full' | 'partial' | ((value: string, patternType: string) => string);
|
|
12
|
+
interface MaskOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Strategy for masking values.
|
|
15
|
+
* - 'full': Replaces matches completely (e.g. [REDACTED_EMAIL], [REDACTED_TOKEN])
|
|
16
|
+
* - 'partial': Leaves partial context (e.g. a***e@gmail.com, ****-****-****-1234)
|
|
17
|
+
* - custom function: (val, type) => string
|
|
18
|
+
* @default 'partial'
|
|
19
|
+
*/
|
|
20
|
+
strategy?: MaskStrategy;
|
|
21
|
+
/**
|
|
22
|
+
* Additional sensitive keys to redact when found as object property names.
|
|
23
|
+
* Key matching is case-insensitive.
|
|
24
|
+
*/
|
|
25
|
+
sensitiveKeys?: (string | RegExp)[];
|
|
26
|
+
/**
|
|
27
|
+
* Whether to mutate the passed object or return a deep clone.
|
|
28
|
+
* @default false (returns a sanitized new copy)
|
|
29
|
+
*/
|
|
30
|
+
inPlace?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Maximum depth for nested object/array traversal to prevent stack overflows.
|
|
33
|
+
* @default 20
|
|
34
|
+
*/
|
|
35
|
+
maxDepth?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Enable or disable specific built-in pattern matchers.
|
|
38
|
+
*/
|
|
39
|
+
patterns?: {
|
|
40
|
+
email?: boolean;
|
|
41
|
+
creditCard?: boolean;
|
|
42
|
+
jwt?: boolean;
|
|
43
|
+
apiKey?: boolean;
|
|
44
|
+
phone?: boolean;
|
|
45
|
+
bearer?: boolean;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Mask an email address (e.g. alice.smith@example.com -> a***h@example.com)
|
|
51
|
+
*/
|
|
52
|
+
declare function maskEmail(email: string, full?: boolean): string;
|
|
53
|
+
/**
|
|
54
|
+
* Mask a credit card number (e.g. 4111-2222-3333-4444 -> ****-****-****-4444)
|
|
55
|
+
*/
|
|
56
|
+
declare function maskCreditCard(card: string, full?: boolean): string;
|
|
57
|
+
/**
|
|
58
|
+
* Mask an API token, JWT, or secret key
|
|
59
|
+
*/
|
|
60
|
+
declare function maskToken(token: string, full?: boolean, label?: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Mask a phone number
|
|
63
|
+
*/
|
|
64
|
+
declare function maskPhone(phone: string, full?: boolean): string;
|
|
65
|
+
/**
|
|
66
|
+
* Determine if an object key name matches known sensitive key terms
|
|
67
|
+
*/
|
|
68
|
+
declare function isSensitiveKey(key: string, customKeys?: (string | RegExp)[]): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Redact sensitive patterns inside a raw string
|
|
71
|
+
*/
|
|
72
|
+
declare function maskString(text: string, options?: MaskOptions): string;
|
|
73
|
+
/**
|
|
74
|
+
* Deeply sanitizes objects, arrays, and primitive values.
|
|
75
|
+
* Handles circular references safely using WeakSet.
|
|
76
|
+
*/
|
|
77
|
+
declare function mask<T = unknown>(value: T, options?: MaskOptions): T;
|
|
78
|
+
|
|
79
|
+
export { DEFAULT_SENSITIVE_KEYS, type MaskOptions, type MaskStrategy, PATTERNS, mask as default, isSensitiveKey, mask, maskCreditCard, maskEmail, maskPhone, maskString, maskToken };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
declare const DEFAULT_SENSITIVE_KEYS: (string | RegExp)[];
|
|
2
|
+
declare const PATTERNS: {
|
|
3
|
+
jwt: RegExp;
|
|
4
|
+
bearer: RegExp;
|
|
5
|
+
apiKey: RegExp;
|
|
6
|
+
creditCard: RegExp;
|
|
7
|
+
email: RegExp;
|
|
8
|
+
phone: RegExp;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type MaskStrategy = 'full' | 'partial' | ((value: string, patternType: string) => string);
|
|
12
|
+
interface MaskOptions {
|
|
13
|
+
/**
|
|
14
|
+
* Strategy for masking values.
|
|
15
|
+
* - 'full': Replaces matches completely (e.g. [REDACTED_EMAIL], [REDACTED_TOKEN])
|
|
16
|
+
* - 'partial': Leaves partial context (e.g. a***e@gmail.com, ****-****-****-1234)
|
|
17
|
+
* - custom function: (val, type) => string
|
|
18
|
+
* @default 'partial'
|
|
19
|
+
*/
|
|
20
|
+
strategy?: MaskStrategy;
|
|
21
|
+
/**
|
|
22
|
+
* Additional sensitive keys to redact when found as object property names.
|
|
23
|
+
* Key matching is case-insensitive.
|
|
24
|
+
*/
|
|
25
|
+
sensitiveKeys?: (string | RegExp)[];
|
|
26
|
+
/**
|
|
27
|
+
* Whether to mutate the passed object or return a deep clone.
|
|
28
|
+
* @default false (returns a sanitized new copy)
|
|
29
|
+
*/
|
|
30
|
+
inPlace?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Maximum depth for nested object/array traversal to prevent stack overflows.
|
|
33
|
+
* @default 20
|
|
34
|
+
*/
|
|
35
|
+
maxDepth?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Enable or disable specific built-in pattern matchers.
|
|
38
|
+
*/
|
|
39
|
+
patterns?: {
|
|
40
|
+
email?: boolean;
|
|
41
|
+
creditCard?: boolean;
|
|
42
|
+
jwt?: boolean;
|
|
43
|
+
apiKey?: boolean;
|
|
44
|
+
phone?: boolean;
|
|
45
|
+
bearer?: boolean;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Mask an email address (e.g. alice.smith@example.com -> a***h@example.com)
|
|
51
|
+
*/
|
|
52
|
+
declare function maskEmail(email: string, full?: boolean): string;
|
|
53
|
+
/**
|
|
54
|
+
* Mask a credit card number (e.g. 4111-2222-3333-4444 -> ****-****-****-4444)
|
|
55
|
+
*/
|
|
56
|
+
declare function maskCreditCard(card: string, full?: boolean): string;
|
|
57
|
+
/**
|
|
58
|
+
* Mask an API token, JWT, or secret key
|
|
59
|
+
*/
|
|
60
|
+
declare function maskToken(token: string, full?: boolean, label?: string): string;
|
|
61
|
+
/**
|
|
62
|
+
* Mask a phone number
|
|
63
|
+
*/
|
|
64
|
+
declare function maskPhone(phone: string, full?: boolean): string;
|
|
65
|
+
/**
|
|
66
|
+
* Determine if an object key name matches known sensitive key terms
|
|
67
|
+
*/
|
|
68
|
+
declare function isSensitiveKey(key: string, customKeys?: (string | RegExp)[]): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Redact sensitive patterns inside a raw string
|
|
71
|
+
*/
|
|
72
|
+
declare function maskString(text: string, options?: MaskOptions): string;
|
|
73
|
+
/**
|
|
74
|
+
* Deeply sanitizes objects, arrays, and primitive values.
|
|
75
|
+
* Handles circular references safely using WeakSet.
|
|
76
|
+
*/
|
|
77
|
+
declare function mask<T = unknown>(value: T, options?: MaskOptions): T;
|
|
78
|
+
|
|
79
|
+
export { DEFAULT_SENSITIVE_KEYS, type MaskOptions, type MaskStrategy, PATTERNS, mask as default, isSensitiveKey, mask, maskCreditCard, maskEmail, maskPhone, maskString, maskToken };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
// src/patterns.ts
|
|
2
|
+
var DEFAULT_SENSITIVE_KEYS = [
|
|
3
|
+
"password",
|
|
4
|
+
"passwd",
|
|
5
|
+
"secret",
|
|
6
|
+
"token",
|
|
7
|
+
"apikey",
|
|
8
|
+
"api_key",
|
|
9
|
+
"auth",
|
|
10
|
+
"authorization",
|
|
11
|
+
"accesstoken",
|
|
12
|
+
"access_token",
|
|
13
|
+
"refreshtoken",
|
|
14
|
+
"refresh_token",
|
|
15
|
+
"cvv",
|
|
16
|
+
"cvc",
|
|
17
|
+
"creditcard",
|
|
18
|
+
"credit_card",
|
|
19
|
+
"cardnumber",
|
|
20
|
+
"card_number",
|
|
21
|
+
"ssn",
|
|
22
|
+
"privatekey",
|
|
23
|
+
"private_key",
|
|
24
|
+
"client_secret",
|
|
25
|
+
"clientsecret",
|
|
26
|
+
"pin"
|
|
27
|
+
];
|
|
28
|
+
var PATTERNS = {
|
|
29
|
+
jwt: /\beyJ[a-zA-Z0-9_-]{10,}\.[a-zA-Z0-9_-]{10,}\.[a-zA-Z0-9_-]{10,}\b/g,
|
|
30
|
+
bearer: /\bBearer\s+[a-zA-Z0-9._~+/-]{15,}={0,2}\b/gi,
|
|
31
|
+
apiKey: /\b(?:AKIA|ABIA|ACCA|ASIA)[0-9A-Z]{16}\b|\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\b|\bgh[pousr]_[0-9a-zA-Z]{36}\b|\bxox[baprs]-[0-9a-zA-Z-]{10,48}\b/g,
|
|
32
|
+
creditCard: /\b(?:\d{4}[ -]?){3}\d{4}\b/g,
|
|
33
|
+
email: /\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\b/g,
|
|
34
|
+
phone: /\b(?:\+?\d{1,3}[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}\b/g
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// src/index.ts
|
|
38
|
+
function maskEmail(email, full = false) {
|
|
39
|
+
if (full) return "[REDACTED_EMAIL]";
|
|
40
|
+
const parts = email.split("@");
|
|
41
|
+
if (parts.length !== 2) return "[REDACTED_EMAIL]";
|
|
42
|
+
const [user, domain] = parts;
|
|
43
|
+
if (!user || user.length <= 2) {
|
|
44
|
+
return `${user[0] ?? "*"}***@${domain}`;
|
|
45
|
+
}
|
|
46
|
+
return `${user[0]}***${user[user.length - 1]}@${domain}`;
|
|
47
|
+
}
|
|
48
|
+
function maskCreditCard(card, full = false) {
|
|
49
|
+
if (full) return "[REDACTED_CREDIT_CARD]";
|
|
50
|
+
const digitsOnly = card.replace(/\D/g, "");
|
|
51
|
+
if (digitsOnly.length < 4) return "****";
|
|
52
|
+
const last4 = digitsOnly.slice(-4);
|
|
53
|
+
return `****-****-****-${last4}`;
|
|
54
|
+
}
|
|
55
|
+
function maskToken(token, full = false, label = "TOKEN") {
|
|
56
|
+
if (full || token.length < 10) return `[REDACTED_${label}]`;
|
|
57
|
+
const prefix = token.slice(0, 4);
|
|
58
|
+
const suffix = token.slice(-4);
|
|
59
|
+
return `${prefix}...${suffix}`;
|
|
60
|
+
}
|
|
61
|
+
function maskPhone(phone, full = false) {
|
|
62
|
+
if (full) return "[REDACTED_PHONE]";
|
|
63
|
+
const digits = phone.replace(/\D/g, "");
|
|
64
|
+
if (digits.length < 4) return "***-***-****";
|
|
65
|
+
const last4 = digits.slice(-4);
|
|
66
|
+
return `***-***-${last4}`;
|
|
67
|
+
}
|
|
68
|
+
function isSensitiveKey(key, customKeys) {
|
|
69
|
+
const normalizedKey = key.toLowerCase().replace(/[-_]/g, "");
|
|
70
|
+
const allKeys = customKeys ? [...DEFAULT_SENSITIVE_KEYS, ...customKeys] : DEFAULT_SENSITIVE_KEYS;
|
|
71
|
+
for (const item of allKeys) {
|
|
72
|
+
if (typeof item === "string") {
|
|
73
|
+
const normalizedItem = item.toLowerCase().replace(/[-_]/g, "");
|
|
74
|
+
if (normalizedKey === normalizedItem || normalizedKey.includes(normalizedItem)) {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
} else if (item instanceof RegExp) {
|
|
78
|
+
if (item.test(key)) return true;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
function maskString(text, options = {}) {
|
|
84
|
+
if (typeof text !== "string" || text.length === 0) return text;
|
|
85
|
+
const strategy = options.strategy ?? "partial";
|
|
86
|
+
const isFull = strategy === "full";
|
|
87
|
+
const customFn = typeof strategy === "function" ? strategy : null;
|
|
88
|
+
const patterns = {
|
|
89
|
+
jwt: options.patterns?.jwt ?? true,
|
|
90
|
+
bearer: options.patterns?.bearer ?? true,
|
|
91
|
+
apiKey: options.patterns?.apiKey ?? true,
|
|
92
|
+
creditCard: options.patterns?.creditCard ?? true,
|
|
93
|
+
email: options.patterns?.email ?? true,
|
|
94
|
+
phone: options.patterns?.phone ?? true
|
|
95
|
+
};
|
|
96
|
+
let result = text;
|
|
97
|
+
if (patterns.bearer) {
|
|
98
|
+
result = result.replace(PATTERNS.bearer, (match) => {
|
|
99
|
+
if (customFn) return customFn(match, "bearer");
|
|
100
|
+
return isFull ? "Bearer [REDACTED_TOKEN]" : `Bearer ${maskToken(match.replace(/^Bearer\s+/i, ""), false, "TOKEN")}`;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (patterns.jwt) {
|
|
104
|
+
result = result.replace(PATTERNS.jwt, (match) => {
|
|
105
|
+
if (customFn) return customFn(match, "jwt");
|
|
106
|
+
return isFull ? "[REDACTED_JWT]" : maskToken(match, false, "JWT");
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (patterns.apiKey) {
|
|
110
|
+
result = result.replace(PATTERNS.apiKey, (match) => {
|
|
111
|
+
if (customFn) return customFn(match, "apiKey");
|
|
112
|
+
return isFull ? "[REDACTED_API_KEY]" : maskToken(match, false, "KEY");
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (patterns.creditCard) {
|
|
116
|
+
result = result.replace(PATTERNS.creditCard, (match) => {
|
|
117
|
+
if (customFn) return customFn(match, "creditCard");
|
|
118
|
+
return maskCreditCard(match, isFull);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (patterns.email) {
|
|
122
|
+
result = result.replace(PATTERNS.email, (match) => {
|
|
123
|
+
if (customFn) return customFn(match, "email");
|
|
124
|
+
return maskEmail(match, isFull);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (patterns.phone) {
|
|
128
|
+
result = result.replace(PATTERNS.phone, (match) => {
|
|
129
|
+
if (customFn) return customFn(match, "phone");
|
|
130
|
+
return maskPhone(match, isFull);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
function mask(value, options = {}) {
|
|
136
|
+
const maxDepth = options.maxDepth ?? 20;
|
|
137
|
+
const inPlace = options.inPlace ?? false;
|
|
138
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
139
|
+
function traverse(current, key, depth) {
|
|
140
|
+
if (depth > maxDepth) return "[MAX_DEPTH_EXCEEDED]";
|
|
141
|
+
if (current === null || current === void 0) {
|
|
142
|
+
return current;
|
|
143
|
+
}
|
|
144
|
+
if (key && isSensitiveKey(key, options.sensitiveKeys)) {
|
|
145
|
+
if (typeof current === "string") {
|
|
146
|
+
const strategy = options.strategy ?? "partial";
|
|
147
|
+
if (typeof strategy === "function") return strategy(current, "sensitiveKey");
|
|
148
|
+
return strategy === "full" ? "[REDACTED]" : maskToken(current, false, "SECRET");
|
|
149
|
+
}
|
|
150
|
+
return "[REDACTED]";
|
|
151
|
+
}
|
|
152
|
+
if (typeof current === "string") {
|
|
153
|
+
return maskString(current, options);
|
|
154
|
+
}
|
|
155
|
+
if (typeof current === "number" || typeof current === "boolean" || typeof current === "bigint") {
|
|
156
|
+
return current;
|
|
157
|
+
}
|
|
158
|
+
if (typeof current === "function") {
|
|
159
|
+
return current;
|
|
160
|
+
}
|
|
161
|
+
if (typeof current === "object") {
|
|
162
|
+
if (seen.has(current)) {
|
|
163
|
+
return "[CIRCULAR]";
|
|
164
|
+
}
|
|
165
|
+
seen.add(current);
|
|
166
|
+
if (current instanceof Date) {
|
|
167
|
+
return new Date(current.getTime());
|
|
168
|
+
}
|
|
169
|
+
if (current instanceof RegExp) {
|
|
170
|
+
return new RegExp(current.source, current.flags);
|
|
171
|
+
}
|
|
172
|
+
if (Array.isArray(current)) {
|
|
173
|
+
const arr = inPlace ? current : new Array(current.length);
|
|
174
|
+
for (let i = 0; i < current.length; i++) {
|
|
175
|
+
arr[i] = traverse(current[i], key, depth + 1);
|
|
176
|
+
}
|
|
177
|
+
return arr;
|
|
178
|
+
}
|
|
179
|
+
const obj = current;
|
|
180
|
+
const target = inPlace ? obj : {};
|
|
181
|
+
for (const prop of Object.keys(obj)) {
|
|
182
|
+
target[prop] = traverse(obj[prop], prop, depth + 1);
|
|
183
|
+
}
|
|
184
|
+
return target;
|
|
185
|
+
}
|
|
186
|
+
return current;
|
|
187
|
+
}
|
|
188
|
+
return traverse(value, null, 0);
|
|
189
|
+
}
|
|
190
|
+
var index_default = mask;
|
|
191
|
+
|
|
192
|
+
export { DEFAULT_SENSITIVE_KEYS, PATTERNS, index_default as default, isSensitiveKey, mask, maskCreditCard, maskEmail, maskPhone, maskString, maskToken };
|
|
193
|
+
//# sourceMappingURL=index.js.map
|
|
194
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/patterns.ts","../src/index.ts"],"names":[],"mappings":";AAAO,IAAM,sBAAA,GAA8C;AAAA,EACzD,UAAA;AAAA,EACA,QAAA;AAAA,EACA,QAAA;AAAA,EACA,OAAA;AAAA,EACA,QAAA;AAAA,EACA,SAAA;AAAA,EACA,MAAA;AAAA,EACA,eAAA;AAAA,EACA,aAAA;AAAA,EACA,cAAA;AAAA,EACA,cAAA;AAAA,EACA,eAAA;AAAA,EACA,KAAA;AAAA,EACA,KAAA;AAAA,EACA,YAAA;AAAA,EACA,aAAA;AAAA,EAC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,GAAA,CAAI,CAAC,IAAI,QAAA,CAAS,OAAA,CAAQ,CAAC,CAAA,EAAG,GAAA,EAAK,QAAQ,CAAC,CAAA;AAAA,QAC9C;AACA,QAAA,OAAO,GAAA;AAAA,MACT;AAEA,MAAA,MAAM,GAAA,GAAM,OAAA;AACZ,MAAA,MAAM,MAAA,GAAkC,OAAA,GAAU,GAAA,GAAM,EAAC;AAEzD,MAAA,KAAA,MAAW,IAAA,IAAQ,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAG;AACnC,QAAA,MAAA,CAAO,IAAI,IAAI,QAAA,CAAS,GAAA,CAAI,IAAI,CAAA,EAAG,IAAA,EAAM,QAAQ,CAAC,CAAA;AAAA,MACpD;AAEA,MAAA,OAAO,MAAA;AAAA,IACT;AAEA,IAAA,OAAO,OAAA;AAAA,EACT;AAEA,EAAA,OAAO,QAAA,CAAS,KAAA,EAAO,IAAA,EAAM,CAAC,CAAA;AAChC;AAEA,IAAO,aAAA,GAAQ","file":"index.js","sourcesContent":["export const DEFAULT_SENSITIVE_KEYS: (string | RegExp)[] = [\n 'password',\n 'passwd',\n 'secret',\n 'token',\n 'apikey',\n 'api_key',\n 'auth',\n 'authorization',\n 'accesstoken',\n 'access_token',\n 'refreshtoken',\n 'refresh_token',\n 'cvv',\n 'cvc',\n 'creditcard',\n 'credit_card',\n 'cardnumber',\n 'card_number',\n 'ssn',\n 'privatekey',\n 'private_key',\n 'client_secret',\n 'clientsecret',\n 'pin',\n];\n\nexport const PATTERNS = {\n jwt: /\\beyJ[a-zA-Z0-9_-]{10,}\\.[a-zA-Z0-9_-]{10,}\\.[a-zA-Z0-9_-]{10,}\\b/g,\n bearer: /\\bBearer\\s+[a-zA-Z0-9._~+/-]{15,}={0,2}\\b/gi,\n apiKey: /\\b(?:AKIA|ABIA|ACCA|ASIA)[0-9A-Z]{16}\\b|\\b(?:sk|pk)_(?:live|test)_[0-9a-zA-Z]{24,}\\b|\\bgh[pousr]_[0-9a-zA-Z]{36}\\b|\\bxox[baprs]-[0-9a-zA-Z-]{10,48}\\b/g,\n creditCard: /\\b(?:\\d{4}[ -]?){3}\\d{4}\\b/g,\n email: /\\b[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+\\b/g,\n phone: /\\b(?:\\+?\\d{1,3}[-.\\s]?)?\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}\\b/g,\n};\n","import { DEFAULT_SENSITIVE_KEYS, PATTERNS } from './patterns.js';\nimport type { MaskOptions, MaskStrategy } from './types.js';\n\nexport * from './types.js';\nexport { DEFAULT_SENSITIVE_KEYS, PATTERNS };\n\n/**\n * Mask an email address (e.g. alice.smith@example.com -> a***h@example.com)\n */\nexport function maskEmail(email: string, full = false): string {\n if (full) return '[REDACTED_EMAIL]';\n const parts = email.split('@');\n if (parts.length !== 2) return '[REDACTED_EMAIL]';\n const [user, domain] = parts;\n if (!user || user.length <= 2) {\n return `${user[0] ?? '*'}***@${domain}`;\n }\n return `${user[0]}***${user[user.length - 1]}@${domain}`;\n}\n\n/**\n * Mask a credit card number (e.g. 4111-2222-3333-4444 -> ****-****-****-4444)\n */\nexport function maskCreditCard(card: string, full = false): string {\n if (full) return '[REDACTED_CREDIT_CARD]';\n const digitsOnly = card.replace(/\\D/g, '');\n if (digitsOnly.length < 4) return '****';\n const last4 = digitsOnly.slice(-4);\n return `****-****-****-${last4}`;\n}\n\n/**\n * Mask an API token, JWT, or secret key\n */\nexport function maskToken(token: string, full = false, label = 'TOKEN'): string {\n if (full || token.length < 10) return `[REDACTED_${label}]`;\n const prefix = token.slice(0, 4);\n const suffix = token.slice(-4);\n return `${prefix}...${suffix}`;\n}\n\n/**\n * Mask a phone number\n */\nexport function maskPhone(phone: string, full = false): string {\n if (full) return '[REDACTED_PHONE]';\n const digits = phone.replace(/\\D/g, '');\n if (digits.length < 4) return '***-***-****';\n const last4 = digits.slice(-4);\n return `***-***-${last4}`;\n}\n\n/**\n * Determine if an object key name matches known sensitive key terms\n */\nexport function isSensitiveKey(key: string, customKeys?: (string | RegExp)[]): boolean {\n const normalizedKey = key.toLowerCase().replace(/[-_]/g, '');\n const allKeys = customKeys ? [...DEFAULT_SENSITIVE_KEYS, ...customKeys] : DEFAULT_SENSITIVE_KEYS;\n\n for (const item of allKeys) {\n if (typeof item === 'string') {\n const normalizedItem = item.toLowerCase().replace(/[-_]/g, '');\n if (normalizedKey === normalizedItem || normalizedKey.includes(normalizedItem)) {\n return true;\n }\n } else if (item instanceof RegExp) {\n if (item.test(key)) return true;\n }\n }\n return false;\n}\n\n/**\n * Redact sensitive patterns inside a raw string\n */\nexport function maskString(text: string, options: MaskOptions = {}): string {\n if (typeof text !== 'string' || text.length === 0) return text;\n\n const strategy = options.strategy ?? 'partial';\n const isFull = strategy === 'full';\n const customFn = typeof strategy === 'function' ? strategy : null;\n\n const patterns = {\n jwt: options.patterns?.jwt ?? true,\n bearer: options.patterns?.bearer ?? true,\n apiKey: options.patterns?.apiKey ?? true,\n creditCard: options.patterns?.creditCard ?? true,\n email: options.patterns?.email ?? true,\n phone: options.patterns?.phone ?? true,\n };\n\n let result = text;\n\n // Bearer tokens\n if (patterns.bearer) {\n result = result.replace(PATTERNS.bearer, (match) => {\n if (customFn) return customFn(match, 'bearer');\n return isFull ? 'Bearer [REDACTED_TOKEN]' : `Bearer ${maskToken(match.replace(/^Bearer\\s+/i, ''), false, 'TOKEN')}`;\n });\n }\n\n // JWTs\n if (patterns.jwt) {\n result = result.replace(PATTERNS.jwt, (match) => {\n if (customFn) return customFn(match, 'jwt');\n return isFull ? '[REDACTED_JWT]' : maskToken(match, false, 'JWT');\n });\n }\n\n // API Keys (AWS, Stripe, GitHub, Slack)\n if (patterns.apiKey) {\n result = result.replace(PATTERNS.apiKey, (match) => {\n if (customFn) return customFn(match, 'apiKey');\n return isFull ? '[REDACTED_API_KEY]' : maskToken(match, false, 'KEY');\n });\n }\n\n // Credit Cards\n if (patterns.creditCard) {\n result = result.replace(PATTERNS.creditCard, (match) => {\n if (customFn) return customFn(match, 'creditCard');\n return maskCreditCard(match, isFull);\n });\n }\n\n // Emails\n if (patterns.email) {\n result = result.replace(PATTERNS.email, (match) => {\n if (customFn) return customFn(match, 'email');\n return maskEmail(match, isFull);\n });\n }\n\n // Phones\n if (patterns.phone) {\n result = result.replace(PATTERNS.phone, (match) => {\n if (customFn) return customFn(match, 'phone');\n return maskPhone(match, isFull);\n });\n }\n\n return result;\n}\n\n/**\n * Deeply sanitizes objects, arrays, and primitive values.\n * Handles circular references safely using WeakSet.\n */\nexport function mask<T = unknown>(value: T, options: MaskOptions = {}): T {\n const maxDepth = options.maxDepth ?? 20;\n const inPlace = options.inPlace ?? false;\n const seen = new WeakSet<object>();\n\n function traverse(current: unknown, key: string | null, depth: number): unknown {\n if (depth > maxDepth) return '[MAX_DEPTH_EXCEEDED]';\n\n if (current === null || current === undefined) {\n return current;\n }\n\n // Check if parent key itself indicates sensitive content\n if (key && isSensitiveKey(key, options.sensitiveKeys)) {\n if (typeof current === 'string') {\n const strategy = options.strategy ?? 'partial';\n if (typeof strategy === 'function') return strategy(current, 'sensitiveKey');\n return strategy === 'full' ? '[REDACTED]' : maskToken(current, false, 'SECRET');\n }\n return '[REDACTED]';\n }\n\n if (typeof current === 'string') {\n return maskString(current, options);\n }\n\n if (typeof current === 'number' || typeof current === 'boolean' || typeof current === 'bigint') {\n return current;\n }\n\n if (typeof current === 'function') {\n return current;\n }\n\n if (typeof current === 'object') {\n if (seen.has(current)) {\n return '[CIRCULAR]';\n }\n seen.add(current);\n\n if (current instanceof Date) {\n return new Date(current.getTime());\n }\n\n if (current instanceof RegExp) {\n return new RegExp(current.source, current.flags);\n }\n\n if (Array.isArray(current)) {\n const arr = inPlace ? (current as unknown[]) : new Array(current.length);\n for (let i = 0; i < current.length; i++) {\n arr[i] = traverse(current[i], key, depth + 1);\n }\n return arr;\n }\n\n const obj = current as Record<string, unknown>;\n const target: Record<string, unknown> = inPlace ? obj : {};\n\n for (const prop of Object.keys(obj)) {\n target[prop] = traverse(obj[prop], prop, depth + 1);\n }\n\n return target;\n }\n\n return current;\n }\n\n return traverse(value, null, 0) as T;\n}\n\nexport default mask;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sensi-mask",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Ultra-fast, zero-dependency deep PII and sensitive data masking for logs, objects, and strings",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup",
|
|
23
|
+
"test": "node --test --experimental-strip-types test/index.test.ts",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"prepublishOnly": "npm run build"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"mask",
|
|
29
|
+
"redact",
|
|
30
|
+
"pii",
|
|
31
|
+
"sensitive",
|
|
32
|
+
"logs",
|
|
33
|
+
"security",
|
|
34
|
+
"jwt",
|
|
35
|
+
"credit-card",
|
|
36
|
+
"sanitizer"
|
|
37
|
+
],
|
|
38
|
+
"author": "Anish Choudhary",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=18.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"tsup": "^8.3.5",
|
|
45
|
+
"typescript": "^5.7.3"
|
|
46
|
+
}
|
|
47
|
+
}
|