@kya-os/checkpoint-nextjs 1.7.0 → 1.9.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/CHANGELOG.md +30 -0
- package/EDGE_RUNTIME_WASM_SETUP.md +12 -12
- package/README.md +144 -251
- package/bin/setup-edge-wasm.js +10 -2
- package/dist/adapt.js +9 -6
- package/dist/adapt.mjs +9 -6
- package/dist/api-client.d.mts +9 -0
- package/dist/api-client.d.ts +9 -0
- package/dist/api-middleware.d.mts +12 -2
- package/dist/api-middleware.d.ts +12 -2
- package/dist/api-middleware.js +28 -31
- package/dist/api-middleware.mjs +29 -32
- package/dist/composed-policy.d.mts +7 -0
- package/dist/composed-policy.d.ts +7 -0
- package/dist/composed-policy.js +14 -2
- package/dist/composed-policy.mjs +15 -3
- package/dist/{config-_nfPN3E3.d.mts → config-CleTeeoW.d.mts} +39 -11
- package/dist/{config-kxFihzR_.d.ts → config-XMOIkxFN.d.ts} +39 -11
- package/dist/create-middleware.d.mts +1 -1
- package/dist/create-middleware.d.ts +1 -1
- package/dist/edge/index.d.mts +3 -1
- package/dist/edge/index.d.ts +3 -1
- package/dist/edge/index.js +5 -16
- package/dist/edge/index.mjs +5 -16
- package/dist/edge-runtime-loader.d.mts +3 -1
- package/dist/edge-runtime-loader.d.ts +3 -1
- package/dist/edge-runtime-loader.js +4 -16
- package/dist/edge-runtime-loader.mjs +5 -17
- package/dist/edge-wasm-middleware.d.mts +1 -1
- package/dist/edge-wasm-middleware.d.ts +1 -1
- package/dist/edge-wasm-middleware.js +1 -1
- package/dist/edge-wasm-middleware.mjs +1 -1
- package/dist/index.d.mts +6 -6
- package/dist/index.d.ts +6 -6
- package/dist/index.js +151 -84
- package/dist/index.mjs +153 -86
- package/dist/middleware-bundler.d.mts +13 -0
- package/dist/middleware-bundler.d.ts +13 -0
- package/dist/middleware-bundler.js +354 -0
- package/dist/middleware-bundler.mjs +352 -0
- package/dist/middleware-edge.d.mts +5 -41
- package/dist/middleware-edge.d.ts +5 -41
- package/dist/middleware-edge.js +161 -97
- package/dist/middleware-edge.mjs +164 -100
- package/dist/middleware-node.d.mts +16 -52
- package/dist/middleware-node.d.ts +16 -52
- package/dist/middleware-node.js +115 -56
- package/dist/middleware-node.mjs +118 -55
- package/dist/middleware.d.mts +1 -1
- package/dist/middleware.d.ts +1 -1
- package/dist/policy.js +6 -1
- package/dist/policy.mjs +6 -1
- package/dist/session-tracker.js +2 -0
- package/dist/session-tracker.mjs +2 -0
- package/dist/translate.d.mts +7 -4
- package/dist/translate.d.ts +7 -4
- package/dist/translate.js +9 -8
- package/dist/translate.mjs +9 -8
- package/dist/{types-D9RQvPNy.d.mts → types-Chgi5TFC.d.mts} +2 -2
- package/dist/{types-D9RQvPNy.d.ts → types-Chgi5TFC.d.ts} +2 -2
- package/package.json +17 -10
- package/dist/signature-verifier.d.mts +0 -33
- package/dist/signature-verifier.d.ts +0 -33
- package/dist/signature-verifier.js +0 -384
- package/dist/signature-verifier.mjs +0 -360
|
@@ -1,50 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as next_server from 'next/server';
|
|
2
2
|
export { initEngineEdge } from '@kya-os/checkpoint-wasm-runtime/orchestrator/edge';
|
|
3
3
|
export { initPolicyEvaluatorEdge } from '@kya-os/checkpoint-wasm-runtime/policy-edge';
|
|
4
|
-
import { C as CheckpointConfig } from './config-
|
|
4
|
+
import { C as CheckpointConfig } from './config-CleTeeoW.mjs';
|
|
5
|
+
import '@kya-os/checkpoint-shared/browser-posture';
|
|
5
6
|
import '@kya-os/checkpoint-wasm-runtime/adapters';
|
|
6
7
|
import '@kya-os/checkpoint-wasm-runtime/engine';
|
|
8
|
+
import '@kya-os/checkpoint-shared';
|
|
7
9
|
import './composed-policy.mjs';
|
|
8
10
|
import '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
9
|
-
import '@kya-os/checkpoint-shared';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* D.3 — Edge-runtime Next.js middleware entry.
|
|
13
|
-
*
|
|
14
|
-
* The async-init equivalent of `./middleware-node.ts`. Mounted under
|
|
15
|
-
* Vercel Edge runtime (the Next.js middleware default) and Cloudflare
|
|
16
|
-
* Workers when Next.js targets the Edge.
|
|
17
|
-
*
|
|
18
|
-
* Differs from the Node entry in exactly two places:
|
|
19
|
-
*
|
|
20
|
-
* 1. Imports `verifyRequestEdge` + `initEngineEdge` from the
|
|
21
|
-
* orchestrator's `./edge` subpath (Edge-WASM-2 from D.1.5)
|
|
22
|
-
* instead of `verifyRequest` from the Node orchestrator entry.
|
|
23
|
-
* 2. Calls `initEngineEdge()` once at module load (eagerly, before
|
|
24
|
-
* any request hits the middleware) so the first request's cold-
|
|
25
|
-
* boot latency is amortised onto deploy time. Subsequent calls
|
|
26
|
-
* to `initEngineEdge` are idempotent.
|
|
27
|
-
*
|
|
28
|
-
* Adapter composition (`buildVerifyOpts`), translate.ts, adapt.ts,
|
|
29
|
-
* verdict-cookie format, X-Checkpoint-* headers — all shared with
|
|
30
|
-
* the Node entry. Cross-runtime parity verified by Phase F's CI gate
|
|
31
|
-
* (D.5 ships the Next.js-specific half).
|
|
32
|
-
*
|
|
33
|
-
* **Public API contract — preserved:** `withCheckpoint(config)`,
|
|
34
|
-
* `CheckpointConfig`. Same exports as Node, same signatures.
|
|
35
|
-
*/
|
|
36
11
|
|
|
37
|
-
|
|
38
|
-
* Build the Checkpoint middleware for Edge runtime. Returns a function
|
|
39
|
-
* `(req) => Promise<NextResponse>` suitable for
|
|
40
|
-
* `export default withCheckpoint({...})` in `middleware.ts` under
|
|
41
|
-
* `export const config = { runtime: 'edge' }`.
|
|
42
|
-
*
|
|
43
|
-
* Idempotent eager init: the first call to `withCheckpoint` kicks off
|
|
44
|
-
* `initEngineEdge()` so the wasm artifact loads while the rest of the
|
|
45
|
-
* factory closure is being built. The first request awaits the same
|
|
46
|
-
* promise; subsequent requests resolve sync.
|
|
47
|
-
*/
|
|
48
|
-
declare function withCheckpoint(config: CheckpointConfig): (req: NextRequest, event?: NextFetchEvent) => Promise<NextResponse>;
|
|
12
|
+
declare function withCheckpoint(config: CheckpointConfig): (req: next_server.NextRequest, event?: Pick<next_server.NextFetchEvent, "waitUntil">) => Promise<next_server.NextResponse>;
|
|
49
13
|
|
|
50
14
|
export { CheckpointConfig, withCheckpoint };
|
|
@@ -1,50 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as next_server from 'next/server';
|
|
2
2
|
export { initEngineEdge } from '@kya-os/checkpoint-wasm-runtime/orchestrator/edge';
|
|
3
3
|
export { initPolicyEvaluatorEdge } from '@kya-os/checkpoint-wasm-runtime/policy-edge';
|
|
4
|
-
import { C as CheckpointConfig } from './config-
|
|
4
|
+
import { C as CheckpointConfig } from './config-XMOIkxFN.js';
|
|
5
|
+
import '@kya-os/checkpoint-shared/browser-posture';
|
|
5
6
|
import '@kya-os/checkpoint-wasm-runtime/adapters';
|
|
6
7
|
import '@kya-os/checkpoint-wasm-runtime/engine';
|
|
8
|
+
import '@kya-os/checkpoint-shared';
|
|
7
9
|
import './composed-policy.js';
|
|
8
10
|
import '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
9
|
-
import '@kya-os/checkpoint-shared';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* D.3 — Edge-runtime Next.js middleware entry.
|
|
13
|
-
*
|
|
14
|
-
* The async-init equivalent of `./middleware-node.ts`. Mounted under
|
|
15
|
-
* Vercel Edge runtime (the Next.js middleware default) and Cloudflare
|
|
16
|
-
* Workers when Next.js targets the Edge.
|
|
17
|
-
*
|
|
18
|
-
* Differs from the Node entry in exactly two places:
|
|
19
|
-
*
|
|
20
|
-
* 1. Imports `verifyRequestEdge` + `initEngineEdge` from the
|
|
21
|
-
* orchestrator's `./edge` subpath (Edge-WASM-2 from D.1.5)
|
|
22
|
-
* instead of `verifyRequest` from the Node orchestrator entry.
|
|
23
|
-
* 2. Calls `initEngineEdge()` once at module load (eagerly, before
|
|
24
|
-
* any request hits the middleware) so the first request's cold-
|
|
25
|
-
* boot latency is amortised onto deploy time. Subsequent calls
|
|
26
|
-
* to `initEngineEdge` are idempotent.
|
|
27
|
-
*
|
|
28
|
-
* Adapter composition (`buildVerifyOpts`), translate.ts, adapt.ts,
|
|
29
|
-
* verdict-cookie format, X-Checkpoint-* headers — all shared with
|
|
30
|
-
* the Node entry. Cross-runtime parity verified by Phase F's CI gate
|
|
31
|
-
* (D.5 ships the Next.js-specific half).
|
|
32
|
-
*
|
|
33
|
-
* **Public API contract — preserved:** `withCheckpoint(config)`,
|
|
34
|
-
* `CheckpointConfig`. Same exports as Node, same signatures.
|
|
35
|
-
*/
|
|
36
11
|
|
|
37
|
-
|
|
38
|
-
* Build the Checkpoint middleware for Edge runtime. Returns a function
|
|
39
|
-
* `(req) => Promise<NextResponse>` suitable for
|
|
40
|
-
* `export default withCheckpoint({...})` in `middleware.ts` under
|
|
41
|
-
* `export const config = { runtime: 'edge' }`.
|
|
42
|
-
*
|
|
43
|
-
* Idempotent eager init: the first call to `withCheckpoint` kicks off
|
|
44
|
-
* `initEngineEdge()` so the wasm artifact loads while the rest of the
|
|
45
|
-
* factory closure is being built. The first request awaits the same
|
|
46
|
-
* promise; subsequent requests resolve sync.
|
|
47
|
-
*/
|
|
48
|
-
declare function withCheckpoint(config: CheckpointConfig): (req: NextRequest, event?: NextFetchEvent) => Promise<NextResponse>;
|
|
12
|
+
declare function withCheckpoint(config: CheckpointConfig): (req: next_server.NextRequest, event?: Pick<next_server.NextFetchEvent, "waitUntil">) => Promise<next_server.NextResponse>;
|
|
49
13
|
|
|
50
14
|
export { CheckpointConfig, withCheckpoint };
|
package/dist/middleware-edge.js
CHANGED
|
@@ -3,61 +3,13 @@
|
|
|
3
3
|
var edge = require('@kya-os/checkpoint-wasm-runtime/orchestrator/edge');
|
|
4
4
|
var policyEdge = require('@kya-os/checkpoint-wasm-runtime/policy-edge');
|
|
5
5
|
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
6
|
-
var server = require('next/server');
|
|
7
6
|
var composedPolicy = require('@kya-os/checkpoint-wasm-runtime/composed-policy');
|
|
8
|
-
require('@kya-os/checkpoint-
|
|
7
|
+
var browserPosture = require('@kya-os/checkpoint-shared/browser-posture');
|
|
9
8
|
var adapters = require('@kya-os/checkpoint-wasm-runtime/adapters');
|
|
10
9
|
var reporter = require('@kya-os/checkpoint-wasm-runtime/reporter');
|
|
10
|
+
var server = require('next/server');
|
|
11
11
|
|
|
12
12
|
// src/middleware-edge.ts
|
|
13
|
-
function adaptToNextResponse(rendered, req) {
|
|
14
|
-
const clientAcceptsHtml = checkpointShared.acceptsHtml(req.headers);
|
|
15
|
-
const verdictCookie = checkpointShared.encodeVerdictCookie(rendered);
|
|
16
|
-
const shape = checkpointShared.classifyResponseShape(rendered, clientAcceptsHtml);
|
|
17
|
-
switch (shape) {
|
|
18
|
-
case "pass-through": {
|
|
19
|
-
const res = server.NextResponse.next();
|
|
20
|
-
applyHeaders(res, rendered.headers);
|
|
21
|
-
setVerdictCookie(res, verdictCookie);
|
|
22
|
-
return res;
|
|
23
|
-
}
|
|
24
|
-
case "redirect": {
|
|
25
|
-
const target = new URL(rendered.headers.Location);
|
|
26
|
-
const res = server.NextResponse.redirect(target);
|
|
27
|
-
applyHeaders(res, rendered.headers);
|
|
28
|
-
setVerdictCookie(res, verdictCookie);
|
|
29
|
-
return res;
|
|
30
|
-
}
|
|
31
|
-
case "html-block": {
|
|
32
|
-
const blockedUrl = new URL(checkpointShared.BLOCKED_PATH, req.url);
|
|
33
|
-
const res = server.NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
34
|
-
applyHeaders(res, rendered.headers);
|
|
35
|
-
setVerdictCookie(res, verdictCookie);
|
|
36
|
-
return res;
|
|
37
|
-
}
|
|
38
|
-
case "json-block": {
|
|
39
|
-
const body = rendered.body ?? {};
|
|
40
|
-
const res = server.NextResponse.json(body, { status: rendered.status });
|
|
41
|
-
applyHeaders(res, rendered.headers);
|
|
42
|
-
setVerdictCookie(res, verdictCookie);
|
|
43
|
-
return res;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
function setVerdictCookie(res, value) {
|
|
48
|
-
res.cookies.set({
|
|
49
|
-
name: checkpointShared.VERDICT_COOKIE_NAME,
|
|
50
|
-
value,
|
|
51
|
-
path: "/",
|
|
52
|
-
sameSite: "lax",
|
|
53
|
-
httpOnly: false
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function applyHeaders(res, headers) {
|
|
57
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
58
|
-
res.headers.set(key, value);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
13
|
var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
|
|
62
14
|
var NOOP_LOGGER = {
|
|
63
15
|
shadowDivergence: () => {
|
|
@@ -72,6 +24,7 @@ function makeComposedPolicyContext(opts) {
|
|
|
72
24
|
return {
|
|
73
25
|
async apply(result, path) {
|
|
74
26
|
const structured = { decision: result.decision, acted: false };
|
|
27
|
+
if (composedPolicy.isCredentialVerificationBlock(result.decision)) return structured;
|
|
75
28
|
const policy = await fetcher.getPolicy(projectId);
|
|
76
29
|
const outcome = await composedPolicy.evaluateComposedPolicy({
|
|
77
30
|
cache,
|
|
@@ -102,7 +55,13 @@ function makeComposedPolicyContext(opts) {
|
|
|
102
55
|
return structured;
|
|
103
56
|
}
|
|
104
57
|
if (outcome.status === "acting") {
|
|
105
|
-
return {
|
|
58
|
+
return {
|
|
59
|
+
decision: outcome.engineDecision,
|
|
60
|
+
acted: true,
|
|
61
|
+
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
62
|
+
// PR5). Conditional spread: absent upstream stays absent here.
|
|
63
|
+
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
64
|
+
};
|
|
106
65
|
}
|
|
107
66
|
return structured;
|
|
108
67
|
},
|
|
@@ -122,7 +81,12 @@ async function applyComposedPolicy(context, result, path) {
|
|
|
122
81
|
if (!context) return;
|
|
123
82
|
try {
|
|
124
83
|
const outcome = await context.apply(result, path);
|
|
125
|
-
if (outcome.acted)
|
|
84
|
+
if (outcome.acted) {
|
|
85
|
+
result.decision = outcome.decision;
|
|
86
|
+
if (outcome.composedBlobHash) {
|
|
87
|
+
result.composedBlobHash = outcome.composedBlobHash;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
126
90
|
} catch {
|
|
127
91
|
}
|
|
128
92
|
}
|
|
@@ -137,8 +101,57 @@ var consoleComposedPolicyLogger = {
|
|
|
137
101
|
);
|
|
138
102
|
}
|
|
139
103
|
};
|
|
140
|
-
|
|
141
|
-
|
|
104
|
+
function adaptToNextResponse(rendered, req) {
|
|
105
|
+
const clientAcceptsHtml = checkpointShared.acceptsHtml(req.headers);
|
|
106
|
+
const verdictCookie = checkpointShared.encodeVerdictCookie(rendered);
|
|
107
|
+
const shape = checkpointShared.classifyResponseShape(rendered, clientAcceptsHtml);
|
|
108
|
+
switch (shape) {
|
|
109
|
+
case "pass-through": {
|
|
110
|
+
const res = server.NextResponse.next();
|
|
111
|
+
applyHeaders(res, rendered);
|
|
112
|
+
setVerdictCookie(res, verdictCookie);
|
|
113
|
+
return res;
|
|
114
|
+
}
|
|
115
|
+
case "redirect": {
|
|
116
|
+
const target = new URL(rendered.headers.Location);
|
|
117
|
+
const res = server.NextResponse.redirect(target);
|
|
118
|
+
applyHeaders(res, rendered);
|
|
119
|
+
setVerdictCookie(res, verdictCookie);
|
|
120
|
+
return res;
|
|
121
|
+
}
|
|
122
|
+
case "html-block": {
|
|
123
|
+
const blockedUrl = new URL(checkpointShared.BLOCKED_PATH, req.url);
|
|
124
|
+
const res = server.NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
125
|
+
applyHeaders(res, rendered);
|
|
126
|
+
setVerdictCookie(res, verdictCookie);
|
|
127
|
+
return res;
|
|
128
|
+
}
|
|
129
|
+
case "json-block": {
|
|
130
|
+
const body = rendered.body ?? {};
|
|
131
|
+
const res = server.NextResponse.json(body, { status: rendered.status });
|
|
132
|
+
applyHeaders(res, rendered);
|
|
133
|
+
setVerdictCookie(res, verdictCookie);
|
|
134
|
+
return res;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function setVerdictCookie(res, value) {
|
|
139
|
+
res.cookies.set({
|
|
140
|
+
name: checkpointShared.VERDICT_COOKIE_NAME,
|
|
141
|
+
value,
|
|
142
|
+
path: "/",
|
|
143
|
+
sameSite: "lax",
|
|
144
|
+
httpOnly: false
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
function applyHeaders(res, rendered) {
|
|
148
|
+
for (const [key, value] of Object.entries(rendered.headers)) {
|
|
149
|
+
res.headers.set(key, value);
|
|
150
|
+
}
|
|
151
|
+
for (const [key, value] of rendered.headerEntries ?? []) {
|
|
152
|
+
res.headers.append(key, value);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
142
155
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
143
156
|
const url = new URL(req.url);
|
|
144
157
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -148,7 +161,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
148
161
|
url: url.pathname + url.search,
|
|
149
162
|
headers: headersToRecord(req.headers),
|
|
150
163
|
body,
|
|
151
|
-
remoteAddress: extractRemoteAddress(req)
|
|
164
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
152
165
|
};
|
|
153
166
|
}
|
|
154
167
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -168,19 +181,84 @@ function headersToRecord(headers) {
|
|
|
168
181
|
});
|
|
169
182
|
return out;
|
|
170
183
|
}
|
|
171
|
-
function extractRemoteAddress(req) {
|
|
172
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
173
|
-
if (xff) {
|
|
174
|
-
const first = xff.split(",")[0]?.trim();
|
|
175
|
-
if (first) return first;
|
|
176
|
-
}
|
|
184
|
+
function extractRemoteAddress(req, policy) {
|
|
177
185
|
const maybeIp = req.ip;
|
|
178
|
-
return
|
|
186
|
+
return checkpointShared.resolveClientIp({
|
|
187
|
+
headers: req.headers,
|
|
188
|
+
frameworkAddress: maybeIp,
|
|
189
|
+
policy
|
|
190
|
+
}).ip ?? void 0;
|
|
179
191
|
}
|
|
180
192
|
|
|
181
|
-
// src/middleware-
|
|
193
|
+
// src/middleware-core.ts
|
|
194
|
+
function makeNextMiddleware(verifyRequest, renderDecisionAsResponse2, config, host) {
|
|
195
|
+
if (config.browserPosture) browserPosture.validateBrowserPostureConfig(config.browserPosture, config.projectId);
|
|
196
|
+
if (config.browserPosture?.protectedRoutes?.length && !config.resolveDelegationChallenge) {
|
|
197
|
+
throw new Error(
|
|
198
|
+
"browserPosture.protectedRoutes requires resolveDelegationChallenge (see createBrowserIntegrityChallengeResolver)"
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
const opts = buildVerifyOpts(config);
|
|
202
|
+
const reporter = buildReporter(config, host.runtime);
|
|
203
|
+
const composed = host.buildComposedContext(config);
|
|
204
|
+
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
205
|
+
const translateOpts = {
|
|
206
|
+
drainJsonBody: config.drainJsonBody,
|
|
207
|
+
clientIpPolicy: config.clientIpPolicy
|
|
208
|
+
};
|
|
209
|
+
return async function checkpointMiddleware(req, event) {
|
|
210
|
+
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
211
|
+
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
212
|
+
trustedRootsResolver,
|
|
213
|
+
req.headers
|
|
214
|
+
);
|
|
215
|
+
const result = await verifyRequest(
|
|
216
|
+
httpLike,
|
|
217
|
+
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
218
|
+
);
|
|
219
|
+
await composedPolicy.applyBrowserPosture(
|
|
220
|
+
result,
|
|
221
|
+
req.headers.get("cookie"),
|
|
222
|
+
config.browserPosture,
|
|
223
|
+
config.projectId
|
|
224
|
+
);
|
|
225
|
+
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
226
|
+
composedPolicy.applyBrowserIntegrityStepUp(result, config.browserPosture, req.nextUrl.pathname);
|
|
227
|
+
if (reporter) {
|
|
228
|
+
const reportPromise = reporter(
|
|
229
|
+
result,
|
|
230
|
+
extractReporterContext(req, config.clientIpPolicy ?? { platform: "direct" })
|
|
231
|
+
);
|
|
232
|
+
if (event) {
|
|
233
|
+
event.waitUntil(reportPromise);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
await dispatchOnResult(config, result, req);
|
|
237
|
+
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
238
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
239
|
+
req.headers,
|
|
240
|
+
result.detectionDetail.detectionClass,
|
|
241
|
+
result.detectionDetail.isAgent
|
|
242
|
+
);
|
|
243
|
+
const challengeContextRequested = result.enforcementMode === "enforce" && result.decision.kind === "Challenge" && config.resolveDelegationChallenge !== void 0;
|
|
244
|
+
const delegationChallenge = challengeContextRequested ? await checkpointShared.resolveChallengeContext(
|
|
245
|
+
config.resolveDelegationChallenge,
|
|
246
|
+
{ result, request: req },
|
|
247
|
+
{
|
|
248
|
+
timeoutMs: config.delegationChallengeTimeoutMs,
|
|
249
|
+
onFailure: config.onDelegationChallengeFailure
|
|
250
|
+
}
|
|
251
|
+
) : void 0;
|
|
252
|
+
const rendered = renderDecisionAsResponse2(result, {
|
|
253
|
+
bodyReadable200,
|
|
254
|
+
delegationChallenge: composedPolicy.validateBrowserIntegrityChallenge(result, delegationChallenge),
|
|
255
|
+
challengeContextRequested
|
|
256
|
+
});
|
|
257
|
+
return adaptToNextResponse(rendered, req);
|
|
258
|
+
};
|
|
259
|
+
}
|
|
182
260
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
183
|
-
var VERSION = "1.
|
|
261
|
+
var VERSION = "1.9.0";
|
|
184
262
|
function buildReporter(config, runtime = "node") {
|
|
185
263
|
if (!config.apiKey) return null;
|
|
186
264
|
return reporter.makeDetectionReporter({
|
|
@@ -194,6 +272,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
194
272
|
});
|
|
195
273
|
}
|
|
196
274
|
function buildTrustedRootsResolver(config, composed) {
|
|
275
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
197
276
|
if (composed?.trustedDelegationRoots) {
|
|
198
277
|
return () => composed.trustedDelegationRoots();
|
|
199
278
|
}
|
|
@@ -206,10 +285,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
206
285
|
const projectId = config.projectId;
|
|
207
286
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
208
287
|
}
|
|
209
|
-
function extractReporterContext(req) {
|
|
288
|
+
function extractReporterContext(req, policy) {
|
|
289
|
+
const resolution = checkpointShared.resolveClientIp({
|
|
290
|
+
headers: req.headers,
|
|
291
|
+
frameworkAddress: req.ip,
|
|
292
|
+
policy
|
|
293
|
+
});
|
|
210
294
|
return {
|
|
211
295
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
212
|
-
ipAddress:
|
|
296
|
+
ipAddress: resolution.ip ?? void 0,
|
|
213
297
|
path: req.nextUrl.pathname,
|
|
214
298
|
url: req.nextUrl.href,
|
|
215
299
|
method: req.method
|
|
@@ -224,6 +308,8 @@ function buildVerifyOpts(config) {
|
|
|
224
308
|
policyEvaluator: overrides.policyEvaluator ?? adapters.makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
225
309
|
clock: adapters.makeSystemClock(),
|
|
226
310
|
tenantHost: config.tenantHost,
|
|
311
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
312
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
227
313
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
228
314
|
reputationBaseline: config.reputationBaseline,
|
|
229
315
|
argusUrl: config.argusUrl,
|
|
@@ -231,37 +317,6 @@ function buildVerifyOpts(config) {
|
|
|
231
317
|
engineConfig: config.engineConfig
|
|
232
318
|
};
|
|
233
319
|
}
|
|
234
|
-
|
|
235
|
-
// src/middleware-edge.ts
|
|
236
|
-
function withCheckpoint(config) {
|
|
237
|
-
void edge.initEngineEdge();
|
|
238
|
-
const opts = buildVerifyOpts(config);
|
|
239
|
-
const reporter = buildReporter(config, "edge");
|
|
240
|
-
const composed = buildComposedContext(config);
|
|
241
|
-
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
242
|
-
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
243
|
-
return async function checkpointMiddlewareEdge(req, event) {
|
|
244
|
-
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
245
|
-
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
246
|
-
trustedRootsResolver,
|
|
247
|
-
req.headers
|
|
248
|
-
);
|
|
249
|
-
const result = await edge.verifyRequestEdge(
|
|
250
|
-
httpLike,
|
|
251
|
-
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
252
|
-
);
|
|
253
|
-
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
254
|
-
if (reporter) {
|
|
255
|
-
const reportPromise = reporter(result, extractReporterContext(req));
|
|
256
|
-
if (event) {
|
|
257
|
-
event.waitUntil(reportPromise);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
await dispatchOnResult(config, result, req);
|
|
261
|
-
const rendered = edge.renderDecisionAsResponse(result);
|
|
262
|
-
return adaptToNextResponse(rendered, req);
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
320
|
async function dispatchOnResult(config, result, req) {
|
|
266
321
|
if (!config.onResult) return;
|
|
267
322
|
try {
|
|
@@ -269,6 +324,15 @@ async function dispatchOnResult(config, result, req) {
|
|
|
269
324
|
} catch {
|
|
270
325
|
}
|
|
271
326
|
}
|
|
327
|
+
|
|
328
|
+
// src/middleware-edge.ts
|
|
329
|
+
function withCheckpoint(config) {
|
|
330
|
+
void edge.initEngineEdge();
|
|
331
|
+
return makeNextMiddleware(edge.verifyRequestEdge, edge.renderDecisionAsResponse, config, {
|
|
332
|
+
runtime: "edge",
|
|
333
|
+
buildComposedContext
|
|
334
|
+
});
|
|
335
|
+
}
|
|
272
336
|
function buildComposedContext(config) {
|
|
273
337
|
if (config.composedPolicyEnforcer) return config.composedPolicyEnforcer;
|
|
274
338
|
if (!config.projectId || !config.cedarWasmModule) return null;
|