@debugbundle/redaction 0.1.0-next.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.
@@ -0,0 +1,15 @@
1
+ export type JsonPrimitive = string | number | boolean | null;
2
+ export type JsonValue = JsonPrimitive | JsonObject | JsonValue[];
3
+ export type JsonObject = {
4
+ [key: string]: JsonValue;
5
+ };
6
+ export interface RedactionResult<T extends JsonValue> {
7
+ redacted: T;
8
+ redacted_fields: string[];
9
+ }
10
+ export interface RedactionOptions {
11
+ sensitiveKeys?: string[];
12
+ replacement?: string;
13
+ }
14
+ export declare function redact<T extends JsonValue>(payload: T, options?: RedactionOptions): RedactionResult<T>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,SAAS,EAAE,CAAC;AACjE,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC;AAEtD,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,SAAS;IAClD,QAAQ,EAAE,CAAC,CAAC;IACZ,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,gBAAgB;IAC/B,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAqHD,wBAAgB,MAAM,CAAC,CAAC,SAAS,SAAS,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,gBAAgB,GAAG,eAAe,CAAC,CAAC,CAAC,CAUtG"}
package/dist/index.js ADDED
@@ -0,0 +1,98 @@
1
+ const DEFAULT_SENSITIVE_KEYS = [
2
+ "password",
3
+ "secret",
4
+ "token",
5
+ "api_key",
6
+ "apikey",
7
+ "access_token",
8
+ "refresh_token",
9
+ "private_key",
10
+ "authorization",
11
+ "bearer",
12
+ "cookie",
13
+ "session_id",
14
+ "passwd",
15
+ "ssn",
16
+ "credit_card",
17
+ "card_number",
18
+ "cvv",
19
+ "cvc",
20
+ "pin",
21
+ "expiry",
22
+ "phone",
23
+ "otp",
24
+ "verification_code"
25
+ ];
26
+ function canonicalizeSensitiveKey(value) {
27
+ return value.replace(/[^a-z0-9]+/g, "");
28
+ }
29
+ function splitKeyIntoSegments(key) {
30
+ return key
31
+ .trim()
32
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
33
+ .toLowerCase()
34
+ .split(/[^a-z0-9]+/)
35
+ .filter((segment) => segment.length > 0);
36
+ }
37
+ function buildKeyCandidates(key) {
38
+ const segments = splitKeyIntoSegments(key);
39
+ const candidates = new Set();
40
+ for (let start = 0; start < segments.length; start += 1) {
41
+ let combined = "";
42
+ for (let end = start; end < segments.length; end += 1) {
43
+ combined += segments[end];
44
+ candidates.add(combined);
45
+ }
46
+ }
47
+ return [...candidates];
48
+ }
49
+ function isSensitiveKey(key, sensitiveKeys) {
50
+ const normalized = key.trim().toLowerCase();
51
+ if (sensitiveKeys.includes(normalized)) {
52
+ return true;
53
+ }
54
+ const canonicalSensitiveKeys = new Set(sensitiveKeys.map(canonicalizeSensitiveKey));
55
+ const candidates = buildKeyCandidates(key);
56
+ return candidates.some((candidate) => canonicalSensitiveKeys.has(candidate));
57
+ }
58
+ function redactInternal(value, path, sensitiveKeys, replacement, touchedPaths, seen) {
59
+ if (Array.isArray(value)) {
60
+ if (seen.has(value)) {
61
+ return "[Circular]";
62
+ }
63
+ seen.add(value);
64
+ const redactedArray = value.map((entry, index) => redactInternal(entry, path.length === 0 ? `[${index}]` : `${path}[${index}]`, sensitiveKeys, replacement, touchedPaths, seen));
65
+ seen.delete(value);
66
+ return redactedArray;
67
+ }
68
+ if (value === null || typeof value !== "object") {
69
+ return value;
70
+ }
71
+ if (seen.has(value)) {
72
+ return "[Circular]";
73
+ }
74
+ seen.add(value);
75
+ const output = {};
76
+ for (const [key, nestedValue] of Object.entries(value)) {
77
+ const nextPath = path.length === 0 ? key : `${path}.${key}`;
78
+ if (isSensitiveKey(key, sensitiveKeys)) {
79
+ output[key] = replacement;
80
+ touchedPaths.push(nextPath);
81
+ continue;
82
+ }
83
+ output[key] = redactInternal(nestedValue, nextPath, sensitiveKeys, replacement, touchedPaths, seen);
84
+ }
85
+ seen.delete(value);
86
+ return output;
87
+ }
88
+ export function redact(payload, options) {
89
+ const sensitiveKeys = options?.sensitiveKeys?.map((key) => key.trim().toLowerCase()) ?? [...DEFAULT_SENSITIVE_KEYS];
90
+ const replacement = options?.replacement ?? "[REDACTED]";
91
+ const touchedPaths = [];
92
+ const redacted = redactInternal(payload, "", sensitiveKeys, replacement, touchedPaths, new WeakSet());
93
+ return {
94
+ redacted,
95
+ redacted_fields: touchedPaths
96
+ };
97
+ }
98
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,MAAM,sBAAsB,GAAG;IAC7B,UAAU;IACV,QAAQ;IACR,OAAO;IACP,SAAS;IACT,QAAQ;IACR,cAAc;IACd,eAAe;IACf,aAAa;IACb,eAAe;IACf,QAAQ;IACR,QAAQ;IACR,YAAY;IACZ,QAAQ;IACR,KAAK;IACL,aAAa;IACb,aAAa;IACb,KAAK;IACL,KAAK;IACL,KAAK;IACL,QAAQ;IACR,OAAO;IACP,KAAK;IACL,mBAAmB;CACX,CAAC;AAEX,SAAS,wBAAwB,CAAC,KAAa;IAC7C,OAAO,KAAK,CAAC,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;AAC1C,CAAC;AAED,SAAS,oBAAoB,CAAC,GAAW;IACvC,OAAO,GAAG;SACP,IAAI,EAAE;SACN,OAAO,CAAC,oB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AC,GAAG,EAAE,aAAa,CAAC,EAAE,CAAC;YACvC,MAAM,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC;YAC1B,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YAC5B,SAAS;QACX,CAAC;QAED,MAAM,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC,WAAW,EAAE,QAAQ,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,IAAI,CAAC,CAAC;IACtG,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACnB,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,MAAM,UAAU,MAAM,CAAsB,OAAU,EAAE,OAA0B;IAChF,MAAM,aAAa,GAAG,OAAO,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,IAAI,CAAC,GAAG,sBAAsB,CAAC,CAAC;IACpH,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI,YAAY,CAAC;IACzD,MAAM,YAAY,GAAa,EAAE,CAAC;IAClC,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,EAAE,EAAE,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,IAAI,OAAO,EAAU,CAAM,CAAC;IAEnH,OAAO;QACL,QAAQ;QACR,eAAe,EAAE,YAAY;KAC9B,CAAC;AACJ,CAAC"}
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@debugbundle/redaction",
3
+ "version": "0.1.0-next.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "license": "AGPL-3.0-only",
7
+ "description": "Sensitive-data redaction helpers for DebugBundle SDKs",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "main": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "dependencies": {}
23
+ }