@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
package/dist/index.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var orchestrator = require('@kya-os/checkpoint-wasm-runtime/orchestrator');
|
|
4
|
+
var browserPosture = require('@kya-os/checkpoint-shared/browser-posture');
|
|
5
|
+
var composedPolicy = require('@kya-os/checkpoint-wasm-runtime/composed-policy');
|
|
4
6
|
var adapters = require('@kya-os/checkpoint-wasm-runtime/adapters');
|
|
5
7
|
var reporter = require('@kya-os/checkpoint-wasm-runtime/reporter');
|
|
6
8
|
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
7
9
|
var server = require('next/server');
|
|
8
|
-
var
|
|
10
|
+
var functions = require('@vercel/functions');
|
|
9
11
|
|
|
10
12
|
// src/middleware-node.ts
|
|
11
13
|
function adaptToNextResponse(rendered, req) {
|
|
@@ -15,28 +17,28 @@ function adaptToNextResponse(rendered, req) {
|
|
|
15
17
|
switch (shape) {
|
|
16
18
|
case "pass-through": {
|
|
17
19
|
const res = server.NextResponse.next();
|
|
18
|
-
applyHeaders(res, rendered
|
|
20
|
+
applyHeaders(res, rendered);
|
|
19
21
|
setVerdictCookie(res, verdictCookie);
|
|
20
22
|
return res;
|
|
21
23
|
}
|
|
22
24
|
case "redirect": {
|
|
23
25
|
const target = new URL(rendered.headers.Location);
|
|
24
26
|
const res = server.NextResponse.redirect(target);
|
|
25
|
-
applyHeaders(res, rendered
|
|
27
|
+
applyHeaders(res, rendered);
|
|
26
28
|
setVerdictCookie(res, verdictCookie);
|
|
27
29
|
return res;
|
|
28
30
|
}
|
|
29
31
|
case "html-block": {
|
|
30
32
|
const blockedUrl = new URL(checkpointShared.BLOCKED_PATH, req.url);
|
|
31
33
|
const res = server.NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
32
|
-
applyHeaders(res, rendered
|
|
34
|
+
applyHeaders(res, rendered);
|
|
33
35
|
setVerdictCookie(res, verdictCookie);
|
|
34
36
|
return res;
|
|
35
37
|
}
|
|
36
38
|
case "json-block": {
|
|
37
39
|
const body = rendered.body ?? {};
|
|
38
40
|
const res = server.NextResponse.json(body, { status: rendered.status });
|
|
39
|
-
applyHeaders(res, rendered
|
|
41
|
+
applyHeaders(res, rendered);
|
|
40
42
|
setVerdictCookie(res, verdictCookie);
|
|
41
43
|
return res;
|
|
42
44
|
}
|
|
@@ -51,10 +53,13 @@ function setVerdictCookie(res, value) {
|
|
|
51
53
|
httpOnly: false
|
|
52
54
|
});
|
|
53
55
|
}
|
|
54
|
-
function applyHeaders(res,
|
|
55
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
56
|
+
function applyHeaders(res, rendered) {
|
|
57
|
+
for (const [key, value] of Object.entries(rendered.headers)) {
|
|
56
58
|
res.headers.set(key, value);
|
|
57
59
|
}
|
|
60
|
+
for (const [key, value] of rendered.headerEntries ?? []) {
|
|
61
|
+
res.headers.append(key, value);
|
|
62
|
+
}
|
|
58
63
|
}
|
|
59
64
|
var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
|
|
60
65
|
var NOOP_LOGGER = {
|
|
@@ -70,6 +75,7 @@ function makeComposedPolicyContext(opts) {
|
|
|
70
75
|
return {
|
|
71
76
|
async apply(result, path) {
|
|
72
77
|
const structured = { decision: result.decision, acted: false };
|
|
78
|
+
if (composedPolicy.isCredentialVerificationBlock(result.decision)) return structured;
|
|
73
79
|
const policy = await fetcher.getPolicy(projectId);
|
|
74
80
|
const outcome = await composedPolicy.evaluateComposedPolicy({
|
|
75
81
|
cache,
|
|
@@ -100,7 +106,13 @@ function makeComposedPolicyContext(opts) {
|
|
|
100
106
|
return structured;
|
|
101
107
|
}
|
|
102
108
|
if (outcome.status === "acting") {
|
|
103
|
-
return {
|
|
109
|
+
return {
|
|
110
|
+
decision: outcome.engineDecision,
|
|
111
|
+
acted: true,
|
|
112
|
+
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
113
|
+
// PR5). Conditional spread: absent upstream stays absent here.
|
|
114
|
+
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
115
|
+
};
|
|
104
116
|
}
|
|
105
117
|
return structured;
|
|
106
118
|
},
|
|
@@ -120,7 +132,12 @@ async function applyComposedPolicy(context, result, path) {
|
|
|
120
132
|
if (!context) return;
|
|
121
133
|
try {
|
|
122
134
|
const outcome = await context.apply(result, path);
|
|
123
|
-
if (outcome.acted)
|
|
135
|
+
if (outcome.acted) {
|
|
136
|
+
result.decision = outcome.decision;
|
|
137
|
+
if (outcome.composedBlobHash) {
|
|
138
|
+
result.composedBlobHash = outcome.composedBlobHash;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
124
141
|
} catch {
|
|
125
142
|
}
|
|
126
143
|
}
|
|
@@ -135,8 +152,6 @@ var consoleComposedPolicyLogger = {
|
|
|
135
152
|
);
|
|
136
153
|
}
|
|
137
154
|
};
|
|
138
|
-
|
|
139
|
-
// src/translate.ts
|
|
140
155
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
141
156
|
const url = new URL(req.url);
|
|
142
157
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -146,7 +161,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
146
161
|
url: url.pathname + url.search,
|
|
147
162
|
headers: headersToRecord(req.headers),
|
|
148
163
|
body,
|
|
149
|
-
remoteAddress: extractRemoteAddress(req)
|
|
164
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
150
165
|
};
|
|
151
166
|
}
|
|
152
167
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -166,47 +181,84 @@ function headersToRecord(headers) {
|
|
|
166
181
|
});
|
|
167
182
|
return out;
|
|
168
183
|
}
|
|
169
|
-
function extractRemoteAddress(req) {
|
|
170
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
171
|
-
if (xff) {
|
|
172
|
-
const first = xff.split(",")[0]?.trim();
|
|
173
|
-
if (first) return first;
|
|
174
|
-
}
|
|
184
|
+
function extractRemoteAddress(req, policy) {
|
|
175
185
|
const maybeIp = req.ip;
|
|
176
|
-
return
|
|
186
|
+
return checkpointShared.resolveClientIp({
|
|
187
|
+
headers: req.headers,
|
|
188
|
+
frameworkAddress: maybeIp,
|
|
189
|
+
policy
|
|
190
|
+
}).ip ?? void 0;
|
|
177
191
|
}
|
|
178
192
|
|
|
179
|
-
// src/middleware-
|
|
180
|
-
function
|
|
193
|
+
// src/middleware-core.ts
|
|
194
|
+
function makeNextMiddleware(verifyRequest2, 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
|
+
}
|
|
181
201
|
const opts = buildVerifyOpts(config);
|
|
182
|
-
const reporter = buildReporter(config);
|
|
183
|
-
const composed = buildComposedContext(config);
|
|
202
|
+
const reporter = buildReporter(config, host.runtime);
|
|
203
|
+
const composed = host.buildComposedContext(config);
|
|
184
204
|
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
185
|
-
const translateOpts = {
|
|
205
|
+
const translateOpts = {
|
|
206
|
+
drainJsonBody: config.drainJsonBody,
|
|
207
|
+
clientIpPolicy: config.clientIpPolicy
|
|
208
|
+
};
|
|
186
209
|
return async function checkpointMiddleware(req, event) {
|
|
187
210
|
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
188
211
|
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
189
212
|
trustedRootsResolver,
|
|
190
213
|
req.headers
|
|
191
214
|
);
|
|
192
|
-
const result = await
|
|
215
|
+
const result = await verifyRequest2(
|
|
193
216
|
httpLike,
|
|
194
217
|
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
195
218
|
);
|
|
219
|
+
await composedPolicy.applyBrowserPosture(
|
|
220
|
+
result,
|
|
221
|
+
req.headers.get("cookie"),
|
|
222
|
+
config.browserPosture,
|
|
223
|
+
config.projectId
|
|
224
|
+
);
|
|
196
225
|
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
226
|
+
composedPolicy.applyBrowserIntegrityStepUp(result, config.browserPosture, req.nextUrl.pathname);
|
|
197
227
|
if (reporter) {
|
|
198
|
-
const reportPromise = reporter(
|
|
228
|
+
const reportPromise = reporter(
|
|
229
|
+
result,
|
|
230
|
+
extractReporterContext(req, config.clientIpPolicy ?? { platform: "direct" })
|
|
231
|
+
);
|
|
199
232
|
if (event) {
|
|
200
233
|
event.waitUntil(reportPromise);
|
|
201
234
|
}
|
|
202
235
|
}
|
|
203
236
|
await dispatchOnResult(config, result, req);
|
|
204
|
-
const
|
|
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
|
+
});
|
|
205
257
|
return adaptToNextResponse(rendered, req);
|
|
206
258
|
};
|
|
207
259
|
}
|
|
208
260
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
209
|
-
var VERSION = "1.
|
|
261
|
+
var VERSION = "1.9.0";
|
|
210
262
|
function buildReporter(config, runtime = "node") {
|
|
211
263
|
if (!config.apiKey) return null;
|
|
212
264
|
return reporter.makeDetectionReporter({
|
|
@@ -220,6 +272,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
220
272
|
});
|
|
221
273
|
}
|
|
222
274
|
function buildTrustedRootsResolver(config, composed) {
|
|
275
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
223
276
|
if (composed?.trustedDelegationRoots) {
|
|
224
277
|
return () => composed.trustedDelegationRoots();
|
|
225
278
|
}
|
|
@@ -232,33 +285,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
232
285
|
const projectId = config.projectId;
|
|
233
286
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
234
287
|
}
|
|
235
|
-
function
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
fetcher: new checkpointShared.PolicyFetcher({
|
|
241
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
242
|
-
apiKey: config.apiKey,
|
|
243
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
244
|
-
}),
|
|
245
|
-
// LAZY dynamic import — NOT a top-level `import` — so the node-only
|
|
246
|
-
// `./policy` glue (`createRequire`/`fs` at module load) is never pulled into
|
|
247
|
-
// the Edge bundle. `middleware-edge.ts` imports helpers from this file, so a
|
|
248
|
-
// top-level `./policy` import would surface as a side-effect import in the
|
|
249
|
-
// edge bundle and boot-fail on Vercel edge. The import is cached after first
|
|
250
|
-
// call; the core's single-flight cache wraps the (now async) compile.
|
|
251
|
-
compile: async (_language, source) => {
|
|
252
|
-
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
253
|
-
return createPolicyEvaluator(source);
|
|
254
|
-
},
|
|
255
|
-
logger: config.debug ? consoleComposedPolicyLogger : void 0
|
|
288
|
+
function extractReporterContext(req, policy) {
|
|
289
|
+
const resolution = checkpointShared.resolveClientIp({
|
|
290
|
+
headers: req.headers,
|
|
291
|
+
frameworkAddress: req.ip,
|
|
292
|
+
policy
|
|
256
293
|
});
|
|
257
|
-
}
|
|
258
|
-
function extractReporterContext(req) {
|
|
259
294
|
return {
|
|
260
295
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
261
|
-
ipAddress:
|
|
296
|
+
ipAddress: resolution.ip ?? void 0,
|
|
262
297
|
path: req.nextUrl.pathname,
|
|
263
298
|
url: req.nextUrl.href,
|
|
264
299
|
method: req.method
|
|
@@ -273,6 +308,8 @@ function buildVerifyOpts(config) {
|
|
|
273
308
|
policyEvaluator: overrides.policyEvaluator ?? adapters.makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
274
309
|
clock: adapters.makeSystemClock(),
|
|
275
310
|
tenantHost: config.tenantHost,
|
|
311
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
312
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
276
313
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
277
314
|
reputationBaseline: config.reputationBaseline,
|
|
278
315
|
argusUrl: config.argusUrl,
|
|
@@ -287,6 +324,33 @@ async function dispatchOnResult(config, result, req) {
|
|
|
287
324
|
} catch {
|
|
288
325
|
}
|
|
289
326
|
}
|
|
327
|
+
function buildComposedContext(config) {
|
|
328
|
+
if (config.composedPolicyEnforcer) return config.composedPolicyEnforcer;
|
|
329
|
+
if (!config.projectId) return null;
|
|
330
|
+
return makeComposedPolicyContext({
|
|
331
|
+
projectId: config.projectId,
|
|
332
|
+
fetcher: new checkpointShared.PolicyFetcher({
|
|
333
|
+
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
334
|
+
apiKey: config.apiKey,
|
|
335
|
+
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
336
|
+
}),
|
|
337
|
+
// Load the Node policy compiler only on first use. This module is imported
|
|
338
|
+
// exclusively by the Node and bundler hosts, never by the Edge pipeline.
|
|
339
|
+
compile: async (_language, source) => {
|
|
340
|
+
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
341
|
+
return createPolicyEvaluator(source);
|
|
342
|
+
},
|
|
343
|
+
logger: config.debug ? consoleComposedPolicyLogger : void 0
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// src/middleware-node.ts
|
|
348
|
+
function withCheckpoint(config) {
|
|
349
|
+
return makeNextMiddleware(orchestrator.verifyRequest, orchestrator.renderDecisionAsResponse, config, {
|
|
350
|
+
runtime: "node",
|
|
351
|
+
buildComposedContext
|
|
352
|
+
});
|
|
353
|
+
}
|
|
290
354
|
var MIGRATION_ERROR = "@kya-os/checkpoint-nextjs's `createAgentShieldMiddleware` / `agentShield` were deleted in Phase D (engine consolidation). The 600-line TS pattern matcher that backed them is gone. Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs` (Node runtime) or `@kya-os/checkpoint-nextjs/edge` (Edge runtime). See packages/checkpoint-nextjs/CHANGELOG.md (1.0.0) for the recipe.";
|
|
291
355
|
function createAgentShieldMiddleware(_config = {}) {
|
|
292
356
|
throw new Error(MIGRATION_ERROR);
|
|
@@ -525,21 +589,9 @@ function resetCheckpointApiClient() {
|
|
|
525
589
|
var AgentShieldClient = CheckpointApiClient;
|
|
526
590
|
var getAgentShieldClient = getCheckpointApiClient;
|
|
527
591
|
var resetAgentShieldClient = resetCheckpointApiClient;
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
const forwardedFor = request.headers.get("x-forwarded-for");
|
|
532
|
-
if (forwardedFor) {
|
|
533
|
-
const ip = forwardedFor.split(",")[0]?.trim();
|
|
534
|
-
if (ip) return ip;
|
|
535
|
-
}
|
|
536
|
-
const realIp = request.headers.get("x-real-ip");
|
|
537
|
-
if (realIp) return realIp;
|
|
538
|
-
const cfIp = request.headers.get("cf-connecting-ip");
|
|
539
|
-
if (cfIp) return cfIp;
|
|
540
|
-
const clientIp = request.headers.get("x-client-ip");
|
|
541
|
-
if (clientIp) return clientIp;
|
|
542
|
-
return void 0;
|
|
592
|
+
function getClientIp(request, policy = { platform: "direct" }) {
|
|
593
|
+
const frameworkAddress = request.ip;
|
|
594
|
+
return checkpointShared.resolveClientIp({ headers: request.headers, frameworkAddress, policy }).ip ?? void 0;
|
|
543
595
|
}
|
|
544
596
|
function safeHostname(url) {
|
|
545
597
|
try {
|
|
@@ -550,9 +602,9 @@ function safeHostname(url) {
|
|
|
550
602
|
}
|
|
551
603
|
|
|
552
604
|
// src/responses/agent-instruction.ts
|
|
553
|
-
var MCP_I_DOCS_URL = "https://
|
|
605
|
+
var MCP_I_DOCS_URL = "https://kya.vouched.id/docs/enforce/kya-os-enforcement";
|
|
554
606
|
var DEFAULT_CONNECT_PATH = "/connect";
|
|
555
|
-
function buildAgentInstructionResponse(request, decision, redirectUrl) {
|
|
607
|
+
function buildAgentInstructionResponse(request, decision, redirectUrl, options) {
|
|
556
608
|
const resolved = resolveUrl(redirectUrl ?? DEFAULT_CONNECT_PATH, request.url);
|
|
557
609
|
const agentName = decision.agentName || decision.agentType || "unknown";
|
|
558
610
|
if (!resolved.searchParams.has("agent")) {
|
|
@@ -603,8 +655,8 @@ It only takes a moment and you won't need to do it again. Once you're done, ask
|
|
|
603
655
|
confidence: decision.confidence
|
|
604
656
|
}
|
|
605
657
|
};
|
|
606
|
-
const
|
|
607
|
-
response.
|
|
658
|
+
const bodyReadable200 = options?.bodyReadable200 ?? false;
|
|
659
|
+
const response = server.NextResponse.json(body, { status: bodyReadable200 ? 200 : 401 });
|
|
608
660
|
response.headers.set(
|
|
609
661
|
"Link",
|
|
610
662
|
`<${authUrl}>; rel="kya-authorize", <${MCP_I_DOCS_URL}>; rel="help"`
|
|
@@ -624,8 +676,6 @@ function resolveUrl(target, baseUrl) {
|
|
|
624
676
|
return new URL(DEFAULT_CONNECT_PATH, baseUrl);
|
|
625
677
|
}
|
|
626
678
|
}
|
|
627
|
-
|
|
628
|
-
// src/api-middleware.ts
|
|
629
679
|
function shouldSkipPath(path, skipPaths) {
|
|
630
680
|
return skipPaths.some((pattern) => checkpointShared.matchPath(path, pattern));
|
|
631
681
|
}
|
|
@@ -723,7 +773,7 @@ function withCheckpointApi(config = {}) {
|
|
|
723
773
|
try {
|
|
724
774
|
const client2 = getClient();
|
|
725
775
|
const userAgent = request.headers.get("user-agent") || void 0;
|
|
726
|
-
const ipAddress = getClientIp(request);
|
|
776
|
+
const ipAddress = getClientIp(request, config.clientIpPolicy);
|
|
727
777
|
const result = await client2.enforce({
|
|
728
778
|
headers: Object.fromEntries(request.headers.entries()),
|
|
729
779
|
userAgent,
|
|
@@ -762,14 +812,16 @@ function withCheckpointApi(config = {}) {
|
|
|
762
812
|
});
|
|
763
813
|
}
|
|
764
814
|
if (client2.isUsingEdge() && result.data.detection) {
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
815
|
+
functions.waitUntil(
|
|
816
|
+
client2.logDetection({
|
|
817
|
+
detection: result.data.detection,
|
|
818
|
+
context: { userAgent, ipAddress, path, url: request.url, method: request.method }
|
|
819
|
+
}).catch((err) => {
|
|
820
|
+
if (config.debug) {
|
|
821
|
+
console.error("[Checkpoint] Log detection failed:", err);
|
|
822
|
+
}
|
|
823
|
+
})
|
|
824
|
+
);
|
|
773
825
|
}
|
|
774
826
|
if (decision.isAgent && config.onAgentDetected) {
|
|
775
827
|
try {
|
|
@@ -797,7 +849,15 @@ function withCheckpointApi(config = {}) {
|
|
|
797
849
|
return buildRedirectResponse(request, decision, config);
|
|
798
850
|
}
|
|
799
851
|
const targetUrl = config.redirectUrl || decision.redirectUrl;
|
|
800
|
-
|
|
852
|
+
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
853
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
854
|
+
request.headers,
|
|
855
|
+
result.data.detection?.detectionClass,
|
|
856
|
+
result.data.detection?.isAgent ?? false
|
|
857
|
+
);
|
|
858
|
+
return buildAgentInstructionResponse(request, decision, targetUrl, {
|
|
859
|
+
bodyReadable200
|
|
860
|
+
});
|
|
801
861
|
}
|
|
802
862
|
case "challenge": {
|
|
803
863
|
return buildRedirectResponse(request, decision, config);
|
|
@@ -985,6 +1045,8 @@ var StatelessSessionChecker = class {
|
|
|
985
1045
|
return null;
|
|
986
1046
|
}
|
|
987
1047
|
}
|
|
1048
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- published
|
|
1049
|
+
// signature; narrowing it is a breaking change, deferred to a major.
|
|
988
1050
|
static setHeaders(response, session) {
|
|
989
1051
|
try {
|
|
990
1052
|
if (response.setHeader) {
|
|
@@ -1009,7 +1071,12 @@ function createContextFromDetection(detection, request) {
|
|
|
1009
1071
|
riskLevel: detection.riskLevel,
|
|
1010
1072
|
path: request.nextUrl.pathname,
|
|
1011
1073
|
method: request.method,
|
|
1012
|
-
|
|
1074
|
+
// Cryptographic-signature methods only, mirroring the composed-policy
|
|
1075
|
+
// VERIFIED_VERIFICATION_METHODS allowlist (checkpoint-wasm-runtime):
|
|
1076
|
+
// 'signature' (bare RFC 9421 keyid) and 'tier1_rfc9421' (RFC 9421 vendor-key
|
|
1077
|
+
// / Web Bot Auth, #4804) are the same crypto class; kya-http/kya-http-delegated
|
|
1078
|
+
// are the draft-kya-http-02 §4.3 request-signature methods.
|
|
1079
|
+
signatureVerified: detection.verificationMethod === "signature" || detection.verificationMethod === "tier1_rfc9421" || detection.verificationMethod === "kya-http" || detection.verificationMethod === "kya-http-delegated",
|
|
1013
1080
|
isAuthenticated: false,
|
|
1014
1081
|
// TODO: integrate with auth
|
|
1015
1082
|
userAgent: request.headers.get("user-agent") || void 0
|