@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/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,6 +1,6 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
2
3
|
import { EnforcementDecision } from './api-client.mjs';
|
|
3
|
-
import '@kya-os/checkpoint-shared';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* API-based AgentShield Middleware for Next.js
|
|
@@ -36,6 +36,8 @@ import '@kya-os/checkpoint-shared';
|
|
|
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
|
|
@@ -76,6 +78,14 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
76
78
|
* @default 'instruct'
|
|
77
79
|
*/
|
|
78
80
|
redirectMode?: 'instruct' | 'http';
|
|
81
|
+
/**
|
|
82
|
+
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
83
|
+
* `negotiated` serves a body-readable HTTP 200 instruct response to
|
|
84
|
+
* cooperative agents (so fetchers that drop 4xx bodies can read it),
|
|
85
|
+
* keeping the spec 401 for everyone else. Defaults to `negotiated` (#3530).
|
|
86
|
+
* A cooperative-UX bridge, NOT an access control.
|
|
87
|
+
*/
|
|
88
|
+
delegationChallengeMode?: ChallengeEnvelopeMode;
|
|
79
89
|
/**
|
|
80
90
|
* Custom blocked response
|
|
81
91
|
*/
|
package/dist/api-middleware.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode } from '@kya-os/checkpoint-shared';
|
|
2
3
|
import { EnforcementDecision } from './api-client.js';
|
|
3
|
-
import '@kya-os/checkpoint-shared';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* API-based AgentShield Middleware for Next.js
|
|
@@ -36,6 +36,8 @@ import '@kya-os/checkpoint-shared';
|
|
|
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
|
|
@@ -76,6 +78,14 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
76
78
|
* @default 'instruct'
|
|
77
79
|
*/
|
|
78
80
|
redirectMode?: 'instruct' | 'http';
|
|
81
|
+
/**
|
|
82
|
+
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
83
|
+
* `negotiated` serves a body-readable HTTP 200 instruct response to
|
|
84
|
+
* cooperative agents (so fetchers that drop 4xx bodies can read it),
|
|
85
|
+
* keeping the spec 401 for everyone else. Defaults to `negotiated` (#3530).
|
|
86
|
+
* A cooperative-UX bridge, NOT an access control.
|
|
87
|
+
*/
|
|
88
|
+
delegationChallengeMode?: ChallengeEnvelopeMode;
|
|
79
89
|
/**
|
|
80
90
|
* Custom blocked response
|
|
81
91
|
*/
|
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,9 +224,9 @@ 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
|
-
function buildAgentInstructionResponse(request, decision, redirectUrl) {
|
|
229
|
+
function buildAgentInstructionResponse(request, decision, redirectUrl, options) {
|
|
241
230
|
const resolved = resolveUrl(redirectUrl ?? DEFAULT_CONNECT_PATH, request.url);
|
|
242
231
|
const agentName = decision.agentName || decision.agentType || "unknown";
|
|
243
232
|
if (!resolved.searchParams.has("agent")) {
|
|
@@ -288,8 +277,8 @@ It only takes a moment and you won't need to do it again. Once you're done, ask
|
|
|
288
277
|
confidence: decision.confidence
|
|
289
278
|
}
|
|
290
279
|
};
|
|
291
|
-
const
|
|
292
|
-
response.
|
|
280
|
+
const bodyReadable200 = options?.bodyReadable200 ?? false;
|
|
281
|
+
const response = server.NextResponse.json(body, { status: bodyReadable200 ? 200 : 401 });
|
|
293
282
|
response.headers.set(
|
|
294
283
|
"Link",
|
|
295
284
|
`<${authUrl}>; rel="kya-authorize", <${MCP_I_DOCS_URL}>; rel="help"`
|
|
@@ -309,8 +298,6 @@ function resolveUrl(target, baseUrl) {
|
|
|
309
298
|
return new URL(DEFAULT_CONNECT_PATH, baseUrl);
|
|
310
299
|
}
|
|
311
300
|
}
|
|
312
|
-
|
|
313
|
-
// src/api-middleware.ts
|
|
314
301
|
function shouldSkipPath(path, skipPaths) {
|
|
315
302
|
return skipPaths.some((pattern) => checkpointShared.matchPath(path, pattern));
|
|
316
303
|
}
|
|
@@ -408,7 +395,7 @@ function withCheckpointApi(config = {}) {
|
|
|
408
395
|
try {
|
|
409
396
|
const client2 = getClient();
|
|
410
397
|
const userAgent = request.headers.get("user-agent") || void 0;
|
|
411
|
-
const ipAddress = getClientIp(request);
|
|
398
|
+
const ipAddress = getClientIp(request, config.clientIpPolicy);
|
|
412
399
|
const result = await client2.enforce({
|
|
413
400
|
headers: Object.fromEntries(request.headers.entries()),
|
|
414
401
|
userAgent,
|
|
@@ -447,14 +434,16 @@ function withCheckpointApi(config = {}) {
|
|
|
447
434
|
});
|
|
448
435
|
}
|
|
449
436
|
if (client2.isUsingEdge() && result.data.detection) {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
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
|
+
);
|
|
458
447
|
}
|
|
459
448
|
if (decision.isAgent && config.onAgentDetected) {
|
|
460
449
|
try {
|
|
@@ -482,7 +471,15 @@ function withCheckpointApi(config = {}) {
|
|
|
482
471
|
return buildRedirectResponse(request, decision, config);
|
|
483
472
|
}
|
|
484
473
|
const targetUrl = config.redirectUrl || decision.redirectUrl;
|
|
485
|
-
|
|
474
|
+
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
475
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
476
|
+
request.headers,
|
|
477
|
+
result.data.detection?.detectionClass,
|
|
478
|
+
result.data.detection?.isAgent ?? false
|
|
479
|
+
);
|
|
480
|
+
return buildAgentInstructionResponse(request, decision, targetUrl, {
|
|
481
|
+
bodyReadable200
|
|
482
|
+
});
|
|
486
483
|
}
|
|
487
484
|
case "challenge": {
|
|
488
485
|
return buildRedirectResponse(request, decision, config);
|
package/dist/api-middleware.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
|
-
import { 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,9 +222,9 @@ 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
|
-
function buildAgentInstructionResponse(request, decision, redirectUrl) {
|
|
227
|
+
function buildAgentInstructionResponse(request, decision, redirectUrl, options) {
|
|
239
228
|
const resolved = resolveUrl(redirectUrl ?? DEFAULT_CONNECT_PATH, request.url);
|
|
240
229
|
const agentName = decision.agentName || decision.agentType || "unknown";
|
|
241
230
|
if (!resolved.searchParams.has("agent")) {
|
|
@@ -286,8 +275,8 @@ It only takes a moment and you won't need to do it again. Once you're done, ask
|
|
|
286
275
|
confidence: decision.confidence
|
|
287
276
|
}
|
|
288
277
|
};
|
|
289
|
-
const
|
|
290
|
-
response.
|
|
278
|
+
const bodyReadable200 = options?.bodyReadable200 ?? false;
|
|
279
|
+
const response = NextResponse.json(body, { status: bodyReadable200 ? 200 : 401 });
|
|
291
280
|
response.headers.set(
|
|
292
281
|
"Link",
|
|
293
282
|
`<${authUrl}>; rel="kya-authorize", <${MCP_I_DOCS_URL}>; rel="help"`
|
|
@@ -307,8 +296,6 @@ function resolveUrl(target, baseUrl) {
|
|
|
307
296
|
return new URL(DEFAULT_CONNECT_PATH, baseUrl);
|
|
308
297
|
}
|
|
309
298
|
}
|
|
310
|
-
|
|
311
|
-
// src/api-middleware.ts
|
|
312
299
|
function shouldSkipPath(path, skipPaths) {
|
|
313
300
|
return skipPaths.some((pattern) => matchPath(path, pattern));
|
|
314
301
|
}
|
|
@@ -406,7 +393,7 @@ function withCheckpointApi(config = {}) {
|
|
|
406
393
|
try {
|
|
407
394
|
const client2 = getClient();
|
|
408
395
|
const userAgent = request.headers.get("user-agent") || void 0;
|
|
409
|
-
const ipAddress = getClientIp(request);
|
|
396
|
+
const ipAddress = getClientIp(request, config.clientIpPolicy);
|
|
410
397
|
const result = await client2.enforce({
|
|
411
398
|
headers: Object.fromEntries(request.headers.entries()),
|
|
412
399
|
userAgent,
|
|
@@ -445,14 +432,16 @@ function withCheckpointApi(config = {}) {
|
|
|
445
432
|
});
|
|
446
433
|
}
|
|
447
434
|
if (client2.isUsingEdge() && result.data.detection) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
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
|
+
);
|
|
456
445
|
}
|
|
457
446
|
if (decision.isAgent && config.onAgentDetected) {
|
|
458
447
|
try {
|
|
@@ -480,7 +469,15 @@ function withCheckpointApi(config = {}) {
|
|
|
480
469
|
return buildRedirectResponse(request, decision, config);
|
|
481
470
|
}
|
|
482
471
|
const targetUrl = config.redirectUrl || decision.redirectUrl;
|
|
483
|
-
|
|
472
|
+
const bodyReadable200 = selectBodyReadable200(
|
|
473
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
474
|
+
request.headers,
|
|
475
|
+
result.data.detection?.detectionClass,
|
|
476
|
+
result.data.detection?.isAgent ?? false
|
|
477
|
+
);
|
|
478
|
+
return buildAgentInstructionResponse(request, decision, targetUrl, {
|
|
479
|
+
bodyReadable200
|
|
480
|
+
});
|
|
484
481
|
}
|
|
485
482
|
case "challenge": {
|
|
486
483
|
return buildRedirectResponse(request, decision, config);
|
|
@@ -45,6 +45,13 @@ interface ShadowDivergenceInfo {
|
|
|
45
45
|
interface ComposedPolicyApplyResult {
|
|
46
46
|
decision: Decision;
|
|
47
47
|
acted: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* SSOT-served revision pin (sha256 of the evaluated bundle, byte-identical
|
|
50
|
+
* to `policy_revisions.composed_blob_hash`) — set ONLY when `acted`, echoed
|
|
51
|
+
* from `PolicyConfig.policySourceHash` (#3246 PR5). Absent on structured
|
|
52
|
+
* decisions and when the dashboard predates the field.
|
|
53
|
+
*/
|
|
54
|
+
composedBlobHash?: string;
|
|
48
55
|
}
|
|
49
56
|
interface ComposedPolicyContext {
|
|
50
57
|
/**
|
|
@@ -45,6 +45,13 @@ interface ShadowDivergenceInfo {
|
|
|
45
45
|
interface ComposedPolicyApplyResult {
|
|
46
46
|
decision: Decision;
|
|
47
47
|
acted: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* SSOT-served revision pin (sha256 of the evaluated bundle, byte-identical
|
|
50
|
+
* to `policy_revisions.composed_blob_hash`) — set ONLY when `acted`, echoed
|
|
51
|
+
* from `PolicyConfig.policySourceHash` (#3246 PR5). Absent on structured
|
|
52
|
+
* decisions and when the dashboard predates the field.
|
|
53
|
+
*/
|
|
54
|
+
composedBlobHash?: string;
|
|
48
55
|
}
|
|
49
56
|
interface ComposedPolicyContext {
|
|
50
57
|
/**
|
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,
|
|
@@ -48,7 +49,13 @@ function makeComposedPolicyContext(opts) {
|
|
|
48
49
|
return structured;
|
|
49
50
|
}
|
|
50
51
|
if (outcome.status === "acting") {
|
|
51
|
-
return {
|
|
52
|
+
return {
|
|
53
|
+
decision: outcome.engineDecision,
|
|
54
|
+
acted: true,
|
|
55
|
+
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
56
|
+
// PR5). Conditional spread: absent upstream stays absent here.
|
|
57
|
+
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
58
|
+
};
|
|
52
59
|
}
|
|
53
60
|
return structured;
|
|
54
61
|
},
|
|
@@ -68,7 +75,12 @@ async function applyComposedPolicy(context, result, path) {
|
|
|
68
75
|
if (!context) return;
|
|
69
76
|
try {
|
|
70
77
|
const outcome = await context.apply(result, path);
|
|
71
|
-
if (outcome.acted)
|
|
78
|
+
if (outcome.acted) {
|
|
79
|
+
result.decision = outcome.decision;
|
|
80
|
+
if (outcome.composedBlobHash) {
|
|
81
|
+
result.composedBlobHash = outcome.composedBlobHash;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
72
84
|
} catch {
|
|
73
85
|
}
|
|
74
86
|
}
|
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,
|
|
@@ -46,7 +47,13 @@ function makeComposedPolicyContext(opts) {
|
|
|
46
47
|
return structured;
|
|
47
48
|
}
|
|
48
49
|
if (outcome.status === "acting") {
|
|
49
|
-
return {
|
|
50
|
+
return {
|
|
51
|
+
decision: outcome.engineDecision,
|
|
52
|
+
acted: true,
|
|
53
|
+
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
54
|
+
// PR5). Conditional spread: absent upstream stays absent here.
|
|
55
|
+
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
56
|
+
};
|
|
50
57
|
}
|
|
51
58
|
return structured;
|
|
52
59
|
},
|
|
@@ -66,7 +73,12 @@ async function applyComposedPolicy(context, result, path) {
|
|
|
66
73
|
if (!context) return;
|
|
67
74
|
try {
|
|
68
75
|
const outcome = await context.apply(result, path);
|
|
69
|
-
if (outcome.acted)
|
|
76
|
+
if (outcome.acted) {
|
|
77
|
+
result.decision = outcome.decision;
|
|
78
|
+
if (outcome.composedBlobHash) {
|
|
79
|
+
result.composedBlobHash = outcome.composedBlobHash;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
70
82
|
} catch {
|
|
71
83
|
}
|
|
72
84
|
}
|
|
@@ -1,16 +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';
|
|
5
|
+
import { ClientIpTrustPolicy, ChallengeEnvelopeMode, ChallengeWireInput, ChallengeContextFailureEvent } from '@kya-os/checkpoint-shared';
|
|
4
6
|
import { ComposedPolicyContext } from './composed-policy.mjs';
|
|
5
7
|
|
|
6
|
-
/**
|
|
7
|
-
* `CheckpointConfig` — the `withCheckpoint` config contract for both the Node
|
|
8
|
-
* (`./node`) and Edge (`./edge`) middleware entries. Extracted from
|
|
9
|
-
* `middleware-node.ts` (a 190-line documented contract); both entries import it
|
|
10
|
-
* from here and re-export it so the public import paths
|
|
11
|
-
* (`@kya-os/checkpoint-nextjs`, `/node`, `/edge`) are unchanged.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
8
|
/**
|
|
15
9
|
* Configuration for `withCheckpoint`.
|
|
16
10
|
*
|
|
@@ -19,17 +13,49 @@ import { ComposedPolicyContext } from './composed-policy.mjs';
|
|
|
19
13
|
* exported during the deprecation window — see D.4 cutover.
|
|
20
14
|
*/
|
|
21
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;
|
|
22
23
|
/**
|
|
23
24
|
* Tenant identifier — typically the customer's dashboard hostname
|
|
24
25
|
* (e.g. `acme.checkpoint.example`). The PolicyEvaluator uses this
|
|
25
26
|
* to look up tenant policy from the dashboard.
|
|
26
27
|
*/
|
|
27
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[];
|
|
28
33
|
/**
|
|
29
34
|
* `'enforce'` (default) blocks; `'observe'` passes everything
|
|
30
35
|
* through with `X-Checkpoint-Would-Have-Been` headers. Per Phase 0.2.
|
|
31
36
|
*/
|
|
32
37
|
enforcementMode?: EnforcementMode;
|
|
38
|
+
/**
|
|
39
|
+
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
40
|
+
* `negotiated` serves a body-readable HTTP 200 step-up response to
|
|
41
|
+
* cooperative agents (fetchers that drop 4xx/5xx bodies), keeping the spec
|
|
42
|
+
* 401/422 for everyone else. Defaults to `negotiated` (#3530). A cooperative-UX
|
|
43
|
+
* bridge, NOT an access control.
|
|
44
|
+
*/
|
|
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;
|
|
33
59
|
/**
|
|
34
60
|
* Argus reputation oracle base URL. Omit to use the trust-by-default
|
|
35
61
|
* baseline (reputation defaults to 1.0; orchestrator logs a one-shot
|
|
@@ -71,7 +97,7 @@ interface CheckpointConfig {
|
|
|
71
97
|
* canonical `_meta.proof.jws` body form. Default `false`.
|
|
72
98
|
*
|
|
73
99
|
* **When to enable** — customers whose agents pre-date Envelope-1
|
|
74
|
-
* (#2537) and ship
|
|
100
|
+
* (#2537) and ship KYA-OS proofs as `{protected,payload,signature}`
|
|
75
101
|
* JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
|
|
76
102
|
* ship compact JWS in the request body's `_meta.proof.jws` field;
|
|
77
103
|
* those don't need this flag.
|
|
@@ -86,12 +112,12 @@ interface CheckpointConfig {
|
|
|
86
112
|
legacyEnvelopeFallback?: boolean;
|
|
87
113
|
/**
|
|
88
114
|
* Read the request body when `content-type` is `application/json` so
|
|
89
|
-
* the orchestrator can extract
|
|
115
|
+
* the orchestrator can extract a KYA-OS envelope from
|
|
90
116
|
* `_meta.proof.jws`. Default `true`.
|
|
91
117
|
*
|
|
92
118
|
* **When to disable** — streaming middlewares that can't tolerate
|
|
93
119
|
* the `req.clone()` memory overhead (one full-body copy is buffered
|
|
94
|
-
* during the read). For those, set `false` and route
|
|
120
|
+
* during the read). For those, set `false` and route KYA-OS
|
|
95
121
|
* envelopes through the `KYA-Delegation` header transport instead
|
|
96
122
|
* (requires `legacyEnvelopeFallback: true`).
|
|
97
123
|
*
|
|
@@ -162,6 +188,8 @@ interface CheckpointConfig {
|
|
|
162
188
|
* Added in `@kya-os/checkpoint-nextjs@1.5.0` (@Policy middleware-Cedar, #3076).
|
|
163
189
|
*/
|
|
164
190
|
projectId?: string;
|
|
191
|
+
/** Signed browser risk; configure explicit route scopes to enable consent step-up. */
|
|
192
|
+
browserPosture?: BrowserPostureConfig;
|
|
165
193
|
/**
|
|
166
194
|
* Policy-fetch cache TTL in seconds. Defaults to 300 (5 minutes). How long
|
|
167
195
|
* a fetched project policy is reused before the middleware refetches from
|