@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
package/dist/adapt.js
CHANGED
|
@@ -11,28 +11,28 @@ function adaptToNextResponse(rendered, req) {
|
|
|
11
11
|
switch (shape) {
|
|
12
12
|
case "pass-through": {
|
|
13
13
|
const res = server.NextResponse.next();
|
|
14
|
-
applyHeaders(res, rendered
|
|
14
|
+
applyHeaders(res, rendered);
|
|
15
15
|
setVerdictCookie(res, verdictCookie);
|
|
16
16
|
return res;
|
|
17
17
|
}
|
|
18
18
|
case "redirect": {
|
|
19
19
|
const target = new URL(rendered.headers.Location);
|
|
20
20
|
const res = server.NextResponse.redirect(target);
|
|
21
|
-
applyHeaders(res, rendered
|
|
21
|
+
applyHeaders(res, rendered);
|
|
22
22
|
setVerdictCookie(res, verdictCookie);
|
|
23
23
|
return res;
|
|
24
24
|
}
|
|
25
25
|
case "html-block": {
|
|
26
26
|
const blockedUrl = new URL(checkpointShared.BLOCKED_PATH, req.url);
|
|
27
27
|
const res = server.NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
28
|
-
applyHeaders(res, rendered
|
|
28
|
+
applyHeaders(res, rendered);
|
|
29
29
|
setVerdictCookie(res, verdictCookie);
|
|
30
30
|
return res;
|
|
31
31
|
}
|
|
32
32
|
case "json-block": {
|
|
33
33
|
const body = rendered.body ?? {};
|
|
34
34
|
const res = server.NextResponse.json(body, { status: rendered.status });
|
|
35
|
-
applyHeaders(res, rendered
|
|
35
|
+
applyHeaders(res, rendered);
|
|
36
36
|
setVerdictCookie(res, verdictCookie);
|
|
37
37
|
return res;
|
|
38
38
|
}
|
|
@@ -47,10 +47,13 @@ function setVerdictCookie(res, value) {
|
|
|
47
47
|
httpOnly: false
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
|
-
function applyHeaders(res,
|
|
51
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
50
|
+
function applyHeaders(res, rendered) {
|
|
51
|
+
for (const [key, value] of Object.entries(rendered.headers)) {
|
|
52
52
|
res.headers.set(key, value);
|
|
53
53
|
}
|
|
54
|
+
for (const [key, value] of rendered.headerEntries ?? []) {
|
|
55
|
+
res.headers.append(key, value);
|
|
56
|
+
}
|
|
54
57
|
}
|
|
55
58
|
|
|
56
59
|
exports.adaptToNextResponse = adaptToNextResponse;
|
package/dist/adapt.mjs
CHANGED
|
@@ -9,28 +9,28 @@ function adaptToNextResponse(rendered, req) {
|
|
|
9
9
|
switch (shape) {
|
|
10
10
|
case "pass-through": {
|
|
11
11
|
const res = NextResponse.next();
|
|
12
|
-
applyHeaders(res, rendered
|
|
12
|
+
applyHeaders(res, rendered);
|
|
13
13
|
setVerdictCookie(res, verdictCookie);
|
|
14
14
|
return res;
|
|
15
15
|
}
|
|
16
16
|
case "redirect": {
|
|
17
17
|
const target = new URL(rendered.headers.Location);
|
|
18
18
|
const res = NextResponse.redirect(target);
|
|
19
|
-
applyHeaders(res, rendered
|
|
19
|
+
applyHeaders(res, rendered);
|
|
20
20
|
setVerdictCookie(res, verdictCookie);
|
|
21
21
|
return res;
|
|
22
22
|
}
|
|
23
23
|
case "html-block": {
|
|
24
24
|
const blockedUrl = new URL(BLOCKED_PATH, req.url);
|
|
25
25
|
const res = NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
26
|
-
applyHeaders(res, rendered
|
|
26
|
+
applyHeaders(res, rendered);
|
|
27
27
|
setVerdictCookie(res, verdictCookie);
|
|
28
28
|
return res;
|
|
29
29
|
}
|
|
30
30
|
case "json-block": {
|
|
31
31
|
const body = rendered.body ?? {};
|
|
32
32
|
const res = NextResponse.json(body, { status: rendered.status });
|
|
33
|
-
applyHeaders(res, rendered
|
|
33
|
+
applyHeaders(res, rendered);
|
|
34
34
|
setVerdictCookie(res, verdictCookie);
|
|
35
35
|
return res;
|
|
36
36
|
}
|
|
@@ -45,10 +45,13 @@ function setVerdictCookie(res, value) {
|
|
|
45
45
|
httpOnly: false
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
function applyHeaders(res,
|
|
49
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
48
|
+
function applyHeaders(res, rendered) {
|
|
49
|
+
for (const [key, value] of Object.entries(rendered.headers)) {
|
|
50
50
|
res.headers.set(key, value);
|
|
51
51
|
}
|
|
52
|
+
for (const [key, value] of rendered.headerEntries ?? []) {
|
|
53
|
+
res.headers.append(key, value);
|
|
54
|
+
}
|
|
52
55
|
}
|
|
53
56
|
|
|
54
57
|
export { adaptToNextResponse };
|
package/dist/api-client.d.mts
CHANGED
|
@@ -65,6 +65,15 @@ type ApiDetectionDetail = Omit<DetectionDetail, 'detectionClass' | 'verification
|
|
|
65
65
|
timestamp?: DetectionDetail['timestamp'];
|
|
66
66
|
/** Detection engine used: 'wasm' or 'javascript-fallback' */
|
|
67
67
|
detectionMethod?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Ordered `detection/*` reason codes (KYA-1107 signal exposure; carried
|
|
70
|
+
* on every enforce detection payload alongside the populated `signals`
|
|
71
|
+
* list). Deliberately `string[]` rather than
|
|
72
|
+
* the registry union so an older SDK build tolerates codes added
|
|
73
|
+
* server-side later; the registry lives in
|
|
74
|
+
* `@kya-os/checkpoint-shared` (`DETECTION_REASON_CODES`).
|
|
75
|
+
*/
|
|
76
|
+
reasonCodes?: string[];
|
|
68
77
|
};
|
|
69
78
|
|
|
70
79
|
/**
|
package/dist/api-client.d.ts
CHANGED
|
@@ -65,6 +65,15 @@ type ApiDetectionDetail = Omit<DetectionDetail, 'detectionClass' | 'verification
|
|
|
65
65
|
timestamp?: DetectionDetail['timestamp'];
|
|
66
66
|
/** Detection engine used: 'wasm' or 'javascript-fallback' */
|
|
67
67
|
detectionMethod?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Ordered `detection/*` reason codes (KYA-1107 signal exposure; carried
|
|
70
|
+
* on every enforce detection payload alongside the populated `signals`
|
|
71
|
+
* list). Deliberately `string[]` rather than
|
|
72
|
+
* the registry union so an older SDK build tolerates codes added
|
|
73
|
+
* server-side later; the registry lives in
|
|
74
|
+
* `@kya-os/checkpoint-shared` (`DETECTION_REASON_CODES`).
|
|
75
|
+
*/
|
|
76
|
+
reasonCodes?: string[];
|
|
68
77
|
};
|
|
69
78
|
|
|
70
79
|
/**
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
2
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
3
3
|
import { EnforcementDecision } from './api-client.mjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -36,6 +36,8 @@ import { EnforcementDecision } from './api-client.mjs';
|
|
|
36
36
|
* Middleware configuration
|
|
37
37
|
*/
|
|
38
38
|
interface CheckpointApiMiddlewareConfig {
|
|
39
|
+
/** Explicit deployment/proxy trust profile for client-IP resolution. */
|
|
40
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
39
41
|
/** API key (or use CHECKPOINT_API_KEY env var) */
|
|
40
42
|
apiKey?: string;
|
|
41
43
|
/** API base URL (defaults to production) */
|
|
@@ -66,7 +68,7 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
66
68
|
/**
|
|
67
69
|
* How the middleware handles a `redirect` / `instruct` action.
|
|
68
70
|
*
|
|
69
|
-
* - `'instruct'` (default): return HTTP 401 with
|
|
71
|
+
* - `'instruct'` (default): return HTTP 401 with a KYA-OS Link header + JSON
|
|
70
72
|
* body pointing the agent at the redirect URL. LLMs surface the URL as a
|
|
71
73
|
* clickable link for the user. Matches the Cloudflare Gateway contract.
|
|
72
74
|
* - `'http'`: legacy behavior — return HTTP 302 with `Location`. Most LLM
|
|
@@ -80,7 +82,7 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
80
82
|
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
81
83
|
* `negotiated` serves a body-readable HTTP 200 instruct response to
|
|
82
84
|
* cooperative agents (so fetchers that drop 4xx bodies can read it),
|
|
83
|
-
* keeping the spec 401 for everyone else. Defaults to `
|
|
85
|
+
* keeping the spec 401 for everyone else. Defaults to `negotiated` (#3530).
|
|
84
86
|
* A cooperative-UX bridge, NOT an access control.
|
|
85
87
|
*/
|
|
86
88
|
delegationChallengeMode?: ChallengeEnvelopeMode;
|
package/dist/api-middleware.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
2
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
3
3
|
import { EnforcementDecision } from './api-client.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -36,6 +36,8 @@ import { EnforcementDecision } from './api-client.js';
|
|
|
36
36
|
* Middleware configuration
|
|
37
37
|
*/
|
|
38
38
|
interface CheckpointApiMiddlewareConfig {
|
|
39
|
+
/** Explicit deployment/proxy trust profile for client-IP resolution. */
|
|
40
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
39
41
|
/** API key (or use CHECKPOINT_API_KEY env var) */
|
|
40
42
|
apiKey?: string;
|
|
41
43
|
/** API base URL (defaults to production) */
|
|
@@ -66,7 +68,7 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
66
68
|
/**
|
|
67
69
|
* How the middleware handles a `redirect` / `instruct` action.
|
|
68
70
|
*
|
|
69
|
-
* - `'instruct'` (default): return HTTP 401 with
|
|
71
|
+
* - `'instruct'` (default): return HTTP 401 with a KYA-OS Link header + JSON
|
|
70
72
|
* body pointing the agent at the redirect URL. LLMs surface the URL as a
|
|
71
73
|
* clickable link for the user. Matches the Cloudflare Gateway contract.
|
|
72
74
|
* - `'http'`: legacy behavior — return HTTP 302 with `Location`. Most LLM
|
|
@@ -80,7 +82,7 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
80
82
|
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
81
83
|
* `negotiated` serves a body-readable HTTP 200 instruct response to
|
|
82
84
|
* cooperative agents (so fetchers that drop 4xx bodies can read it),
|
|
83
|
-
* keeping the spec 401 for everyone else. Defaults to `
|
|
85
|
+
* keeping the spec 401 for everyone else. Defaults to `negotiated` (#3530).
|
|
84
86
|
* A cooperative-UX bridge, NOT an access control.
|
|
85
87
|
*/
|
|
86
88
|
delegationChallengeMode?: ChallengeEnvelopeMode;
|
package/dist/api-middleware.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var server = require('next/server');
|
|
4
4
|
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
5
|
+
var functions = require('@vercel/functions');
|
|
5
6
|
|
|
6
7
|
// src/api-middleware.ts
|
|
7
8
|
|
|
@@ -210,21 +211,9 @@ function getCheckpointApiClient(config) {
|
|
|
210
211
|
}
|
|
211
212
|
return clientInstance;
|
|
212
213
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const forwardedFor = request.headers.get("x-forwarded-for");
|
|
217
|
-
if (forwardedFor) {
|
|
218
|
-
const ip = forwardedFor.split(",")[0]?.trim();
|
|
219
|
-
if (ip) return ip;
|
|
220
|
-
}
|
|
221
|
-
const realIp = request.headers.get("x-real-ip");
|
|
222
|
-
if (realIp) return realIp;
|
|
223
|
-
const cfIp = request.headers.get("cf-connecting-ip");
|
|
224
|
-
if (cfIp) return cfIp;
|
|
225
|
-
const clientIp = request.headers.get("x-client-ip");
|
|
226
|
-
if (clientIp) return clientIp;
|
|
227
|
-
return void 0;
|
|
214
|
+
function getClientIp(request, policy = { platform: "direct" }) {
|
|
215
|
+
const frameworkAddress = request.ip;
|
|
216
|
+
return checkpointShared.resolveClientIp({ headers: request.headers, frameworkAddress, policy }).ip ?? void 0;
|
|
228
217
|
}
|
|
229
218
|
function safeHostname(url) {
|
|
230
219
|
try {
|
|
@@ -235,7 +224,7 @@ function safeHostname(url) {
|
|
|
235
224
|
}
|
|
236
225
|
|
|
237
226
|
// src/responses/agent-instruction.ts
|
|
238
|
-
var MCP_I_DOCS_URL = "https://
|
|
227
|
+
var MCP_I_DOCS_URL = "https://kya.vouched.id/docs/enforce/kya-os-enforcement";
|
|
239
228
|
var DEFAULT_CONNECT_PATH = "/connect";
|
|
240
229
|
function buildAgentInstructionResponse(request, decision, redirectUrl, options) {
|
|
241
230
|
const resolved = resolveUrl(redirectUrl ?? DEFAULT_CONNECT_PATH, request.url);
|
|
@@ -290,9 +279,6 @@ It only takes a moment and you won't need to do it again. Once you're done, ask
|
|
|
290
279
|
};
|
|
291
280
|
const bodyReadable200 = options?.bodyReadable200 ?? false;
|
|
292
281
|
const response = server.NextResponse.json(body, { status: bodyReadable200 ? 200 : 401 });
|
|
293
|
-
if (!bodyReadable200) {
|
|
294
|
-
response.headers.set("WWW-Authenticate", `KYA realm="api", authorization_uri="${authUrl}"`);
|
|
295
|
-
}
|
|
296
282
|
response.headers.set(
|
|
297
283
|
"Link",
|
|
298
284
|
`<${authUrl}>; rel="kya-authorize", <${MCP_I_DOCS_URL}>; rel="help"`
|
|
@@ -312,8 +298,6 @@ function resolveUrl(target, baseUrl) {
|
|
|
312
298
|
return new URL(DEFAULT_CONNECT_PATH, baseUrl);
|
|
313
299
|
}
|
|
314
300
|
}
|
|
315
|
-
|
|
316
|
-
// src/api-middleware.ts
|
|
317
301
|
function shouldSkipPath(path, skipPaths) {
|
|
318
302
|
return skipPaths.some((pattern) => checkpointShared.matchPath(path, pattern));
|
|
319
303
|
}
|
|
@@ -411,7 +395,7 @@ function withCheckpointApi(config = {}) {
|
|
|
411
395
|
try {
|
|
412
396
|
const client2 = getClient();
|
|
413
397
|
const userAgent = request.headers.get("user-agent") || void 0;
|
|
414
|
-
const ipAddress = getClientIp(request);
|
|
398
|
+
const ipAddress = getClientIp(request, config.clientIpPolicy);
|
|
415
399
|
const result = await client2.enforce({
|
|
416
400
|
headers: Object.fromEntries(request.headers.entries()),
|
|
417
401
|
userAgent,
|
|
@@ -450,14 +434,16 @@ function withCheckpointApi(config = {}) {
|
|
|
450
434
|
});
|
|
451
435
|
}
|
|
452
436
|
if (client2.isUsingEdge() && result.data.detection) {
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
437
|
+
functions.waitUntil(
|
|
438
|
+
client2.logDetection({
|
|
439
|
+
detection: result.data.detection,
|
|
440
|
+
context: { userAgent, ipAddress, path, url: request.url, method: request.method }
|
|
441
|
+
}).catch((err) => {
|
|
442
|
+
if (config.debug) {
|
|
443
|
+
console.error("[Checkpoint] Log detection failed:", err);
|
|
444
|
+
}
|
|
445
|
+
})
|
|
446
|
+
);
|
|
461
447
|
}
|
|
462
448
|
if (decision.isAgent && config.onAgentDetected) {
|
|
463
449
|
try {
|
|
@@ -486,9 +472,10 @@ function withCheckpointApi(config = {}) {
|
|
|
486
472
|
}
|
|
487
473
|
const targetUrl = config.redirectUrl || decision.redirectUrl;
|
|
488
474
|
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
489
|
-
config.delegationChallengeMode ?? "
|
|
475
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
490
476
|
request.headers,
|
|
491
|
-
result.data.detection?.detectionClass
|
|
477
|
+
result.data.detection?.detectionClass,
|
|
478
|
+
result.data.detection?.isAgent ?? false
|
|
492
479
|
);
|
|
493
480
|
return buildAgentInstructionResponse(request, decision, targetUrl, {
|
|
494
481
|
bodyReadable200
|
package/dist/api-middleware.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
|
-
import { selectBodyReadable200, matchPath } from '@kya-os/checkpoint-shared';
|
|
2
|
+
import { selectBodyReadable200, matchPath, resolveClientIp } from '@kya-os/checkpoint-shared';
|
|
3
|
+
import { waitUntil } from '@vercel/functions';
|
|
3
4
|
|
|
4
5
|
// src/api-middleware.ts
|
|
5
6
|
|
|
@@ -208,21 +209,9 @@ function getCheckpointApiClient(config) {
|
|
|
208
209
|
}
|
|
209
210
|
return clientInstance;
|
|
210
211
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const forwardedFor = request.headers.get("x-forwarded-for");
|
|
215
|
-
if (forwardedFor) {
|
|
216
|
-
const ip = forwardedFor.split(",")[0]?.trim();
|
|
217
|
-
if (ip) return ip;
|
|
218
|
-
}
|
|
219
|
-
const realIp = request.headers.get("x-real-ip");
|
|
220
|
-
if (realIp) return realIp;
|
|
221
|
-
const cfIp = request.headers.get("cf-connecting-ip");
|
|
222
|
-
if (cfIp) return cfIp;
|
|
223
|
-
const clientIp = request.headers.get("x-client-ip");
|
|
224
|
-
if (clientIp) return clientIp;
|
|
225
|
-
return void 0;
|
|
212
|
+
function getClientIp(request, policy = { platform: "direct" }) {
|
|
213
|
+
const frameworkAddress = request.ip;
|
|
214
|
+
return resolveClientIp({ headers: request.headers, frameworkAddress, policy }).ip ?? void 0;
|
|
226
215
|
}
|
|
227
216
|
function safeHostname(url) {
|
|
228
217
|
try {
|
|
@@ -233,7 +222,7 @@ function safeHostname(url) {
|
|
|
233
222
|
}
|
|
234
223
|
|
|
235
224
|
// src/responses/agent-instruction.ts
|
|
236
|
-
var MCP_I_DOCS_URL = "https://
|
|
225
|
+
var MCP_I_DOCS_URL = "https://kya.vouched.id/docs/enforce/kya-os-enforcement";
|
|
237
226
|
var DEFAULT_CONNECT_PATH = "/connect";
|
|
238
227
|
function buildAgentInstructionResponse(request, decision, redirectUrl, options) {
|
|
239
228
|
const resolved = resolveUrl(redirectUrl ?? DEFAULT_CONNECT_PATH, request.url);
|
|
@@ -288,9 +277,6 @@ It only takes a moment and you won't need to do it again. Once you're done, ask
|
|
|
288
277
|
};
|
|
289
278
|
const bodyReadable200 = options?.bodyReadable200 ?? false;
|
|
290
279
|
const response = NextResponse.json(body, { status: bodyReadable200 ? 200 : 401 });
|
|
291
|
-
if (!bodyReadable200) {
|
|
292
|
-
response.headers.set("WWW-Authenticate", `KYA realm="api", authorization_uri="${authUrl}"`);
|
|
293
|
-
}
|
|
294
280
|
response.headers.set(
|
|
295
281
|
"Link",
|
|
296
282
|
`<${authUrl}>; rel="kya-authorize", <${MCP_I_DOCS_URL}>; rel="help"`
|
|
@@ -310,8 +296,6 @@ function resolveUrl(target, baseUrl) {
|
|
|
310
296
|
return new URL(DEFAULT_CONNECT_PATH, baseUrl);
|
|
311
297
|
}
|
|
312
298
|
}
|
|
313
|
-
|
|
314
|
-
// src/api-middleware.ts
|
|
315
299
|
function shouldSkipPath(path, skipPaths) {
|
|
316
300
|
return skipPaths.some((pattern) => matchPath(path, pattern));
|
|
317
301
|
}
|
|
@@ -409,7 +393,7 @@ function withCheckpointApi(config = {}) {
|
|
|
409
393
|
try {
|
|
410
394
|
const client2 = getClient();
|
|
411
395
|
const userAgent = request.headers.get("user-agent") || void 0;
|
|
412
|
-
const ipAddress = getClientIp(request);
|
|
396
|
+
const ipAddress = getClientIp(request, config.clientIpPolicy);
|
|
413
397
|
const result = await client2.enforce({
|
|
414
398
|
headers: Object.fromEntries(request.headers.entries()),
|
|
415
399
|
userAgent,
|
|
@@ -448,14 +432,16 @@ function withCheckpointApi(config = {}) {
|
|
|
448
432
|
});
|
|
449
433
|
}
|
|
450
434
|
if (client2.isUsingEdge() && result.data.detection) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
435
|
+
waitUntil(
|
|
436
|
+
client2.logDetection({
|
|
437
|
+
detection: result.data.detection,
|
|
438
|
+
context: { userAgent, ipAddress, path, url: request.url, method: request.method }
|
|
439
|
+
}).catch((err) => {
|
|
440
|
+
if (config.debug) {
|
|
441
|
+
console.error("[Checkpoint] Log detection failed:", err);
|
|
442
|
+
}
|
|
443
|
+
})
|
|
444
|
+
);
|
|
459
445
|
}
|
|
460
446
|
if (decision.isAgent && config.onAgentDetected) {
|
|
461
447
|
try {
|
|
@@ -484,9 +470,10 @@ function withCheckpointApi(config = {}) {
|
|
|
484
470
|
}
|
|
485
471
|
const targetUrl = config.redirectUrl || decision.redirectUrl;
|
|
486
472
|
const bodyReadable200 = selectBodyReadable200(
|
|
487
|
-
config.delegationChallengeMode ?? "
|
|
473
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
488
474
|
request.headers,
|
|
489
|
-
result.data.detection?.detectionClass
|
|
475
|
+
result.data.detection?.detectionClass,
|
|
476
|
+
result.data.detection?.isAgent ?? false
|
|
490
477
|
);
|
|
491
478
|
return buildAgentInstructionResponse(request, decision, targetUrl, {
|
|
492
479
|
bodyReadable200
|
package/dist/composed-policy.js
CHANGED
|
@@ -18,6 +18,7 @@ function makeComposedPolicyContext(opts) {
|
|
|
18
18
|
return {
|
|
19
19
|
async apply(result, path) {
|
|
20
20
|
const structured = { decision: result.decision, acted: false };
|
|
21
|
+
if (composedPolicy.isCredentialVerificationBlock(result.decision)) return structured;
|
|
21
22
|
const policy = await fetcher.getPolicy(projectId);
|
|
22
23
|
const outcome = await composedPolicy.evaluateComposedPolicy({
|
|
23
24
|
cache,
|
package/dist/composed-policy.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { makeComposedPolicyCache, evaluateComposedPolicy, verifyResultToAuthorizeInput } from '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
1
|
+
import { makeComposedPolicyCache, isCredentialVerificationBlock, evaluateComposedPolicy, verifyResultToAuthorizeInput } from '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
2
2
|
import { requestCarriesDelegationProof } from '@kya-os/checkpoint-shared';
|
|
3
3
|
|
|
4
4
|
// src/composed-policy.ts
|
|
@@ -16,6 +16,7 @@ function makeComposedPolicyContext(opts) {
|
|
|
16
16
|
return {
|
|
17
17
|
async apply(result, path) {
|
|
18
18
|
const structured = { decision: result.decision, acted: false };
|
|
19
|
+
if (isCredentialVerificationBlock(result.decision)) return structured;
|
|
19
20
|
const policy = await fetcher.getPolicy(projectId);
|
|
20
21
|
const outcome = await evaluateComposedPolicy({
|
|
21
22
|
cache,
|
|
@@ -1,17 +1,10 @@
|
|
|
1
|
+
import { BrowserPostureConfig } from '@kya-os/checkpoint-shared/browser-posture';
|
|
1
2
|
import { NextRequest } from 'next/server';
|
|
2
3
|
import { DidResolverAdapter, StatusListCacheAdapter, ReputationOracleAdapter, PolicyEvaluatorAdapter } from '@kya-os/checkpoint-wasm-runtime/adapters';
|
|
3
4
|
import { EnforcementMode, VerifyResult, EngineConfig } from '@kya-os/checkpoint-wasm-runtime/engine';
|
|
4
|
-
import { ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
5
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode, ChallengeWireInput, ChallengeContextFailureEvent } from '@kya-os/checkpoint-shared';
|
|
5
6
|
import { ComposedPolicyContext } from './composed-policy.mjs';
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
* `CheckpointConfig` — the `withCheckpoint` config contract for both the Node
|
|
9
|
-
* (`./node`) and Edge (`./edge`) middleware entries. Extracted from
|
|
10
|
-
* `middleware-node.ts` (a 190-line documented contract); both entries import it
|
|
11
|
-
* from here and re-export it so the public import paths
|
|
12
|
-
* (`@kya-os/checkpoint-nextjs`, `/node`, `/edge`) are unchanged.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
8
|
/**
|
|
16
9
|
* Configuration for `withCheckpoint`.
|
|
17
10
|
*
|
|
@@ -20,12 +13,23 @@ import { ComposedPolicyContext } from './composed-policy.mjs';
|
|
|
20
13
|
* exported during the deprecation window — see D.4 cutover.
|
|
21
14
|
*/
|
|
22
15
|
interface CheckpointConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Selects the infrastructure allowed to vouch for client IP headers.
|
|
18
|
+
* Defaults to direct peer/framework resolution; set `platform: 'vercel'`
|
|
19
|
+
* for Vercel-hosted requests or `platform: 'trusted-proxy'` with an
|
|
20
|
+
* explicit hop count for a configured proxy chain.
|
|
21
|
+
*/
|
|
22
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
23
23
|
/**
|
|
24
24
|
* Tenant identifier — typically the customer's dashboard hostname
|
|
25
25
|
* (e.g. `acme.checkpoint.example`). The PolicyEvaluator uses this
|
|
26
26
|
* to look up tenant policy from the dashboard.
|
|
27
27
|
*/
|
|
28
28
|
tenantHost: string;
|
|
29
|
+
/** Exact org DID used to verify the consent credential's audience. */
|
|
30
|
+
resourceOrgDid?: string;
|
|
31
|
+
/** Explicit trusted delegation issuers; otherwise resolved from project policy. */
|
|
32
|
+
trustedDelegationRoots?: string[];
|
|
29
33
|
/**
|
|
30
34
|
* `'enforce'` (default) blocks; `'observe'` passes everything
|
|
31
35
|
* through with `X-Checkpoint-Would-Have-Been` headers. Per Phase 0.2.
|
|
@@ -35,10 +39,23 @@ interface CheckpointConfig {
|
|
|
35
39
|
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
36
40
|
* `negotiated` serves a body-readable HTTP 200 step-up response to
|
|
37
41
|
* cooperative agents (fetchers that drop 4xx/5xx bodies), keeping the spec
|
|
38
|
-
* 401/422 for everyone else. Defaults to `
|
|
42
|
+
* 401/422 for everyone else. Defaults to `negotiated` (#3530). A cooperative-UX
|
|
39
43
|
* bridge, NOT an access control.
|
|
40
44
|
*/
|
|
41
45
|
delegationChallengeMode?: ChallengeEnvelopeMode;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve a policy `Challenge` into complete registered wire context.
|
|
48
|
+
* The callback performs any Authorization-Host registration asynchronously;
|
|
49
|
+
* the synchronous engine renderer never fabricates a sid, consent URI, or proof.
|
|
50
|
+
*/
|
|
51
|
+
resolveDelegationChallenge?: (context: {
|
|
52
|
+
result: VerifyResult;
|
|
53
|
+
request: NextRequest;
|
|
54
|
+
}) => ChallengeWireInput | undefined | Promise<ChallengeWireInput | undefined>;
|
|
55
|
+
/** Registration/resolution budget. Defaults to 3000 milliseconds. */
|
|
56
|
+
delegationChallengeTimeoutMs?: number;
|
|
57
|
+
/** Sanitized registration failure hook for metrics and operator warnings. */
|
|
58
|
+
onDelegationChallengeFailure?: (event: ChallengeContextFailureEvent) => void;
|
|
42
59
|
/**
|
|
43
60
|
* Argus reputation oracle base URL. Omit to use the trust-by-default
|
|
44
61
|
* baseline (reputation defaults to 1.0; orchestrator logs a one-shot
|
|
@@ -80,7 +97,7 @@ interface CheckpointConfig {
|
|
|
80
97
|
* canonical `_meta.proof.jws` body form. Default `false`.
|
|
81
98
|
*
|
|
82
99
|
* **When to enable** — customers whose agents pre-date Envelope-1
|
|
83
|
-
* (#2537) and ship
|
|
100
|
+
* (#2537) and ship KYA-OS proofs as `{protected,payload,signature}`
|
|
84
101
|
* JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
|
|
85
102
|
* ship compact JWS in the request body's `_meta.proof.jws` field;
|
|
86
103
|
* those don't need this flag.
|
|
@@ -95,12 +112,12 @@ interface CheckpointConfig {
|
|
|
95
112
|
legacyEnvelopeFallback?: boolean;
|
|
96
113
|
/**
|
|
97
114
|
* Read the request body when `content-type` is `application/json` so
|
|
98
|
-
* the orchestrator can extract
|
|
115
|
+
* the orchestrator can extract a KYA-OS envelope from
|
|
99
116
|
* `_meta.proof.jws`. Default `true`.
|
|
100
117
|
*
|
|
101
118
|
* **When to disable** — streaming middlewares that can't tolerate
|
|
102
119
|
* the `req.clone()` memory overhead (one full-body copy is buffered
|
|
103
|
-
* during the read). For those, set `false` and route
|
|
120
|
+
* during the read). For those, set `false` and route KYA-OS
|
|
104
121
|
* envelopes through the `KYA-Delegation` header transport instead
|
|
105
122
|
* (requires `legacyEnvelopeFallback: true`).
|
|
106
123
|
*
|
|
@@ -171,6 +188,8 @@ interface CheckpointConfig {
|
|
|
171
188
|
* Added in `@kya-os/checkpoint-nextjs@1.5.0` (@Policy middleware-Cedar, #3076).
|
|
172
189
|
*/
|
|
173
190
|
projectId?: string;
|
|
191
|
+
/** Signed browser risk; configure explicit route scopes to enable consent step-up. */
|
|
192
|
+
browserPosture?: BrowserPostureConfig;
|
|
174
193
|
/**
|
|
175
194
|
* Policy-fetch cache TTL in seconds. Defaults to 300 (5 minutes). How long
|
|
176
195
|
* a fetched project policy is reused before the middleware refetches from
|
|
@@ -1,17 +1,10 @@
|
|
|
1
|
+
import { BrowserPostureConfig } from '@kya-os/checkpoint-shared/browser-posture';
|
|
1
2
|
import { NextRequest } from 'next/server';
|
|
2
3
|
import { DidResolverAdapter, StatusListCacheAdapter, ReputationOracleAdapter, PolicyEvaluatorAdapter } from '@kya-os/checkpoint-wasm-runtime/adapters';
|
|
3
4
|
import { EnforcementMode, VerifyResult, EngineConfig } from '@kya-os/checkpoint-wasm-runtime/engine';
|
|
4
|
-
import { ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
5
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode, ChallengeWireInput, ChallengeContextFailureEvent } from '@kya-os/checkpoint-shared';
|
|
5
6
|
import { ComposedPolicyContext } from './composed-policy.js';
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
* `CheckpointConfig` — the `withCheckpoint` config contract for both the Node
|
|
9
|
-
* (`./node`) and Edge (`./edge`) middleware entries. Extracted from
|
|
10
|
-
* `middleware-node.ts` (a 190-line documented contract); both entries import it
|
|
11
|
-
* from here and re-export it so the public import paths
|
|
12
|
-
* (`@kya-os/checkpoint-nextjs`, `/node`, `/edge`) are unchanged.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
8
|
/**
|
|
16
9
|
* Configuration for `withCheckpoint`.
|
|
17
10
|
*
|
|
@@ -20,12 +13,23 @@ import { ComposedPolicyContext } from './composed-policy.js';
|
|
|
20
13
|
* exported during the deprecation window — see D.4 cutover.
|
|
21
14
|
*/
|
|
22
15
|
interface CheckpointConfig {
|
|
16
|
+
/**
|
|
17
|
+
* Selects the infrastructure allowed to vouch for client IP headers.
|
|
18
|
+
* Defaults to direct peer/framework resolution; set `platform: 'vercel'`
|
|
19
|
+
* for Vercel-hosted requests or `platform: 'trusted-proxy'` with an
|
|
20
|
+
* explicit hop count for a configured proxy chain.
|
|
21
|
+
*/
|
|
22
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
23
23
|
/**
|
|
24
24
|
* Tenant identifier — typically the customer's dashboard hostname
|
|
25
25
|
* (e.g. `acme.checkpoint.example`). The PolicyEvaluator uses this
|
|
26
26
|
* to look up tenant policy from the dashboard.
|
|
27
27
|
*/
|
|
28
28
|
tenantHost: string;
|
|
29
|
+
/** Exact org DID used to verify the consent credential's audience. */
|
|
30
|
+
resourceOrgDid?: string;
|
|
31
|
+
/** Explicit trusted delegation issuers; otherwise resolved from project policy. */
|
|
32
|
+
trustedDelegationRoots?: string[];
|
|
29
33
|
/**
|
|
30
34
|
* `'enforce'` (default) blocks; `'observe'` passes everything
|
|
31
35
|
* through with `X-Checkpoint-Would-Have-Been` headers. Per Phase 0.2.
|
|
@@ -35,10 +39,23 @@ interface CheckpointConfig {
|
|
|
35
39
|
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
36
40
|
* `negotiated` serves a body-readable HTTP 200 step-up response to
|
|
37
41
|
* cooperative agents (fetchers that drop 4xx/5xx bodies), keeping the spec
|
|
38
|
-
* 401/422 for everyone else. Defaults to `
|
|
42
|
+
* 401/422 for everyone else. Defaults to `negotiated` (#3530). A cooperative-UX
|
|
39
43
|
* bridge, NOT an access control.
|
|
40
44
|
*/
|
|
41
45
|
delegationChallengeMode?: ChallengeEnvelopeMode;
|
|
46
|
+
/**
|
|
47
|
+
* Resolve a policy `Challenge` into complete registered wire context.
|
|
48
|
+
* The callback performs any Authorization-Host registration asynchronously;
|
|
49
|
+
* the synchronous engine renderer never fabricates a sid, consent URI, or proof.
|
|
50
|
+
*/
|
|
51
|
+
resolveDelegationChallenge?: (context: {
|
|
52
|
+
result: VerifyResult;
|
|
53
|
+
request: NextRequest;
|
|
54
|
+
}) => ChallengeWireInput | undefined | Promise<ChallengeWireInput | undefined>;
|
|
55
|
+
/** Registration/resolution budget. Defaults to 3000 milliseconds. */
|
|
56
|
+
delegationChallengeTimeoutMs?: number;
|
|
57
|
+
/** Sanitized registration failure hook for metrics and operator warnings. */
|
|
58
|
+
onDelegationChallengeFailure?: (event: ChallengeContextFailureEvent) => void;
|
|
42
59
|
/**
|
|
43
60
|
* Argus reputation oracle base URL. Omit to use the trust-by-default
|
|
44
61
|
* baseline (reputation defaults to 1.0; orchestrator logs a one-shot
|
|
@@ -80,7 +97,7 @@ interface CheckpointConfig {
|
|
|
80
97
|
* canonical `_meta.proof.jws` body form. Default `false`.
|
|
81
98
|
*
|
|
82
99
|
* **When to enable** — customers whose agents pre-date Envelope-1
|
|
83
|
-
* (#2537) and ship
|
|
100
|
+
* (#2537) and ship KYA-OS proofs as `{protected,payload,signature}`
|
|
84
101
|
* JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
|
|
85
102
|
* ship compact JWS in the request body's `_meta.proof.jws` field;
|
|
86
103
|
* those don't need this flag.
|
|
@@ -95,12 +112,12 @@ interface CheckpointConfig {
|
|
|
95
112
|
legacyEnvelopeFallback?: boolean;
|
|
96
113
|
/**
|
|
97
114
|
* Read the request body when `content-type` is `application/json` so
|
|
98
|
-
* the orchestrator can extract
|
|
115
|
+
* the orchestrator can extract a KYA-OS envelope from
|
|
99
116
|
* `_meta.proof.jws`. Default `true`.
|
|
100
117
|
*
|
|
101
118
|
* **When to disable** — streaming middlewares that can't tolerate
|
|
102
119
|
* the `req.clone()` memory overhead (one full-body copy is buffered
|
|
103
|
-
* during the read). For those, set `false` and route
|
|
120
|
+
* during the read). For those, set `false` and route KYA-OS
|
|
104
121
|
* envelopes through the `KYA-Delegation` header transport instead
|
|
105
122
|
* (requires `legacyEnvelopeFallback: true`).
|
|
106
123
|
*
|
|
@@ -171,6 +188,8 @@ interface CheckpointConfig {
|
|
|
171
188
|
* Added in `@kya-os/checkpoint-nextjs@1.5.0` (@Policy middleware-Cedar, #3076).
|
|
172
189
|
*/
|
|
173
190
|
projectId?: string;
|
|
191
|
+
/** Signed browser risk; configure explicit route scopes to enable consent step-up. */
|
|
192
|
+
browserPosture?: BrowserPostureConfig;
|
|
174
193
|
/**
|
|
175
194
|
* Policy-fetch cache TTL in seconds. Defaults to 300 (5 minutes). How long
|
|
176
195
|
* a fetched project policy is reused before the middleware refetches from
|