@steadphast/connector-nextjs 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +169 -14
- package/dist/behavioral-collector.d.ts +8 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +19 -0
- package/dist/config.d.ts +19 -0
- package/dist/index.js +351 -22
- package/dist/server.js +867 -11
- package/package.json +12 -2
package/dist/index.js
CHANGED
|
@@ -66,6 +66,57 @@ function missingApiKeyDiagnostic(customEnvVar) {
|
|
|
66
66
|
return `Steadphast connector skipped event: no usable API key resolved from ${STEADPHAST_API_KEY_ENV_VAR} or deprecated fallback ${LEGACY_DISCOVER_API_KEY_ENV_VAR}. ${STEADPHAST_API_KEY_ENV_VAR} takes precedence when defined, even when blank.`;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
// ../connector-core/src/query.ts
|
|
70
|
+
var ENCODED_QUESTION_MARK = /%3f/i;
|
|
71
|
+
var ENCODED_PAIR_SEPARATOR = /&|%26/i;
|
|
72
|
+
var ENCODED_KEY_END = /=|%3d/i;
|
|
73
|
+
function redactQuery(search) {
|
|
74
|
+
const withoutFragment = search.split("#", 1)[0] ?? "";
|
|
75
|
+
const query = withoutFragment.startsWith("?") ? withoutFragment.slice(1) : withoutFragment;
|
|
76
|
+
const keys = query.split("&").map((pair) => pair.split("=", 1)[0] ?? "").filter((key) => key.length > 0);
|
|
77
|
+
return keys.length > 0 ? `?${keys.map((key) => `${key}=`).join("&")}` : "";
|
|
78
|
+
}
|
|
79
|
+
function redactPath(pathAndQuery) {
|
|
80
|
+
const withoutFragment = pathAndQuery.split("#", 1)[0] ?? "";
|
|
81
|
+
const queryStart = withoutFragment.indexOf("?");
|
|
82
|
+
const pathPart = queryStart === -1 ? withoutFragment : withoutFragment.slice(0, queryStart);
|
|
83
|
+
const query = queryStart === -1 ? "" : withoutFragment.slice(queryStart);
|
|
84
|
+
return `${redactPathPart(pathPart)}${redactQuery(query)}`;
|
|
85
|
+
}
|
|
86
|
+
function redactUrlLike(value) {
|
|
87
|
+
let prefix;
|
|
88
|
+
if (value.startsWith("//")) {
|
|
89
|
+
prefix = "//";
|
|
90
|
+
} else if (value.startsWith("/")) {
|
|
91
|
+
return redactPath(value);
|
|
92
|
+
} else if (/^https:\/\//i.test(value)) {
|
|
93
|
+
prefix = value.slice(0, "https://".length);
|
|
94
|
+
} else if (/^http:\/\//i.test(value)) {
|
|
95
|
+
prefix = value.slice(0, "http://".length);
|
|
96
|
+
} else {
|
|
97
|
+
return void 0;
|
|
98
|
+
}
|
|
99
|
+
const rest = value.slice(prefix.length);
|
|
100
|
+
const authorityEnd = rest.search(/[/?#]/);
|
|
101
|
+
const authority = authorityEnd === -1 ? rest : rest.slice(0, authorityEnd);
|
|
102
|
+
const tail = authorityEnd === -1 ? "" : rest.slice(authorityEnd);
|
|
103
|
+
const host = authority.slice(authority.lastIndexOf("@") + 1);
|
|
104
|
+
return `${prefix}${host}${redactPath(tail)}`;
|
|
105
|
+
}
|
|
106
|
+
function redactPathPart(pathPart) {
|
|
107
|
+
const encoded = ENCODED_QUESTION_MARK.exec(pathPart);
|
|
108
|
+
const prefix = encoded === null ? pathPart : pathPart.slice(0, encoded.index);
|
|
109
|
+
const redactedPrefix = prefix.replaceAll(
|
|
110
|
+
/;([^/;=]*)=[^/;]*/g,
|
|
111
|
+
(_match, name) => `;${name}=`
|
|
112
|
+
);
|
|
113
|
+
if (encoded === null) {
|
|
114
|
+
return redactedPrefix;
|
|
115
|
+
}
|
|
116
|
+
const keys = pathPart.slice(encoded.index + encoded[0].length).split(ENCODED_PAIR_SEPARATOR).map((pair) => pair.split(ENCODED_KEY_END, 1)[0] ?? "").filter((key) => key.length > 0);
|
|
117
|
+
return keys.length > 0 ? `${redactedPrefix}${encoded[0]}${keys.map((key) => `${key}=`).join("&")}` : redactedPrefix;
|
|
118
|
+
}
|
|
119
|
+
|
|
69
120
|
// ../connector-core/src/headers.ts
|
|
70
121
|
var EXCLUDED_HEADER_IDENTITIES = /* @__PURE__ */ new Set([
|
|
71
122
|
"authorization",
|
|
@@ -109,29 +160,148 @@ var EXCLUDED_HEADER_IDENTITIES = /* @__PURE__ */ new Set([
|
|
|
109
160
|
var EXCLUDED_HEADER_SUBSTRINGS = ["cryptogram", "networktoken"];
|
|
110
161
|
var PAYMENT_CREDENTIAL_MARKER_HEADER = "x-steadphast-payment-credential";
|
|
111
162
|
var PAYMENT_AUTHORIZATION_MARKER_VALUE = "payment-authorization";
|
|
163
|
+
var COOKIE_NAMES_MARKER_HEADER = "x-steadphast-cookie-names";
|
|
164
|
+
var COOKIE_NAME_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
165
|
+
var MAX_COOKIE_NAME_BYTES = 64;
|
|
166
|
+
var MAX_COOKIE_NAMES = 32;
|
|
167
|
+
var MAX_COOKIE_NAMES_VALUE_BYTES = 1024;
|
|
168
|
+
var PRESENCE_ONLY_IDENTITIES = /* @__PURE__ */ new Set(["nextrouterstatetree", "nexturl"]);
|
|
169
|
+
var PRESENCE_ONLY_VALUE = "present";
|
|
170
|
+
var PROXY_URL_IDENTITIES = /* @__PURE__ */ new Set([
|
|
171
|
+
"xforwardeduri",
|
|
172
|
+
"xoriginalurl",
|
|
173
|
+
"xoriginaluri",
|
|
174
|
+
"xenvoyoriginalpath",
|
|
175
|
+
"xrewriteurl"
|
|
176
|
+
]);
|
|
177
|
+
var DEDICATED_PEER_HEADERS = /* @__PURE__ */ new Set([
|
|
178
|
+
"user-agent",
|
|
179
|
+
"signature",
|
|
180
|
+
"signature-input",
|
|
181
|
+
"x-forwarded-for",
|
|
182
|
+
"x-vercel-forwarded-for",
|
|
183
|
+
"x-vercel-id",
|
|
184
|
+
"x-vercel-ja4-digest",
|
|
185
|
+
"x-vercel-ja3-digest"
|
|
186
|
+
]);
|
|
112
187
|
function captureRequestHeaders(headers, options) {
|
|
113
188
|
const captured = /* @__PURE__ */ Object.create(null);
|
|
114
189
|
const additionalExclusions = new Set(
|
|
115
190
|
options?.excludedHeaders?.map((name) => name.trim().toLowerCase()) ?? []
|
|
116
191
|
);
|
|
192
|
+
const excludedIdentities = new Set(
|
|
193
|
+
[...additionalExclusions].map((name) => collapseHeaderName(name))
|
|
194
|
+
);
|
|
195
|
+
const excludesCookie = excludedIdentities.has("cookie");
|
|
196
|
+
const excludesSetCookie = excludedIdentities.has("setcookie");
|
|
197
|
+
const excludesCookieNamesMarker = excludedIdentities.has(
|
|
198
|
+
"xsteadphastcookienames"
|
|
199
|
+
);
|
|
200
|
+
const cookieNames = [];
|
|
117
201
|
let paymentCredential = false;
|
|
118
202
|
headers.forEach((value, name) => {
|
|
119
203
|
const lowerName = name.toLowerCase();
|
|
120
|
-
|
|
204
|
+
const identity = collapseHeaderName(lowerName);
|
|
205
|
+
if (lowerName === PAYMENT_CREDENTIAL_MARKER_HEADER || identity === "xsteadphastcookienames") {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (identity === "cookie") {
|
|
209
|
+
if (!excludesCookie) cookieNames.push(...cookieHeaderNames(value));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (identity === "setcookie") {
|
|
213
|
+
if (!excludesSetCookie && lowerName !== "set-cookie") {
|
|
214
|
+
cookieNames.push(...setCookieNames([value]));
|
|
215
|
+
}
|
|
121
216
|
return;
|
|
122
217
|
}
|
|
123
|
-
if (
|
|
218
|
+
if (identity === "paymentauthorization") {
|
|
124
219
|
paymentCredential = true;
|
|
125
220
|
}
|
|
126
|
-
if (
|
|
127
|
-
|
|
221
|
+
if (isExcludedHeader(lowerName) || additionalExclusions.has(lowerName)) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (identity === "referer" || identity === "referrer") {
|
|
225
|
+
const reduced = reduceReferer(value);
|
|
226
|
+
if (reduced !== void 0) captured[lowerName] = reduced;
|
|
227
|
+
return;
|
|
128
228
|
}
|
|
229
|
+
if (PRESENCE_ONLY_IDENTITIES.has(identity)) {
|
|
230
|
+
captured[lowerName] = PRESENCE_ONLY_VALUE;
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (PROXY_URL_IDENTITIES.has(identity)) {
|
|
234
|
+
captured[lowerName] = redactUrlLike(value) ?? redactPath(value);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
captured[lowerName] = value.includes("?") && !DEDICATED_PEER_HEADERS.has(lowerName) ? redactUrlLike(value) ?? value : value;
|
|
129
238
|
});
|
|
239
|
+
if (!excludesSetCookie) {
|
|
240
|
+
cookieNames.push(...setCookieNames(setCookieValues(headers)));
|
|
241
|
+
}
|
|
130
242
|
if (paymentCredential && !additionalExclusions.has(PAYMENT_CREDENTIAL_MARKER_HEADER)) {
|
|
131
243
|
captured[PAYMENT_CREDENTIAL_MARKER_HEADER] = PAYMENT_AUTHORIZATION_MARKER_VALUE;
|
|
132
244
|
}
|
|
245
|
+
const cookieNamesValue = normalizeCookieNames(cookieNames);
|
|
246
|
+
if (cookieNamesValue !== void 0 && !excludesCookieNamesMarker) {
|
|
247
|
+
captured[COOKIE_NAMES_MARKER_HEADER] = cookieNamesValue;
|
|
248
|
+
}
|
|
133
249
|
return captured;
|
|
134
250
|
}
|
|
251
|
+
function reduceReferer(value) {
|
|
252
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
253
|
+
const code = value.charCodeAt(index);
|
|
254
|
+
if (code <= 32 || code === 127 || code === 92) {
|
|
255
|
+
return void 0;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const schemeEnd = value.indexOf("://");
|
|
259
|
+
if (schemeEnd === -1) {
|
|
260
|
+
return void 0;
|
|
261
|
+
}
|
|
262
|
+
const authority = value.slice(schemeEnd + 3).split(/[/?#]/, 1)[0] ?? "";
|
|
263
|
+
if (authority.length === 0 || /[^\u0021-\u007e]|[@%]/.test(authority)) {
|
|
264
|
+
return void 0;
|
|
265
|
+
}
|
|
266
|
+
let url;
|
|
267
|
+
try {
|
|
268
|
+
url = new URL(value);
|
|
269
|
+
} catch {
|
|
270
|
+
return void 0;
|
|
271
|
+
}
|
|
272
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
273
|
+
return void 0;
|
|
274
|
+
}
|
|
275
|
+
return url.origin;
|
|
276
|
+
}
|
|
277
|
+
function cookieHeaderNames(value) {
|
|
278
|
+
return value.split(";").flatMap((segment) => cookieName(segment.trim()));
|
|
279
|
+
}
|
|
280
|
+
function setCookieNames(values) {
|
|
281
|
+
return values.flatMap((value) => cookieName(value.split(";", 1)[0] ?? ""));
|
|
282
|
+
}
|
|
283
|
+
function cookieName(pair) {
|
|
284
|
+
const separator = pair.indexOf("=");
|
|
285
|
+
if (separator === -1) {
|
|
286
|
+
return [];
|
|
287
|
+
}
|
|
288
|
+
const name = pair.slice(0, separator).trim();
|
|
289
|
+
return name.length <= MAX_COOKIE_NAME_BYTES && COOKIE_NAME_PATTERN.test(name) ? [name] : [];
|
|
290
|
+
}
|
|
291
|
+
function setCookieValues(headers) {
|
|
292
|
+
if (typeof headers.getSetCookie === "function") {
|
|
293
|
+
return headers.getSetCookie();
|
|
294
|
+
}
|
|
295
|
+
const joined = headers.get("set-cookie");
|
|
296
|
+
return joined === null ? [] : [joined];
|
|
297
|
+
}
|
|
298
|
+
function normalizeCookieNames(names) {
|
|
299
|
+
const kept = [...new Set(names)].sort().slice(0, MAX_COOKIE_NAMES);
|
|
300
|
+
while (kept.length > 0 && kept.join(", ").length > MAX_COOKIE_NAMES_VALUE_BYTES) {
|
|
301
|
+
kept.pop();
|
|
302
|
+
}
|
|
303
|
+
return kept.length > 0 ? kept.join(", ") : void 0;
|
|
304
|
+
}
|
|
135
305
|
function parseForwardedChain(headerValue) {
|
|
136
306
|
if (headerValue === null) {
|
|
137
307
|
return void 0;
|
|
@@ -147,6 +317,61 @@ function collapseHeaderName(name) {
|
|
|
147
317
|
return name.replaceAll(/[^0-9A-Za-z]/g, "").toLowerCase();
|
|
148
318
|
}
|
|
149
319
|
|
|
320
|
+
// ../connector-core/src/signed-components.ts
|
|
321
|
+
var QUERY_BEARING_COMPONENTS = /* @__PURE__ */ new Set([
|
|
322
|
+
"@query",
|
|
323
|
+
"@query-param",
|
|
324
|
+
"@target-uri",
|
|
325
|
+
"@request-target"
|
|
326
|
+
]);
|
|
327
|
+
function signatureInputCoversQuery(signatureInput) {
|
|
328
|
+
return signatureInputCovers(signatureInput, QUERY_BEARING_COMPONENTS);
|
|
329
|
+
}
|
|
330
|
+
function signatureInputCoversPath(signatureInput) {
|
|
331
|
+
return signatureInputCovers(signatureInput, PATH_COMPONENTS) || signatureInputCoversQuery(signatureInput);
|
|
332
|
+
}
|
|
333
|
+
var PATH_COMPONENTS = /* @__PURE__ */ new Set(["@path"]);
|
|
334
|
+
function signatureInputCovers(signatureInput, components) {
|
|
335
|
+
if (signatureInput === null) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
let depth = 0;
|
|
339
|
+
let index = 0;
|
|
340
|
+
let covered = false;
|
|
341
|
+
while (index < signatureInput.length) {
|
|
342
|
+
const char = signatureInput[index];
|
|
343
|
+
if (char === '"') {
|
|
344
|
+
let end = index + 1;
|
|
345
|
+
let value = "";
|
|
346
|
+
while (end < signatureInput.length && signatureInput[end] !== '"') {
|
|
347
|
+
if (signatureInput[end] === "\\") {
|
|
348
|
+
end += 1;
|
|
349
|
+
}
|
|
350
|
+
value += signatureInput[end] ?? "";
|
|
351
|
+
end += 1;
|
|
352
|
+
}
|
|
353
|
+
if (end >= signatureInput.length) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
if (depth > 0 && signatureInput[index - 1] !== "=" && components.has(value.toLowerCase())) {
|
|
357
|
+
covered = true;
|
|
358
|
+
}
|
|
359
|
+
index = end + 1;
|
|
360
|
+
continue;
|
|
361
|
+
}
|
|
362
|
+
if (char === "(") {
|
|
363
|
+
depth += 1;
|
|
364
|
+
} else if (char === ")") {
|
|
365
|
+
if (depth === 0) {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
depth -= 1;
|
|
369
|
+
}
|
|
370
|
+
index += 1;
|
|
371
|
+
}
|
|
372
|
+
return covered && depth === 0;
|
|
373
|
+
}
|
|
374
|
+
|
|
150
375
|
// ../connector-core/src/shipper.ts
|
|
151
376
|
var DEFAULT_SHIP_TIMEOUT_MS = 5e3;
|
|
152
377
|
var droppedEventCount = 0;
|
|
@@ -163,6 +388,7 @@ var SELF_HOSTED_CONNECTOR_ASSERTION = "nextjs-self-hosted";
|
|
|
163
388
|
var ENVELOPE_INGEST_PATHNAME = "/v1/connectors/nextjs/events";
|
|
164
389
|
var COLLECTOR_HEADER_PREFIX = "x-steadphast-collector-";
|
|
165
390
|
var COLLECTOR_CONTEXT_HEADER = `${COLLECTOR_HEADER_PREFIX}context`;
|
|
391
|
+
var BEHAVIORAL_CONTEXT_HEADER = `${COLLECTOR_HEADER_PREFIX}behavioral`;
|
|
166
392
|
var LEGACY_CANONICAL_INGEST_PATHNAME = "/v1/events";
|
|
167
393
|
var DiscoverConfigError = class extends TypeError {
|
|
168
394
|
name = "DiscoverConfigError";
|
|
@@ -306,19 +532,24 @@ function stripCollectorHeaders(headers) {
|
|
|
306
532
|
for (const name of names) headers.delete(name);
|
|
307
533
|
return true;
|
|
308
534
|
}
|
|
309
|
-
function forwardedRequestHeaders(request, context) {
|
|
535
|
+
function forwardedRequestHeaders(request, context, behavioralContext) {
|
|
310
536
|
const headers = new Headers(request.headers);
|
|
311
537
|
stripCollectorHeaders(headers);
|
|
312
538
|
if (context !== void 0) headers.set(COLLECTOR_CONTEXT_HEADER, context);
|
|
539
|
+
if (behavioralContext !== void 0)
|
|
540
|
+
headers.set(BEHAVIORAL_CONTEXT_HEADER, behavioralContext);
|
|
313
541
|
return headers;
|
|
314
542
|
}
|
|
315
543
|
function applyForwardedHeaders(result, forwarded) {
|
|
316
|
-
|
|
317
|
-
|
|
544
|
+
const response = result ?? NextResponse.next({ request: { headers: forwarded } });
|
|
545
|
+
const headers = response.headers;
|
|
546
|
+
if (forwarded.has(BEHAVIORAL_CONTEXT_HEADER)) {
|
|
547
|
+
headers.set("cache-control", "private, no-store, max-age=0");
|
|
548
|
+
headers.set("cdn-cache-control", "no-store");
|
|
549
|
+
headers.set("vercel-cdn-cache-control", "no-store");
|
|
318
550
|
}
|
|
319
|
-
const headers = result.headers;
|
|
320
551
|
if (!headers.has(NEXT_HEADER) && !headers.has(REWRITE_HEADER)) {
|
|
321
|
-
return
|
|
552
|
+
return response;
|
|
322
553
|
}
|
|
323
554
|
const override = headers.get(OVERRIDE_HEADERS);
|
|
324
555
|
if (override === null) {
|
|
@@ -328,7 +559,7 @@ function applyForwardedHeaders(result, forwarded) {
|
|
|
328
559
|
names2.push(name);
|
|
329
560
|
}
|
|
330
561
|
headers.set(OVERRIDE_HEADERS, names2.join(","));
|
|
331
|
-
return
|
|
562
|
+
return response;
|
|
332
563
|
}
|
|
333
564
|
const names = override.split(",").map((name) => name.trim()).filter((name) => {
|
|
334
565
|
if (!name.toLowerCase().startsWith(COLLECTOR_HEADER_PREFIX)) return true;
|
|
@@ -340,8 +571,13 @@ function applyForwardedHeaders(result, forwarded) {
|
|
|
340
571
|
headers.set(`${OVERRIDE_PREFIX}${COLLECTOR_CONTEXT_HEADER}`, context);
|
|
341
572
|
names.push(COLLECTOR_CONTEXT_HEADER);
|
|
342
573
|
}
|
|
574
|
+
const behavioral = forwarded.get(BEHAVIORAL_CONTEXT_HEADER);
|
|
575
|
+
if (behavioral !== null) {
|
|
576
|
+
headers.set(`${OVERRIDE_PREFIX}${BEHAVIORAL_CONTEXT_HEADER}`, behavioral);
|
|
577
|
+
names.push(BEHAVIORAL_CONTEXT_HEADER);
|
|
578
|
+
}
|
|
343
579
|
headers.set(OVERRIDE_HEADERS, names.join(","));
|
|
344
|
-
return
|
|
580
|
+
return response;
|
|
345
581
|
}
|
|
346
582
|
|
|
347
583
|
// src/delivery.ts
|
|
@@ -449,7 +685,12 @@ function prepareEvidenceEnvelope(request, options, capturedAt = /* @__PURE__ */
|
|
|
449
685
|
if (method.length > MAX_METHOD_BYTES || !HTTP_METHOD_PATTERN.test(method)) {
|
|
450
686
|
return void 0;
|
|
451
687
|
}
|
|
452
|
-
const
|
|
688
|
+
const signed = request.headers.get(SIGNATURE_HEADER) !== null;
|
|
689
|
+
const signatureInputValue = request.headers.get(SIGNATURE_INPUT_HEADER);
|
|
690
|
+
const externalUrl = resolveExternalUrl(request.url, deployment, {
|
|
691
|
+
rawPath: signed && signatureInputCoversPath(signatureInputValue),
|
|
692
|
+
rawQuery: signed && signatureInputCoversQuery(signatureInputValue)
|
|
693
|
+
});
|
|
453
694
|
if (externalUrl === void 0) {
|
|
454
695
|
return void 0;
|
|
455
696
|
}
|
|
@@ -505,7 +746,7 @@ function prepareEvidenceEnvelope(request, options, capturedAt = /* @__PURE__ */
|
|
|
505
746
|
recordId: platform.request_id === void 0 ? void 0 : `${recordIdPrefix}:${platform.request_id}`
|
|
506
747
|
};
|
|
507
748
|
}
|
|
508
|
-
function resolveExternalUrl(runtimeUrl, deployment) {
|
|
749
|
+
function resolveExternalUrl(runtimeUrl, deployment, keep) {
|
|
509
750
|
let url;
|
|
510
751
|
try {
|
|
511
752
|
url = new URL(runtimeUrl);
|
|
@@ -522,7 +763,7 @@ function resolveExternalUrl(runtimeUrl, deployment) {
|
|
|
522
763
|
if (url.protocol !== "https:" || url.username.length > 0 || url.password.length > 0 || url.hash.length > 0 || utf8Length(url.host) > MAX_AUTHORITY_BYTES) {
|
|
523
764
|
return void 0;
|
|
524
765
|
}
|
|
525
|
-
const serialized = url.toString()
|
|
766
|
+
const serialized = keep.rawQuery ? url.toString() : `${url.origin}${keep.rawPath ? url.pathname : redactPath(url.pathname)}${redactQuery(url.search)}`;
|
|
526
767
|
return utf8Length(serialized) > MAX_EXTERNAL_URL_BYTES ? void 0 : serialized;
|
|
527
768
|
}
|
|
528
769
|
function captureHeaders(headers, deployment, excludedHeaders) {
|
|
@@ -692,6 +933,9 @@ var API_KEY_VERIFIER_DOMAIN = "steadphast-discover:api-key:v1";
|
|
|
692
933
|
var NONCE_KEY_SALT = "steadphast-discover/client-nonce/v1/salt";
|
|
693
934
|
var NONCE_KEY_INFO = "steadphast-discover/client-nonce/v1";
|
|
694
935
|
var RENDER_CONTEXT_DOMAIN = "steadphast-discover/collector-render/v1";
|
|
936
|
+
var T1_KEY_SALT = "steadphast-discover/client-t1/v1/salt";
|
|
937
|
+
var T1_KEY_INFO = "steadphast-discover/client-t1/v1";
|
|
938
|
+
var T1_RENDER_DOMAIN = "steadphast-discover/behavioral-render/v1";
|
|
695
939
|
var MAX_RID_BYTES = 512;
|
|
696
940
|
var MAX_KEY_ID_BYTES = 64;
|
|
697
941
|
var TAG_BYTES = 32;
|
|
@@ -715,7 +959,72 @@ function createNonceMinter(apiKey) {
|
|
|
715
959
|
}
|
|
716
960
|
const keyIdBytes = utf8(parsed.keyId);
|
|
717
961
|
let nonceKey;
|
|
962
|
+
let t1Key;
|
|
718
963
|
return {
|
|
964
|
+
async mintBehavioralContext(context) {
|
|
965
|
+
t1Key ??= deriveNonceKey(parsed, keyIdBytes, T1_KEY_SALT, T1_KEY_INFO);
|
|
966
|
+
const header = utf8(JSON.stringify(context));
|
|
967
|
+
const tag = new Uint8Array(
|
|
968
|
+
await crypto.subtle.sign(
|
|
969
|
+
"HMAC",
|
|
970
|
+
await t1Key,
|
|
971
|
+
concat(utf8(T1_RENDER_DOMAIN), header)
|
|
972
|
+
)
|
|
973
|
+
);
|
|
974
|
+
const wire = bytes(header.byteLength + TAG_BYTES);
|
|
975
|
+
wire.set(header);
|
|
976
|
+
wire.set(tag, header.byteLength);
|
|
977
|
+
return base64UrlNoPad(wire);
|
|
978
|
+
},
|
|
979
|
+
async behavioralFromContext(context, policy) {
|
|
980
|
+
if (context.length > 2048 || policy.mode !== "own") return void 0;
|
|
981
|
+
try {
|
|
982
|
+
const binary = atob(context.replace(/-/g, "+").replace(/_/g, "/"));
|
|
983
|
+
const wire = bytes(binary.length);
|
|
984
|
+
for (let i = 0; i < binary.length; i++) wire[i] = binary.charCodeAt(i);
|
|
985
|
+
if (wire.length <= TAG_BYTES) return void 0;
|
|
986
|
+
const header = wire.subarray(0, -TAG_BYTES);
|
|
987
|
+
t1Key ??= deriveNonceKey(parsed, keyIdBytes, T1_KEY_SALT, T1_KEY_INFO);
|
|
988
|
+
if (!await crypto.subtle.verify(
|
|
989
|
+
"HMAC",
|
|
990
|
+
await t1Key,
|
|
991
|
+
wire.subarray(-TAG_BYTES),
|
|
992
|
+
concat(utf8(T1_RENDER_DOMAIN), header)
|
|
993
|
+
))
|
|
994
|
+
return void 0;
|
|
995
|
+
const input = JSON.parse(
|
|
996
|
+
new TextDecoder().decode(header)
|
|
997
|
+
);
|
|
998
|
+
if (Date.now() - input.issued_at_ms > 6e5 || input.issued_at_ms > Date.now() + 3e4)
|
|
999
|
+
return void 0;
|
|
1000
|
+
const binding = utf8(
|
|
1001
|
+
JSON.stringify({
|
|
1002
|
+
version: 1,
|
|
1003
|
+
key_id: parsed.keyId,
|
|
1004
|
+
source: input.source,
|
|
1005
|
+
rid: input.rid,
|
|
1006
|
+
site: input.site,
|
|
1007
|
+
issued_at_ms: input.issued_at_ms,
|
|
1008
|
+
expires_at_ms: input.issued_at_ms + 6e5,
|
|
1009
|
+
revision: policy.revision,
|
|
1010
|
+
timing_enabled: policy.timing_enabled
|
|
1011
|
+
})
|
|
1012
|
+
);
|
|
1013
|
+
const tag = new Uint8Array(
|
|
1014
|
+
await crypto.subtle.sign("HMAC", await t1Key, binding)
|
|
1015
|
+
);
|
|
1016
|
+
const token = bytes(binding.byteLength + TAG_BYTES);
|
|
1017
|
+
token.set(binding);
|
|
1018
|
+
token.set(tag, binding.byteLength);
|
|
1019
|
+
return {
|
|
1020
|
+
nonce: base64UrlNoPad(token),
|
|
1021
|
+
path: input.path,
|
|
1022
|
+
issuedAt: input.issued_at_ms
|
|
1023
|
+
};
|
|
1024
|
+
} catch {
|
|
1025
|
+
return void 0;
|
|
1026
|
+
}
|
|
1027
|
+
},
|
|
719
1028
|
async mintRenderContext(rid, servedAtMs) {
|
|
720
1029
|
const header = encodeHeader(rid, servedAtMs, keyIdBytes);
|
|
721
1030
|
if (header === void 0) return void 0;
|
|
@@ -786,7 +1095,7 @@ function encodeHeader(rid, servedAtMs, keyIdBytes) {
|
|
|
786
1095
|
header.set(keyIdBytes, offset);
|
|
787
1096
|
return header;
|
|
788
1097
|
}
|
|
789
|
-
async function deriveNonceKey(parsed, keyIdBytes) {
|
|
1098
|
+
async function deriveNonceKey(parsed, keyIdBytes, salt = NONCE_KEY_SALT, info = NONCE_KEY_INFO) {
|
|
790
1099
|
const secretKey = await crypto.subtle.importKey(
|
|
791
1100
|
"raw",
|
|
792
1101
|
utf8(parsed.secret),
|
|
@@ -806,8 +1115,8 @@ async function deriveNonceKey(parsed, keyIdBytes) {
|
|
|
806
1115
|
{
|
|
807
1116
|
name: "HKDF",
|
|
808
1117
|
hash: "SHA-256",
|
|
809
|
-
salt: utf8(
|
|
810
|
-
info: concat(utf8(
|
|
1118
|
+
salt: utf8(salt),
|
|
1119
|
+
info: concat(utf8(info), keyIdBytes)
|
|
811
1120
|
},
|
|
812
1121
|
hkdfKey,
|
|
813
1122
|
TAG_BYTES * 8
|
|
@@ -874,7 +1183,7 @@ function buildMiddleware(config, ingestUrl, handler) {
|
|
|
874
1183
|
console.warn(missingApiKeyDiagnostic(settings.apiKeyEnvVar));
|
|
875
1184
|
didWarnMissingApiKey = true;
|
|
876
1185
|
}
|
|
877
|
-
} else if (settings.collector) {
|
|
1186
|
+
} else if (settings.collector || settings.behavioralRoutes.length > 0) {
|
|
878
1187
|
return captureWithCollector(
|
|
879
1188
|
settings,
|
|
880
1189
|
minters.for(apiKey),
|
|
@@ -902,6 +1211,7 @@ function snapshotSettings(config, ingestUrl) {
|
|
|
902
1211
|
apiKeyEnvVar: config.apiKeyEnvVar,
|
|
903
1212
|
captureAllPaths: config.captureAllPaths === true,
|
|
904
1213
|
collector: config.collector === true,
|
|
1214
|
+
behavioralRoutes: config.collector === false || (config.behavioral?.mode ?? "own") !== "own" ? [] : [...config.behavioral?.routes ?? []],
|
|
905
1215
|
connectorAssertion: connectorAssertionFor(deployment),
|
|
906
1216
|
endpoint: ingestUrl,
|
|
907
1217
|
envelopeOptions: {
|
|
@@ -938,9 +1248,22 @@ function ship2(settings, apiKey, prepared, servedAtMs, event) {
|
|
|
938
1248
|
}
|
|
939
1249
|
async function captureWithCollector(settings, minter, apiKey, request, event, handler, scrubbed) {
|
|
940
1250
|
const prepared = prepareEvidenceEnvelope(request, settings.envelopeOptions);
|
|
941
|
-
const
|
|
1251
|
+
const path = new URL(request.url).pathname;
|
|
1252
|
+
const behavioral = settings.behavioralRoutes.includes(path) && !path.split("/").some(
|
|
1253
|
+
(part) => /^(auth|login|signin|signup|password|checkout|payment|billing|health|medical)$/i.test(
|
|
1254
|
+
part
|
|
1255
|
+
)
|
|
1256
|
+
);
|
|
1257
|
+
const armed = prepared?.recordId !== void 0 && minter !== void 0 && isDocumentNavigation(request) && (settings.collector || behavioral);
|
|
942
1258
|
const servedAt = armed ? Date.now() : void 0;
|
|
943
1259
|
const context = armed && prepared?.recordId !== void 0 && servedAt !== void 0 ? await minter.mintRenderContext(prepared.recordId, servedAt) : void 0;
|
|
1260
|
+
const behavioralContext = armed && behavioral && minter && prepared?.recordId && servedAt !== void 0 ? await minter.mintBehavioralContext({
|
|
1261
|
+
rid: prepared.recordId,
|
|
1262
|
+
site: new URL(prepared.envelope.request.external_url).host,
|
|
1263
|
+
path,
|
|
1264
|
+
source: settings.connectorAssertion,
|
|
1265
|
+
issued_at_ms: servedAt
|
|
1266
|
+
}) : void 0;
|
|
944
1267
|
if (context === void 0) {
|
|
945
1268
|
ship2(settings, apiKey, prepared, void 0, event);
|
|
946
1269
|
return scrubbed ? delegateWithHeaders(
|
|
@@ -950,12 +1273,18 @@ async function captureWithCollector(settings, minter, apiKey, request, event, ha
|
|
|
950
1273
|
forwardedRequestHeaders(request, void 0)
|
|
951
1274
|
) : delegate(handler, request, event);
|
|
952
1275
|
}
|
|
953
|
-
ship2(
|
|
1276
|
+
ship2(
|
|
1277
|
+
settings,
|
|
1278
|
+
apiKey,
|
|
1279
|
+
prepared,
|
|
1280
|
+
settings.collector ? servedAt : void 0,
|
|
1281
|
+
event
|
|
1282
|
+
);
|
|
954
1283
|
return delegateWithHeaders(
|
|
955
1284
|
handler,
|
|
956
1285
|
request,
|
|
957
1286
|
event,
|
|
958
|
-
forwardedRequestHeaders(request, context)
|
|
1287
|
+
forwardedRequestHeaders(request, context, behavioralContext)
|
|
959
1288
|
);
|
|
960
1289
|
}
|
|
961
1290
|
function delegate(handler, request, event) {
|
|
@@ -966,7 +1295,7 @@ function delegate(handler, request, event) {
|
|
|
966
1295
|
}
|
|
967
1296
|
async function delegateWithHeaders(handler, request, event, forwarded) {
|
|
968
1297
|
if (handler === void 0) {
|
|
969
|
-
return
|
|
1298
|
+
return applyForwardedHeaders(void 0, forwarded);
|
|
970
1299
|
}
|
|
971
1300
|
const forwardedRequest = new request.constructor(
|
|
972
1301
|
request,
|