@kya-os/checkpoint-nextjs 1.7.1 → 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 +11 -11
- package/README.md +144 -251
- package/bin/setup-edge-wasm.js +9 -1
- 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 +5 -3
- package/dist/api-middleware.d.ts +5 -3
- package/dist/api-middleware.js +19 -32
- package/dist/api-middleware.mjs +20 -33
- package/dist/composed-policy.js +1 -0
- package/dist/composed-policy.mjs +2 -1
- package/dist/{config-DAwIA4DB.d.mts → config-CleTeeoW.d.mts} +32 -13
- package/dist/{config-DyU4l5er.d.ts → config-XMOIkxFN.d.ts} +32 -13
- 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 +126 -85
- package/dist/index.mjs +128 -87
- 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 +4 -40
- package/dist/middleware-edge.d.ts +4 -40
- package/dist/middleware-edge.js +148 -100
- package/dist/middleware-edge.mjs +151 -103
- package/dist/middleware-node.d.mts +15 -51
- package/dist/middleware-node.d.ts +15 -51
- package/dist/middleware-node.js +99 -56
- package/dist/middleware-node.mjs +102 -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
|
@@ -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';
|
|
7
8
|
import '@kya-os/checkpoint-shared';
|
|
8
9
|
import './composed-policy.mjs';
|
|
9
10
|
import '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
10
11
|
|
|
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
|
-
|
|
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';
|
|
7
8
|
import '@kya-os/checkpoint-shared';
|
|
8
9
|
import './composed-policy.js';
|
|
9
10
|
import '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
10
11
|
|
|
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
|
-
|
|
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,
|
|
@@ -148,8 +101,57 @@ var consoleComposedPolicyLogger = {
|
|
|
148
101
|
);
|
|
149
102
|
}
|
|
150
103
|
};
|
|
151
|
-
|
|
152
|
-
|
|
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
|
+
}
|
|
153
155
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
154
156
|
const url = new URL(req.url);
|
|
155
157
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -159,7 +161,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
159
161
|
url: url.pathname + url.search,
|
|
160
162
|
headers: headersToRecord(req.headers),
|
|
161
163
|
body,
|
|
162
|
-
remoteAddress: extractRemoteAddress(req)
|
|
164
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
163
165
|
};
|
|
164
166
|
}
|
|
165
167
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -179,19 +181,84 @@ function headersToRecord(headers) {
|
|
|
179
181
|
});
|
|
180
182
|
return out;
|
|
181
183
|
}
|
|
182
|
-
function extractRemoteAddress(req) {
|
|
183
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
184
|
-
if (xff) {
|
|
185
|
-
const first = xff.split(",")[0]?.trim();
|
|
186
|
-
if (first) return first;
|
|
187
|
-
}
|
|
184
|
+
function extractRemoteAddress(req, policy) {
|
|
188
185
|
const maybeIp = req.ip;
|
|
189
|
-
return
|
|
186
|
+
return checkpointShared.resolveClientIp({
|
|
187
|
+
headers: req.headers,
|
|
188
|
+
frameworkAddress: maybeIp,
|
|
189
|
+
policy
|
|
190
|
+
}).ip ?? void 0;
|
|
190
191
|
}
|
|
191
192
|
|
|
192
|
-
// 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
|
+
}
|
|
193
260
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
194
|
-
var VERSION = "1.
|
|
261
|
+
var VERSION = "1.9.0";
|
|
195
262
|
function buildReporter(config, runtime = "node") {
|
|
196
263
|
if (!config.apiKey) return null;
|
|
197
264
|
return reporter.makeDetectionReporter({
|
|
@@ -205,6 +272,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
205
272
|
});
|
|
206
273
|
}
|
|
207
274
|
function buildTrustedRootsResolver(config, composed) {
|
|
275
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
208
276
|
if (composed?.trustedDelegationRoots) {
|
|
209
277
|
return () => composed.trustedDelegationRoots();
|
|
210
278
|
}
|
|
@@ -217,10 +285,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
217
285
|
const projectId = config.projectId;
|
|
218
286
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
219
287
|
}
|
|
220
|
-
function extractReporterContext(req) {
|
|
288
|
+
function extractReporterContext(req, policy) {
|
|
289
|
+
const resolution = checkpointShared.resolveClientIp({
|
|
290
|
+
headers: req.headers,
|
|
291
|
+
frameworkAddress: req.ip,
|
|
292
|
+
policy
|
|
293
|
+
});
|
|
221
294
|
return {
|
|
222
295
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
223
|
-
ipAddress:
|
|
296
|
+
ipAddress: resolution.ip ?? void 0,
|
|
224
297
|
path: req.nextUrl.pathname,
|
|
225
298
|
url: req.nextUrl.href,
|
|
226
299
|
method: req.method
|
|
@@ -235,6 +308,8 @@ function buildVerifyOpts(config) {
|
|
|
235
308
|
policyEvaluator: overrides.policyEvaluator ?? adapters.makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
236
309
|
clock: adapters.makeSystemClock(),
|
|
237
310
|
tenantHost: config.tenantHost,
|
|
311
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
312
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
238
313
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
239
314
|
reputationBaseline: config.reputationBaseline,
|
|
240
315
|
argusUrl: config.argusUrl,
|
|
@@ -242,42 +317,6 @@ function buildVerifyOpts(config) {
|
|
|
242
317
|
engineConfig: config.engineConfig
|
|
243
318
|
};
|
|
244
319
|
}
|
|
245
|
-
|
|
246
|
-
// src/middleware-edge.ts
|
|
247
|
-
function withCheckpoint(config) {
|
|
248
|
-
void edge.initEngineEdge();
|
|
249
|
-
const opts = buildVerifyOpts(config);
|
|
250
|
-
const reporter = buildReporter(config, "edge");
|
|
251
|
-
const composed = buildComposedContext(config);
|
|
252
|
-
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
253
|
-
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
254
|
-
return async function checkpointMiddlewareEdge(req, event) {
|
|
255
|
-
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
256
|
-
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
257
|
-
trustedRootsResolver,
|
|
258
|
-
req.headers
|
|
259
|
-
);
|
|
260
|
-
const result = await edge.verifyRequestEdge(
|
|
261
|
-
httpLike,
|
|
262
|
-
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
263
|
-
);
|
|
264
|
-
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
265
|
-
if (reporter) {
|
|
266
|
-
const reportPromise = reporter(result, extractReporterContext(req));
|
|
267
|
-
if (event) {
|
|
268
|
-
event.waitUntil(reportPromise);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
await dispatchOnResult(config, result, req);
|
|
272
|
-
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
273
|
-
config.delegationChallengeMode ?? "spec-401",
|
|
274
|
-
req.headers,
|
|
275
|
-
result.detectionDetail.detectionClass
|
|
276
|
-
);
|
|
277
|
-
const rendered = edge.renderDecisionAsResponse(result, { bodyReadable200 });
|
|
278
|
-
return adaptToNextResponse(rendered, req);
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
320
|
async function dispatchOnResult(config, result, req) {
|
|
282
321
|
if (!config.onResult) return;
|
|
283
322
|
try {
|
|
@@ -285,6 +324,15 @@ async function dispatchOnResult(config, result, req) {
|
|
|
285
324
|
} catch {
|
|
286
325
|
}
|
|
287
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
|
+
}
|
|
288
336
|
function buildComposedContext(config) {
|
|
289
337
|
if (config.composedPolicyEnforcer) return config.composedPolicyEnforcer;
|
|
290
338
|
if (!config.projectId || !config.cedarWasmModule) return null;
|