@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/README.md
CHANGED
|
@@ -31,10 +31,9 @@ authoritative terms.
|
|
|
31
31
|
|
|
32
32
|
## Requirements
|
|
33
33
|
|
|
34
|
-
- Next.js 15
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
work. Next.js 14 and earlier are not supported.
|
|
34
|
+
- Next.js 15 or 16 (`^15.0.0 || ^16.0.0`) and React 19 (`^19.0.0`) are peer dependencies.
|
|
35
|
+
Tested with Next.js 15.5.23 (`middleware.ts`) and 16.3.2 (`proxy.ts`); other releases on
|
|
36
|
+
those two Next.js lines are expected to work. Next.js 14 and earlier are not supported.
|
|
38
37
|
- A Steadphast API key, created in the dashboard under **Settings → API keys** with **Used with**
|
|
39
38
|
set to the mode you deploy: **Next.js on Vercel** or **Next.js self-hosted**. A key is issued for
|
|
40
39
|
one deployment mode and is rejected by the other.
|
|
@@ -55,6 +54,51 @@ The package has no runtime dependencies; install no other Steadphast package. On
|
|
|
55
54
|
earlier, `npm install` stops with `ERESOLVE` and `pnpm add` warns about an unmet peer: upgrade
|
|
56
55
|
Next.js rather than forcing the install.
|
|
57
56
|
|
|
57
|
+
## Let a coding agent do the setup
|
|
58
|
+
|
|
59
|
+
If a coding agent (Claude Code, Codex, Cursor, or similar) has access to your application's
|
|
60
|
+
repository, paste the prompt for your deployment mode into it. The prompt sends the agent to this
|
|
61
|
+
README, has it install the package, write the entry file, propose the routes to exclude from
|
|
62
|
+
capture, and run the build, and it stops before anything that needs your account: it never creates,
|
|
63
|
+
asks for, or writes an API key, and it does not deploy. Review the routes it excluded before you
|
|
64
|
+
deploy. In the self-hosted prompt, replace `https://www.example.com` and `www.example.com` with
|
|
65
|
+
your public origin and host first.
|
|
66
|
+
|
|
67
|
+
### Prompt: Next.js on Vercel
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
Integrate the Steadphast Next.js connector into this application. The site is deployed on Vercel.
|
|
71
|
+
|
|
72
|
+
The integration guide is the package README. Follow it exactly and do not invent configuration options it does not document.
|
|
73
|
+
|
|
74
|
+
Steps:
|
|
75
|
+
1. Before installing anything, check the Next.js version in package.json. The connector supports Next.js 15 (entry file middleware.ts) and Next.js 16 (entry file proxy.ts). If the app is on an older version, stop and tell me; do not upgrade Next.js.
|
|
76
|
+
2. Install @steadphast/connector-nextjs with the package manager this repo already uses, then read node_modules/@steadphast/connector-nextjs/README.md in full before changing anything. If your package manager does not create node_modules, print the README with: npm view @steadphast/connector-nextjs readme
|
|
77
|
+
3. Create the entry file for our Next.js version beside the app or pages directory (or under src/ if the app lives there). Default-export withDiscover with kind: "vercel", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"], and a recordIdPrefix that names this deployment. Export a config with a matcher that excludes _next/static, _next/image, favicon.ico, and every route that handles form submissions, authentication, payments, or other sensitive actions in this app. List the routes you excluded and why so I can review them.
|
|
78
|
+
4. Run the production build and fix any errors. Do not start a dev server.
|
|
79
|
+
5. Do not deploy. Do not create, request, or write an API key anywhere; never put a key in a file or in a NEXT_PUBLIC_ variable. I will generate the key in Steadphast under Settings, API keys, with "Used with" set to "Next.js on Vercel", and set STEADPHAST_API_KEY in the Vercel project's environment myself.
|
|
80
|
+
|
|
81
|
+
Finish with a short handoff that tells me: the files you changed, the routes excluded from capture, the exact remaining manual steps (set the variable, redeploy), and how to verify: request the production domain with an AI crawler user agent, for example curl -A GPTBot, then check Explore in the Steadphast dashboard, which shows AI traffic by default.
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Prompt: Next.js self-hosted
|
|
85
|
+
|
|
86
|
+
```text
|
|
87
|
+
Integrate the Steadphast Next.js connector into this application in self-hosted mode. The site runs with next start on our own server behind nginx, and visitors reach it at https://www.example.com.
|
|
88
|
+
|
|
89
|
+
The integration guide is the package README. Follow it exactly and do not invent configuration options it does not document.
|
|
90
|
+
|
|
91
|
+
Steps:
|
|
92
|
+
1. Before installing anything, check the Next.js version in package.json. The connector supports Next.js 15 (entry file middleware.ts, which must also export runtime: "nodejs" in its config) and Next.js 16 (entry file proxy.ts, which takes no runtime option). If the app is on an older version, stop and tell me; do not upgrade Next.js.
|
|
93
|
+
2. Install @steadphast/connector-nextjs with the package manager this repo already uses, then read node_modules/@steadphast/connector-nextjs/README.md in full before changing anything. If your package manager does not create node_modules, print the README with: npm view @steadphast/connector-nextjs readme
|
|
94
|
+
3. Create the entry file for our Next.js version beside the app or pages directory (or under src/ if the app lives there). Default-export withDiscover with kind: "self-hosted", publicOrigin: "https://www.example.com", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"], and a recordIdPrefix that names this deployment. Export a config with a matcher that excludes _next/static, _next/image, favicon.ico, and every route that handles form submissions, authentication, payments, or other sensitive actions in this app. List the routes you excluded and why so I can review them.
|
|
95
|
+
4. Run the production build and fix any errors. Do not start a dev server. On Next.js 15 with runtime: "nodejs", the build output shows no Middleware line and .next/server/middleware-manifest.json stays empty; that is expected. The middleware is registered in .next/server/functions-config-manifest.json instead. Keep runtime: "nodejs".
|
|
96
|
+
5. Copy the reference nginx configuration from the README's Next.js self-hosted section into deploy/nginx/nginx.conf and change only the values its table lists: the public host and port (www.example.com on 443, in every occurrence, keeping the [.] spelling inside the map regular expression), the certificate paths, and the proxy_pass listener (127.0.0.1:39515). Keep every other directive exactly as written; it is what sets X-Steadphast-Client-Ip from $remote_addr and X-Steadphast-Request-Id from $request_id on every proxied request and answers 421 to any other host. Do not run nginx, systemctl, certbot, or sudo, and do not connect to any server; I will apply this on the host.
|
|
97
|
+
6. Do not deploy. Do not create, request, or write an API key anywhere; never put a key in a file or in a NEXT_PUBLIC_ variable. I will generate the key in Steadphast under Settings, API keys, with "Used with" set to "Next.js self-hosted", and set STEADPHAST_API_KEY in the environment of the next start process myself.
|
|
98
|
+
|
|
99
|
+
Finish with a short handoff that tells me: the files you changed, the routes excluded from capture, the exact remaining manual steps on the host (install the nginx config, the key, restart), and how to verify: request the site with an AI crawler user agent, for example curl -A GPTBot, then check Explore in the Steadphast dashboard, which shows AI traffic by default.
|
|
100
|
+
```
|
|
101
|
+
|
|
58
102
|
## Quick start: Next.js on Vercel
|
|
59
103
|
|
|
60
104
|
### Next.js 15: `middleware.ts`
|
|
@@ -100,11 +144,19 @@ export const config = {
|
|
|
100
144
|
### Set the API key
|
|
101
145
|
|
|
102
146
|
Add `STEADPHAST_API_KEY` to every Vercel environment where capture should be enabled. It is a
|
|
103
|
-
server-only secret: never give it a `NEXT_PUBLIC_*` name. When no key is set, the connector
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
147
|
+
server-only secret: never give it a `NEXT_PUBLIC_*` name. When no key is set, the connector ships
|
|
148
|
+
nothing and the visitor response is unaffected; it logs one warning per running instance, on the
|
|
149
|
+
first matched request that is not a skipped static asset, that begins
|
|
150
|
+
`Steadphast connector skipped event`.
|
|
151
|
+
Look for it in the deployment's runtime logs. Adding the variable takes effect on the next
|
|
152
|
+
deployment, so redeploy after you set it.
|
|
153
|
+
|
|
154
|
+
Deploy, make a request to a matched path on your production domain (deployment-specific
|
|
155
|
+
`*.vercel.app` URLs are protected by Vercel Deployment Protection by default), and open **Explore**
|
|
156
|
+
in the Steadphast dashboard. The event normally appears within about 60 seconds. Explore opens on AI
|
|
157
|
+
traffic only, so a request from your own browser is hidden until you switch the traffic filter to
|
|
158
|
+
all traffic; a request sent with an AI crawler's user agent, such as `curl -A GPTBot`, appears in the
|
|
159
|
+
default view.
|
|
108
160
|
|
|
109
161
|
### Keep your own Middleware or Proxy logic
|
|
110
162
|
|
|
@@ -136,9 +188,9 @@ Self-hosted mode is for a Next.js application started with `next start` on a ser
|
|
|
136
188
|
machine, or container you operate, with nginx terminating client TLS directly in front of it. It
|
|
137
189
|
is the same package configured with `kind: "self-hosted"`. Use it in either entry file; for
|
|
138
190
|
Next.js 15 `middleware.ts` also add `runtime: "nodejs"` to the exported `config` (Next.js 16
|
|
139
|
-
`proxy.ts` is already Node-only and does not accept the `runtime` option). The full setup guide
|
|
140
|
-
|
|
141
|
-
|
|
191
|
+
`proxy.ts` is already Node-only and does not accept the `runtime` option). The full setup guide is in the
|
|
192
|
+
dashboard at <https://app.steadphast.ai/docs/integrations/nextjs-self-hosted>; the reference nginx
|
|
193
|
+
configuration it uses is reproduced [below](#reference-nginx-configuration).
|
|
142
194
|
|
|
143
195
|
```ts
|
|
144
196
|
import { withDiscover } from "@steadphast/connector-nextjs";
|
|
@@ -174,10 +226,9 @@ is reachable without nginx, or a proxy that passes client headers through, lets
|
|
|
174
226
|
the IP recorded for its requests.
|
|
175
227
|
|
|
176
228
|
The supported topology is exactly one nginx directly in front of the application, and the
|
|
177
|
-
supported configuration is the reference nginx configuration
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
`proxy_pass` address). It:
|
|
229
|
+
supported configuration is the [reference nginx configuration](#reference-nginx-configuration)
|
|
230
|
+
below. Start from that file and change only the values it lists (public host and port, certificate
|
|
231
|
+
paths, and the loopback `proxy_pass` address). It:
|
|
181
232
|
|
|
182
233
|
- terminates client TLS directly on TLS 1.2 or 1.3 and refuses a handshake without the configured
|
|
183
234
|
server name;
|
|
@@ -195,6 +246,131 @@ front of nginx, a public direct Next.js port, and pass-through internal headers
|
|
|
195
246
|
the connector cannot detect them, and a CDN in front makes the recorded client IP that CDN's
|
|
196
247
|
address. No TLS fingerprint is available in this mode.
|
|
197
248
|
|
|
249
|
+
### Reference nginx configuration
|
|
250
|
+
|
|
251
|
+
This is the complete `nginx.conf` Steadphast tests with, reproduced exactly. It replaces the
|
|
252
|
+
distribution's `nginx.conf` and everything it includes from `conf.d` or `sites-enabled` rather than
|
|
253
|
+
being added beside them. Install it as `nginx.conf` in a directory of its own (for example
|
|
254
|
+
`/etc/steadphast/nginx/`) so its relative paths resolve there, check it with
|
|
255
|
+
`nginx -t -p /etc/steadphast/nginx/ -c nginx.conf`, and start nginx as root with
|
|
256
|
+
`nginx -e stderr -p /etc/steadphast/nginx/ -c nginx.conf -g "daemon off;"`. It needs nginx 1.25.1
|
|
257
|
+
or later.
|
|
258
|
+
|
|
259
|
+
```nginx
|
|
260
|
+
# Steadphast Next.js connector: reference nginx configuration for self-hosted mode.
|
|
261
|
+
#
|
|
262
|
+
# nginx is the only process that terminates client TLS. It accepts exactly one public host,
|
|
263
|
+
# sets the two internal connector headers from facts it established itself, and proxies to a
|
|
264
|
+
# Next.js `next start` listener bound to loopback. The public scheme, host, and port served
|
|
265
|
+
# here must equal the connector's `publicOrigin` exactly. The placeholder host and port used
|
|
266
|
+
# below are replaced with your own; see the values table in the setup guide.
|
|
267
|
+
#
|
|
268
|
+
# This is a complete nginx.conf. Install it as `nginx.conf` inside an explicit prefix so the
|
|
269
|
+
# relative TLS and temp paths resolve there, then run
|
|
270
|
+
# `nginx -e stderr -p <prefix>/ -c nginx.conf -g "daemon off;"`.
|
|
271
|
+
#
|
|
272
|
+
# Trust boundary (do not weaken):
|
|
273
|
+
# * x-steadphast-client-ip <- $remote_addr, the direct TCP peer. Nothing may sit in front
|
|
274
|
+
# of nginx: no CDN and no load balancer.
|
|
275
|
+
# * x-steadphast-request-id <- $request_id, the request identifier nginx generates itself.
|
|
276
|
+
# * Host / X-Forwarded-Host / X-Forwarded-Proto <- the routing and transport facts nginx checked.
|
|
277
|
+
# * X-Forwarded-For <- the client's value plus $remote_addr, kept only as untrusted
|
|
278
|
+
# evidence.
|
|
279
|
+
# * Anything else the client sends (X-Real-IP, x-vercel-*, a public x-request-id, ...) is
|
|
280
|
+
# passed through as ordinary request data; the connector never uses it as the client IP or
|
|
281
|
+
# request ID in self-hosted mode.
|
|
282
|
+
|
|
283
|
+
worker_processes 1;
|
|
284
|
+
pid nginx.pid;
|
|
285
|
+
error_log stderr warn;
|
|
286
|
+
|
|
287
|
+
events {
|
|
288
|
+
worker_connections 1024;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
http {
|
|
292
|
+
access_log off;
|
|
293
|
+
server_tokens off;
|
|
294
|
+
client_body_temp_path client_body_temp;
|
|
295
|
+
proxy_temp_path proxy_temp;
|
|
296
|
+
fastcgi_temp_path fastcgi_temp;
|
|
297
|
+
uwsgi_temp_path uwsgi_temp;
|
|
298
|
+
scgi_temp_path scgi_temp;
|
|
299
|
+
|
|
300
|
+
# `$request` retains the raw HTTP/1 request-target. Accept origin-form only with exactly one
|
|
301
|
+
# leading slash; an absolute-form target must spell this listener's complete HTTPS authority.
|
|
302
|
+
map $request $steadphast_request_target_ok {
|
|
303
|
+
default 0;
|
|
304
|
+
"~^[^ ]+ /(?:[^/ ][^ ]*)? HTTP/[0-9.]+$" 1;
|
|
305
|
+
"~^[^ ]+ https://connector[.]steadphast[.]test:39516(?:/[^ ]*|[?][^ ]*)? HTTP/[0-9.]+$" 1;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
# Handshake policy belongs to the shared/default context: nginx selects a virtual server only
|
|
309
|
+
# after the ClientHello, so the default and named handshakes must inherit the same minimum.
|
|
310
|
+
ssl_protocols TLSv1.2 TLSv1.3;
|
|
311
|
+
|
|
312
|
+
# Catch-all: a handshake without the configured SNI is refused outright.
|
|
313
|
+
server {
|
|
314
|
+
listen 39516 ssl default_server;
|
|
315
|
+
server_name _;
|
|
316
|
+
ssl_reject_handshake on;
|
|
317
|
+
return 421;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
server {
|
|
321
|
+
listen 39516 ssl;
|
|
322
|
+
http2 on;
|
|
323
|
+
server_name connector.steadphast.test;
|
|
324
|
+
|
|
325
|
+
ssl_certificate steadphast/tls.crt;
|
|
326
|
+
ssl_certificate_key steadphast/tls.key;
|
|
327
|
+
ssl_prefer_server_ciphers off;
|
|
328
|
+
|
|
329
|
+
# Require the raw request-target and complete wire authority, then independently check
|
|
330
|
+
# nginx's normalized host. `$host` drops an absolute-form target's port.
|
|
331
|
+
if ($steadphast_request_target_ok = 0) {
|
|
332
|
+
return 421;
|
|
333
|
+
}
|
|
334
|
+
if ($http_host != "connector.steadphast.test:39516") {
|
|
335
|
+
return 421;
|
|
336
|
+
}
|
|
337
|
+
if ($host != "connector.steadphast.test") {
|
|
338
|
+
return 421;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
location / {
|
|
342
|
+
# The private Next.js listener: `next start -H 127.0.0.1 -p 39515`.
|
|
343
|
+
proxy_pass http://127.0.0.1:39515;
|
|
344
|
+
proxy_http_version 1.1;
|
|
345
|
+
proxy_set_header Connection "";
|
|
346
|
+
|
|
347
|
+
# Validated external facts replace whatever the client sent.
|
|
348
|
+
proxy_set_header Host "connector.steadphast.test:39516";
|
|
349
|
+
proxy_set_header X-Forwarded-Host "connector.steadphast.test:39516";
|
|
350
|
+
proxy_set_header X-Forwarded-Proto "https";
|
|
351
|
+
|
|
352
|
+
# Internal connector headers: always overwritten, never preserved. `proxy_set_header`
|
|
353
|
+
# replaces every client-supplied occurrence, so duplicates cannot leak through.
|
|
354
|
+
proxy_set_header X-Steadphast-Client-Ip $remote_addr;
|
|
355
|
+
proxy_set_header X-Steadphast-Request-Id $request_id;
|
|
356
|
+
|
|
357
|
+
# Untrusted evidence only: the client chain with our direct peer appended.
|
|
358
|
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
The reference is written for a placeholder public host and port and a Next.js listener on loopback. Adapt it by changing only these values and keep every directive. In the `map` block the host is a regular expression, so each dot is written `[.]` to match a literal dot; keep that spelling when you substitute your own host, for example `www[.]example[.]com`:
|
|
365
|
+
|
|
366
|
+
| Reference value | Replace with |
|
|
367
|
+
|---|---|
|
|
368
|
+
| `connector.steadphast.test:39516` (the absolute-form request-target `map`, the `$http_host` check, and both the `proxy_set_header Host` and `X-Forwarded-Host` lines) | Your public authority, identical in every occurrence and identical to `publicOrigin`. On the default port `443` clients send a bare `Host`, so use `www.example.com` with no port in all of these and set `publicOrigin` to `https://www.example.com`; on any other port keep the `host:port` form in all of them and in `publicOrigin` |
|
|
369
|
+
| `connector.steadphast.test` (`server_name` and the `$host` check) | Your public host |
|
|
370
|
+
| `listen 39516 ssl` in both `server` blocks | Your public port |
|
|
371
|
+
| `ssl_certificate` and `ssl_certificate_key` | Your certificate and key paths |
|
|
372
|
+
| `proxy_pass http://127.0.0.1:39515` | The loopback address and port of your `next start` process |
|
|
373
|
+
|
|
198
374
|
## Configuration reference
|
|
199
375
|
|
|
200
376
|
```ts
|
|
@@ -246,7 +422,11 @@ and redirects are rejected, so your API key is never sent over plaintext or to a
|
|
|
246
422
|
## What is captured
|
|
247
423
|
|
|
248
424
|
Each captured request produces one strict JSON record containing the deployment mode, capture
|
|
249
|
-
timestamp, your `recordIdPrefix`, request metadata
|
|
425
|
+
timestamp, your `recordIdPrefix`, request metadata and platform metadata. With `collector: true`,
|
|
426
|
+
an eligible forwarded document navigation appends `collector_served: true` and `served_at`
|
|
427
|
+
(Unix milliseconds). These fields report the connector's serving intent, not proof that the
|
|
428
|
+
component rendered or the script executed. Every other envelope, including collector-off
|
|
429
|
+
installations, ends at `platform` as in 0.5.x.
|
|
250
430
|
|
|
251
431
|
- **Request:** method, URL, and headers. The connector reads nothing else. It never reads, clones,
|
|
252
432
|
buffers, or transmits the request body, so every downstream body consumer in your application
|
|
@@ -282,14 +462,26 @@ The built-in filter is a **limited, fixed set**, not a general secret detector.
|
|
|
282
462
|
case-insensitively with separators removed (`x-api-key`, `X_Api_Key`, and `xapikey` are the same
|
|
283
463
|
name). The exact set is:
|
|
284
464
|
|
|
285
|
-
- `authorization`, `x-authorization`, and `
|
|
465
|
+
- `authorization`, `x-authorization`, `proxy-authorization`, and `payment-authorization` (but not
|
|
466
|
+
`x-proxy-authorization`);
|
|
286
467
|
- `api-key`, `api-token`, `auth-key`, `access-key`, `secret-key`, `auth-token`, `access-token`,
|
|
287
468
|
`bearer-token`, `refresh-token`, `id-token`, `session-token`, and `csrf-token`, each with or
|
|
288
469
|
without an `x-` prefix;
|
|
289
470
|
- the bare names `token` and `key` (but not `x-token` or `x-key`);
|
|
290
471
|
- any name ending in `apikey`, `authtoken`, or `accesstoken`, and any name containing `cryptogram`
|
|
291
472
|
or `networktoken`;
|
|
292
|
-
- `agentic-payment-container` and Vercel's `x-vercel-protection-bypass
|
|
473
|
+
- `agentic-payment-container` and Vercel's `x-vercel-protection-bypass`, `x-vercel-oidc-token`, and
|
|
474
|
+
`x-vercel-proxy-signature`;
|
|
475
|
+
|
|
476
|
+
A `Payment-Authorization` header is the payment credential of the Machine Payments Protocol (MPP).
|
|
477
|
+
Its value is never sent. In its place the captured map carries
|
|
478
|
+
`x-steadphast-payment-credential: payment-authorization`, which records only that a credential was
|
|
479
|
+
presented. It is not evidence that a payment succeeded or that a person authorized one. The
|
|
480
|
+
connector removes any `x-steadphast-payment-credential` header the visitor sends, so only the
|
|
481
|
+
connector sets it. List it in `excludedHeaders` to suppress the marker.
|
|
482
|
+
|
|
483
|
+
Vercel's visitor location headers (`x-vercel-ip-country`, `-country-region`, `-city`, `-latitude`,
|
|
484
|
+
`-longitude`, `-postal-code`) are captured; add them to `excludedHeaders` to keep them out.
|
|
293
485
|
|
|
294
486
|
Every other header is captured, including custom credential headers such as `x-token`,
|
|
295
487
|
`x-github-token`, `x-openai-key`, `x-internal-secret`, or `x-<vendor>-signature`. Audit the
|
|
@@ -320,12 +512,125 @@ Delivery does not block the visitor response: the connector serializes the recor
|
|
|
320
512
|
changes the response status, headers, or body, and never propagates an error into your composed
|
|
321
513
|
handler.
|
|
322
514
|
|
|
515
|
+
## Optional: the T0 client collector
|
|
516
|
+
|
|
517
|
+
Unreleased version 0.6.0 adds the **T0 client collector**: a small clean-room script, inlined into
|
|
518
|
+
document HTML, that observes browser realms and posts one beacon to `POST /v1/client/t0`.
|
|
519
|
+
The server joins the beacon to its event by the envelope's `record_id_prefix:request_id` identity
|
|
520
|
+
and records `client_environment`; the six client-side flags are derived from that evidence.
|
|
521
|
+
|
|
522
|
+
It is **off by default**, including when the server component is mounted without configuration.
|
|
523
|
+
Every request has inbound `x-steadphast-collector-*` headers removed, even while collection is off.
|
|
524
|
+
Before enabling any tenant, ship the privacy statement line and customer install/notice guidance,
|
|
525
|
+
resolve applicable consent requirements with counsel, and obtain that tenant's explicit opt-in.
|
|
526
|
+
Merging or installing the package does not enable collection.
|
|
527
|
+
|
|
528
|
+
Keep one server-only configuration and pass it to both entry points:
|
|
529
|
+
|
|
530
|
+
```ts
|
|
531
|
+
// discover-config.ts: never import this module into a Client Component.
|
|
532
|
+
import type { DiscoverNextConfig } from "@steadphast/connector-nextjs";
|
|
533
|
+
|
|
534
|
+
export const discoverConfig = {
|
|
535
|
+
kind: "vercel",
|
|
536
|
+
apiUrl: "https://api.steadphast.ai/v1/connectors/nextjs/events",
|
|
537
|
+
excludedHeaders: ["cookie"],
|
|
538
|
+
recordIdPrefix: "vercel-production",
|
|
539
|
+
collector: false, // Set true only after the tenant's activation prerequisites are complete.
|
|
540
|
+
} satisfies DiscoverNextConfig;
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
```ts
|
|
544
|
+
// middleware.ts (15.5) or proxy.ts (16.3)
|
|
545
|
+
import { withDiscover } from "@steadphast/connector-nextjs";
|
|
546
|
+
import { discoverConfig } from "./discover-config";
|
|
547
|
+
|
|
548
|
+
export default withDiscover(discoverConfig);
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
```tsx
|
|
552
|
+
// app/layout.tsx
|
|
553
|
+
import { DiscoverCollector } from "@steadphast/connector-nextjs/server";
|
|
554
|
+
import { discoverConfig } from "../discover-config";
|
|
555
|
+
|
|
556
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
557
|
+
return (
|
|
558
|
+
<html lang="en">
|
|
559
|
+
<body>
|
|
560
|
+
{children}
|
|
561
|
+
<DiscoverCollector config={discoverConfig} />
|
|
562
|
+
</body>
|
|
563
|
+
</html>
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
What the wrapper does with `collector: true`, on a **document navigation** only (a `GET` whose
|
|
569
|
+
`Sec-Fetch-Dest` is `document`, or, when that header is absent, whose `Accept` names `text/html`):
|
|
570
|
+
|
|
571
|
+
- authenticates a render context bound to `record_id_prefix:request_id` and the current
|
|
572
|
+
millisecond with the server's connector API key;
|
|
573
|
+
- forwards only `x-steadphast-collector-context`, after stripping every inbound header in that
|
|
574
|
+
namespace; this domain-separated context is not a browser beacon nonce;
|
|
575
|
+
- appends `collector_served: true` and `served_at` to the envelope. The server holds later
|
|
576
|
+
requests only after a valid beacon has confirmed installation; a late first beacon can confirm it.
|
|
577
|
+
|
|
578
|
+
The wrapper does not read or rewrite the response body. It forwards the authenticated context with
|
|
579
|
+
`NextResponse.next({ request: { headers } })`, including composed next/rewrite responses.
|
|
580
|
+
`DiscoverCollector` separately requires `config.collector === true`, resolves the endpoint only
|
|
581
|
+
from that server configuration, validates the context and mints the browser nonce through the
|
|
582
|
+
server-side HMAC path. Missing configuration, missing context and forged context render nothing.
|
|
583
|
+
Visitor endpoint, nonce and enablement headers are never trusted. The component needs the App
|
|
584
|
+
Router (`next/headers`); the Pages Router is not supported. `react` ^19 is a peer for `./server`.
|
|
585
|
+
|
|
586
|
+
The collector observes the main window, a Blob worker, and a sandboxed `srcdoc` iframe, and reports
|
|
587
|
+
realm cross-checks (a spoofed `navigator` that disagrees across realms), native-function integrity,
|
|
588
|
+
headless and automation markers, a coarse screen/WebGL/feature profile, and interaction *counts*
|
|
589
|
+
(untrusted clicks, whether any trusted input happened). It registers no listener that reads what
|
|
590
|
+
was typed, and it never touches form contents, pointer coordinates, the clipboard, storage,
|
|
591
|
+
cookies, extension URLs, WebRTC, or any persistent identifier. Its one keepalive `text/plain`
|
|
592
|
+
POST explicitly uses `credentials: "omit"`, with a UTF-8 body capped at 2000 bytes. It sends at
|
|
593
|
+
`DOMContentLoaded` plus a short realm grace, on early exit/backgrounding, or at the 1500 ms cap,
|
|
594
|
+
then removes its listeners, worker, Blob URL and iframe. The bundle is gated below 8 KB gzipped.
|
|
595
|
+
|
|
596
|
+
Availability is factual: `available`, `unsupported-api`, `script-failure`, `background-tab`,
|
|
597
|
+
`early-exit` or `insufficient-observation`. Missing realms are not disagreements; discarded fields
|
|
598
|
+
cannot leave stale cross-realm findings. Availability alone changes no classification verdict.
|
|
599
|
+
|
|
600
|
+
**Content-Security-Policy.** Supply an independently server-generated application CSP nonce as
|
|
601
|
+
`<DiscoverCollector config={discoverConfig} nonce={cspNonce} />`; never read it from an unsanitized
|
|
602
|
+
visitor header. The browser beacon still receives its separate HMAC nonce. The iframe uses that
|
|
603
|
+
script nonce under its own restrictive policy and observes whether a nonce-less probe executes
|
|
604
|
+
there, not whether the customer's page happens to permit inline scripts. Under a strict parent
|
|
605
|
+
policy, allow the intended Blob worker and frame or expect incomplete availability. No policy is
|
|
606
|
+
weakened automatically; unsupported or failed observation is not automation evidence.
|
|
607
|
+
|
|
608
|
+
## Repository verification
|
|
609
|
+
|
|
610
|
+
From a repository checkout:
|
|
611
|
+
|
|
612
|
+
```sh
|
|
613
|
+
nix develop -c pnpm -C ts --filter @steadphast/connector-nextjs test
|
|
614
|
+
nix develop -c pnpm -C ts --filter @steadphast/connector-nextjs build
|
|
615
|
+
nix develop -c pnpm -C ts deadcode
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
The build has two public entries and two repository-only loopback fixture entries. Knip tracks
|
|
619
|
+
the build entrypoints explicitly because the Next.js fixtures import their compiled paths;
|
|
620
|
+
the private entries remain excluded from package exports and published files. The hosted
|
|
621
|
+
`nextjs-connector-checks` gate exercises both supported runtimes through nginx and the live API.
|
|
622
|
+
The durable replay test waits for the first committed value and then for every replay job to
|
|
623
|
+
settle; asynchronous acceptance does not promise FIFO completion.
|
|
624
|
+
|
|
323
625
|
## Troubleshooting
|
|
324
626
|
|
|
325
|
-
- **No events appear:**
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
627
|
+
- **No events appear:** switch the Explore traffic filter to all traffic first, because the default
|
|
628
|
+
view shows AI traffic only. Then confirm the API key was issued for the mode you set in `kind`
|
|
629
|
+
(**Next.js on Vercel** or **Next.js self-hosted**), that `STEADPHAST_API_KEY` is set in the
|
|
630
|
+
deployed environment and the process or deployment was restarted after you set it, that the entry
|
|
631
|
+
file default-exports the wrapper, that `recordIdPrefix` is within its 128-byte limit, and that the
|
|
632
|
+
matcher or static-asset filter did not skip the path. A `Steadphast connector skipped event` line
|
|
633
|
+
in the runtime logs means no key reached the process.
|
|
329
634
|
- **The deployment fails at startup:** `apiUrl` is not HTTPS or its path is neither `/` nor
|
|
330
635
|
`/v1/connectors/nextjs/events`, `publicOrigin` is not a bare HTTPS origin, or `apiKeyEnvVar`
|
|
331
636
|
names a `NEXT_PUBLIC_*` variable. The thrown `DiscoverConfigError` names the setting.
|
|
@@ -333,8 +638,9 @@ handler.
|
|
|
333
638
|
are captured, so a plain `http://localhost` server never ships. Front the server with an HTTPS
|
|
334
639
|
proxy or send `X-Forwarded-Proto: https`, which Next.js honors when building the request URL.
|
|
335
640
|
- **Self-hosted events have no client IP:** nginx is not overwriting `X-Steadphast-Client-Ip`, or
|
|
336
|
-
the request bypassed nginx. Compare your configuration with the
|
|
337
|
-
|
|
641
|
+
the request bypassed nginx. Compare your configuration with the
|
|
642
|
+
[reference nginx configuration](#reference-nginx-configuration) and confirm the Next.js port is
|
|
643
|
+
not reachable from outside.
|
|
338
644
|
- **Self-hosted requests answer `421`:** nginx received a `Host` or absolute-form request target
|
|
339
645
|
that does not match its configured public authority. Make the `server_name`, the `Host` checks,
|
|
340
646
|
the `proxy_set_header Host` lines, and `publicOrigin` agree with each other.
|
|
@@ -347,6 +653,12 @@ handler.
|
|
|
347
653
|
|
|
348
654
|
## Upgrading
|
|
349
655
|
|
|
656
|
+
- **0.6.0** (unreleased) adds the opt-in T0 client collector. It also stops sending the
|
|
657
|
+
`Payment-Authorization` credential and sends the `x-steadphast-payment-credential` presence
|
|
658
|
+
marker in its place. Upgrade the connector and the Steadphast API together: the API rejects an
|
|
659
|
+
older envelope that still carries `Payment-Authorization`.
|
|
660
|
+
- **0.5.4** Documentation only: this README now carries the reference nginx configuration and the
|
|
661
|
+
coding-agent setup prompts; no runtime change.
|
|
350
662
|
- **0.5.3** Documentation and metadata only; no runtime change.
|
|
351
663
|
- **0.5.2** changes documentation and package metadata only; the shipped code is identical to
|
|
352
664
|
0.5.1, and no upgrade action is needed.
|
package/dist/config.d.ts
CHANGED
|
@@ -21,6 +21,14 @@ export interface DiscoverNextCommonConfig {
|
|
|
21
21
|
excludedHeaders?: ReadonlyArray<string>;
|
|
22
22
|
recordIdPrefix?: string;
|
|
23
23
|
timeoutMs?: number;
|
|
24
|
+
/**
|
|
25
|
+
* Serve the T0 client collector (ADR 0037). Off by default. Both middleware and the server
|
|
26
|
+
* component must receive this server-owned configuration with collector explicitly enabled.
|
|
27
|
+
* The middleware authenticates a render context for captured document navigations; the server
|
|
28
|
+
* component verifies it and mints the beacon nonce. Neither nonce nor destination is accepted
|
|
29
|
+
* from visitor headers. Disabled requests have no collector output or envelope keys.
|
|
30
|
+
*/
|
|
31
|
+
collector?: boolean;
|
|
24
32
|
}
|
|
25
33
|
/** The connector configuration: the explicit deployment mode plus the common transport settings. */
|
|
26
34
|
export type DiscoverNextConfig = DiscoverDeploymentMode & DiscoverNextCommonConfig;
|
|
@@ -37,6 +45,12 @@ export declare const SELF_HOSTED_CONNECTOR_ASSERTION: DiscoverConnectorAssertion
|
|
|
37
45
|
* `CanonicalEvent`, and it must never be admitted as one.
|
|
38
46
|
*/
|
|
39
47
|
export declare const ENVELOPE_INGEST_PATHNAME = "/v1/connectors/nextjs/events";
|
|
48
|
+
/** The unauthenticated, CORS-simple T0 beacon route the collector posts to (ADR 0037 decision 5). */
|
|
49
|
+
export declare const BEACON_PATHNAME = "/v1/client/t0";
|
|
50
|
+
/** Reserved namespace: every inbound member is stripped before capture or delegation. */
|
|
51
|
+
export declare const COLLECTOR_HEADER_PREFIX = "x-steadphast-collector-";
|
|
52
|
+
/** Authenticated middleware-to-render context, domain-separated from browser beacon nonces. */
|
|
53
|
+
export declare const COLLECTOR_CONTEXT_HEADER = "x-steadphast-collector-context";
|
|
40
54
|
export declare class DiscoverConfigError extends TypeError {
|
|
41
55
|
readonly name = "DiscoverConfigError";
|
|
42
56
|
}
|
|
@@ -63,3 +77,5 @@ export declare function parseStrictUrl(value: string, setting: string): URL;
|
|
|
63
77
|
export declare function connectorAssertionFor(mode: DiscoverDeploymentMode): DiscoverConnectorAssertion;
|
|
64
78
|
/** Validates the discriminated mode and returns the resolved `publicOrigin` for self-hosted. */
|
|
65
79
|
export declare function resolveDeploymentMode(mode: DiscoverDeploymentMode): DiscoverDeploymentMode;
|
|
80
|
+
/** Resolves only server-owned environment settings, never request metadata. */
|
|
81
|
+
export declare function resolveApiKey(customEnvVar: string | undefined): string | undefined;
|