@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.
Files changed (153) hide show
  1. package/dist/bson.d.ts +28 -0
  2. package/dist/bson.d.ts.map +1 -0
  3. package/dist/bson.js +37 -0
  4. package/dist/bson.js.map +1 -0
  5. package/dist/classification.d.ts +69 -0
  6. package/dist/classification.d.ts.map +1 -0
  7. package/dist/classification.js +104 -0
  8. package/dist/classification.js.map +1 -0
  9. package/dist/compare.d.ts +36 -0
  10. package/dist/compare.d.ts.map +1 -0
  11. package/dist/compare.js +123 -0
  12. package/dist/compare.js.map +1 -0
  13. package/dist/context.d.ts +78 -0
  14. package/dist/context.d.ts.map +1 -0
  15. package/dist/context.js +110 -0
  16. package/dist/context.js.map +1 -0
  17. package/dist/derive.d.ts +110 -0
  18. package/dist/derive.d.ts.map +1 -0
  19. package/dist/derive.js +153 -0
  20. package/dist/derive.js.map +1 -0
  21. package/dist/destination.d.ts +120 -0
  22. package/dist/destination.d.ts.map +1 -0
  23. package/dist/destination.js +352 -0
  24. package/dist/destination.js.map +1 -0
  25. package/dist/deterministic.d.ts +145 -0
  26. package/dist/deterministic.d.ts.map +1 -0
  27. package/dist/deterministic.js +352 -0
  28. package/dist/deterministic.js.map +1 -0
  29. package/dist/dev-mode.d.ts +177 -0
  30. package/dist/dev-mode.d.ts.map +1 -0
  31. package/dist/dev-mode.js +503 -0
  32. package/dist/dev-mode.js.map +1 -0
  33. package/dist/environment.d.ts +181 -0
  34. package/dist/environment.d.ts.map +1 -0
  35. package/dist/environment.js +558 -0
  36. package/dist/environment.js.map +1 -0
  37. package/dist/errors.d.ts +23 -0
  38. package/dist/errors.d.ts.map +1 -0
  39. package/dist/errors.js +21 -0
  40. package/dist/errors.js.map +1 -0
  41. package/dist/global-state.d.ts +8 -0
  42. package/dist/global-state.d.ts.map +1 -0
  43. package/dist/global-state.js +22 -0
  44. package/dist/global-state.js.map +1 -0
  45. package/dist/hash.d.ts +74 -0
  46. package/dist/hash.d.ts.map +1 -0
  47. package/dist/hash.js +253 -0
  48. package/dist/hash.js.map +1 -0
  49. package/dist/hygiene.d.ts +242 -0
  50. package/dist/hygiene.d.ts.map +1 -0
  51. package/dist/hygiene.js +257 -0
  52. package/dist/hygiene.js.map +1 -0
  53. package/dist/index.d.ts +85 -0
  54. package/dist/index.d.ts.map +1 -0
  55. package/dist/index.js +233 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/mask.d.ts +51 -0
  58. package/dist/mask.d.ts.map +1 -0
  59. package/dist/mask.js +461 -0
  60. package/dist/mask.js.map +1 -0
  61. package/dist/own-property.d.ts +20 -0
  62. package/dist/own-property.d.ts.map +1 -0
  63. package/dist/own-property.js +34 -0
  64. package/dist/own-property.js.map +1 -0
  65. package/dist/patterns.d.ts +12 -0
  66. package/dist/patterns.d.ts.map +1 -0
  67. package/dist/patterns.js +19 -0
  68. package/dist/patterns.js.map +1 -0
  69. package/dist/policy.d.ts +51 -0
  70. package/dist/policy.d.ts.map +1 -0
  71. package/dist/policy.js +181 -0
  72. package/dist/policy.js.map +1 -0
  73. package/dist/probe.d.ts +141 -0
  74. package/dist/probe.d.ts.map +1 -0
  75. package/dist/probe.js +213 -0
  76. package/dist/probe.js.map +1 -0
  77. package/dist/redact-copy.d.ts +61 -0
  78. package/dist/redact-copy.d.ts.map +1 -0
  79. package/dist/redact-copy.js +217 -0
  80. package/dist/redact-copy.js.map +1 -0
  81. package/dist/redact.d.ts +141 -0
  82. package/dist/redact.d.ts.map +1 -0
  83. package/dist/redact.js +514 -0
  84. package/dist/redact.js.map +1 -0
  85. package/dist/redaction-marker.d.ts +48 -0
  86. package/dist/redaction-marker.d.ts.map +1 -0
  87. package/dist/redaction-marker.js +138 -0
  88. package/dist/redaction-marker.js.map +1 -0
  89. package/dist/redaction-observer.d.ts +108 -0
  90. package/dist/redaction-observer.d.ts.map +1 -0
  91. package/dist/redaction-observer.js +103 -0
  92. package/dist/redaction-observer.js.map +1 -0
  93. package/dist/redaction-scope.d.ts +21 -0
  94. package/dist/redaction-scope.d.ts.map +1 -0
  95. package/dist/redaction-scope.js +44 -0
  96. package/dist/redaction-scope.js.map +1 -0
  97. package/dist/reference.d.ts +101 -0
  98. package/dist/reference.d.ts.map +1 -0
  99. package/dist/reference.js +123 -0
  100. package/dist/reference.js.map +1 -0
  101. package/dist/regexp.d.ts +25 -0
  102. package/dist/regexp.d.ts.map +1 -0
  103. package/dist/regexp.js +61 -0
  104. package/dist/regexp.js.map +1 -0
  105. package/dist/release.d.ts +88 -0
  106. package/dist/release.d.ts.map +1 -0
  107. package/dist/release.js +108 -0
  108. package/dist/release.js.map +1 -0
  109. package/dist/reveal-budget.d.ts +148 -0
  110. package/dist/reveal-budget.d.ts.map +1 -0
  111. package/dist/reveal-budget.js +314 -0
  112. package/dist/reveal-budget.js.map +1 -0
  113. package/dist/revocation.d.ts +168 -0
  114. package/dist/revocation.d.ts.map +1 -0
  115. package/dist/revocation.js +541 -0
  116. package/dist/revocation.js.map +1 -0
  117. package/dist/sealing.d.ts +283 -0
  118. package/dist/sealing.d.ts.map +1 -0
  119. package/dist/sealing.js +523 -0
  120. package/dist/sealing.js.map +1 -0
  121. package/dist/sensitive-string.d.ts +919 -0
  122. package/dist/sensitive-string.d.ts.map +1 -0
  123. package/dist/sensitive-string.js +2260 -0
  124. package/dist/sensitive-string.js.map +1 -0
  125. package/dist/serialized.d.ts +33 -0
  126. package/dist/serialized.d.ts.map +1 -0
  127. package/dist/serialized.js +65 -0
  128. package/dist/serialized.js.map +1 -0
  129. package/dist/template.d.ts +69 -0
  130. package/dist/template.d.ts.map +1 -0
  131. package/dist/template.js +158 -0
  132. package/dist/template.js.map +1 -0
  133. package/dist/ttl.d.ts +104 -0
  134. package/dist/ttl.d.ts.map +1 -0
  135. package/dist/ttl.js +330 -0
  136. package/dist/ttl.js.map +1 -0
  137. package/dist/types.d.ts +859 -0
  138. package/dist/types.d.ts.map +1 -0
  139. package/dist/types.js +13 -0
  140. package/dist/types.js.map +1 -0
  141. package/dist/winston.d.ts +114 -0
  142. package/dist/winston.d.ts.map +1 -0
  143. package/dist/winston.js +170 -0
  144. package/dist/winston.js.map +1 -0
  145. package/dist/worker.d.ts +398 -0
  146. package/dist/worker.d.ts.map +1 -0
  147. package/dist/worker.js +861 -0
  148. package/dist/worker.js.map +1 -0
  149. package/dist/wrap.d.ts +287 -0
  150. package/dist/wrap.d.ts.map +1 -0
  151. package/dist/wrap.js +553 -0
  152. package/dist/wrap.js.map +1 -0
  153. package/package.json +10 -10
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MARKER_PREFIX = void 0;
4
+ exports.redactionMatchers = redactionMatchers;
5
+ exports.containsRedactionMarker = containsRedactionMarker;
6
+ exports.resetRedactionMarkerCache = resetRedactionMarkerCache;
7
+ const policy_js_1 = require("./policy.js");
8
+ /**
9
+ * Recognising this library's own redacted output in a string.
10
+ *
11
+ * Every guard that refuses to hand a redaction onward needs the same answer:
12
+ * given a string that is about to be written to a column, sent in a request
13
+ * body or stored in a secret, does it carry the text a `SensitiveString`
14
+ * renders as? Three packages arrived at that question independently and wrote
15
+ * three detectors, and the two written for a database missed the one case core
16
+ * had already provided for, {@link SensitivityPolicy.markerPattern}. This is
17
+ * the one detector, in the package that owns the format.
18
+ *
19
+ * What it cannot do is stated once here rather than in each caller. This is a
20
+ * heuristic. A legitimate value containing a marker as text is refused, which
21
+ * is the correct direction: the string is indistinguishable from the corruption
22
+ * the guards exist to catch. And a policy whose `redact` varies with its input
23
+ * and declares no `markerPattern` is recognised only if it uses the default
24
+ * prefix, because there is nothing constant to look for.
25
+ */
26
+ /**
27
+ * Two probes handed to a policy's `redact` to learn what its marker looks like.
28
+ *
29
+ * Two rather than one, and of different lengths, because one probe cannot tell
30
+ * a constant redaction from a value-dependent one. A policy that renders
31
+ * `len:29` gives the same shape for one probe as a constant would, and a needle
32
+ * derived from it would fire on unrelated strings while missing every real
33
+ * marker. Two probes that disagree is the test for both.
34
+ *
35
+ * Both are conspicuous and non-secret, so a needle accidentally derived from
36
+ * one is inert rather than dangerous.
37
+ */
38
+ const PROBE_A = 'SENSITIVE_STRING_MARKER_PROBE';
39
+ const PROBE_B = 'SENSITIVE_STRING_MARKER_PROBE_OF_A_DELIBERATELY_DIFFERENT_LENGTH';
40
+ /** The prefix every default policy in core produces. */
41
+ exports.MARKER_PREFIX = '[REDACTED:';
42
+ let cachedVersion = -1;
43
+ let cached = [];
44
+ /**
45
+ * Every matcher that recognises a redaction the policy registry can produce.
46
+ *
47
+ * Derived from the registry rather than hardcoded, because `registerPolicy()`
48
+ * accepts a `redact` of the caller's own and a guard that only knew the default
49
+ * shape would miss exactly the deployment that customised it. Three sources,
50
+ * in the order they are cheapest to be sure of:
51
+ *
52
+ * {@link MARKER_PREFIX} unconditionally, which covers every default policy and
53
+ * every class this process has never registered. That last part is what a
54
+ * persistence guard needs and a transport guard does not: a column written by
55
+ * an older release, or by a service that registered a class this one does not
56
+ * have, still holds `[REDACTED:whatever]`, and the reader has to recognise it.
57
+ *
58
+ * Each policy's `markerPattern`, for a value-dependent redaction that declares
59
+ * its own shape. `deterministicRedaction()` sets it. Read through `search()`
60
+ * rather than `test()`, because a caller's pattern may carry `g` despite the
61
+ * warning on the field, and `test()` on a global pattern advances `lastIndex`
62
+ * so the next call starts mid-string and misses.
63
+ *
64
+ * Each policy's constant redaction, discovered with the two probes above.
65
+ *
66
+ * Memoized against {@link policyRegistryVersion}, so the probes run once per
67
+ * registry change rather than once per bound parameter. The version moves on an
68
+ * `overwrite` re-registration too, which a memo keyed on the set of class names
69
+ * would not see.
70
+ */
71
+ function redactionMatchers() {
72
+ const version = (0, policy_js_1.policyRegistryVersion)();
73
+ if (version === cachedVersion)
74
+ return cached;
75
+ const matchers = [exports.MARKER_PREFIX];
76
+ for (const klass of (0, policy_js_1.getRegisteredClasses)()) {
77
+ let policy;
78
+ try {
79
+ policy = (0, policy_js_1.getPolicy)(klass);
80
+ }
81
+ catch {
82
+ // Registered a moment ago and gone now. Nothing to derive.
83
+ continue;
84
+ }
85
+ // Declared on `SensitivityPolicy` by core, and checked rather than trusted:
86
+ // a policy arrives from an application and the field is optional.
87
+ if (policy.markerPattern instanceof RegExp)
88
+ matchers.push(policy.markerPattern);
89
+ let first;
90
+ let second;
91
+ try {
92
+ first = policy.redact(PROBE_A);
93
+ second = policy.redact(PROBE_B);
94
+ }
95
+ catch {
96
+ // A policy that refuses to redact a probe contributes no needle. The
97
+ // prefix above still covers it if it uses the default format.
98
+ continue;
99
+ }
100
+ if (typeof first !== 'string' || first === '' || first !== second)
101
+ continue;
102
+ if (!matchers.includes(first))
103
+ matchers.push(first);
104
+ }
105
+ cachedVersion = version;
106
+ cached = matchers;
107
+ return matchers;
108
+ }
109
+ /**
110
+ * Whether a string carries a redaction marker anywhere in it.
111
+ *
112
+ * Containment rather than equality, because the ways a marker reaches a column
113
+ * or a request body are concatenation and interpolation as often as a bare
114
+ * assignment: a template string built over a wrapped value produces a marker
115
+ * embedded in a longer one, and that value is corrupt in exactly the same way.
116
+ */
117
+ function containsRedactionMarker(value) {
118
+ if (typeof value !== 'string' || value === '')
119
+ return false;
120
+ for (const matcher of redactionMatchers()) {
121
+ if (typeof matcher === 'string' ? value.includes(matcher) : value.search(matcher) !== -1) {
122
+ return true;
123
+ }
124
+ }
125
+ return false;
126
+ }
127
+ /**
128
+ * Drop the memo.
129
+ *
130
+ * Rarely needed: {@link policyRegistryVersion} already moves on every
131
+ * registration and every reset. It exists for a test that mutates a policy
132
+ * object in place, which the registry cannot see.
133
+ */
134
+ function resetRedactionMarkerCache() {
135
+ cachedVersion = -1;
136
+ cached = [];
137
+ }
138
+ //# sourceMappingURL=redaction-marker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction-marker.js","sourceRoot":"","sources":["../src/redaction-marker.ts"],"names":[],"mappings":";;;AAqEA,8CAqCC;AAUD,0DA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AM;YAAE,SAAS;QAC5E,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;YAAE,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACtD,CAAC;IAED,aAAa,GAAG,OAAO,CAAC;IACxB,MAAM,GAAG,QAAQ,CAAC;IAClB,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,uBAAuB,CAAC,KAAa;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,KAAK,CAAC;IAE5D,KAAK,MAAM,OAAO,IAAI,iBAAiB,EAAE,EAAE,CAAC;QAC1C,IAAI,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACzF,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,yBAAyB;IACvC,aAAa,GAAG,CAAC,CAAC,CAAC;IACnB,MAAM,GAAG,EAAE,CAAC;AACd,CAAC"}
@@ -0,0 +1,108 @@
1
+ import type { SensitivityClass } from './types.js';
2
+ /**
3
+ * Observation of redactions, which is the count that says what the library
4
+ * prevented.
5
+ *
6
+ * A reveal is the sanctioned path and is already audited. A redaction is the
7
+ * other one: every time `toString()`, `toJSON()` or the inspect hook renders a
8
+ * marker, some caller was about to write the plain string somewhere, and the
9
+ * marker went instead. Nothing recorded that before this module. The rendering
10
+ * runs on the `'safe'` read, which fires no audit hook, charges no budget and
11
+ * consults no policy, and it must stay that way: a log line must not need
12
+ * authorization. So observation sits beside the rendering rather than inside
13
+ * the gate, and reports the class, the label and the surface, never the value
14
+ * and never its rendering.
15
+ *
16
+ * The registry is process-global under rule R6 and keyed on `Symbol.for()`, so
17
+ * a second installed copy of core shares it. Any code in the process can
18
+ * subscribe or unsubscribe; this protects a dashboard against a missing
19
+ * subscription, not against a hostile caller.
20
+ *
21
+ * What an unobserved process pays is one array-length read per rendering,
22
+ * priced in the changeset. The observed path adds one frozen object and one
23
+ * call per observer.
24
+ */
25
+ /**
26
+ * Where a redaction was rendered.
27
+ *
28
+ * - `'toString'`: a coercion. A template literal, string concatenation,
29
+ * `String()`, or a direct call. This is the surface T13 describes, where the
30
+ * rendering may be a prevented log line or a broken request, and the event
31
+ * cannot tell the two apart.
32
+ * - `'toJSON'`: `JSON.stringify()`, or a serializer that honours `toJSON()`.
33
+ * A JSON logger and a JSON request body both arrive here.
34
+ * - `'inspect'`: `util.inspect()` and `console.log()`.
35
+ * - `'structure'`: a walker replaced the value inside a structure it was
36
+ * asked to clean. `redactInStructure`, `redactCopy`, `safeStringify`,
37
+ * `safeLog`, the winston format and the pino serializer all report here,
38
+ * and a `serialize()` result one of them replaced reports here too. This is
39
+ * the one surface that is unambiguously a sink that installed redaction.
40
+ */
41
+ export type RedactionSurface = 'toString' | 'toJSON' | 'inspect' | 'structure';
42
+ /**
43
+ * One rendering of a redaction marker.
44
+ *
45
+ * Exactly these three fields, frozen, and shared by every observer of the
46
+ * same rendering. The label is `undefined` for a `serialize()` result a walker
47
+ * replaced: the label in a wire shape is data off the wire, and an event that
48
+ * carried it would let a request body choose a metric dimension.
49
+ */
50
+ export interface RedactionEvent {
51
+ readonly klass: SensitivityClass;
52
+ readonly label: string | undefined;
53
+ readonly surface: RedactionSurface;
54
+ }
55
+ /**
56
+ * Receives every redaction rendered in the process while subscribed.
57
+ *
58
+ * Must not throw. One that does is reported once through `process.emitWarning`
59
+ * and its later throws are swallowed, because a redaction is a log line and a
60
+ * log line must never throw. Its own redactions are not observed: a dashboard
61
+ * that logs a wrapped value inside its observer would otherwise recurse.
62
+ */
63
+ export type RedactionObserver = (event: RedactionEvent) => void;
64
+ interface ObserverState {
65
+ /**
66
+ * Replaced rather than mutated on every subscribe and unsubscribe, so a
67
+ * notification in flight iterates the list it started with.
68
+ */
69
+ observers: readonly RedactionObserver[];
70
+ /** True while observers are being called, so a nested rendering is not reported. */
71
+ notifying: boolean;
72
+ /** Observers already reported for throwing, so a broken one warns once. */
73
+ readonly warned: WeakSet<RedactionObserver>;
74
+ }
75
+ /** @internal */
76
+ export declare const redactionObservers: ObserverState;
77
+ /**
78
+ * Subscribe to every redaction rendered in this process.
79
+ *
80
+ * Returns the function that unsubscribes. Calling it twice is harmless.
81
+ * Subscribing the same function twice notifies it twice, and each unsubscribe
82
+ * removes one subscription.
83
+ *
84
+ * The event carries the class, the label and the surface. The rendering is not
85
+ * in it, deliberately: under a deterministic-redaction policy the marker is a
86
+ * pseudonym of the value, and a metric dimension is the wrong place for one.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * const stop = observeRedactions(({ klass, label, surface }) => {
91
+ * redactions.inc({ klass, label: label ?? '', surface });
92
+ * });
93
+ * ```
94
+ */
95
+ export declare function observeRedactions(observer: RedactionObserver): () => void;
96
+ /**
97
+ * Report one rendering to every observer.
98
+ *
99
+ * Called after the rendering succeeded, never before: a policy whose `redact`
100
+ * throws rendered nothing, so there is nothing to count. The caller checks
101
+ * `redactionObservers.observers.length` first, so an unobserved process never
102
+ * enters this function.
103
+ *
104
+ * @internal
105
+ */
106
+ export declare function notifyRedaction(klass: SensitivityClass, label: string | undefined, surface: RedactionSurface): void;
107
+ export {};
108
+ //# sourceMappingURL=redaction-observer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction-observer.d.ts","sourceRoot":"","sources":["../src/redaction-observer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;;;;;;;;;;;;;;GAeG;AACH,MAAM,MAAM,gBAAgB,GAAG,UAAU,GAAG,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAC;AAE/E;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC;CACpC;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;AAEhE,UAAU,aAAa;IACrB;;;OAGG;IACH,SAAS,EAAE,SAAS,iBAAiB,EAAE,CAAC;IACxC,oFAAoF;IACpF,SAAS,EAAE,OAAO,CAAC;IACnB,2EAA2E;IAC3E,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAC7C;AAED,gBAAgB;AAChB,eAAO,MAAM,kBAAkB,eAI5B,CAAC;AAEJ;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,iBAAiB,GAAG,MAAM,IAAI,CAuBzE;AAED;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,gBAAgB,EACvB,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,OAAO,EAAE,gBAAgB,GACxB,IAAI,CA4BN"}
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.redactionObservers = void 0;
4
+ exports.observeRedactions = observeRedactions;
5
+ exports.notifyRedaction = notifyRedaction;
6
+ const global_state_js_1 = require("./global-state.js");
7
+ /** @internal */
8
+ exports.redactionObservers = (0, global_state_js_1.getGlobalSingleton)('redaction-observers', () => ({
9
+ observers: [],
10
+ notifying: false,
11
+ warned: new WeakSet(),
12
+ }));
13
+ /**
14
+ * Subscribe to every redaction rendered in this process.
15
+ *
16
+ * Returns the function that unsubscribes. Calling it twice is harmless.
17
+ * Subscribing the same function twice notifies it twice, and each unsubscribe
18
+ * removes one subscription.
19
+ *
20
+ * The event carries the class, the label and the surface. The rendering is not
21
+ * in it, deliberately: under a deterministic-redaction policy the marker is a
22
+ * pseudonym of the value, and a metric dimension is the wrong place for one.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const stop = observeRedactions(({ klass, label, surface }) => {
27
+ * redactions.inc({ klass, label: label ?? '', surface });
28
+ * });
29
+ * ```
30
+ */
31
+ function observeRedactions(observer) {
32
+ if (typeof observer !== 'function') {
33
+ throw new TypeError('observeRedactions() requires a function');
34
+ }
35
+ const state = exports.redactionObservers;
36
+ state.observers = [...state.observers, observer];
37
+ let subscribed = true;
38
+ return () => {
39
+ if (!subscribed) {
40
+ return;
41
+ }
42
+ subscribed = false;
43
+ const index = state.observers.indexOf(observer);
44
+ if (index !== -1) {
45
+ state.observers = [...state.observers.slice(0, index), ...state.observers.slice(index + 1)];
46
+ }
47
+ };
48
+ }
49
+ /**
50
+ * Report one rendering to every observer.
51
+ *
52
+ * Called after the rendering succeeded, never before: a policy whose `redact`
53
+ * throws rendered nothing, so there is nothing to count. The caller checks
54
+ * `redactionObservers.observers.length` first, so an unobserved process never
55
+ * enters this function.
56
+ *
57
+ * @internal
58
+ */
59
+ function notifyRedaction(klass, label, surface) {
60
+ const state = exports.redactionObservers;
61
+ // An observer that renders a wrapped value, by logging it, say, reaches
62
+ // here again from inside its own notification. Reporting that would recurse
63
+ // until the stack ran out, and each level would swallow the overflow of the
64
+ // one below. The nested rendering still produces its marker; it is just not
65
+ // an event.
66
+ if (state.notifying) {
67
+ return;
68
+ }
69
+ const observers = state.observers;
70
+ const event = Object.freeze({ klass, label, surface });
71
+ state.notifying = true;
72
+ try {
73
+ for (const observer of observers) {
74
+ try {
75
+ observer(event);
76
+ }
77
+ catch (error) {
78
+ warnOnce(state, observer, error);
79
+ }
80
+ }
81
+ }
82
+ finally {
83
+ state.notifying = false;
84
+ }
85
+ }
86
+ /**
87
+ * Say once, on stderr, that an observer is broken.
88
+ *
89
+ * Once per observer rather than once per process, so a second broken
90
+ * exporter is not hidden behind the first, and once rather than every time,
91
+ * because the rendering that triggered it is a log line and a warning per log
92
+ * line is a second log.
93
+ */
94
+ function warnOnce(state, observer, error) {
95
+ if (state.warned.has(observer)) {
96
+ return;
97
+ }
98
+ state.warned.add(observer);
99
+ const reason = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
100
+ process.emitWarning(`A redaction observer threw (${reason}). Redaction is unaffected, and later throws from ` +
101
+ 'this observer are swallowed without a warning.', 'SensitiveStringWarning');
102
+ }
103
+ //# sourceMappingURL=redaction-observer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction-observer.js","sourceRoot":"","sources":["../src/redaction-observer.ts"],"names":[],"mappings":";;;AA0GA,8CAuBC;AAYD,0CAgCC;AA7KD,uDAAuD;AAiFvD,gBAAgB;AACH,QAAA,kBAAkB,GAAG,IAAA,oCAAkB,EAAgB,qBAAqB,EAAE,GAAG,EAAE,CAAC,CAAC;IAChG,SAAS,EAAE,EAAE;IACb,SAAS,EAAE,KAAK;IAChB,MAAM,EAAE,IAAI,OAAO,EAAE;CACtB,CAAC,CAAC,CAAC;AAEJ;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,iBAAiB,CAAC,QAA2B;IAC3D,IAAI,OAAO,QAAQ,KAAK,UAAU,EAAE,CAAC;QACnC,MAAM,IAAI,SAAS,CAAC,yCAAyC,CAAC,CAAC;IACjE,CAAC;IAED,MAAM,KAAK,GAAG,0BAAkB,CAAC;IACjC,KAAK,CAAC,SAAS,GAAG,CAAC,GAAG,KAAK,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAEjD,IAAI,UAAU,GAAG,IAAI,CAAC;IAEtB,OAAO,GAAG,EAAE;QACV,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,OAAO;QACT,CAAC;QAED,UAAU,GAAG,KAAK,CAAC;QAEnB,MAAM,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,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O;IACT,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAE3B,MAAM,MAAM,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAE1F,OAAO,CAAC,WAAW,CACjB,+BAA+B,MAAM,oDAAoD;QACvF,gDAAgD,EAClD,wBAAwB,CACzB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { SensitiveStringError } from './errors.js';
2
+ import type { SensitivityClass } from './types.js';
3
+ /** Run caller code on the walk's behalf, with the mark set for its duration. */
4
+ export declare function runInsideRedaction<T>(operation: () => T): T;
5
+ /** Whether a redaction walk is running caller code right now. */
6
+ export declare function isInsideRedaction(): boolean;
7
+ /**
8
+ * Raised when `reveal()`, `mask()`, `serialize()` or a transform is called from
9
+ * inside a `toJSON()` that a redaction walk is running.
10
+ *
11
+ * A caller sees this only by catching it inside that `toJSON()`: the walk
12
+ * itself absorbs the throw and redacts the object's own properties instead.
13
+ */
14
+ export declare class RevealDuringRedactionError extends SensitiveStringError {
15
+ readonly klass: SensitivityClass;
16
+ readonly label?: string | undefined;
17
+ readonly name = "RevealDuringRedactionError";
18
+ readonly code = "reveal-during-redaction";
19
+ constructor(klass: SensitivityClass, label?: string | undefined);
20
+ }
21
+ //# sourceMappingURL=redaction-scope.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction-scope.d.ts","sourceRoot":"","sources":["../src/redaction-scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AA+BnD,gFAAgF;AAChF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,GAAG,CAAC,CAQ3D;AAED,iEAAiE;AACjE,wBAAgB,iBAAiB,IAAI,OAAO,CAE3C;AAED;;;;;;GAMG;AACH,qBAAa,0BAA2B,SAAQ,oBAAoB;IAKhE,QAAQ,CAAC,KAAK,EAAE,gBAAgB;IAChC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM;IALzB,SAAkB,IAAI,gCAAgC;IACtD,SAAkB,IAAI,6BAA6B;gBAGxC,KAAK,EAAE,gBAAgB,EACvB,KAAK,CAAC,EAAE,MAAM,YAAA;CAQ1B"}
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RevealDuringRedactionError = void 0;
4
+ exports.runInsideRedaction = runInsideRedaction;
5
+ exports.isInsideRedaction = isInsideRedaction;
6
+ const errors_js_1 = require("./errors.js");
7
+ const global_state_js_1 = require("./global-state.js");
8
+ const scope = (0, global_state_js_1.getGlobalSingleton)('redaction-scope', () => ({ depth: 0 }));
9
+ /** Run caller code on the walk's behalf, with the mark set for its duration. */
10
+ function runInsideRedaction(operation) {
11
+ scope.depth += 1;
12
+ try {
13
+ return operation();
14
+ }
15
+ finally {
16
+ scope.depth -= 1;
17
+ }
18
+ }
19
+ /** Whether a redaction walk is running caller code right now. */
20
+ function isInsideRedaction() {
21
+ return scope.depth > 0;
22
+ }
23
+ /**
24
+ * Raised when `reveal()`, `mask()`, `serialize()` or a transform is called from
25
+ * inside a `toJSON()` that a redaction walk is running.
26
+ *
27
+ * A caller sees this only by catching it inside that `toJSON()`: the walk
28
+ * itself absorbs the throw and redacts the object's own properties instead.
29
+ */
30
+ class RevealDuringRedactionError extends errors_js_1.SensitiveStringError {
31
+ klass;
32
+ label;
33
+ name = 'RevealDuringRedactionError';
34
+ code = 'reveal-during-redaction';
35
+ constructor(klass, label) {
36
+ super(`Cannot reveal a ${klass} value${label !== undefined ? ` '${label}'` : ''} from inside a ` +
37
+ 'redaction walk: a toJSON() that reveals would put the raw value into a log line. ' +
38
+ 'Return the SensitiveString itself and let the walk redact it.');
39
+ this.klass = klass;
40
+ this.label = label;
41
+ }
42
+ }
43
+ exports.RevealDuringRedactionError = RevealDuringRedactionError;
44
+ //# sourceMappingURL=redaction-scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redaction-scope.js","sourceRoot":"","sources":["../src/redaction-scope.ts"],"names":[],"mappings":";;;AAkCA,gDAQC;AAGD,8CAEC;AA/CD,2CAAmD;AACnD,uDAAuD;AA8BvD,MAAM,KAAK,GAAG,IAAA,oCAAkB,EAAiB,iBAAiB,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAE1F,gFAAgF;AAChF,SAAgB,kBAAkB,CAAI,SAAkB;IACtD,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC;IAEjB,IAAI,CAAC;QACH,OAAO,SAAS,EAAE,CAAC;IACrB,CAAC;YAAS,CAAC;QACT,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC;IACnB,CAAC;AACH,CAAC;AAED,iEAAiE;AACjE,SAAgB,iBAAiB;IAC/B,OAAO,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC;AACzB,CAAC;AAED;;;;;;GAMG;AACH,MAAa,0BAA2B,SAAQ,gCAAoB;IAKvD;IACA;IALO,IAAI,GAAG,4BAA4B,CAAC;IACpC,IAAI,GAAG,yBAAyB,CAAC;IAEnD,YACW,KAAuB,EACvB,KAAc;QAEvB,KAAK,CACH,mBAAmB,KAAK,SAAS,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,iBAAiB;YACxF,mFAAmF;YACnF,+DAA+D,CAClE,CAAC;QAPO,UAAK,GAAL,KAAK,CAAkB;QACvB,UAAK,GAAL,KAAK,CAAS;IAOzB,CAAC;CACF;AAdD,gEAcC"}
@@ -0,0 +1,101 @@
1
+ import type { SensitivityClass } from './types.js';
2
+ /**
3
+ * The wire shape of a pointer to a value in a secrets store, and the guards
4
+ * that recognize one.
5
+ *
6
+ * `serialize()` puts the raw value on the wire and asks the transport for
7
+ * confidentiality. A reference asks for nothing: it names where the value is
8
+ * and the receiver fetches it with its own credentials, so the value never
9
+ * leaves the process that read it. That is the whole of what this file knows.
10
+ * Minting a reference needs the store identity, the version and the provider,
11
+ * and resolving one needs credentials and an authorization decision, so both
12
+ * live in `@platform-sec/sensitive-string-secrets`. The format lives here for
13
+ * the same reason the `serialize()` format does: the walks that must decide
14
+ * what to do with a wire object when they meet one are here.
15
+ *
16
+ * What they decide, for this shape, is to leave it alone. A reference carries
17
+ * no secret material at any version of the format, and reading one gets an
18
+ * attacker no closer to the value than the label already does, since resolving
19
+ * it needs credentials of their own. Redacting it would break the debugging
20
+ * that a reference-passing system exists to make possible, which is being able
21
+ * to see which secret a job refers to. So there is no `redactReference` next to
22
+ * `redactSerialized`, the walks descend into a reference as they would into any
23
+ * other plain object, and `containsSensitive()` answers false for one. Those
24
+ * are asserted positively in `reference.test.ts` rather than left to be
25
+ * incidental, so that a later change to redact everything carrying a
26
+ * `__sensitive`-shaped marker fails a test and arrives here.
27
+ */
28
+ /**
29
+ * The only format version this build understands.
30
+ *
31
+ * A receiver that does not recognize a version refuses rather than guessing,
32
+ * and the refusal names the version it saw and nothing else from the payload.
33
+ */
34
+ export declare const REFERENCE_VERSION = 1;
35
+ /**
36
+ * A pointer to a value in a secrets store. Carries no secret material.
37
+ *
38
+ * `klass` and `label` are advisory: they arrive from the sender, and a receiver
39
+ * that took either as given would let the sender choose how its own runtime
40
+ * treats the value it fetches. A resolver derives the label itself and takes
41
+ * the more restrictive of its own class and this one, which is the direction a
42
+ * wire field is allowed to move a classification in.
43
+ */
44
+ export interface SensitiveValueReference {
45
+ readonly __sensitiveRef: true;
46
+ readonly v: typeof REFERENCE_VERSION;
47
+ /** Logical store identity, agreed out of band. Not a URL and not an account id. */
48
+ readonly store: string;
49
+ /** The logical path within that store, under the label rule. */
50
+ readonly path: string;
51
+ /** The concrete version the sender read. Absent means the sender pinned none. */
52
+ readonly version?: string;
53
+ /** One field of a multi-value secret, as a dot path. */
54
+ readonly field?: string;
55
+ /** Advisory. A resolver takes the more restrictive of this and its own rule. */
56
+ readonly klass?: SensitivityClass;
57
+ /** Advisory. A resolver derives its own under the label rule. */
58
+ readonly label?: string;
59
+ }
60
+ /**
61
+ * Whether an object carries the reference marker, judged by the marker alone.
62
+ *
63
+ * Separate from {@link isSensitiveValueReference} because the two answer
64
+ * different questions. This one asks whether a sender meant to send a
65
+ * reference, which is what decides between "this is not a reference at all" and
66
+ * "this is a reference this build cannot accept": a receiver that answered the
67
+ * first for a marked object with a version it does not know would report a
68
+ * caller's own payload as malformed rather than reporting the version skew it
69
+ * actually found.
70
+ *
71
+ * The read goes through `probeProperty` because a walk runs it against every
72
+ * object it reaches, and an object that refuses the question is not a payload
73
+ * this library built.
74
+ */
75
+ export declare function carriesReferenceMarker(value: object): boolean;
76
+ /**
77
+ * The format version a marked object claims, or `undefined` when it claims
78
+ * none this build can read.
79
+ *
80
+ * A receiver refusing a version it does not know has to name the version it
81
+ * saw, and it must name a number rather than whatever the sender put in the
82
+ * field, so the read is guarded and anything that is not a number answers
83
+ * `undefined`.
84
+ */
85
+ export declare function referenceFormatVersion(value: object): number | undefined;
86
+ /**
87
+ * Whether a value is a well-formed reference of the version this build knows.
88
+ *
89
+ * Structural rather than nominal, because a reference crosses a process
90
+ * boundary as JSON and arrives as a plain object that no `instanceof` can
91
+ * recognize. Every required member is checked, so a resolver that has this
92
+ * answer can read the members it needs without repeating the question, and the
93
+ * advisory `klass` is checked against the policy registry here rather than
94
+ * accepted as any string: a class the receiving process never registered is not
95
+ * a class it can act on.
96
+ *
97
+ * A registered class is still only advisory. This answers whether the field is
98
+ * readable, never whether the receiver should honour it.
99
+ */
100
+ export declare function isSensitiveValueReference(value: unknown): value is SensitiveValueReference;
101
+ //# sourceMappingURL=reference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reference.d.ts","sourceRoot":"","sources":["../src/reference.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAEnC;;;;;;;;GAQG;AACH,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,cAAc,EAAE,IAAI,CAAC;IAC9B,QAAQ,CAAC,CAAC,EAAE,OAAO,iBAAiB,CAAC;IACrC,mFAAmF;IACnF,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,gEAAgE;IAChE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,iFAAiF;IACjF,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,wDAAwD;IACxD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,gFAAgF;IAChF,QAAQ,CAAC,KAAK,CAAC,EAAE,gBAAgB,CAAC;IAClC,iEAAiE;IACjE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE7D;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIxE;AAYD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,uBAAuB,CAoC1F"}