@apifuse/provider-sdk 2.2.0-beta.58 → 2.2.0-beta.60
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/AUTHORING.md +25 -0
- package/CHANGELOG.md +8 -0
- package/README.md +18 -2
- package/bin/apifuse-dev.ts +2 -1
- package/bin/apifuse-pack-types.ts +49 -0
- package/bin/apifuse-perf.ts +41 -4
- package/bin/apifuse-record.ts +9 -2
- package/dist/cli/templates/provider/README.md.tpl +15 -3
- package/dist/config/loader.d.ts +7 -0
- package/dist/config/loader.js +43 -18
- package/dist/declaration-validation.d.ts +2 -1
- package/dist/declaration-validation.js +39 -0
- package/dist/define.d.ts +4 -4
- package/dist/define.js +25 -1
- package/dist/engine.d.ts +16 -0
- package/dist/engine.js +59 -3
- package/dist/error-resolution.js +13 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +4 -2
- package/dist/lint.js +38 -17
- package/dist/provider.d.ts +3 -3
- package/dist/provider.js +2 -2
- package/dist/runtime/bounded-expiring-map.d.ts +21 -0
- package/dist/runtime/bounded-expiring-map.js +64 -0
- package/dist/runtime/browser.js +2 -1
- package/dist/runtime/cache.d.ts +2 -0
- package/dist/runtime/cache.js +11 -3
- package/dist/runtime/diagnostic-env-values.d.ts +5 -0
- package/dist/runtime/diagnostic-env-values.js +57 -0
- package/dist/runtime/diagnostic-env.d.ts +11 -0
- package/dist/runtime/diagnostic-env.js +35 -0
- package/dist/runtime/diagnostic-redactor.d.ts +49 -0
- package/dist/runtime/diagnostic-redactor.js +569 -0
- package/dist/runtime/egress-lease.d.ts +65 -0
- package/dist/runtime/egress-lease.js +236 -0
- package/dist/runtime/env.js +2 -1
- package/dist/runtime/http.js +1 -0
- package/dist/runtime/instrumentation.js +3 -0
- package/dist/runtime/ocr.js +2 -1
- package/dist/runtime/otlp.js +5 -4
- package/dist/runtime/proxy-nodemaven.js +3 -2
- package/dist/runtime/proxy-retry-policy.d.ts +4 -1
- package/dist/runtime/proxy-retry-policy.js +11 -2
- package/dist/runtime/request-options.d.ts +7 -0
- package/dist/runtime/request-options.js +62 -22
- package/dist/runtime/request-telemetry.d.ts +7 -9
- package/dist/runtime/resolver-config.d.ts +7 -0
- package/dist/runtime/resolver-config.js +8 -0
- package/dist/runtime/resolver-public.d.ts +3 -1
- package/dist/runtime/resolver-public.js +1 -1
- package/dist/runtime/resolver-shared.d.ts +9 -0
- package/dist/runtime/resolver-shared.js +57 -0
- package/dist/runtime/resolver-telemetry.d.ts +134 -0
- package/dist/runtime/resolver-telemetry.js +204 -0
- package/dist/runtime/resolver-usage.d.ts +24 -0
- package/dist/runtime/resolver-usage.js +60 -0
- package/dist/runtime/resolver-vendors/bindings.d.ts +5 -0
- package/dist/runtime/resolver-vendors/bindings.js +5 -0
- package/dist/runtime/resolver-vendors/browser.js +0 -1
- package/dist/runtime/resolver-vendors/capsolver.d.ts +2 -2
- package/dist/runtime/resolver-vendors/capsolver.js +21 -9
- package/dist/runtime/resolver-vendors/hypersolutions.d.ts +27 -0
- package/dist/runtime/resolver-vendors/hypersolutions.js +369 -0
- package/dist/runtime/resolver-vendors/twocaptcha.d.ts +2 -2
- package/dist/runtime/resolver-vendors/twocaptcha.js +26 -10
- package/dist/runtime/resolver-vendors/types.d.ts +35 -5
- package/dist/runtime/resolver-vendors/types.js +20 -10
- package/dist/runtime/resolver.d.ts +5 -1
- package/dist/runtime/resolver.js +400 -111
- package/dist/runtime/stealth-akamai-sbsd.d.ts +54 -0
- package/dist/runtime/stealth-akamai-sbsd.js +142 -0
- package/dist/runtime/stealth.js +547 -53
- package/dist/runtime/stt.js +3 -2
- package/dist/runtime/trace.d.ts +5 -0
- package/dist/runtime/trace.js +115 -20
- package/dist/schema.d.ts +20 -0
- package/dist/schema.js +72 -2
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/server/self-test-redaction.js +8 -33
- package/dist/server/self-test.d.ts +4 -0
- package/dist/server/self-test.js +10 -1
- package/dist/server/sensitive-values.d.ts +26 -0
- package/dist/server/sensitive-values.js +131 -0
- package/dist/server/serve-implementation.d.ts +7 -1
- package/dist/server/serve-implementation.js +394 -125
- package/dist/server/trace-output.d.ts +2 -1
- package/dist/server/trace-output.js +46 -14
- package/dist/server/types.d.ts +6 -0
- package/dist/server/types.js +14 -0
- package/dist/stealth/profiles.d.ts +7 -1
- package/dist/stealth/profiles.js +9 -0
- package/dist/testing/run.js +6 -0
- package/dist/trace-sanitization.d.ts +4 -3
- package/dist/trace-sanitization.js +45 -22
- package/dist/types.d.ts +106 -10
- package/package.json +2 -1
- package/src/cli/templates/provider/README.md.tpl +15 -3
- package/src/config/loader.ts +47 -18
- package/src/declaration-validation.ts +47 -1
- package/src/define.ts +45 -3
- package/src/engine.ts +78 -3
- package/src/error-resolution.ts +13 -0
- package/src/index.ts +34 -1
- package/src/lint.ts +44 -18
- package/src/provider.ts +10 -0
- package/src/runtime/bounded-expiring-map.ts +67 -0
- package/src/runtime/browser.ts +2 -1
- package/src/runtime/cache.ts +13 -3
- package/src/runtime/diagnostic-env-values.ts +56 -0
- package/src/runtime/diagnostic-env.ts +55 -0
- package/src/runtime/diagnostic-redactor.ts +662 -0
- package/src/runtime/egress-lease.ts +341 -0
- package/src/runtime/env.ts +2 -1
- package/src/runtime/http.ts +1 -0
- package/src/runtime/instrumentation.ts +3 -0
- package/src/runtime/ocr.ts +2 -1
- package/src/runtime/otlp.ts +5 -4
- package/src/runtime/proxy-nodemaven.ts +3 -2
- package/src/runtime/proxy-retry-policy.ts +12 -1
- package/src/runtime/request-options.ts +76 -28
- package/src/runtime/request-telemetry.ts +5 -7
- package/src/runtime/resolver-config.ts +12 -0
- package/src/runtime/resolver-public.ts +21 -0
- package/src/runtime/resolver-shared.ts +75 -0
- package/src/runtime/resolver-telemetry.ts +362 -0
- package/src/runtime/resolver-usage.ts +96 -0
- package/src/runtime/resolver-vendors/bindings.ts +5 -0
- package/src/runtime/resolver-vendors/browser.ts +0 -1
- package/src/runtime/resolver-vendors/capsolver.ts +22 -3
- package/src/runtime/resolver-vendors/hypersolutions.ts +480 -0
- package/src/runtime/resolver-vendors/twocaptcha.ts +26 -12
- package/src/runtime/resolver-vendors/types.ts +60 -12
- package/src/runtime/resolver.ts +468 -126
- package/src/runtime/stealth-akamai-sbsd.ts +207 -0
- package/src/runtime/stealth.ts +727 -95
- package/src/runtime/stt.ts +3 -2
- package/src/runtime/trace.ts +168 -26
- package/src/schema.ts +80 -2
- package/src/server/index.ts +21 -0
- package/src/server/self-test-redaction.ts +10 -40
- package/src/server/self-test.ts +13 -0
- package/src/server/sensitive-values.ts +200 -0
- package/src/server/serve-implementation.ts +561 -136
- package/src/server/trace-output.ts +77 -17
- package/src/server/types.ts +14 -0
- package/src/stealth/profiles.ts +10 -0
- package/src/testing/run.ts +6 -0
- package/src/trace-sanitization.ts +70 -23
- package/src/types.ts +124 -19
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { allSensitiveValueVariants, normalizePercentEscapes, REDACTED_QUERY_VALUE, } from "./request-options.js";
|
|
3
|
+
export const REDACTION_FAILED = "[REDACTION_FAILED]";
|
|
4
|
+
// No supported credential source needs 1–3 character secrets. Registering these
|
|
5
|
+
// common tokens damages unrelated IDs and diagnostics even with token boundaries.
|
|
6
|
+
// Apply the same floor to static, fallback and request inventories (as self-test does).
|
|
7
|
+
const MINIMUM_REGISTERED_VALUE_LENGTH = 4;
|
|
8
|
+
/** Observer failures must never return the original diagnostic, including via ?? fallbacks. */
|
|
9
|
+
export function redactDiagnosticText(text, redact) {
|
|
10
|
+
try {
|
|
11
|
+
const result = redact ? redact(text) : text;
|
|
12
|
+
return typeof result === "string" ? result : REDACTION_FAILED;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return REDACTION_FAILED;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function node() {
|
|
19
|
+
return { next: new Map() };
|
|
20
|
+
}
|
|
21
|
+
function compile(variants) {
|
|
22
|
+
const matcher = {
|
|
23
|
+
exact: node(),
|
|
24
|
+
folded: node(),
|
|
25
|
+
hasFolded: false,
|
|
26
|
+
// Only long values participate in encoded matching. Unreserved ASCII is
|
|
27
|
+
// percent-encodable too (%6f for o); encodeURIComponent(value) === value
|
|
28
|
+
// does NOT prove that residual escapes cannot conceal that credential.
|
|
29
|
+
canMatchPercentEncoded: variants.some((variant) => variant.caseInsensitive),
|
|
30
|
+
size: variants.length,
|
|
31
|
+
minimumLength: variants.reduce((minimum, variant) => Math.min(minimum, variant.value.length), Infinity),
|
|
32
|
+
cache: new Map(),
|
|
33
|
+
};
|
|
34
|
+
if (variants.some((variant) => !variant.caseInsensitive))
|
|
35
|
+
matcher.encoded = compile(variants.filter((variant) => variant.caseInsensitive));
|
|
36
|
+
// A few request credentials are cheaper as native literal searches; large
|
|
37
|
+
// process-static inventories use tries to avoid per-needle scans.
|
|
38
|
+
if (variants.length <= 16) {
|
|
39
|
+
matcher.needles = variants.map((variant) => ({
|
|
40
|
+
...variant,
|
|
41
|
+
value: variant.caseInsensitive ? variant.value.toLowerCase() : variant.value,
|
|
42
|
+
}));
|
|
43
|
+
matcher.hasFolded = variants.some((variant) => variant.caseInsensitive);
|
|
44
|
+
return matcher;
|
|
45
|
+
}
|
|
46
|
+
for (const variant of variants) {
|
|
47
|
+
let current = variant.caseInsensitive ? matcher.folded : matcher.exact;
|
|
48
|
+
if (variant.caseInsensitive)
|
|
49
|
+
matcher.hasFolded = true;
|
|
50
|
+
const value = variant.caseInsensitive ? variant.value.toLowerCase() : variant.value;
|
|
51
|
+
for (let index = 0; index < value.length; index++) {
|
|
52
|
+
const character = value[index];
|
|
53
|
+
let child = current.next.get(character);
|
|
54
|
+
if (!child) {
|
|
55
|
+
child = node();
|
|
56
|
+
current.next.set(character, child);
|
|
57
|
+
}
|
|
58
|
+
current = child;
|
|
59
|
+
}
|
|
60
|
+
current.boundary =
|
|
61
|
+
current.boundary === undefined
|
|
62
|
+
? variant.requiresTokenBoundary
|
|
63
|
+
: current.boundary && variant.requiresTokenBoundary;
|
|
64
|
+
}
|
|
65
|
+
return matcher;
|
|
66
|
+
}
|
|
67
|
+
const compiledValues = new WeakMap();
|
|
68
|
+
export function compileDiagnosticSensitiveValues(values) {
|
|
69
|
+
const handle = Object.freeze({
|
|
70
|
+
kind: "compiled-diagnostic-sensitive-values",
|
|
71
|
+
});
|
|
72
|
+
const registered = values.filter((value) => value.length >= MINIMUM_REGISTERED_VALUE_LENGTH);
|
|
73
|
+
const variants = allSensitiveValueVariants(registered);
|
|
74
|
+
const primary = compile(variants), check = compile(variants);
|
|
75
|
+
compiledValues.set(handle, {
|
|
76
|
+
primary,
|
|
77
|
+
check,
|
|
78
|
+
criticalPrimary: primary.encoded ?? primary,
|
|
79
|
+
criticalCheck: check.encoded ?? check,
|
|
80
|
+
values: new Set(registered),
|
|
81
|
+
});
|
|
82
|
+
return handle;
|
|
83
|
+
}
|
|
84
|
+
// Outside-context reads are process lifetime data, bounded like request harvesting.
|
|
85
|
+
const PROCESS_VALUE_LIMIT = 1024;
|
|
86
|
+
const PROCESS_BYTE_LIMIT = 64 * 1024;
|
|
87
|
+
function createDiagnosticFallback() {
|
|
88
|
+
return {
|
|
89
|
+
values: new Set(),
|
|
90
|
+
bytes: 0,
|
|
91
|
+
suppressed: false,
|
|
92
|
+
version: 0,
|
|
93
|
+
compiledVersion: -1,
|
|
94
|
+
compiled: undefined,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const processFallback = createDiagnosticFallback();
|
|
98
|
+
const fallbackScopes = new AsyncLocalStorage();
|
|
99
|
+
const processCompositions = new WeakMap();
|
|
100
|
+
/**
|
|
101
|
+
* Give an independent runtime/configuration evaluation its own fallback lifetime.
|
|
102
|
+
* Apps created inside the scope retain that inventory, including across awaits and
|
|
103
|
+
* after the callback returns. Ordinary startup/CLI reads keep the process lifetime.
|
|
104
|
+
* This also lets test harnesses isolate synthetic env reads without clearing live
|
|
105
|
+
* apps' credentials or weakening registration, matching, or exhaustion policies.
|
|
106
|
+
*/
|
|
107
|
+
export function withDiagnosticFallbackScope(fn) {
|
|
108
|
+
return fallbackScopes.run(createDiagnosticFallback(), fn);
|
|
109
|
+
}
|
|
110
|
+
/** Re-enter the app's existing lifetime, including for detached request callbacks. */
|
|
111
|
+
export function withAppDiagnosticFallback(staticValues, fn) {
|
|
112
|
+
return fallbackScopes.run(processCompositions.get(staticValues) ?? currentDiagnosticFallback(), fn);
|
|
113
|
+
}
|
|
114
|
+
function currentDiagnosticFallback() {
|
|
115
|
+
return fallbackScopes.getStore() ?? processFallback;
|
|
116
|
+
}
|
|
117
|
+
export function suppressProcessDiagnosticValues() {
|
|
118
|
+
const fallback = currentDiagnosticFallback();
|
|
119
|
+
if (fallback.suppressed)
|
|
120
|
+
return;
|
|
121
|
+
fallback.suppressed = true;
|
|
122
|
+
console.warn("[apifuse] diagnostic redaction failed closed; reason=static_registry_limit; limit=1024_entries/65536_bytes");
|
|
123
|
+
}
|
|
124
|
+
export function registerProcessDiagnosticValues(values) {
|
|
125
|
+
const fallback = currentDiagnosticFallback();
|
|
126
|
+
if (fallback.suppressed)
|
|
127
|
+
return;
|
|
128
|
+
for (const value of values) {
|
|
129
|
+
if (value.length < MINIMUM_REGISTERED_VALUE_LENGTH || fallback.values.has(value))
|
|
130
|
+
continue;
|
|
131
|
+
const bytes = Buffer.byteLength(value);
|
|
132
|
+
if (fallback.values.size >= PROCESS_VALUE_LIMIT ||
|
|
133
|
+
fallback.bytes + bytes > PROCESS_BYTE_LIMIT) {
|
|
134
|
+
suppressProcessDiagnosticValues();
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
fallback.values.add(value);
|
|
138
|
+
fallback.bytes += bytes;
|
|
139
|
+
fallback.version++;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** The app's static inventory and the shared outside-context fallback form one static part. */
|
|
143
|
+
export function compileProcessDiagnosticSensitiveValues(values) {
|
|
144
|
+
const handle = compileDiagnosticSensitiveValues(values);
|
|
145
|
+
processCompositions.set(handle, currentDiagnosticFallback());
|
|
146
|
+
return handle;
|
|
147
|
+
}
|
|
148
|
+
function isTokenCharacter(value) {
|
|
149
|
+
return value !== undefined && /[\p{L}\p{N}]/u.test(value);
|
|
150
|
+
}
|
|
151
|
+
function matchLength(root, comparable, original, start) {
|
|
152
|
+
let current = root;
|
|
153
|
+
let length = 0;
|
|
154
|
+
for (let index = start; index < comparable.length; index++) {
|
|
155
|
+
const child = current.next.get(comparable[index]);
|
|
156
|
+
if (!child)
|
|
157
|
+
break;
|
|
158
|
+
current = child;
|
|
159
|
+
if (current.boundary !== undefined &&
|
|
160
|
+
(!current.boundary ||
|
|
161
|
+
(!isTokenCharacter(original[start - 1]) && !isTokenCharacter(original[index + 1])))) {
|
|
162
|
+
length = index - start + 1;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return length;
|
|
166
|
+
}
|
|
167
|
+
function replaceNeedles(text, matcher, checkOnly) {
|
|
168
|
+
const comparable = text.includes("%") ? normalizePercentEscapes(text) : text;
|
|
169
|
+
const folded = matcher.hasFolded ? comparable.toLowerCase() : comparable;
|
|
170
|
+
let cursor = 0;
|
|
171
|
+
let output = "";
|
|
172
|
+
while (cursor < text.length) {
|
|
173
|
+
let first = text.length;
|
|
174
|
+
let length = 0;
|
|
175
|
+
for (const variant of matcher.needles ?? []) {
|
|
176
|
+
const source = variant.caseInsensitive ? folded : text;
|
|
177
|
+
let index = source.indexOf(variant.value, cursor);
|
|
178
|
+
while (index !== -1) {
|
|
179
|
+
const end = index + variant.value.length;
|
|
180
|
+
const markerStart = source.lastIndexOf("[", index);
|
|
181
|
+
const withinMarker = markerStart !== -1 && index < markerStart + markerLength(comparable, markerStart);
|
|
182
|
+
if (!withinMarker &&
|
|
183
|
+
(!variant.requiresTokenBoundary ||
|
|
184
|
+
(!isTokenCharacter(source[index - 1]) && !isTokenCharacter(source[end]))))
|
|
185
|
+
break;
|
|
186
|
+
index = source.indexOf(variant.value, index + 1);
|
|
187
|
+
}
|
|
188
|
+
if (index === -1)
|
|
189
|
+
continue;
|
|
190
|
+
if (checkOnly || (variant.caseInsensitive && folded.length !== text.length))
|
|
191
|
+
return REDACTED_QUERY_VALUE;
|
|
192
|
+
if (index < first || (index === first && variant.value.length > length)) {
|
|
193
|
+
first = index;
|
|
194
|
+
length = variant.value.length;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (!length)
|
|
198
|
+
break;
|
|
199
|
+
output += text.slice(cursor, first) + REDACTED_QUERY_VALUE;
|
|
200
|
+
cursor = first + length;
|
|
201
|
+
}
|
|
202
|
+
return cursor ? output + text.slice(cursor) : text;
|
|
203
|
+
}
|
|
204
|
+
/** Literal tries have no secret-built regular expressions and no per-needle no-match scans. */
|
|
205
|
+
function replaceMatches(text, matchers, checkOnly = false) {
|
|
206
|
+
if (matchers.length === 1 && matchers[0].needles)
|
|
207
|
+
return replaceNeedles(text, matchers[0], checkOnly);
|
|
208
|
+
const comparable = text.includes("%") ? normalizePercentEscapes(text) : text;
|
|
209
|
+
const folded = matchers.some((matcher) => matcher.hasFolded)
|
|
210
|
+
? comparable.toLowerCase()
|
|
211
|
+
: comparable;
|
|
212
|
+
// Unicode case folding can change offsets. Suppress the field on a folded match.
|
|
213
|
+
if (folded.length !== comparable.length) {
|
|
214
|
+
for (const matcher of matchers) {
|
|
215
|
+
for (let index = 0; index < folded.length; index++) {
|
|
216
|
+
if (matchLength(matcher.folded, folded, folded, index))
|
|
217
|
+
return REDACTED_QUERY_VALUE;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
let cursor = 0;
|
|
222
|
+
let output = "";
|
|
223
|
+
for (let index = 0; index < text.length; index++) {
|
|
224
|
+
// Sentinels are emitted constants, not evidence of a residual credential.
|
|
225
|
+
const marker = markerLength(text, index);
|
|
226
|
+
if (marker) {
|
|
227
|
+
index += marker - 1;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
let length = 0;
|
|
231
|
+
for (const matcher of matchers) {
|
|
232
|
+
length = Math.max(length, matchLength(matcher.exact, text, text, index));
|
|
233
|
+
if (matcher.hasFolded && folded.length === comparable.length) {
|
|
234
|
+
length = Math.max(length, matchLength(matcher.folded, folded, text, index));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (!length)
|
|
238
|
+
continue;
|
|
239
|
+
if (checkOnly)
|
|
240
|
+
return REDACTED_QUERY_VALUE;
|
|
241
|
+
output += text.slice(cursor, index) + REDACTED_QUERY_VALUE;
|
|
242
|
+
cursor = index + length;
|
|
243
|
+
index = cursor - 1;
|
|
244
|
+
}
|
|
245
|
+
return cursor === 0 ? text : output + text.slice(cursor);
|
|
246
|
+
}
|
|
247
|
+
function decodePercent(text) {
|
|
248
|
+
try {
|
|
249
|
+
return decodeURIComponent(text);
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
// An unrelated malformed escape must not hide valid encoded credential text.
|
|
253
|
+
return text.replace(/(?:%[\da-f]{2})+/gi, (part) => {
|
|
254
|
+
try {
|
|
255
|
+
return decodeURIComponent(part);
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
return part.replace(/%([\da-f]{2})/gi, (encodedByte, hex) => {
|
|
259
|
+
const byte = Number.parseInt(hex, 16);
|
|
260
|
+
return byte < 128 ? String.fromCharCode(byte) : encodedByte;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function applyMatcher(text, matcher) {
|
|
267
|
+
if (!matcher.size)
|
|
268
|
+
return text;
|
|
269
|
+
// ASCII cannot grow under NFC/case folding; without escapes, a shorter field
|
|
270
|
+
// cannot contain any needle. Keep decoding and Unicode checks on the full path.
|
|
271
|
+
if (text.length < matcher.minimumLength && !text.includes("%") && !/[\u0080-\uffff]/.test(text))
|
|
272
|
+
return text;
|
|
273
|
+
const cached = matcher.cache.get(text);
|
|
274
|
+
if (cached !== undefined)
|
|
275
|
+
return cached;
|
|
276
|
+
const matchers = [matcher];
|
|
277
|
+
const direct = replaceMatches(text, matchers);
|
|
278
|
+
let result = direct;
|
|
279
|
+
let normalized = direct;
|
|
280
|
+
for (let round = 0; round < 3 && normalized.includes("%"); round++) {
|
|
281
|
+
const decoded = decodePercent(normalized);
|
|
282
|
+
if (decoded === normalized)
|
|
283
|
+
break;
|
|
284
|
+
normalized = decoded;
|
|
285
|
+
if (replaceMatches(normalized, [matcher.encoded ?? matcher], true) !== normalized) {
|
|
286
|
+
result = REDACTED_QUERY_VALUE;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// A residual escape is unresolved after the fixed work budget: emit less,
|
|
290
|
+
// if this nonempty registry can match percent-encoded credentials, even when
|
|
291
|
+
// another representation matched. Both independent roles use this.
|
|
292
|
+
if (matcher.canMatchPercentEncoded && /%[\da-f]{2}/i.test(normalized))
|
|
293
|
+
result = REDACTION_FAILED;
|
|
294
|
+
const nfc = normalized.normalize("NFC");
|
|
295
|
+
if (result !== REDACTION_FAILED &&
|
|
296
|
+
nfc !== normalized &&
|
|
297
|
+
replaceMatches(nfc, [matcher.encoded ?? matcher], true) !== nfc)
|
|
298
|
+
result = REDACTED_QUERY_VALUE;
|
|
299
|
+
// Matchers are immutable; additions compile a new request matcher. Independent
|
|
300
|
+
// request-local caches reuse results across repeated span/resource exports. Bound
|
|
301
|
+
// entry count and input size; process-static lookup data never caches request text.
|
|
302
|
+
if (text.length <= 4096) {
|
|
303
|
+
if (matcher.cache.size >= 256)
|
|
304
|
+
matcher.cache.clear();
|
|
305
|
+
matcher.cache.set(text, result);
|
|
306
|
+
}
|
|
307
|
+
return result;
|
|
308
|
+
}
|
|
309
|
+
function applyMatchers(text, matchers) {
|
|
310
|
+
let result = text;
|
|
311
|
+
for (const matcher of matchers) {
|
|
312
|
+
const candidate = applyMatcher(text, matcher);
|
|
313
|
+
if (candidate === text)
|
|
314
|
+
continue;
|
|
315
|
+
// Static and request secrets may overlap. Scan original text independently
|
|
316
|
+
// so an earlier replacement cannot hide the rest of a longer credential.
|
|
317
|
+
if (result !== text && result !== candidate)
|
|
318
|
+
return REDACTED_QUERY_VALUE;
|
|
319
|
+
result = candidate;
|
|
320
|
+
}
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
/** One small mutable registry per request, composed with precompiled process-static values. */
|
|
324
|
+
export function createDiagnosticRedactor(values = [], staticValues) {
|
|
325
|
+
const compiled = staticValues ? compiledValues.get(staticValues) : undefined;
|
|
326
|
+
// Share immutable lookup data only. Raw diagnostic cache keys die with this scope.
|
|
327
|
+
const requestMatcher = (matcher) => ({ ...matcher, cache: new Map() });
|
|
328
|
+
const staticPrimary = compiled?.primary.size ? requestMatcher(compiled.primary) : undefined;
|
|
329
|
+
const staticCheck = compiled?.check.size ? requestMatcher(compiled.check) : undefined;
|
|
330
|
+
const staticCriticalPrimary = compiled?.criticalPrimary.size
|
|
331
|
+
? compiled.criticalPrimary === compiled.primary
|
|
332
|
+
? staticPrimary
|
|
333
|
+
: requestMatcher(compiled.criticalPrimary)
|
|
334
|
+
: undefined;
|
|
335
|
+
const staticCriticalCheck = compiled?.criticalCheck.size
|
|
336
|
+
? compiled.criticalCheck === compiled.check
|
|
337
|
+
? staticCheck
|
|
338
|
+
: requestMatcher(compiled.criticalCheck)
|
|
339
|
+
: undefined;
|
|
340
|
+
const known = new Set();
|
|
341
|
+
const fallback = staticValues === undefined ? undefined : processCompositions.get(staticValues);
|
|
342
|
+
const has = (value) => known.has(value) ||
|
|
343
|
+
compiled?.values.has(value) === true ||
|
|
344
|
+
fallback?.values.has(value) === true;
|
|
345
|
+
let seenProcessVersion = -1;
|
|
346
|
+
let fallbackPrimary = [];
|
|
347
|
+
let fallbackCheck = [];
|
|
348
|
+
let fallbackCriticalPrimary = [];
|
|
349
|
+
let fallbackCriticalCheck = [];
|
|
350
|
+
let requestPrimary;
|
|
351
|
+
let requestCheck;
|
|
352
|
+
let suppressed = false;
|
|
353
|
+
let primary = staticPrimary ? [staticPrimary] : [];
|
|
354
|
+
let check = staticCheck ? [staticCheck] : [];
|
|
355
|
+
let criticalPrimary = staticCriticalPrimary ? [staticCriticalPrimary] : [];
|
|
356
|
+
let criticalCheck = staticCriticalCheck ? [staticCriticalCheck] : [];
|
|
357
|
+
const variants = [];
|
|
358
|
+
let uncompiled = [];
|
|
359
|
+
const add = (incoming) => {
|
|
360
|
+
for (const value of incoming) {
|
|
361
|
+
if (value.length < MINIMUM_REGISTERED_VALUE_LENGTH || has(value))
|
|
362
|
+
continue;
|
|
363
|
+
known.add(value);
|
|
364
|
+
uncompiled.push(value);
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
const ensureCompiled = () => {
|
|
368
|
+
const processChanged = fallback !== undefined && seenProcessVersion !== fallback.version;
|
|
369
|
+
if (!uncompiled.length && !processChanged)
|
|
370
|
+
return;
|
|
371
|
+
if (processChanged) {
|
|
372
|
+
if (fallback.compiledVersion !== fallback.version) {
|
|
373
|
+
fallback.compiled = compileDiagnosticSensitiveValues([...fallback.values]);
|
|
374
|
+
fallback.compiledVersion = fallback.version;
|
|
375
|
+
}
|
|
376
|
+
const compiledFallback = fallback.compiled && compiledValues.get(fallback.compiled);
|
|
377
|
+
fallbackPrimary = compiledFallback?.primary.size
|
|
378
|
+
? [requestMatcher(compiledFallback.primary)]
|
|
379
|
+
: [];
|
|
380
|
+
fallbackCheck = compiledFallback?.check.size ? [requestMatcher(compiledFallback.check)] : [];
|
|
381
|
+
fallbackCriticalPrimary = compiledFallback?.criticalPrimary.size
|
|
382
|
+
? [requestMatcher(compiledFallback.criticalPrimary)]
|
|
383
|
+
: [];
|
|
384
|
+
fallbackCriticalCheck = compiledFallback?.criticalCheck.size
|
|
385
|
+
? [requestMatcher(compiledFallback.criticalCheck)]
|
|
386
|
+
: [];
|
|
387
|
+
seenProcessVersion = fallback.version;
|
|
388
|
+
}
|
|
389
|
+
if (uncompiled.length) {
|
|
390
|
+
// Generate each value's variants once, lazily on the first actual match.
|
|
391
|
+
variants.push(...allSensitiveValueVariants(uncompiled));
|
|
392
|
+
uncompiled = [];
|
|
393
|
+
requestPrimary = compile(variants);
|
|
394
|
+
requestCheck = compile(variants);
|
|
395
|
+
}
|
|
396
|
+
primary = [
|
|
397
|
+
...(staticPrimary ? [staticPrimary] : []),
|
|
398
|
+
...fallbackPrimary,
|
|
399
|
+
...(requestPrimary ? [requestPrimary] : []),
|
|
400
|
+
];
|
|
401
|
+
check = [
|
|
402
|
+
...(staticCheck ? [staticCheck] : []),
|
|
403
|
+
...fallbackCheck,
|
|
404
|
+
...(requestCheck ? [requestCheck] : []),
|
|
405
|
+
];
|
|
406
|
+
criticalPrimary = [
|
|
407
|
+
...(staticCriticalPrimary ? [staticCriticalPrimary] : []),
|
|
408
|
+
...fallbackCriticalPrimary,
|
|
409
|
+
...(requestPrimary ? [requestPrimary.encoded ?? requestPrimary] : []),
|
|
410
|
+
];
|
|
411
|
+
criticalCheck = [
|
|
412
|
+
...(staticCriticalCheck ? [staticCriticalCheck] : []),
|
|
413
|
+
...fallbackCriticalCheck,
|
|
414
|
+
...(requestCheck ? [requestCheck.encoded ?? requestCheck] : []),
|
|
415
|
+
];
|
|
416
|
+
};
|
|
417
|
+
add(values);
|
|
418
|
+
const protect = (callback, critical = false, freeText = !critical) => (text) => {
|
|
419
|
+
if (text === REDACTED_QUERY_VALUE || text === REDACTION_FAILED)
|
|
420
|
+
return text;
|
|
421
|
+
if ((suppressed || fallback?.suppressed) && freeText)
|
|
422
|
+
return REDACTION_FAILED;
|
|
423
|
+
ensureCompiled();
|
|
424
|
+
const result = redactDiagnosticText(text, callback);
|
|
425
|
+
ensureCompiled(); // A replaced callback may register a value during the call.
|
|
426
|
+
if ((suppressed || fallback?.suppressed) && freeText)
|
|
427
|
+
return REDACTION_FAILED;
|
|
428
|
+
// This private matcher is a separate object from the callback's matcher,
|
|
429
|
+
// survives callback replacement, and sees additions made after trace creation.
|
|
430
|
+
return applyMatchers(result, critical ? criticalCheck : check) === result
|
|
431
|
+
? result
|
|
432
|
+
: REDACTION_FAILED;
|
|
433
|
+
};
|
|
434
|
+
let safeRedact = protect((text) => applyMatchers(text, primary));
|
|
435
|
+
let safeCritical = protect((text) => applyMatchers(text, criticalPrimary), true);
|
|
436
|
+
let safeStructured = protect((text) => applyMatchers(text, primary), false, false);
|
|
437
|
+
const critical = (text) => safeCritical(text);
|
|
438
|
+
const structured = (text) => safeStructured(text);
|
|
439
|
+
const isSuppressed = () => suppressed || fallback?.suppressed === true;
|
|
440
|
+
bindCriticalDiagnosticRedactor(safeRedact, critical, structured, isSuppressed);
|
|
441
|
+
return {
|
|
442
|
+
add,
|
|
443
|
+
has,
|
|
444
|
+
redactCritical: critical,
|
|
445
|
+
redactStructured: structured,
|
|
446
|
+
get suppressed() {
|
|
447
|
+
return isSuppressed();
|
|
448
|
+
},
|
|
449
|
+
suppress() {
|
|
450
|
+
suppressed = true;
|
|
451
|
+
},
|
|
452
|
+
get redact() {
|
|
453
|
+
return safeRedact;
|
|
454
|
+
},
|
|
455
|
+
set redact(callback) {
|
|
456
|
+
safeRedact = protect(callback);
|
|
457
|
+
safeCritical = protect(callback, true);
|
|
458
|
+
safeStructured = protect(callback, false, false);
|
|
459
|
+
bindCriticalDiagnosticRedactor(safeRedact, critical, structured, isSuppressed);
|
|
460
|
+
},
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
const requestRegistries = new WeakMap();
|
|
464
|
+
/** Internal binding; neither the registry nor its values enter provider-facing context types. */
|
|
465
|
+
export function bindDiagnosticSensitiveRegistry(trace, registry) {
|
|
466
|
+
requestRegistries.set(trace, registry);
|
|
467
|
+
}
|
|
468
|
+
export function registerDiagnosticSensitiveValues(trace, values) {
|
|
469
|
+
requestRegistries.get(trace)?.add(values);
|
|
470
|
+
}
|
|
471
|
+
// Policy metadata is internal: public callbacks remain backward-compatible unary functions.
|
|
472
|
+
const criticalRedactors = new WeakMap();
|
|
473
|
+
const structuredRedactors = new WeakMap();
|
|
474
|
+
const redactionSuppression = new WeakMap();
|
|
475
|
+
export function bindCriticalDiagnosticRedactor(redact, critical, structured, isSuppressed) {
|
|
476
|
+
criticalRedactors.set(redact, critical);
|
|
477
|
+
if (structured)
|
|
478
|
+
structuredRedactors.set(redact, structured);
|
|
479
|
+
if (isSuppressed)
|
|
480
|
+
redactionSuppression.set(redact, isSuppressed);
|
|
481
|
+
}
|
|
482
|
+
export function isDiagnosticRedactionSuppressed(redact) {
|
|
483
|
+
return redact ? redactionSuppression.get(redact)?.() === true : false;
|
|
484
|
+
}
|
|
485
|
+
/** Typed values and keys still match known credentials, but never blanket suppression. */
|
|
486
|
+
export function diagnosticStructuredRedactor(redact) {
|
|
487
|
+
return redact ? (structuredRedactors.get(redact) ?? redact) : undefined;
|
|
488
|
+
}
|
|
489
|
+
export function redactDiagnosticAttributeKey(key, redact) {
|
|
490
|
+
return redactDiagnosticText(key, diagnosticStructuredRedactor(diagnosticAttributeRedactor(key, redact)));
|
|
491
|
+
}
|
|
492
|
+
export function redactCriticalDiagnosticText(text, redact) {
|
|
493
|
+
return redactDiagnosticText(text, redact ? (criticalRedactors.get(redact) ?? redact) : undefined);
|
|
494
|
+
}
|
|
495
|
+
export function diagnosticAttributeRedactor(key, redact) {
|
|
496
|
+
if (CLOSED_ENUM_ATTRIBUTE_KEYS.has(key))
|
|
497
|
+
return diagnosticStructuredRedactor(redact);
|
|
498
|
+
return CRITICAL_ATTRIBUTE_KEYS.has(key) && redact
|
|
499
|
+
? (criticalRedactors.get(redact) ?? redact)
|
|
500
|
+
: redact;
|
|
501
|
+
}
|
|
502
|
+
// Structured debugging contracts use only >=8-character values. Other attributes are free text.
|
|
503
|
+
const CRITICAL_ATTRIBUTE_KEYS = new Set([
|
|
504
|
+
"request_id",
|
|
505
|
+
"requestId",
|
|
506
|
+
"trace_id",
|
|
507
|
+
"traceId",
|
|
508
|
+
"span_id",
|
|
509
|
+
"spanId",
|
|
510
|
+
"route",
|
|
511
|
+
"operation_id",
|
|
512
|
+
"operation",
|
|
513
|
+
"http.status_code",
|
|
514
|
+
]);
|
|
515
|
+
// SDK-owned closed enums are contracts, like typed numbers and booleans. This
|
|
516
|
+
// exempts only exhaustion suppression; registered credentials still match.
|
|
517
|
+
const CLOSED_ENUM_ATTRIBUTE_KEYS = new Set([
|
|
518
|
+
"outcome",
|
|
519
|
+
"code",
|
|
520
|
+
"errorClass",
|
|
521
|
+
"phase",
|
|
522
|
+
"cacheStatus",
|
|
523
|
+
"identitySource",
|
|
524
|
+
"taxonomy",
|
|
525
|
+
"taxonomyVersion",
|
|
526
|
+
]);
|
|
527
|
+
// Bigints normalize to strings for OTLP. Carry their type policy through internal
|
|
528
|
+
// detached copies without treating arbitrary numeric-looking free text as typed.
|
|
529
|
+
const bigintAttributes = new WeakMap();
|
|
530
|
+
export function isDiagnosticBigintAttribute(attributes, key) {
|
|
531
|
+
return bigintAttributes.get(attributes)?.has(key) === true;
|
|
532
|
+
}
|
|
533
|
+
export function copyDiagnosticAttributeTypes(source, target) {
|
|
534
|
+
const keys = Object.keys(source).filter((key) => (typeof source[key] === "bigint" || isDiagnosticBigintAttribute(source, key)) &&
|
|
535
|
+
String(source[key]) === target[key]);
|
|
536
|
+
if (keys.length)
|
|
537
|
+
bigintAttributes.set(target, new Set(keys));
|
|
538
|
+
return target;
|
|
539
|
+
}
|
|
540
|
+
/** Reserve existing keys so independently redacted names never drop an attribute. */
|
|
541
|
+
export function redactedKeyAllocator(keys) {
|
|
542
|
+
// Snapshot the inventory now, but allocate its lookup only if a key changes.
|
|
543
|
+
const inventory = [...keys];
|
|
544
|
+
let reserved;
|
|
545
|
+
let sequence = 0;
|
|
546
|
+
return () => {
|
|
547
|
+
reserved ??= new Set(inventory);
|
|
548
|
+
let key;
|
|
549
|
+
do {
|
|
550
|
+
key = `[REDACTED#${++sequence}]`;
|
|
551
|
+
} while (reserved.has(key));
|
|
552
|
+
reserved.add(key);
|
|
553
|
+
return key;
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
export function isRedactedKey(key) {
|
|
557
|
+
return /^\[REDACTED#\d+\]$/.test(key);
|
|
558
|
+
}
|
|
559
|
+
function markerLength(text, index) {
|
|
560
|
+
if (text[index] !== "[")
|
|
561
|
+
return 0;
|
|
562
|
+
if (text.startsWith(REDACTED_QUERY_VALUE, index))
|
|
563
|
+
return REDACTED_QUERY_VALUE.length;
|
|
564
|
+
if (text.startsWith(REDACTION_FAILED, index))
|
|
565
|
+
return REDACTION_FAILED.length;
|
|
566
|
+
if (text.startsWith("[REDACTED#", index))
|
|
567
|
+
return /^\[REDACTED#\d+\]/.exec(text.slice(index))?.[0].length ?? 0;
|
|
568
|
+
return 0;
|
|
569
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { ProxyVendorName } from "../config/loader.js";
|
|
2
|
+
/**
|
|
3
|
+
* AES-256-GCM key for the engine ceremony egress lease handle (ADR-0010 v1.1).
|
|
4
|
+
* Engine host only: the `APIFUSE__ENGINE__` family is rejected from provider
|
|
5
|
+
* secrets and filtered from provider env projections. Supply at least 32 random
|
|
6
|
+
* bytes (for example `openssl rand -base64 32`). Rotating the key fails
|
|
7
|
+
* verification of every outstanding handle (`EGRESS_LEASE_INVALID`, 409): the
|
|
8
|
+
* affected ceremonies restart; they do not expire.
|
|
9
|
+
*/
|
|
10
|
+
export declare const APIFUSE__ENGINE__CEREMONY_LEASE_KEY = "APIFUSE__ENGINE__CEREMONY_LEASE_KEY";
|
|
11
|
+
/** Internal option key shared only by the server assembler and stealth runtime. */
|
|
12
|
+
export declare const ENGINE_CEREMONY_EGRESS_LEASE: unique symbol;
|
|
13
|
+
export type CeremonyEgressBinding = {
|
|
14
|
+
readonly vendor: ProxyVendorName;
|
|
15
|
+
readonly proxyUrl: string;
|
|
16
|
+
readonly poolIndex: number;
|
|
17
|
+
readonly affinityKey: string;
|
|
18
|
+
readonly refreshEpoch: number;
|
|
19
|
+
/**
|
|
20
|
+
* Vendor session lifetime the endpoint was allocated for (`proxy.session.lifetimeMinutes`,
|
|
21
|
+
* Smartproxy `life`, NodeMaven SID window). It is the upper bound of the lease, counted
|
|
22
|
+
* from the mint (the first successful attempt), so a Smartproxy lease can outlive the
|
|
23
|
+
* vendor session by the extraction-cache reuse window; expiry then drops and rebinds.
|
|
24
|
+
* The bound is not a vendor guarantee (ADR-0010: a raw Smartproxy endpoint is not a hard
|
|
25
|
+
* lease): the stealth runtime releases the binding as soon as the endpoint fails at the
|
|
26
|
+
* transport, so a dead endpoint is left before the lifetime elapses.
|
|
27
|
+
*/
|
|
28
|
+
readonly lifetimeMinutes: number;
|
|
29
|
+
};
|
|
30
|
+
export type CeremonyEgressLeaseRuntime = {
|
|
31
|
+
/** Exact egress bound to this ceremony; undefined until the first successful attempt binds one. */
|
|
32
|
+
readonly binding: CeremonyEgressBinding | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Drops the binding once its vendor session lifetime has elapsed so the next attempt
|
|
35
|
+
* selects and binds a fresh endpoint. Returns true when a binding was dropped: the
|
|
36
|
+
* caller must also discard challenge state that was only valid for that endpoint.
|
|
37
|
+
*/
|
|
38
|
+
dropExpiredBinding(): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Drops the binding unconditionally; the stealth runtime calls this when the bound
|
|
41
|
+
* endpoint fails at the transport. Same return and caller obligation as
|
|
42
|
+
* `dropExpiredBinding`.
|
|
43
|
+
*/
|
|
44
|
+
dropBinding(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Number of bindings dropped so far (expiry or release). Every stealth session created
|
|
47
|
+
* from one client shares the lease; a session compares this to what it last observed to
|
|
48
|
+
* retire endpoint-bound challenge state and replay records after another session moved
|
|
49
|
+
* the ceremony.
|
|
50
|
+
*/
|
|
51
|
+
readonly generation: number;
|
|
52
|
+
bind(binding: CeremonyEgressBinding): void;
|
|
53
|
+
handle(): string | undefined;
|
|
54
|
+
};
|
|
55
|
+
export declare function createCeremonyEgressLeaseRuntime(options: {
|
|
56
|
+
readonly tenantId?: string;
|
|
57
|
+
readonly providerId: string;
|
|
58
|
+
readonly flowId: string;
|
|
59
|
+
readonly affinityKey: string;
|
|
60
|
+
readonly handle?: string;
|
|
61
|
+
/** Server-only registration before a handle can reach request diagnostics. */
|
|
62
|
+
readonly onHandle?: (handle: string) => void;
|
|
63
|
+
readonly environment?: Readonly<Record<string, string | undefined>>;
|
|
64
|
+
readonly now?: () => number;
|
|
65
|
+
}): CeremonyEgressLeaseRuntime;
|