@steadphast/connector-nextjs 0.5.3 → 0.6.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 +337 -25
- package/dist/config.d.ts +16 -0
- package/dist/index.js +444 -127
- package/dist/server.d.ts +14 -0
- package/dist/server.js +293 -0
- package/package.json +20 -9
package/dist/index.js
CHANGED
|
@@ -1,114 +1,3 @@
|
|
|
1
|
-
// src/config.ts
|
|
2
|
-
var SELF_HOSTED_CLIENT_IP_HEADER = "x-steadphast-client-ip";
|
|
3
|
-
var SELF_HOSTED_REQUEST_ID_HEADER = "x-steadphast-request-id";
|
|
4
|
-
var MAX_SELF_HOSTED_CLIENT_IP_LENGTH = 45;
|
|
5
|
-
var VERCEL_CONNECTOR_ASSERTION = "vercel-routing-middleware";
|
|
6
|
-
var SELF_HOSTED_CONNECTOR_ASSERTION = "nextjs-self-hosted";
|
|
7
|
-
var ENVELOPE_INGEST_PATHNAME = "/v1/connectors/nextjs/events";
|
|
8
|
-
var LEGACY_CANONICAL_INGEST_PATHNAME = "/v1/events";
|
|
9
|
-
var DiscoverConfigError = class extends TypeError {
|
|
10
|
-
name = "DiscoverConfigError";
|
|
11
|
-
};
|
|
12
|
-
function validateApiKeyEnvVar(apiKeyEnvVar) {
|
|
13
|
-
if (apiKeyEnvVar?.startsWith("NEXT_PUBLIC_")) {
|
|
14
|
-
throw new DiscoverConfigError(
|
|
15
|
-
"Steadphast connector apiKeyEnvVar must reference a server-only environment variable."
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
function resolveProductionIngestUrl(apiUrl) {
|
|
20
|
-
const url = parseStrictUrl(apiUrl, "apiUrl");
|
|
21
|
-
if (url.protocol !== "https:") {
|
|
22
|
-
throw new DiscoverConfigError(
|
|
23
|
-
"Steadphast connector apiUrl must use the https scheme."
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
if (url.pathname === LEGACY_CANONICAL_INGEST_PATHNAME) {
|
|
27
|
-
throw new DiscoverConfigError(
|
|
28
|
-
`Steadphast connector apiUrl must not target the canonical-event ingest route; use the Discover API origin or the exact ${ENVELOPE_INGEST_PATHNAME} envelope ingest URL.`
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
if (url.pathname !== "/" && url.pathname !== ENVELOPE_INGEST_PATHNAME) {
|
|
32
|
-
throw new DiscoverConfigError(
|
|
33
|
-
`Steadphast connector apiUrl must be the Discover API origin or the exact ${ENVELOPE_INGEST_PATHNAME} envelope ingest URL.`
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
return `${url.origin}${ENVELOPE_INGEST_PATHNAME}`;
|
|
37
|
-
}
|
|
38
|
-
function resolvePublicOrigin(publicOrigin) {
|
|
39
|
-
const url = parseStrictUrl(publicOrigin, "publicOrigin");
|
|
40
|
-
if (url.protocol !== "https:") {
|
|
41
|
-
throw new DiscoverConfigError(
|
|
42
|
-
"Steadphast connector publicOrigin must use the https scheme."
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
if (url.pathname !== "/") {
|
|
46
|
-
throw new DiscoverConfigError(
|
|
47
|
-
"Steadphast connector publicOrigin must be an origin without a path."
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
return url.origin;
|
|
51
|
-
}
|
|
52
|
-
function parseStrictUrl(value, setting) {
|
|
53
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
54
|
-
throw new DiscoverConfigError(
|
|
55
|
-
`Steadphast connector ${setting} must be a non-empty absolute URL string.`
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
if (value.includes("?") || value.includes("#")) {
|
|
59
|
-
throw new DiscoverConfigError(
|
|
60
|
-
`Steadphast connector ${setting} must not contain a query or fragment.`
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
if (value.includes("@")) {
|
|
64
|
-
throw new DiscoverConfigError(
|
|
65
|
-
`Steadphast connector ${setting} must not contain userinfo.`
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
let url;
|
|
69
|
-
try {
|
|
70
|
-
url = new URL(value);
|
|
71
|
-
} catch {
|
|
72
|
-
throw new DiscoverConfigError(
|
|
73
|
-
`Steadphast connector ${setting} must be an absolute URL.`
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
if (url.hostname.length === 0) {
|
|
77
|
-
throw new DiscoverConfigError(
|
|
78
|
-
`Steadphast connector ${setting} must contain a host.`
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
if (url.username.length > 0 || url.password.length > 0) {
|
|
82
|
-
throw new DiscoverConfigError(
|
|
83
|
-
`Steadphast connector ${setting} must not contain userinfo.`
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
return url;
|
|
87
|
-
}
|
|
88
|
-
function connectorAssertionFor(mode) {
|
|
89
|
-
return mode.kind === "vercel" ? VERCEL_CONNECTOR_ASSERTION : SELF_HOSTED_CONNECTOR_ASSERTION;
|
|
90
|
-
}
|
|
91
|
-
function resolveDeploymentMode(mode) {
|
|
92
|
-
if (typeof mode !== "object" || mode === null) {
|
|
93
|
-
throw new DiscoverConfigError(
|
|
94
|
-
'Steadphast connector mode must be { kind: "vercel" } or { kind: "self-hosted", publicOrigin }.'
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
switch (mode.kind) {
|
|
98
|
-
case "vercel":
|
|
99
|
-
return { kind: "vercel" };
|
|
100
|
-
case "self-hosted":
|
|
101
|
-
return {
|
|
102
|
-
kind: "self-hosted",
|
|
103
|
-
publicOrigin: resolvePublicOrigin(mode.publicOrigin)
|
|
104
|
-
};
|
|
105
|
-
default:
|
|
106
|
-
throw new DiscoverConfigError(
|
|
107
|
-
'Steadphast connector mode.kind must be "vercel" or "self-hosted".'
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
1
|
// ../connector-core/src/assets.ts
|
|
113
2
|
var ALWAYS_TRACK_PATHS = /* @__PURE__ */ new Set(["/robots.txt", "/sitemap.xml"]);
|
|
114
3
|
var STATIC_ASSET_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
@@ -182,6 +71,7 @@ var EXCLUDED_HEADER_IDENTITIES = /* @__PURE__ */ new Set([
|
|
|
182
71
|
"authorization",
|
|
183
72
|
"xauthorization",
|
|
184
73
|
"proxyauthorization",
|
|
74
|
+
"paymentauthorization",
|
|
185
75
|
"apikey",
|
|
186
76
|
"xapikey",
|
|
187
77
|
"apitoken",
|
|
@@ -208,21 +98,38 @@ var EXCLUDED_HEADER_IDENTITIES = /* @__PURE__ */ new Set([
|
|
|
208
98
|
"xcsrftoken",
|
|
209
99
|
"agenticpaymentcontainer",
|
|
210
100
|
"xvercelprotectionbypass",
|
|
101
|
+
// Vercel-minted request credentials (issue #705): project OIDC JWT and proxy signature.
|
|
102
|
+
"xverceloidctoken",
|
|
103
|
+
"xvercelproxysignature",
|
|
104
|
+
// Visitor location headers (x-vercel-ip-city, -latitude, -longitude, -postal-code, -country,
|
|
105
|
+
// -country-region) are deliberately not excluded: they are captured as evidence (issue #705).
|
|
211
106
|
"token",
|
|
212
107
|
"key"
|
|
213
108
|
]);
|
|
214
109
|
var EXCLUDED_HEADER_SUBSTRINGS = ["cryptogram", "networktoken"];
|
|
110
|
+
var PAYMENT_CREDENTIAL_MARKER_HEADER = "x-steadphast-payment-credential";
|
|
111
|
+
var PAYMENT_AUTHORIZATION_MARKER_VALUE = "payment-authorization";
|
|
215
112
|
function captureRequestHeaders(headers, options) {
|
|
216
113
|
const captured = /* @__PURE__ */ Object.create(null);
|
|
217
114
|
const additionalExclusions = new Set(
|
|
218
115
|
options?.excludedHeaders?.map((name) => name.trim().toLowerCase()) ?? []
|
|
219
116
|
);
|
|
117
|
+
let paymentCredential = false;
|
|
220
118
|
headers.forEach((value, name) => {
|
|
221
119
|
const lowerName = name.toLowerCase();
|
|
120
|
+
if (lowerName === PAYMENT_CREDENTIAL_MARKER_HEADER) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (collapseHeaderName(lowerName) === "paymentauthorization") {
|
|
124
|
+
paymentCredential = true;
|
|
125
|
+
}
|
|
222
126
|
if (!isExcludedHeader(lowerName) && !additionalExclusions.has(lowerName)) {
|
|
223
127
|
captured[lowerName] = value;
|
|
224
128
|
}
|
|
225
129
|
});
|
|
130
|
+
if (paymentCredential && !additionalExclusions.has(PAYMENT_CREDENTIAL_MARKER_HEADER)) {
|
|
131
|
+
captured[PAYMENT_CREDENTIAL_MARKER_HEADER] = PAYMENT_AUTHORIZATION_MARKER_VALUE;
|
|
132
|
+
}
|
|
226
133
|
return captured;
|
|
227
134
|
}
|
|
228
135
|
function parseForwardedChain(headerValue) {
|
|
@@ -247,8 +154,195 @@ function recordDroppedEvents(count) {
|
|
|
247
154
|
droppedEventCount += count;
|
|
248
155
|
}
|
|
249
156
|
|
|
157
|
+
// src/config.ts
|
|
158
|
+
var SELF_HOSTED_CLIENT_IP_HEADER = "x-steadphast-client-ip";
|
|
159
|
+
var SELF_HOSTED_REQUEST_ID_HEADER = "x-steadphast-request-id";
|
|
160
|
+
var MAX_SELF_HOSTED_CLIENT_IP_LENGTH = 45;
|
|
161
|
+
var VERCEL_CONNECTOR_ASSERTION = "vercel-routing-middleware";
|
|
162
|
+
var SELF_HOSTED_CONNECTOR_ASSERTION = "nextjs-self-hosted";
|
|
163
|
+
var ENVELOPE_INGEST_PATHNAME = "/v1/connectors/nextjs/events";
|
|
164
|
+
var COLLECTOR_HEADER_PREFIX = "x-steadphast-collector-";
|
|
165
|
+
var COLLECTOR_CONTEXT_HEADER = `${COLLECTOR_HEADER_PREFIX}context`;
|
|
166
|
+
var LEGACY_CANONICAL_INGEST_PATHNAME = "/v1/events";
|
|
167
|
+
var DiscoverConfigError = class extends TypeError {
|
|
168
|
+
name = "DiscoverConfigError";
|
|
169
|
+
};
|
|
170
|
+
function validateApiKeyEnvVar(apiKeyEnvVar) {
|
|
171
|
+
if (apiKeyEnvVar?.startsWith("NEXT_PUBLIC_")) {
|
|
172
|
+
throw new DiscoverConfigError(
|
|
173
|
+
"Steadphast connector apiKeyEnvVar must reference a server-only environment variable."
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function resolveProductionIngestUrl(apiUrl) {
|
|
178
|
+
const url = parseStrictUrl(apiUrl, "apiUrl");
|
|
179
|
+
if (url.protocol !== "https:") {
|
|
180
|
+
throw new DiscoverConfigError(
|
|
181
|
+
"Steadphast connector apiUrl must use the https scheme."
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
if (url.pathname === LEGACY_CANONICAL_INGEST_PATHNAME) {
|
|
185
|
+
throw new DiscoverConfigError(
|
|
186
|
+
`Steadphast connector apiUrl must not target the canonical-event ingest route; use the Discover API origin or the exact ${ENVELOPE_INGEST_PATHNAME} envelope ingest URL.`
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
if (url.pathname !== "/" && url.pathname !== ENVELOPE_INGEST_PATHNAME) {
|
|
190
|
+
throw new DiscoverConfigError(
|
|
191
|
+
`Steadphast connector apiUrl must be the Discover API origin or the exact ${ENVELOPE_INGEST_PATHNAME} envelope ingest URL.`
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
return `${url.origin}${ENVELOPE_INGEST_PATHNAME}`;
|
|
195
|
+
}
|
|
196
|
+
function resolvePublicOrigin(publicOrigin) {
|
|
197
|
+
const url = parseStrictUrl(publicOrigin, "publicOrigin");
|
|
198
|
+
if (url.protocol !== "https:") {
|
|
199
|
+
throw new DiscoverConfigError(
|
|
200
|
+
"Steadphast connector publicOrigin must use the https scheme."
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
if (url.pathname !== "/") {
|
|
204
|
+
throw new DiscoverConfigError(
|
|
205
|
+
"Steadphast connector publicOrigin must be an origin without a path."
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return url.origin;
|
|
209
|
+
}
|
|
210
|
+
function parseStrictUrl(value, setting) {
|
|
211
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
212
|
+
throw new DiscoverConfigError(
|
|
213
|
+
`Steadphast connector ${setting} must be a non-empty absolute URL string.`
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (value.includes("?") || value.includes("#")) {
|
|
217
|
+
throw new DiscoverConfigError(
|
|
218
|
+
`Steadphast connector ${setting} must not contain a query or fragment.`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
if (value.includes("@")) {
|
|
222
|
+
throw new DiscoverConfigError(
|
|
223
|
+
`Steadphast connector ${setting} must not contain userinfo.`
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
let url;
|
|
227
|
+
try {
|
|
228
|
+
url = new URL(value);
|
|
229
|
+
} catch {
|
|
230
|
+
throw new DiscoverConfigError(
|
|
231
|
+
`Steadphast connector ${setting} must be an absolute URL.`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
if (url.hostname.length === 0) {
|
|
235
|
+
throw new DiscoverConfigError(
|
|
236
|
+
`Steadphast connector ${setting} must contain a host.`
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
if (url.username.length > 0 || url.password.length > 0) {
|
|
240
|
+
throw new DiscoverConfigError(
|
|
241
|
+
`Steadphast connector ${setting} must not contain userinfo.`
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
return url;
|
|
245
|
+
}
|
|
246
|
+
function connectorAssertionFor(mode) {
|
|
247
|
+
return mode.kind === "vercel" ? VERCEL_CONNECTOR_ASSERTION : SELF_HOSTED_CONNECTOR_ASSERTION;
|
|
248
|
+
}
|
|
249
|
+
function resolveDeploymentMode(mode) {
|
|
250
|
+
if (typeof mode !== "object" || mode === null) {
|
|
251
|
+
throw new DiscoverConfigError(
|
|
252
|
+
'Steadphast connector mode must be { kind: "vercel" } or { kind: "self-hosted", publicOrigin }.'
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
switch (mode.kind) {
|
|
256
|
+
case "vercel":
|
|
257
|
+
return { kind: "vercel" };
|
|
258
|
+
case "self-hosted":
|
|
259
|
+
return {
|
|
260
|
+
kind: "self-hosted",
|
|
261
|
+
publicOrigin: resolvePublicOrigin(mode.publicOrigin)
|
|
262
|
+
};
|
|
263
|
+
default:
|
|
264
|
+
throw new DiscoverConfigError(
|
|
265
|
+
'Steadphast connector mode.kind must be "vercel" or "self-hosted".'
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function resolveApiKey(customEnvVar) {
|
|
270
|
+
const runtime = globalThis;
|
|
271
|
+
const env = runtime.process?.env;
|
|
272
|
+
return customEnvVar !== void 0 ? env?.[customEnvVar] : resolveApiEnvironmentValue(
|
|
273
|
+
env?.[STEADPHAST_API_KEY_ENV_VAR],
|
|
274
|
+
env?.[LEGACY_DISCOVER_API_KEY_ENV_VAR]
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
250
278
|
// src/middleware.ts
|
|
279
|
+
import { NextResponse as NextResponse2 } from "next/server";
|
|
280
|
+
|
|
281
|
+
// src/collector-serve.ts
|
|
251
282
|
import { NextResponse } from "next/server";
|
|
283
|
+
var NEXT_HEADER = "x-middleware-next";
|
|
284
|
+
var REWRITE_HEADER = "x-middleware-rewrite";
|
|
285
|
+
var OVERRIDE_HEADERS = "x-middleware-override-headers";
|
|
286
|
+
var OVERRIDE_PREFIX = "x-middleware-request-";
|
|
287
|
+
function isDocumentNavigation(request) {
|
|
288
|
+
if (request.method !== "GET") {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
const destination = request.headers.get("sec-fetch-dest");
|
|
292
|
+
if (destination !== null) {
|
|
293
|
+
return destination === "document";
|
|
294
|
+
}
|
|
295
|
+
return (request.headers.get("accept") ?? "").includes("text/html");
|
|
296
|
+
}
|
|
297
|
+
function stripCollectorHeaders(headers) {
|
|
298
|
+
let names;
|
|
299
|
+
for (const name of headers.keys()) {
|
|
300
|
+
if (name.startsWith(COLLECTOR_HEADER_PREFIX)) {
|
|
301
|
+
names ??= [];
|
|
302
|
+
names.push(name);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (!names) return false;
|
|
306
|
+
for (const name of names) headers.delete(name);
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
function forwardedRequestHeaders(request, context) {
|
|
310
|
+
const headers = new Headers(request.headers);
|
|
311
|
+
stripCollectorHeaders(headers);
|
|
312
|
+
if (context !== void 0) headers.set(COLLECTOR_CONTEXT_HEADER, context);
|
|
313
|
+
return headers;
|
|
314
|
+
}
|
|
315
|
+
function applyForwardedHeaders(result, forwarded) {
|
|
316
|
+
if (result === void 0 || result === null) {
|
|
317
|
+
return NextResponse.next({ request: { headers: forwarded } });
|
|
318
|
+
}
|
|
319
|
+
const headers = result.headers;
|
|
320
|
+
if (!headers.has(NEXT_HEADER) && !headers.has(REWRITE_HEADER)) {
|
|
321
|
+
return result;
|
|
322
|
+
}
|
|
323
|
+
const override = headers.get(OVERRIDE_HEADERS);
|
|
324
|
+
if (override === null) {
|
|
325
|
+
const names2 = [];
|
|
326
|
+
for (const [name, value] of forwarded) {
|
|
327
|
+
headers.set(`${OVERRIDE_PREFIX}${name}`, value);
|
|
328
|
+
names2.push(name);
|
|
329
|
+
}
|
|
330
|
+
headers.set(OVERRIDE_HEADERS, names2.join(","));
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
const names = override.split(",").map((name) => name.trim()).filter((name) => {
|
|
334
|
+
if (!name.toLowerCase().startsWith(COLLECTOR_HEADER_PREFIX)) return true;
|
|
335
|
+
headers.delete(`${OVERRIDE_PREFIX}${name}`);
|
|
336
|
+
return false;
|
|
337
|
+
});
|
|
338
|
+
const context = forwarded.get(COLLECTOR_CONTEXT_HEADER);
|
|
339
|
+
if (context !== null) {
|
|
340
|
+
headers.set(`${OVERRIDE_PREFIX}${COLLECTOR_CONTEXT_HEADER}`, context);
|
|
341
|
+
names.push(COLLECTOR_CONTEXT_HEADER);
|
|
342
|
+
}
|
|
343
|
+
headers.set(OVERRIDE_HEADERS, names.join(","));
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
252
346
|
|
|
253
347
|
// src/delivery.ts
|
|
254
348
|
var ACCEPTED_STATUS = 202;
|
|
@@ -335,7 +429,13 @@ var HTTP_METHOD_PATTERN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
|
|
|
335
429
|
var SELF_HOSTED_REQUEST_ID_PATTERN = /^[A-Za-z0-9._~-]{1,128}$/;
|
|
336
430
|
var IPV4_PATTERN = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
|
|
337
431
|
var IPV6_CHARSET_PATTERN = /^[0-9A-Fa-f:.]+$/;
|
|
338
|
-
function
|
|
432
|
+
function finalizeEvidenceEnvelope(envelope, servedAtMs) {
|
|
433
|
+
const body = JSON.stringify(
|
|
434
|
+
servedAtMs === void 0 ? envelope : { ...envelope, collector_served: true, served_at: servedAtMs }
|
|
435
|
+
);
|
|
436
|
+
return utf8Length(body) > MAX_ENVELOPE_BYTES ? void 0 : body;
|
|
437
|
+
}
|
|
438
|
+
function prepareEvidenceEnvelope(request, options, capturedAt = /* @__PURE__ */ new Date()) {
|
|
339
439
|
const deployment = options.deployment;
|
|
340
440
|
const capturedAtIso = formatCapturedAt(capturedAt);
|
|
341
441
|
if (capturedAtIso === void 0) {
|
|
@@ -400,8 +500,10 @@ function serializeEvidenceEnvelope(request, options, capturedAt = /* @__PURE__ *
|
|
|
400
500
|
},
|
|
401
501
|
platform
|
|
402
502
|
};
|
|
403
|
-
|
|
404
|
-
|
|
503
|
+
return {
|
|
504
|
+
envelope,
|
|
505
|
+
recordId: platform.request_id === void 0 ? void 0 : `${recordIdPrefix}:${platform.request_id}`
|
|
506
|
+
};
|
|
405
507
|
}
|
|
406
508
|
function resolveExternalUrl(runtimeUrl, deployment) {
|
|
407
509
|
let url;
|
|
@@ -584,6 +686,172 @@ function utf8Length(value) {
|
|
|
584
686
|
return UTF8_ENCODER.encode(value).byteLength;
|
|
585
687
|
}
|
|
586
688
|
|
|
689
|
+
// src/nonce.ts
|
|
690
|
+
var LAYOUT_VERSION = 1;
|
|
691
|
+
var API_KEY_VERIFIER_DOMAIN = "steadphast-discover:api-key:v1";
|
|
692
|
+
var NONCE_KEY_SALT = "steadphast-discover/client-nonce/v1/salt";
|
|
693
|
+
var NONCE_KEY_INFO = "steadphast-discover/client-nonce/v1";
|
|
694
|
+
var RENDER_CONTEXT_DOMAIN = "steadphast-discover/collector-render/v1";
|
|
695
|
+
var MAX_RID_BYTES = 512;
|
|
696
|
+
var MAX_KEY_ID_BYTES = 64;
|
|
697
|
+
var TAG_BYTES = 32;
|
|
698
|
+
var UTF8 = new TextEncoder();
|
|
699
|
+
function parseApiKey(apiKey) {
|
|
700
|
+
const separator = apiKey.indexOf(".");
|
|
701
|
+
if (separator <= 0 || separator === apiKey.length - 1) {
|
|
702
|
+
return void 0;
|
|
703
|
+
}
|
|
704
|
+
const keyId = apiKey.slice(0, separator);
|
|
705
|
+
const secret = apiKey.slice(separator + 1);
|
|
706
|
+
if (UTF8.encode(keyId).byteLength > MAX_KEY_ID_BYTES) {
|
|
707
|
+
return void 0;
|
|
708
|
+
}
|
|
709
|
+
return { keyId, secret };
|
|
710
|
+
}
|
|
711
|
+
function createNonceMinter(apiKey) {
|
|
712
|
+
const parsed = parseApiKey(apiKey);
|
|
713
|
+
if (parsed === void 0) {
|
|
714
|
+
return void 0;
|
|
715
|
+
}
|
|
716
|
+
const keyIdBytes = utf8(parsed.keyId);
|
|
717
|
+
let nonceKey;
|
|
718
|
+
return {
|
|
719
|
+
async mintRenderContext(rid, servedAtMs) {
|
|
720
|
+
const header = encodeHeader(rid, servedAtMs, keyIdBytes);
|
|
721
|
+
if (header === void 0) return void 0;
|
|
722
|
+
nonceKey ??= deriveNonceKey(parsed, keyIdBytes);
|
|
723
|
+
const tag = new Uint8Array(
|
|
724
|
+
await crypto.subtle.sign(
|
|
725
|
+
"HMAC",
|
|
726
|
+
await nonceKey,
|
|
727
|
+
concat(utf8(RENDER_CONTEXT_DOMAIN), header)
|
|
728
|
+
)
|
|
729
|
+
);
|
|
730
|
+
const context = bytes(header.byteLength + TAG_BYTES);
|
|
731
|
+
context.set(header);
|
|
732
|
+
context.set(tag, header.byteLength);
|
|
733
|
+
return base64UrlNoPad(context);
|
|
734
|
+
},
|
|
735
|
+
async nonceFromRenderContext(context) {
|
|
736
|
+
if (context.length > 850 || !/^[A-Za-z0-9_-]+$/.test(context)) {
|
|
737
|
+
return void 0;
|
|
738
|
+
}
|
|
739
|
+
let decoded;
|
|
740
|
+
try {
|
|
741
|
+
const binary = atob(context.replace(/-/g, "+").replace(/_/g, "/"));
|
|
742
|
+
decoded = bytes(binary.length);
|
|
743
|
+
for (let i = 0; i < binary.length; i++)
|
|
744
|
+
decoded[i] = binary.charCodeAt(i);
|
|
745
|
+
} catch {
|
|
746
|
+
return void 0;
|
|
747
|
+
}
|
|
748
|
+
if (decoded.byteLength <= TAG_BYTES) return void 0;
|
|
749
|
+
const header = decoded.subarray(0, -TAG_BYTES);
|
|
750
|
+
nonceKey ??= deriveNonceKey(parsed, keyIdBytes);
|
|
751
|
+
if (!await crypto.subtle.verify(
|
|
752
|
+
"HMAC",
|
|
753
|
+
await nonceKey,
|
|
754
|
+
decoded.subarray(-TAG_BYTES),
|
|
755
|
+
concat(utf8(RENDER_CONTEXT_DOMAIN), header)
|
|
756
|
+
))
|
|
757
|
+
return void 0;
|
|
758
|
+
const tag = new Uint8Array(
|
|
759
|
+
await crypto.subtle.sign("HMAC", await nonceKey, header)
|
|
760
|
+
);
|
|
761
|
+
decoded.set(tag, header.byteLength);
|
|
762
|
+
return base64UrlNoPad(decoded);
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
function encodeHeader(rid, servedAtMs, keyIdBytes) {
|
|
767
|
+
const ridBytes = utf8(rid);
|
|
768
|
+
if (ridBytes.byteLength === 0 || ridBytes.byteLength > MAX_RID_BYTES || !Number.isSafeInteger(servedAtMs) || servedAtMs < 0) {
|
|
769
|
+
return void 0;
|
|
770
|
+
}
|
|
771
|
+
const header = bytes(
|
|
772
|
+
1 + 2 + ridBytes.byteLength + 8 + 1 + keyIdBytes.byteLength
|
|
773
|
+
);
|
|
774
|
+
const view = new DataView(header.buffer);
|
|
775
|
+
let offset = 0;
|
|
776
|
+
header[offset] = LAYOUT_VERSION;
|
|
777
|
+
offset += 1;
|
|
778
|
+
view.setUint16(offset, ridBytes.byteLength);
|
|
779
|
+
offset += 2;
|
|
780
|
+
header.set(ridBytes, offset);
|
|
781
|
+
offset += ridBytes.byteLength;
|
|
782
|
+
view.setBigUint64(offset, BigInt(servedAtMs));
|
|
783
|
+
offset += 8;
|
|
784
|
+
header[offset] = keyIdBytes.byteLength;
|
|
785
|
+
offset += 1;
|
|
786
|
+
header.set(keyIdBytes, offset);
|
|
787
|
+
return header;
|
|
788
|
+
}
|
|
789
|
+
async function deriveNonceKey(parsed, keyIdBytes) {
|
|
790
|
+
const secretKey = await crypto.subtle.importKey(
|
|
791
|
+
"raw",
|
|
792
|
+
utf8(parsed.secret),
|
|
793
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
794
|
+
false,
|
|
795
|
+
["sign"]
|
|
796
|
+
);
|
|
797
|
+
const ikm = await crypto.subtle.sign(
|
|
798
|
+
"HMAC",
|
|
799
|
+
secretKey,
|
|
800
|
+
concat(utf8(API_KEY_VERIFIER_DOMAIN), keyIdBytes)
|
|
801
|
+
);
|
|
802
|
+
const hkdfKey = await crypto.subtle.importKey("raw", ikm, "HKDF", false, [
|
|
803
|
+
"deriveBits"
|
|
804
|
+
]);
|
|
805
|
+
const nonceKeyBits = await crypto.subtle.deriveBits(
|
|
806
|
+
{
|
|
807
|
+
name: "HKDF",
|
|
808
|
+
hash: "SHA-256",
|
|
809
|
+
salt: utf8(NONCE_KEY_SALT),
|
|
810
|
+
info: concat(utf8(NONCE_KEY_INFO), keyIdBytes)
|
|
811
|
+
},
|
|
812
|
+
hkdfKey,
|
|
813
|
+
TAG_BYTES * 8
|
|
814
|
+
);
|
|
815
|
+
return crypto.subtle.importKey(
|
|
816
|
+
"raw",
|
|
817
|
+
nonceKeyBits,
|
|
818
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
819
|
+
false,
|
|
820
|
+
["sign", "verify"]
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
function bytes(length) {
|
|
824
|
+
return new Uint8Array(new ArrayBuffer(length));
|
|
825
|
+
}
|
|
826
|
+
function utf8(text) {
|
|
827
|
+
return UTF8.encode(text);
|
|
828
|
+
}
|
|
829
|
+
function concat(prefix, suffix) {
|
|
830
|
+
const out = bytes(prefix.byteLength + 1 + suffix.byteLength);
|
|
831
|
+
out.set(prefix, 0);
|
|
832
|
+
out[prefix.byteLength] = 0;
|
|
833
|
+
out.set(suffix, prefix.byteLength + 1);
|
|
834
|
+
return out;
|
|
835
|
+
}
|
|
836
|
+
function base64UrlNoPad(bytes2) {
|
|
837
|
+
let binary = "";
|
|
838
|
+
for (const byte of bytes2) {
|
|
839
|
+
binary += String.fromCharCode(byte);
|
|
840
|
+
}
|
|
841
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
842
|
+
}
|
|
843
|
+
var NonceMinterCache = class {
|
|
844
|
+
#apiKey;
|
|
845
|
+
#minter;
|
|
846
|
+
for(apiKey) {
|
|
847
|
+
if (this.#apiKey !== apiKey) {
|
|
848
|
+
this.#apiKey = apiKey;
|
|
849
|
+
this.#minter = createNonceMinter(apiKey);
|
|
850
|
+
}
|
|
851
|
+
return this.#minter;
|
|
852
|
+
}
|
|
853
|
+
};
|
|
854
|
+
|
|
587
855
|
// src/middleware.ts
|
|
588
856
|
function withDiscover(config, handler) {
|
|
589
857
|
return buildMiddleware(
|
|
@@ -595,8 +863,10 @@ function withDiscover(config, handler) {
|
|
|
595
863
|
function buildMiddleware(config, ingestUrl, handler) {
|
|
596
864
|
validateApiKeyEnvVar(config.apiKeyEnvVar);
|
|
597
865
|
const settings = snapshotSettings(config, ingestUrl);
|
|
866
|
+
const minters = new NonceMinterCache();
|
|
598
867
|
let didWarnMissingApiKey = false;
|
|
599
868
|
return (request, event) => {
|
|
869
|
+
const scrubbed = stripCollectorHeaders(request.headers);
|
|
600
870
|
if (settings.captureAllPaths || !isSkippableStaticAsset(request.url)) {
|
|
601
871
|
const apiKey = resolveApiKey(settings.apiKeyEnvVar);
|
|
602
872
|
if (apiKey === void 0 || apiKey.length === 0) {
|
|
@@ -604,11 +874,26 @@ function buildMiddleware(config, ingestUrl, handler) {
|
|
|
604
874
|
console.warn(missingApiKeyDiagnostic(settings.apiKeyEnvVar));
|
|
605
875
|
didWarnMissingApiKey = true;
|
|
606
876
|
}
|
|
877
|
+
} else if (settings.collector) {
|
|
878
|
+
return captureWithCollector(
|
|
879
|
+
settings,
|
|
880
|
+
minters.for(apiKey),
|
|
881
|
+
apiKey,
|
|
882
|
+
request,
|
|
883
|
+
event,
|
|
884
|
+
handler,
|
|
885
|
+
scrubbed
|
|
886
|
+
);
|
|
607
887
|
} else {
|
|
608
888
|
capture(settings, apiKey, request, event);
|
|
609
889
|
}
|
|
610
890
|
}
|
|
611
|
-
return
|
|
891
|
+
return scrubbed ? delegateWithHeaders(
|
|
892
|
+
handler,
|
|
893
|
+
request,
|
|
894
|
+
event,
|
|
895
|
+
new Headers(request.headers)
|
|
896
|
+
) : delegate(handler, request, event);
|
|
612
897
|
};
|
|
613
898
|
}
|
|
614
899
|
function snapshotSettings(config, ingestUrl) {
|
|
@@ -616,6 +901,7 @@ function snapshotSettings(config, ingestUrl) {
|
|
|
616
901
|
return {
|
|
617
902
|
apiKeyEnvVar: config.apiKeyEnvVar,
|
|
618
903
|
captureAllPaths: config.captureAllPaths === true,
|
|
904
|
+
collector: config.collector === true,
|
|
619
905
|
connectorAssertion: connectorAssertionFor(deployment),
|
|
620
906
|
endpoint: ingestUrl,
|
|
621
907
|
envelopeOptions: {
|
|
@@ -627,7 +913,16 @@ function snapshotSettings(config, ingestUrl) {
|
|
|
627
913
|
};
|
|
628
914
|
}
|
|
629
915
|
function capture(settings, apiKey, request, event) {
|
|
630
|
-
|
|
916
|
+
ship2(
|
|
917
|
+
settings,
|
|
918
|
+
apiKey,
|
|
919
|
+
prepareEvidenceEnvelope(request, settings.envelopeOptions),
|
|
920
|
+
void 0,
|
|
921
|
+
event
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
function ship2(settings, apiKey, prepared, servedAtMs, event) {
|
|
925
|
+
const body = prepared === void 0 ? void 0 : finalizeEvidenceEnvelope(prepared.envelope, servedAtMs);
|
|
631
926
|
if (body === void 0) {
|
|
632
927
|
recordDroppedEvents(1);
|
|
633
928
|
return;
|
|
@@ -641,23 +936,45 @@ function capture(settings, apiKey, request, event) {
|
|
|
641
936
|
})
|
|
642
937
|
);
|
|
643
938
|
}
|
|
939
|
+
async function captureWithCollector(settings, minter, apiKey, request, event, handler, scrubbed) {
|
|
940
|
+
const prepared = prepareEvidenceEnvelope(request, settings.envelopeOptions);
|
|
941
|
+
const armed = prepared?.recordId !== void 0 && minter !== void 0 && isDocumentNavigation(request);
|
|
942
|
+
const servedAt = armed ? Date.now() : void 0;
|
|
943
|
+
const context = armed && prepared?.recordId !== void 0 && servedAt !== void 0 ? await minter.mintRenderContext(prepared.recordId, servedAt) : void 0;
|
|
944
|
+
if (context === void 0) {
|
|
945
|
+
ship2(settings, apiKey, prepared, void 0, event);
|
|
946
|
+
return scrubbed ? delegateWithHeaders(
|
|
947
|
+
handler,
|
|
948
|
+
request,
|
|
949
|
+
event,
|
|
950
|
+
forwardedRequestHeaders(request, void 0)
|
|
951
|
+
) : delegate(handler, request, event);
|
|
952
|
+
}
|
|
953
|
+
ship2(settings, apiKey, prepared, servedAt, event);
|
|
954
|
+
return delegateWithHeaders(
|
|
955
|
+
handler,
|
|
956
|
+
request,
|
|
957
|
+
event,
|
|
958
|
+
forwardedRequestHeaders(request, context)
|
|
959
|
+
);
|
|
960
|
+
}
|
|
644
961
|
function delegate(handler, request, event) {
|
|
645
962
|
if (handler === void 0) {
|
|
646
|
-
return
|
|
963
|
+
return NextResponse2.next();
|
|
647
964
|
}
|
|
648
965
|
return handler(request, event);
|
|
649
966
|
}
|
|
650
|
-
function
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
}
|
|
654
|
-
function resolveApiKey(customEnvVar) {
|
|
655
|
-
if (customEnvVar !== void 0) {
|
|
656
|
-
return readRuntimeEnv(customEnvVar);
|
|
967
|
+
async function delegateWithHeaders(handler, request, event, forwarded) {
|
|
968
|
+
if (handler === void 0) {
|
|
969
|
+
return NextResponse2.next({ request: { headers: forwarded } });
|
|
657
970
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
971
|
+
const forwardedRequest = new request.constructor(
|
|
972
|
+
request,
|
|
973
|
+
{ headers: forwarded }
|
|
974
|
+
);
|
|
975
|
+
return applyForwardedHeaders(
|
|
976
|
+
await handler(forwardedRequest, event),
|
|
977
|
+
forwarded
|
|
661
978
|
);
|
|
662
979
|
}
|
|
663
980
|
export {
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* App Router collector entry. Server-owned configuration enables rendering and supplies the
|
|
3
|
+
* destination; authenticated middleware context authorizes server-side beacon nonce minting.
|
|
4
|
+
* This entry is separate so middleware never imports next/headers or React.
|
|
5
|
+
*/
|
|
6
|
+
import type { ReactElement } from "react";
|
|
7
|
+
import { type DiscoverNextConfig } from "./config.js";
|
|
8
|
+
export interface DiscoverCollectorProps {
|
|
9
|
+
/** Server-owned tenant settings shared with withDiscover. Missing settings fail closed. */
|
|
10
|
+
config?: DiscoverNextConfig;
|
|
11
|
+
/** Optional server-owned Content Security Policy nonce. Never copied from visitor headers. */
|
|
12
|
+
nonce?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function DiscoverCollector(props?: DiscoverCollectorProps): Promise<ReactElement | null>;
|