@platform-sec/sensitive-string-core 0.6.0 → 0.6.1
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/dist/bson.d.ts +28 -0
- package/dist/bson.d.ts.map +1 -0
- package/dist/bson.js +37 -0
- package/dist/bson.js.map +1 -0
- package/dist/classification.d.ts +69 -0
- package/dist/classification.d.ts.map +1 -0
- package/dist/classification.js +104 -0
- package/dist/classification.js.map +1 -0
- package/dist/compare.d.ts +36 -0
- package/dist/compare.d.ts.map +1 -0
- package/dist/compare.js +123 -0
- package/dist/compare.js.map +1 -0
- package/dist/context.d.ts +78 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +110 -0
- package/dist/context.js.map +1 -0
- package/dist/derive.d.ts +110 -0
- package/dist/derive.d.ts.map +1 -0
- package/dist/derive.js +153 -0
- package/dist/derive.js.map +1 -0
- package/dist/destination.d.ts +120 -0
- package/dist/destination.d.ts.map +1 -0
- package/dist/destination.js +352 -0
- package/dist/destination.js.map +1 -0
- package/dist/deterministic.d.ts +145 -0
- package/dist/deterministic.d.ts.map +1 -0
- package/dist/deterministic.js +352 -0
- package/dist/deterministic.js.map +1 -0
- package/dist/dev-mode.d.ts +177 -0
- package/dist/dev-mode.d.ts.map +1 -0
- package/dist/dev-mode.js +503 -0
- package/dist/dev-mode.js.map +1 -0
- package/dist/environment.d.ts +181 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +558 -0
- package/dist/environment.js.map +1 -0
- package/dist/errors.d.ts +23 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +21 -0
- package/dist/errors.js.map +1 -0
- package/dist/global-state.d.ts +8 -0
- package/dist/global-state.d.ts.map +1 -0
- package/dist/global-state.js +22 -0
- package/dist/global-state.js.map +1 -0
- package/dist/hash.d.ts +74 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +253 -0
- package/dist/hash.js.map +1 -0
- package/dist/hygiene.d.ts +242 -0
- package/dist/hygiene.d.ts.map +1 -0
- package/dist/hygiene.js +257 -0
- package/dist/hygiene.js.map +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +233 -0
- package/dist/index.js.map +1 -0
- package/dist/mask.d.ts +51 -0
- package/dist/mask.d.ts.map +1 -0
- package/dist/mask.js +461 -0
- package/dist/mask.js.map +1 -0
- package/dist/own-property.d.ts +20 -0
- package/dist/own-property.d.ts.map +1 -0
- package/dist/own-property.js +34 -0
- package/dist/own-property.js.map +1 -0
- package/dist/patterns.d.ts +12 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +19 -0
- package/dist/patterns.js.map +1 -0
- package/dist/policy.d.ts +51 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +181 -0
- package/dist/policy.js.map +1 -0
- package/dist/probe.d.ts +141 -0
- package/dist/probe.d.ts.map +1 -0
- package/dist/probe.js +213 -0
- package/dist/probe.js.map +1 -0
- package/dist/redact-copy.d.ts +61 -0
- package/dist/redact-copy.d.ts.map +1 -0
- package/dist/redact-copy.js +217 -0
- package/dist/redact-copy.js.map +1 -0
- package/dist/redact.d.ts +141 -0
- package/dist/redact.d.ts.map +1 -0
- package/dist/redact.js +514 -0
- package/dist/redact.js.map +1 -0
- package/dist/redaction-marker.d.ts +48 -0
- package/dist/redaction-marker.d.ts.map +1 -0
- package/dist/redaction-marker.js +138 -0
- package/dist/redaction-marker.js.map +1 -0
- package/dist/redaction-observer.d.ts +108 -0
- package/dist/redaction-observer.d.ts.map +1 -0
- package/dist/redaction-observer.js +103 -0
- package/dist/redaction-observer.js.map +1 -0
- package/dist/redaction-scope.d.ts +21 -0
- package/dist/redaction-scope.d.ts.map +1 -0
- package/dist/redaction-scope.js +44 -0
- package/dist/redaction-scope.js.map +1 -0
- package/dist/reference.d.ts +101 -0
- package/dist/reference.d.ts.map +1 -0
- package/dist/reference.js +123 -0
- package/dist/reference.js.map +1 -0
- package/dist/regexp.d.ts +25 -0
- package/dist/regexp.d.ts.map +1 -0
- package/dist/regexp.js +61 -0
- package/dist/regexp.js.map +1 -0
- package/dist/release.d.ts +88 -0
- package/dist/release.d.ts.map +1 -0
- package/dist/release.js +108 -0
- package/dist/release.js.map +1 -0
- package/dist/reveal-budget.d.ts +148 -0
- package/dist/reveal-budget.d.ts.map +1 -0
- package/dist/reveal-budget.js +314 -0
- package/dist/reveal-budget.js.map +1 -0
- package/dist/revocation.d.ts +168 -0
- package/dist/revocation.d.ts.map +1 -0
- package/dist/revocation.js +541 -0
- package/dist/revocation.js.map +1 -0
- package/dist/sealing.d.ts +283 -0
- package/dist/sealing.d.ts.map +1 -0
- package/dist/sealing.js +523 -0
- package/dist/sealing.js.map +1 -0
- package/dist/sensitive-string.d.ts +919 -0
- package/dist/sensitive-string.d.ts.map +1 -0
- package/dist/sensitive-string.js +2260 -0
- package/dist/sensitive-string.js.map +1 -0
- package/dist/serialized.d.ts +33 -0
- package/dist/serialized.d.ts.map +1 -0
- package/dist/serialized.js +65 -0
- package/dist/serialized.js.map +1 -0
- package/dist/template.d.ts +69 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +158 -0
- package/dist/template.js.map +1 -0
- package/dist/ttl.d.ts +104 -0
- package/dist/ttl.d.ts.map +1 -0
- package/dist/ttl.js +330 -0
- package/dist/ttl.js.map +1 -0
- package/dist/types.d.ts +859 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/dist/winston.d.ts +114 -0
- package/dist/winston.d.ts.map +1 -0
- package/dist/winston.js +170 -0
- package/dist/winston.js.map +1 -0
- package/dist/worker.d.ts +398 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +861 -0
- package/dist/worker.js.map +1 -0
- package/dist/wrap.d.ts +287 -0
- package/dist/wrap.d.ts.map +1 -0
- package/dist/wrap.js +553 -0
- package/dist/wrap.js.map +1 -0
- package/package.json +10 -10
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global-state.js","sourceRoot":"","sources":["../src/global-state.ts"],"names":[],"mappings":";;AAQA,gDAYC;AApBD,MAAM,mBAAmB,GAAG,qCAAqC,CAAC;AAElE;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAI,IAAY,EAAE,UAAmB;IACrE,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,mBAAmB,IAAI,IAAI,EAAE,CAAC,CAAC;IACzD,MAAM,OAAO,GAAG,UAA4D,CAAC;IAC7E,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAE9B,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO,QAAa,CAAC;IACvB,CAAC;IAED,MAAM,KAAK,GAAG,UAAU,EAAE,CAAC;IAC3B,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACrB,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/dist/hash.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { HashAlgorithm, HashAuditDetails, HashEncoding, HashOptions, HmacAuditDetails, HmacOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A validated set of options for one digest.
|
|
4
|
+
*
|
|
5
|
+
* Producing one requires no access to the raw value, so a rejected call is
|
|
6
|
+
* refused before anything is read, charged or audited.
|
|
7
|
+
*/
|
|
8
|
+
export interface HashPlan {
|
|
9
|
+
readonly operation: 'hash' | 'hmac';
|
|
10
|
+
readonly algorithm: HashAlgorithm;
|
|
11
|
+
readonly encoding: HashEncoding;
|
|
12
|
+
/** The salt, as bytes. Always `undefined` for an HMAC, which has a key instead. */
|
|
13
|
+
readonly salt: Uint8Array | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Validate the options for a digest.
|
|
17
|
+
*
|
|
18
|
+
* @param operation - Which method is asking, for the error messages.
|
|
19
|
+
* @param algorithm - The requested algorithm, unvalidated.
|
|
20
|
+
* @param options - The caller's options, unvalidated.
|
|
21
|
+
*/
|
|
22
|
+
export declare function planHash(operation: 'hash' | 'hmac', algorithm: HashAlgorithm, options: HashOptions | HmacOptions): HashPlan;
|
|
23
|
+
/**
|
|
24
|
+
* An HMAC key as bytes.
|
|
25
|
+
*
|
|
26
|
+
* The floor is {@link MIN_DERIVATION_KEY_BYTES}, the same one `deriveKeyedToken`
|
|
27
|
+
* applies and for the same reason: below it the key is enumerable alongside the
|
|
28
|
+
* value, and a keyed digest that an attacker can recompute is an unkeyed one
|
|
29
|
+
* that looks stronger in review.
|
|
30
|
+
*
|
|
31
|
+
* @param key - The caller's key, unvalidated. A protected key has already been
|
|
32
|
+
* read through the gate by the time it arrives here.
|
|
33
|
+
*/
|
|
34
|
+
export declare function requireHmacKey(key: unknown): Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* The audit record for a plan.
|
|
37
|
+
*
|
|
38
|
+
* Derived from the options alone, so the hook runs at the same point in
|
|
39
|
+
* `hash()` as it does in `reveal()`: before the digest exists, and early enough
|
|
40
|
+
* that a sink which throws stops the operation.
|
|
41
|
+
*/
|
|
42
|
+
export declare function hashAuditDetails(plan: HashPlan): HashAuditDetails;
|
|
43
|
+
/** The same, for `hmac()`. */
|
|
44
|
+
export declare function hmacAuditDetails(plan: HashPlan, protectedKey: boolean): HmacAuditDetails;
|
|
45
|
+
/**
|
|
46
|
+
* Compute the digest.
|
|
47
|
+
*
|
|
48
|
+
* The salt is prepended rather than appended: appending it leaves the digest
|
|
49
|
+
* open to a length extension, and prepending is also what every other
|
|
50
|
+
* implementation of `hash(salt, value)` does, which is what makes the result
|
|
51
|
+
* reproducible outside this library.
|
|
52
|
+
*
|
|
53
|
+
* @param value - The raw value, already authorized and audited by the caller.
|
|
54
|
+
* @param plan - Validated options from {@link planHash}.
|
|
55
|
+
*/
|
|
56
|
+
export declare function applyHash(value: string, plan: HashPlan): string;
|
|
57
|
+
/**
|
|
58
|
+
* Compute the keyed digest.
|
|
59
|
+
*
|
|
60
|
+
* @param value - The raw value, already authorized and audited by the caller.
|
|
61
|
+
* @param key - The key bytes from {@link requireHmacKey}.
|
|
62
|
+
* @param plan - Validated options from {@link planHash}.
|
|
63
|
+
*/
|
|
64
|
+
export declare function applyHmac(value: string, key: Uint8Array, plan: HashPlan): string;
|
|
65
|
+
/**
|
|
66
|
+
* The label a digest carries.
|
|
67
|
+
*
|
|
68
|
+
* `${label}:hash` when the value has one, and the bare operation when it does
|
|
69
|
+
* not. Never `undefined:hash`: a label appears in audit records and in
|
|
70
|
+
* `inspect()` output, and a rendered `undefined` there reads as a bug in the
|
|
71
|
+
* library rather than as a value nobody labelled.
|
|
72
|
+
*/
|
|
73
|
+
export declare function hashLabel(label: string | undefined, operation: 'hash' | 'hmac'): string;
|
|
74
|
+
//# sourceMappingURL=hash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.d.ts","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EACb,gBAAgB,EAChB,YAAY,EACZ,WAAW,EACX,gBAAgB,EAChB,WAAW,EACZ,MAAM,YAAY,CAAC;AAmEpB;;;;;GAKG;AACH,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IACpC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC;IAClC,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,mFAAmF;IACnF,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,SAAS,CAAC;CACvC;AA+FD;;;;;;GAMG;AACH,wBAAgB,QAAQ,CACtB,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,SAAS,EAAE,aAAa,EACxB,OAAO,EAAE,WAAW,GAAG,WAAW,GACjC,QAAQ,CA2BV;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,GAAG,UAAU,CAyBvD;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,QAAQ,GAAG,gBAAgB,CAOjE;AAED,8BAA8B;AAC9B,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,GAAG,gBAAgB,CAOxF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,CAU/D;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,CAKhF;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAEvF"}
|
package/dist/hash.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.planHash = planHash;
|
|
4
|
+
exports.requireHmacKey = requireHmacKey;
|
|
5
|
+
exports.hashAuditDetails = hashAuditDetails;
|
|
6
|
+
exports.hmacAuditDetails = hmacAuditDetails;
|
|
7
|
+
exports.applyHash = applyHash;
|
|
8
|
+
exports.applyHmac = applyHmac;
|
|
9
|
+
exports.hashLabel = hashLabel;
|
|
10
|
+
const node_crypto_1 = require("node:crypto");
|
|
11
|
+
const derive_js_1 = require("./derive.js");
|
|
12
|
+
/**
|
|
13
|
+
* One-way transformations of a protected value, on `node:crypto` alone.
|
|
14
|
+
*
|
|
15
|
+
* This module holds the algorithm allowlist, the option validation and the
|
|
16
|
+
* digest itself. It does not read a `SensitiveString`, does not authorize and
|
|
17
|
+
* does not audit: the class reads the value through the ordinary gate and
|
|
18
|
+
* hands it here, so a digest costs exactly one audited exposure and appears in
|
|
19
|
+
* the log as one.
|
|
20
|
+
*
|
|
21
|
+
* What is deliberately absent is password storage. A digest from here is fast
|
|
22
|
+
* by construction, which is the property a password hash must not have, and
|
|
23
|
+
* the memory-hard functions that do have it need a native module that rule R7
|
|
24
|
+
* keeps out of `core`. That lives in `@platform-sec/sensitive-string-kdf`.
|
|
25
|
+
*/
|
|
26
|
+
/** Digest algorithms this library will compute. */
|
|
27
|
+
const ALLOWED_ALGORITHMS = [
|
|
28
|
+
'sha256',
|
|
29
|
+
'sha384',
|
|
30
|
+
'sha512',
|
|
31
|
+
'sha3-256',
|
|
32
|
+
'sha3-512',
|
|
33
|
+
];
|
|
34
|
+
const ALLOWED_ALGORITHM_SET = new Set(ALLOWED_ALGORITHMS);
|
|
35
|
+
/**
|
|
36
|
+
* Names worth refusing with a reason rather than with a list.
|
|
37
|
+
*
|
|
38
|
+
* Every one of them is reachable from `node:crypto`, so a caller who asks for
|
|
39
|
+
* one has usually copied it from code that predates the break. The message
|
|
40
|
+
* says what is wrong with it instead of leaving the caller to guess which of
|
|
41
|
+
* the allowed names is the near equivalent.
|
|
42
|
+
*/
|
|
43
|
+
const BROKEN_ALGORITHMS = new Map([
|
|
44
|
+
['md5', 'MD5 collides in milliseconds'],
|
|
45
|
+
['md5-sha1', 'MD5 collides in milliseconds'],
|
|
46
|
+
['md4', 'MD4 is broken beyond collisions'],
|
|
47
|
+
['sha1', 'SHA-1 collisions have been demonstrated'],
|
|
48
|
+
['sha-1', 'SHA-1 collisions have been demonstrated'],
|
|
49
|
+
['ripemd160', 'RIPEMD-160 has a 160-bit output and no modern review'],
|
|
50
|
+
['rmd160', 'RIPEMD-160 has a 160-bit output and no modern review'],
|
|
51
|
+
]);
|
|
52
|
+
const ALLOWED_ENCODINGS = ['hex', 'base64', 'base64url'];
|
|
53
|
+
const ALLOWED_ENCODING_SET = new Set(ALLOWED_ENCODINGS);
|
|
54
|
+
/**
|
|
55
|
+
* What this Node build can actually compute.
|
|
56
|
+
*
|
|
57
|
+
* The allowlist says what the library is willing to use; this says what OpenSSL
|
|
58
|
+
* underneath it offers, which is not the same thing on a FIPS build, where
|
|
59
|
+
* SHA-3 can be missing. Read once, on first use rather than at import, and
|
|
60
|
+
* cached: `getHashes()` allocates an array of every digest name the build
|
|
61
|
+
* knows.
|
|
62
|
+
*/
|
|
63
|
+
let availableAlgorithms;
|
|
64
|
+
function isAvailable(algorithm) {
|
|
65
|
+
availableAlgorithms ??= new Set((0, node_crypto_1.getHashes)());
|
|
66
|
+
return availableAlgorithms.has(algorithm);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Describe a rejected option without disclosing it.
|
|
70
|
+
*/
|
|
71
|
+
function describeType(value) {
|
|
72
|
+
return value === null ? 'null' : typeof value;
|
|
73
|
+
}
|
|
74
|
+
function requireAlgorithm(algorithm, operation) {
|
|
75
|
+
if (typeof algorithm !== 'string') {
|
|
76
|
+
throw new TypeError(`SensitiveString.${operation}: algorithm must be a string, received ${describeType(algorithm)}`);
|
|
77
|
+
}
|
|
78
|
+
const normalized = algorithm.toLowerCase();
|
|
79
|
+
const broken = BROKEN_ALGORITHMS.get(normalized);
|
|
80
|
+
if (broken !== undefined) {
|
|
81
|
+
throw new Error(`SensitiveString.${operation}: '${algorithm}' is not available because ${broken}; ` +
|
|
82
|
+
`use one of ${ALLOWED_ALGORITHMS.join(', ')}`);
|
|
83
|
+
}
|
|
84
|
+
if (!ALLOWED_ALGORITHM_SET.has(normalized)) {
|
|
85
|
+
throw new TypeError(`SensitiveString.${operation}: unknown algorithm '${algorithm}'; ` +
|
|
86
|
+
`use one of ${ALLOWED_ALGORITHMS.join(', ')}`);
|
|
87
|
+
}
|
|
88
|
+
const known = normalized;
|
|
89
|
+
if (!isAvailable(known)) {
|
|
90
|
+
throw new Error(`SensitiveString.${operation}: '${known}' is not available in this Node build's crypto ` +
|
|
91
|
+
'provider, which a FIPS build is the usual reason for');
|
|
92
|
+
}
|
|
93
|
+
return known;
|
|
94
|
+
}
|
|
95
|
+
function requireEncoding(encoding, operation) {
|
|
96
|
+
if (encoding === undefined) {
|
|
97
|
+
return 'hex';
|
|
98
|
+
}
|
|
99
|
+
if (typeof encoding !== 'string' || !ALLOWED_ENCODING_SET.has(encoding)) {
|
|
100
|
+
throw new TypeError(`SensitiveString.${operation}: encoding must be one of ${ALLOWED_ENCODINGS.join(', ')}, ` +
|
|
101
|
+
`received ${typeof encoding === 'string' ? `'${encoding}'` : describeType(encoding)}`);
|
|
102
|
+
}
|
|
103
|
+
return encoding;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The salt as bytes.
|
|
107
|
+
*
|
|
108
|
+
* An empty salt is refused rather than ignored. It produces exactly the digest
|
|
109
|
+
* of the unsalted value, so accepting it would let `salt: ''` read as salted
|
|
110
|
+
* everywhere it appears, including in the audit record.
|
|
111
|
+
*/
|
|
112
|
+
function requireSalt(salt) {
|
|
113
|
+
if (salt === undefined) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
const bytes = typeof salt === 'string'
|
|
117
|
+
? new TextEncoder().encode(salt)
|
|
118
|
+
: salt instanceof Uint8Array
|
|
119
|
+
? salt
|
|
120
|
+
: undefined;
|
|
121
|
+
if (bytes === undefined) {
|
|
122
|
+
throw new TypeError(`SensitiveString.hash: salt must be a string or a Uint8Array, received ${describeType(salt)}`);
|
|
123
|
+
}
|
|
124
|
+
if (bytes.length === 0) {
|
|
125
|
+
throw new RangeError('SensitiveString.hash: salt must not be empty; a zero-length salt is the unsalted digest ' +
|
|
126
|
+
'under another name');
|
|
127
|
+
}
|
|
128
|
+
return bytes;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Validate the options for a digest.
|
|
132
|
+
*
|
|
133
|
+
* @param operation - Which method is asking, for the error messages.
|
|
134
|
+
* @param algorithm - The requested algorithm, unvalidated.
|
|
135
|
+
* @param options - The caller's options, unvalidated.
|
|
136
|
+
*/
|
|
137
|
+
function planHash(operation, algorithm, options) {
|
|
138
|
+
if (options === null || typeof options !== 'object') {
|
|
139
|
+
throw new TypeError(`SensitiveString.${operation}: options must be an object, received ${describeType(options)}`);
|
|
140
|
+
}
|
|
141
|
+
// In argument order, so a call that gets both the algorithm and an option
|
|
142
|
+
// wrong reports the algorithm: it is the decision that matters, and the one
|
|
143
|
+
// the caller is likelier to have copied from somewhere older.
|
|
144
|
+
const checked = requireAlgorithm(algorithm, operation);
|
|
145
|
+
const encoding = requireEncoding(options.encoding, operation);
|
|
146
|
+
// An HMAC takes a key, not a salt. Silently ignoring one would leave a caller
|
|
147
|
+
// believing a keyed digest was also salted.
|
|
148
|
+
if (operation === 'hmac' && options.salt !== undefined) {
|
|
149
|
+
throw new Error('SensitiveString.hmac: salt does not apply to a keyed digest; the key is what separates it');
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
operation,
|
|
153
|
+
algorithm: checked,
|
|
154
|
+
encoding,
|
|
155
|
+
salt: operation === 'hash' ? requireSalt(options.salt) : undefined,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* An HMAC key as bytes.
|
|
160
|
+
*
|
|
161
|
+
* The floor is {@link MIN_DERIVATION_KEY_BYTES}, the same one `deriveKeyedToken`
|
|
162
|
+
* applies and for the same reason: below it the key is enumerable alongside the
|
|
163
|
+
* value, and a keyed digest that an attacker can recompute is an unkeyed one
|
|
164
|
+
* that looks stronger in review.
|
|
165
|
+
*
|
|
166
|
+
* @param key - The caller's key, unvalidated. A protected key has already been
|
|
167
|
+
* read through the gate by the time it arrives here.
|
|
168
|
+
*/
|
|
169
|
+
function requireHmacKey(key) {
|
|
170
|
+
const bytes = typeof key === 'string'
|
|
171
|
+
? new TextEncoder().encode(key)
|
|
172
|
+
: key instanceof Uint8Array
|
|
173
|
+
? key
|
|
174
|
+
: undefined;
|
|
175
|
+
if (bytes === undefined) {
|
|
176
|
+
throw new TypeError('SensitiveString.hmac: key must be a string, a Uint8Array, or a SensitiveString, ' +
|
|
177
|
+
`received ${describeType(key)}`);
|
|
178
|
+
}
|
|
179
|
+
if (bytes.length < derive_js_1.MIN_DERIVATION_KEY_BYTES) {
|
|
180
|
+
// The length is metadata about the key rather than the key, so naming it is
|
|
181
|
+
// safe and it is the one thing that tells the caller what to fix.
|
|
182
|
+
throw new RangeError(`SensitiveString.hmac: key must be at least ${derive_js_1.MIN_DERIVATION_KEY_BYTES} bytes, received ` +
|
|
183
|
+
`${bytes.length}; 32 bytes from randomBytes or a secrets manager is the recommendation`);
|
|
184
|
+
}
|
|
185
|
+
return bytes;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The audit record for a plan.
|
|
189
|
+
*
|
|
190
|
+
* Derived from the options alone, so the hook runs at the same point in
|
|
191
|
+
* `hash()` as it does in `reveal()`: before the digest exists, and early enough
|
|
192
|
+
* that a sink which throws stops the operation.
|
|
193
|
+
*/
|
|
194
|
+
function hashAuditDetails(plan) {
|
|
195
|
+
return Object.freeze({
|
|
196
|
+
operation: 'hash',
|
|
197
|
+
algorithm: plan.algorithm,
|
|
198
|
+
encoding: plan.encoding,
|
|
199
|
+
salted: plan.salt !== undefined,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/** The same, for `hmac()`. */
|
|
203
|
+
function hmacAuditDetails(plan, protectedKey) {
|
|
204
|
+
return Object.freeze({
|
|
205
|
+
operation: 'hmac',
|
|
206
|
+
algorithm: plan.algorithm,
|
|
207
|
+
encoding: plan.encoding,
|
|
208
|
+
protectedKey,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Compute the digest.
|
|
213
|
+
*
|
|
214
|
+
* The salt is prepended rather than appended: appending it leaves the digest
|
|
215
|
+
* open to a length extension, and prepending is also what every other
|
|
216
|
+
* implementation of `hash(salt, value)` does, which is what makes the result
|
|
217
|
+
* reproducible outside this library.
|
|
218
|
+
*
|
|
219
|
+
* @param value - The raw value, already authorized and audited by the caller.
|
|
220
|
+
* @param plan - Validated options from {@link planHash}.
|
|
221
|
+
*/
|
|
222
|
+
function applyHash(value, plan) {
|
|
223
|
+
const hash = (0, node_crypto_1.createHash)(plan.algorithm);
|
|
224
|
+
if (plan.salt !== undefined) {
|
|
225
|
+
hash.update(plan.salt);
|
|
226
|
+
}
|
|
227
|
+
hash.update(value, 'utf8');
|
|
228
|
+
return hash.digest(plan.encoding);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Compute the keyed digest.
|
|
232
|
+
*
|
|
233
|
+
* @param value - The raw value, already authorized and audited by the caller.
|
|
234
|
+
* @param key - The key bytes from {@link requireHmacKey}.
|
|
235
|
+
* @param plan - Validated options from {@link planHash}.
|
|
236
|
+
*/
|
|
237
|
+
function applyHmac(value, key, plan) {
|
|
238
|
+
const hmac = (0, node_crypto_1.createHmac)(plan.algorithm, key);
|
|
239
|
+
hmac.update(value, 'utf8');
|
|
240
|
+
return hmac.digest(plan.encoding);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* The label a digest carries.
|
|
244
|
+
*
|
|
245
|
+
* `${label}:hash` when the value has one, and the bare operation when it does
|
|
246
|
+
* not. Never `undefined:hash`: a label appears in audit records and in
|
|
247
|
+
* `inspect()` output, and a rendered `undefined` there reads as a bug in the
|
|
248
|
+
* library rather than as a value nobody labelled.
|
|
249
|
+
*/
|
|
250
|
+
function hashLabel(label, operation) {
|
|
251
|
+
return label === undefined ? operation : `${label}:${operation}`;
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=hash.js.map
|
package/dist/hash.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../src/hash.ts"],"names":[],"mappings":";;AA6LA,4BA+BC;AAaD,wCAyBC;AASD,4CAOC;AAGD,4CAOC;AAaD,8BAUC;AASD,8BAKC;AAUD,8BAEC;AA7UD,6CAAgE;AAShE,2CAAuD;AAEvD;;;;;;;;;;;;;GAaG;AAEH,mDAAmD;AACnD,MAAM,kBAAkB,GAA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CAAC;QACnB,SAAS,EAAE,MAAe;QAC1B,SAAS,EAAE,IAAI,CAAC,SAAS;QACzB,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,YAAY;KACb,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,SAAS,CAAC,KAAa,EAAE,IAAc;IACrD,MAAM,IAAI,GAAG,IAAA,wBAAU,EAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAExC,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAC5B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACzB,CAAC;IAED,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAE3B,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS,CAAC,KAAa,EAAE,GAAe,EAAE,IAAc;IACtE,MAAM,IAAI,GAAG,IAAA,wBAAU,EAAC,IAAI,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IAC7C,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAE3B,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACpC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,SAAS,CAAC,KAAyB,EAAE,SAA0B;IAC7E,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,KAAK,IAAI,SAAS,EAAE,CAAC;AACnE,CAAC"}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { SensitiveStringError } from './errors.js';
|
|
2
|
+
import type { SensitivityClass } from './types.js';
|
|
3
|
+
import type { SensitiveString } from './sensitive-string.js';
|
|
4
|
+
/**
|
|
5
|
+
* What this library can do about the plaintext already in the V8 heap, which
|
|
6
|
+
* is less than the phrase "memory hygiene" suggests and is worth stating
|
|
7
|
+
* before the API rather than after it.
|
|
8
|
+
*
|
|
9
|
+
* JavaScript cannot overwrite the backing store of a string it holds a
|
|
10
|
+
* reference to. There is no primitive for it, and encoding the value into a
|
|
11
|
+
* buffer so the buffer can be zeroed only adds a second copy, in external
|
|
12
|
+
* memory a heap snapshot cannot even show. The one thing available is dropping
|
|
13
|
+
* the last reference and letting the collector reclaim the memory on its own
|
|
14
|
+
* schedule.
|
|
15
|
+
*
|
|
16
|
+
* So `clear()` drops *the library's* reference. Whether that was the last
|
|
17
|
+
* reference is a property of the calling code: a route that wraps
|
|
18
|
+
* `req.body.token` and keeps the parsed body alive for the rest of the request
|
|
19
|
+
* gains nothing from it, because the instance's value and `req.body.token` are
|
|
20
|
+
* the same string object. The shape this feature is for is a credential the
|
|
21
|
+
* library holds alone, loaded once and used once.
|
|
22
|
+
*
|
|
23
|
+
* Two process-global switches sit beside it, both off by default and both
|
|
24
|
+
* under rule R6: they narrow an accident, not an attacker who is already
|
|
25
|
+
* running code in the process.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Raised when an operation needs the raw value of a cleared instance.
|
|
29
|
+
*
|
|
30
|
+
* Distinguished from `ExpiredSensitiveValueError` so a caller can tell a value
|
|
31
|
+
* that lapsed from one this process destroyed: the first may be worth fetching
|
|
32
|
+
* again, the second means the code holds a reference it was done with.
|
|
33
|
+
*/
|
|
34
|
+
export declare class ClearedSensitiveValueError extends SensitiveStringError {
|
|
35
|
+
readonly clearedAt: Date;
|
|
36
|
+
readonly attemptedAt: Date;
|
|
37
|
+
readonly label?: string | undefined;
|
|
38
|
+
readonly name = "ClearedSensitiveValueError";
|
|
39
|
+
readonly code = "cleared-value";
|
|
40
|
+
constructor(clearedAt: Date, attemptedAt: Date, label?: string | undefined);
|
|
41
|
+
/** Milliseconds between the clear and the attempted access. */
|
|
42
|
+
get clearedFor(): number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Process-global memory hygiene settings.
|
|
46
|
+
*
|
|
47
|
+
* Both are read at construction, so a value built before {@link setMemoryHygiene}
|
|
48
|
+
* runs is neither frozen nor tracked. Configure this alongside policy
|
|
49
|
+
* registration in the process's entry module, before anything wraps a secret.
|
|
50
|
+
*/
|
|
51
|
+
export interface MemoryHygieneConfig {
|
|
52
|
+
/**
|
|
53
|
+
* Freeze each new instance at construction, so its public surface cannot be
|
|
54
|
+
* rewritten.
|
|
55
|
+
*
|
|
56
|
+
* What that blocks is more than the obvious: an own `toJSON` replaces
|
|
57
|
+
* redaction outright on the path most likely to reach a log aggregator, and
|
|
58
|
+
* a `klass` rewrite downgrades a credential to a class with different
|
|
59
|
+
* redaction, permitted partial queries, permitted masking, and no
|
|
60
|
+
* constant-time comparison. Prototype replacement goes too.
|
|
61
|
+
*
|
|
62
|
+
* What it does not touch: `#private` fields, so `clear()` still works, and
|
|
63
|
+
* the shared state an instance points at, so `renew()` still moves a frozen
|
|
64
|
+
* value's deadline. Freezing an object does not freeze what it references.
|
|
65
|
+
*
|
|
66
|
+
* Costs roughly 51 ns per construction, on Node 22.15.0. It is off by
|
|
67
|
+
* default for the other reason: it is a breaking change for any consumer, or
|
|
68
|
+
* any framework, that decorates an instance with a property of its own.
|
|
69
|
+
*
|
|
70
|
+
* Default `false`.
|
|
71
|
+
*/
|
|
72
|
+
freezeInstances?: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Track instances so {@link clearAllSensitiveValues} can reach them.
|
|
75
|
+
*
|
|
76
|
+
* Costs about 202 bytes per instance, roughly 87% on top of the instance
|
|
77
|
+
* itself, split between a `WeakRef` in a `Set` and its `FinalizationRegistry`
|
|
78
|
+
* entry. A service that wraps a few dozen credentials at boot will never
|
|
79
|
+
* notice. A service that wraps a value per request roughly doubles the
|
|
80
|
+
* memory cost of doing so.
|
|
81
|
+
*
|
|
82
|
+
* Default `false`.
|
|
83
|
+
*/
|
|
84
|
+
trackInstances?: boolean;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Every switch resolved, which is what the singleton holds.
|
|
88
|
+
*
|
|
89
|
+
* Module-private, and `getMemoryHygiene` spells its return type out rather than
|
|
90
|
+
* naming it: a consumer reading the declaration should see the shape, not an
|
|
91
|
+
* alias it cannot import.
|
|
92
|
+
*/
|
|
93
|
+
type ResolvedHygiene = Readonly<Required<MemoryHygieneConfig>>;
|
|
94
|
+
/**
|
|
95
|
+
* The tracked value's type, loose on purpose: this module is imported by
|
|
96
|
+
* `sensitive-string.ts`, so the only binding it may take from there is a type.
|
|
97
|
+
*/
|
|
98
|
+
type TrackedValue = SensitiveString<SensitivityClass>;
|
|
99
|
+
interface HygieneGlobals {
|
|
100
|
+
config: ResolvedHygiene;
|
|
101
|
+
/**
|
|
102
|
+
* Every instance built while tracking was on and not yet collected.
|
|
103
|
+
*
|
|
104
|
+
* A `WeakRef` each, so tracking never keeps alive the thing it exists to
|
|
105
|
+
* destroy. The set is not a census: it lags collection by at least a turn,
|
|
106
|
+
* for the reasons on `clearAllSensitiveValues`.
|
|
107
|
+
*/
|
|
108
|
+
tracked: Set<WeakRef<TrackedValue>>;
|
|
109
|
+
/** Prunes the set as instances are collected, so it does not grow forever. */
|
|
110
|
+
finalizer: FinalizationRegistry<WeakRef<TrackedValue>>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The one registry and configuration every physical copy of core shares.
|
|
114
|
+
*
|
|
115
|
+
* Exported for `sensitive-string.ts`, which reads `config` on the construction
|
|
116
|
+
* path: one property chain rather than a call through an accessor, which is
|
|
117
|
+
* what a process that never turns either switch on pays for the feature.
|
|
118
|
+
*
|
|
119
|
+
* @internal
|
|
120
|
+
*/
|
|
121
|
+
export declare const hygiene: HygieneGlobals;
|
|
122
|
+
/**
|
|
123
|
+
* Start tracking an instance, so a later bulk clear can reach it.
|
|
124
|
+
*
|
|
125
|
+
* The instance is its own unregister token, so `clearAllSensitiveValues` can
|
|
126
|
+
* drop what it has already destroyed.
|
|
127
|
+
*
|
|
128
|
+
* @internal
|
|
129
|
+
*/
|
|
130
|
+
export declare function trackInstance(instance: TrackedValue): void;
|
|
131
|
+
/**
|
|
132
|
+
* Configure memory hygiene for this process. Merges: an unset field is left as
|
|
133
|
+
* it was, so turning tracking on later does not silently unfreeze.
|
|
134
|
+
*
|
|
135
|
+
* Both switches are read at construction, so this belongs in the entry module
|
|
136
|
+
* beside `registerPolicy`, before anything wraps a value. An instance built
|
|
137
|
+
* before this call is neither frozen nor tracked, and there is no way to
|
|
138
|
+
* retrofit either onto it.
|
|
139
|
+
*
|
|
140
|
+
* Per-isolate, like every other process-global in this library: a worker
|
|
141
|
+
* thread has its own `globalThis` and so its own configuration and its own
|
|
142
|
+
* tracked set. Configuring the main thread configures the main thread.
|
|
143
|
+
*
|
|
144
|
+
* @param config - The switches to change.
|
|
145
|
+
* @throws TypeError If the configuration, or one of its fields, has the wrong type.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* setMemoryHygiene({ trackInstances: true, freezeInstances: true });
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function setMemoryHygiene(config: MemoryHygieneConfig): void;
|
|
153
|
+
/**
|
|
154
|
+
* The configuration in force, with every default resolved.
|
|
155
|
+
*
|
|
156
|
+
* A frozen copy of the live object rather than the object itself, so a caller
|
|
157
|
+
* cannot reconfigure the process by writing to what it reads back. The result
|
|
158
|
+
* is accepted by {@link setMemoryHygiene}, so it can be saved and restored.
|
|
159
|
+
*/
|
|
160
|
+
export declare function getMemoryHygiene(): Readonly<Required<MemoryHygieneConfig>>;
|
|
161
|
+
/**
|
|
162
|
+
* Clear every instance this process is still tracking, and report how many.
|
|
163
|
+
*
|
|
164
|
+
* Only reaches values constructed while `trackInstances` was on, so this
|
|
165
|
+
* returns `0` in a process that never turned tracking on. It is the only way
|
|
166
|
+
* to reach a derivative whose holder cannot be named, which is what makes it
|
|
167
|
+
* more than a convenience: a substring of 13 characters or more pins its
|
|
168
|
+
* parent's entire backing store, so the parent's own `clear()` frees nothing
|
|
169
|
+
* until that substring is cleared too, and the substring is usually a local
|
|
170
|
+
* some function has already dropped.
|
|
171
|
+
*
|
|
172
|
+
* The walk reaches origins and derivatives in whatever order the set holds
|
|
173
|
+
* them, so it clears values whose origin it has already cleared. That is
|
|
174
|
+
* deliberate: `clear()` is idempotent per instance and not per lineage, so a
|
|
175
|
+
* derivative reached after its origin still drops its own copy and releases
|
|
176
|
+
* its own pin.
|
|
177
|
+
*
|
|
178
|
+
* The set is emptied of what this call reached, and is not empty afterwards in
|
|
179
|
+
* any promised sense: entries whose instance has been collected are removed by
|
|
180
|
+
* a `FinalizationRegistry` that needs at least two GC cycles and two macrotask
|
|
181
|
+
* turns to run, and a pass that calls `deref()` on an entry keeps that target
|
|
182
|
+
* alive to the end of the current job. The set's size is a lagging indicator
|
|
183
|
+
* with no bound worth promising.
|
|
184
|
+
*
|
|
185
|
+
* On a `SIGTERM` handler, call it after the drain and not before it: clearing
|
|
186
|
+
* at the top of the handler destroys credentials that in-flight requests are
|
|
187
|
+
* still using. It buys nothing at all if the handler ends in `process.exit()`;
|
|
188
|
+
* what it narrows is the window a process stays alive after draining, where a
|
|
189
|
+
* heap snapshot or a crash dump can still be taken.
|
|
190
|
+
*
|
|
191
|
+
* @returns How many instances were cleared by this call.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* process.on('SIGTERM', async () => {
|
|
196
|
+
* await server.close(); // stop accepting, drain what is in flight
|
|
197
|
+
* await pool.end(); // nothing still needs a credential
|
|
198
|
+
* clearAllSensitiveValues(); // now the values are dead weight
|
|
199
|
+
* });
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
export declare function clearAllSensitiveValues(): number;
|
|
203
|
+
/**
|
|
204
|
+
* Restore the defaults and forget every tracked instance.
|
|
205
|
+
*
|
|
206
|
+
* Test-only, and gated exactly as `resetPolicies()` is: it undoes process-wide
|
|
207
|
+
* configuration, so a production caller reaching for it is describing a bug.
|
|
208
|
+
* It does not clear the instances it forgets, because forgetting is not
|
|
209
|
+
* destroying and a test that wanted them cleared has
|
|
210
|
+
* {@link clearAllSensitiveValues} for that.
|
|
211
|
+
*
|
|
212
|
+
* The finalizer entries for forgotten instances are left registered. Their
|
|
213
|
+
* callbacks delete a ref from a set that no longer holds it, which costs
|
|
214
|
+
* nothing and is the only alternative to `deref()`ing every entry, which would
|
|
215
|
+
* keep every one of them alive to the end of the turn.
|
|
216
|
+
*/
|
|
217
|
+
export declare function resetMemoryHygiene(): void;
|
|
218
|
+
/**
|
|
219
|
+
* How many refs the tracked set currently holds.
|
|
220
|
+
*
|
|
221
|
+
* Not a census of live values, and read it as a ceiling rather than a count.
|
|
222
|
+
* It is `0` in any process that never turned `trackInstances` on, since
|
|
223
|
+
* nothing is tracked there. Where tracking is on it counts every instance
|
|
224
|
+
* built since, minus the ones a `FinalizationRegistry` has already pruned, and
|
|
225
|
+
* that pruning needs at least two GC cycles and two macrotask turns, so a value
|
|
226
|
+
* that became garbage a moment ago is still in this number and there is no
|
|
227
|
+
* bound on how long it stays.
|
|
228
|
+
*
|
|
229
|
+
* Exported because a gauge is the one reader that wants it besides the suite
|
|
230
|
+
* measuring that lag: `@platform-sec/sensitive-string-metrics` publishes it as
|
|
231
|
+
* `sensitive_values_tracked`, under that name rather than an `active` one
|
|
232
|
+
* precisely because the lag makes "active" a claim this number cannot support.
|
|
233
|
+
*
|
|
234
|
+
* There is deliberately no per-class breakdown. Splitting the set by class
|
|
235
|
+
* means calling `deref()` on every entry, and a `deref()` that returns an
|
|
236
|
+
* instance keeps that instance alive to the end of the current job, so a
|
|
237
|
+
* gauge scraped every fifteen seconds would extend the life of everything it
|
|
238
|
+
* measured.
|
|
239
|
+
*/
|
|
240
|
+
export declare function trackedInstanceCount(): number;
|
|
241
|
+
export {};
|
|
242
|
+
//# sourceMappingURL=hygiene.d.ts.map
|
|
@@ -0,0 +1 @@
|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