@kya-os/checkpoint-nextjs 1.0.1 → 1.1.1
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 +105 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/edge-runtime-loader.d.mts +44 -8
- package/dist/edge-runtime-loader.d.ts +44 -8
- package/dist/edge-runtime-loader.js +67 -46
- package/dist/edge-runtime-loader.mjs +64 -47
- package/dist/index.js +24 -9
- package/dist/index.mjs +24 -9
- package/dist/middleware-edge.js +17 -8
- package/dist/middleware-edge.mjs +17 -8
- package/dist/middleware-node.d.mts +36 -0
- package/dist/middleware-node.d.ts +36 -0
- package/dist/middleware-node.js +17 -8
- package/dist/middleware-node.mjs +17 -8
- package/dist/policy.js +7 -1
- package/dist/policy.mjs +7 -1
- package/dist/translate.d.mts +36 -9
- package/dist/translate.d.ts +36 -9
- package/dist/translate.js +13 -6
- package/dist/translate.mjs +13 -6
- package/dist/wasm-middleware.d.mts +29 -10
- package/dist/wasm-middleware.d.ts +29 -10
- package/package.json +3 -3
package/dist/middleware-edge.mjs
CHANGED
|
@@ -56,21 +56,28 @@ function applyHeaders(res, headers) {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
// src/translate.ts
|
|
59
|
-
function nextRequestToHttpLike(req) {
|
|
59
|
+
async function nextRequestToHttpLike(req, opts = {}) {
|
|
60
60
|
const url = new URL(req.url);
|
|
61
|
+
const body = await tryDrainJsonBody(req, opts);
|
|
61
62
|
return {
|
|
62
63
|
method: req.method,
|
|
63
64
|
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
64
65
|
url: url.pathname + url.search,
|
|
65
66
|
headers: headersToRecord(req.headers),
|
|
66
|
-
|
|
67
|
-
// The orchestrator routes to PlainHttp when body is falsy, which
|
|
68
|
-
// is the right call for streaming middlewares that don't want to
|
|
69
|
-
// buffer the request body just to detect agents.
|
|
70
|
-
body: null,
|
|
67
|
+
body,
|
|
71
68
|
remoteAddress: extractRemoteAddress(req)
|
|
72
69
|
};
|
|
73
70
|
}
|
|
71
|
+
async function tryDrainJsonBody(req, opts) {
|
|
72
|
+
if (opts.drainJsonBody === false) return null;
|
|
73
|
+
const contentType = req.headers.get("content-type") ?? "";
|
|
74
|
+
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
75
|
+
try {
|
|
76
|
+
return await req.clone().text();
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
74
81
|
function headersToRecord(headers) {
|
|
75
82
|
const out = {};
|
|
76
83
|
headers.forEach((value, key) => {
|
|
@@ -100,7 +107,8 @@ function buildVerifyOpts(config) {
|
|
|
100
107
|
tenantHost: config.tenantHost,
|
|
101
108
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
102
109
|
reputationBaseline: config.reputationBaseline,
|
|
103
|
-
argusUrl: config.argusUrl
|
|
110
|
+
argusUrl: config.argusUrl,
|
|
111
|
+
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
|
|
104
112
|
};
|
|
105
113
|
}
|
|
106
114
|
|
|
@@ -108,8 +116,9 @@ function buildVerifyOpts(config) {
|
|
|
108
116
|
function withCheckpoint(config) {
|
|
109
117
|
void initEngineEdge();
|
|
110
118
|
const opts = buildVerifyOpts(config);
|
|
119
|
+
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
111
120
|
return async function checkpointMiddlewareEdge(req) {
|
|
112
|
-
const httpLike = nextRequestToHttpLike(req);
|
|
121
|
+
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
113
122
|
const result = await verifyRequestEdge(httpLike, opts);
|
|
114
123
|
await dispatchOnResult(config, result, req);
|
|
115
124
|
const rendered = renderDecisionAsResponse(result);
|
|
@@ -58,6 +58,41 @@ interface CheckpointConfig {
|
|
|
58
58
|
* response.
|
|
59
59
|
*/
|
|
60
60
|
onResult?: (result: VerifyResult, req: NextRequest) => void | Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Accept legacy `KYA-Delegation`-header envelope form alongside the
|
|
63
|
+
* canonical `_meta.proof.jws` body form. Default `false`.
|
|
64
|
+
*
|
|
65
|
+
* **When to enable** — customers whose agents pre-date Envelope-1
|
|
66
|
+
* (#2537) and ship MCP-I proofs as `{protected,payload,signature}`
|
|
67
|
+
* JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
|
|
68
|
+
* ship compact JWS in the request body's `_meta.proof.jws` field;
|
|
69
|
+
* those don't need this flag.
|
|
70
|
+
*
|
|
71
|
+
* Forwarded to the orchestrator's `VerifyRequestOpts.legacyEnvelopeFallback`.
|
|
72
|
+
* Both transports (header + body) are honored when this is `true`;
|
|
73
|
+
* the orchestrator's detection order is body first, then header
|
|
74
|
+
* (`packages/checkpoint-wasm-runtime/src/engine/orchestrator/build-agent-request.ts`).
|
|
75
|
+
*
|
|
76
|
+
* SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
|
|
77
|
+
*/
|
|
78
|
+
legacyEnvelopeFallback?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Read the request body when `content-type` is `application/json` so
|
|
81
|
+
* the orchestrator can extract an MCP-I envelope from
|
|
82
|
+
* `_meta.proof.jws`. Default `true`.
|
|
83
|
+
*
|
|
84
|
+
* **When to disable** — streaming middlewares that can't tolerate
|
|
85
|
+
* the `req.clone()` memory overhead (one full-body copy is buffered
|
|
86
|
+
* during the read). For those, set `false` and route MCP-I
|
|
87
|
+
* envelopes through the `KYA-Delegation` header transport instead
|
|
88
|
+
* (requires `legacyEnvelopeFallback: true`).
|
|
89
|
+
*
|
|
90
|
+
* The clone preserves `req.body` for downstream handlers — disabling
|
|
91
|
+
* is a performance optimization, not a correctness fix.
|
|
92
|
+
*
|
|
93
|
+
* SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
|
|
94
|
+
*/
|
|
95
|
+
drainJsonBody?: boolean;
|
|
61
96
|
}
|
|
62
97
|
/**
|
|
63
98
|
* Build the Checkpoint middleware. Returns a function `(req) => NextResponse`
|
|
@@ -84,6 +119,7 @@ declare function buildVerifyOpts(config: CheckpointConfig): {
|
|
|
84
119
|
enforcementMode: EnforcementMode;
|
|
85
120
|
reputationBaseline: number | undefined;
|
|
86
121
|
argusUrl: string | undefined;
|
|
122
|
+
legacyEnvelopeFallback: boolean;
|
|
87
123
|
};
|
|
88
124
|
|
|
89
125
|
export { type CheckpointConfig, buildVerifyOpts as _buildVerifyOpts, withCheckpoint };
|
|
@@ -58,6 +58,41 @@ interface CheckpointConfig {
|
|
|
58
58
|
* response.
|
|
59
59
|
*/
|
|
60
60
|
onResult?: (result: VerifyResult, req: NextRequest) => void | Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Accept legacy `KYA-Delegation`-header envelope form alongside the
|
|
63
|
+
* canonical `_meta.proof.jws` body form. Default `false`.
|
|
64
|
+
*
|
|
65
|
+
* **When to enable** — customers whose agents pre-date Envelope-1
|
|
66
|
+
* (#2537) and ship MCP-I proofs as `{protected,payload,signature}`
|
|
67
|
+
* JSON in a `KYA-Delegation` HTTP header. Post-Envelope-1 agents
|
|
68
|
+
* ship compact JWS in the request body's `_meta.proof.jws` field;
|
|
69
|
+
* those don't need this flag.
|
|
70
|
+
*
|
|
71
|
+
* Forwarded to the orchestrator's `VerifyRequestOpts.legacyEnvelopeFallback`.
|
|
72
|
+
* Both transports (header + body) are honored when this is `true`;
|
|
73
|
+
* the orchestrator's detection order is body first, then header
|
|
74
|
+
* (`packages/checkpoint-wasm-runtime/src/engine/orchestrator/build-agent-request.ts`).
|
|
75
|
+
*
|
|
76
|
+
* SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
|
|
77
|
+
*/
|
|
78
|
+
legacyEnvelopeFallback?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Read the request body when `content-type` is `application/json` so
|
|
81
|
+
* the orchestrator can extract an MCP-I envelope from
|
|
82
|
+
* `_meta.proof.jws`. Default `true`.
|
|
83
|
+
*
|
|
84
|
+
* **When to disable** — streaming middlewares that can't tolerate
|
|
85
|
+
* the `req.clone()` memory overhead (one full-body copy is buffered
|
|
86
|
+
* during the read). For those, set `false` and route MCP-I
|
|
87
|
+
* envelopes through the `KYA-Delegation` header transport instead
|
|
88
|
+
* (requires `legacyEnvelopeFallback: true`).
|
|
89
|
+
*
|
|
90
|
+
* The clone preserves `req.body` for downstream handlers — disabling
|
|
91
|
+
* is a performance optimization, not a correctness fix.
|
|
92
|
+
*
|
|
93
|
+
* SDK-Envelope-Plumbing-1 (#2594). Added in `@kya-os/checkpoint-nextjs@1.1.0`.
|
|
94
|
+
*/
|
|
95
|
+
drainJsonBody?: boolean;
|
|
61
96
|
}
|
|
62
97
|
/**
|
|
63
98
|
* Build the Checkpoint middleware. Returns a function `(req) => NextResponse`
|
|
@@ -84,6 +119,7 @@ declare function buildVerifyOpts(config: CheckpointConfig): {
|
|
|
84
119
|
enforcementMode: EnforcementMode;
|
|
85
120
|
reputationBaseline: number | undefined;
|
|
86
121
|
argusUrl: string | undefined;
|
|
122
|
+
legacyEnvelopeFallback: boolean;
|
|
87
123
|
};
|
|
88
124
|
|
|
89
125
|
export { type CheckpointConfig, buildVerifyOpts as _buildVerifyOpts, withCheckpoint };
|
package/dist/middleware-node.js
CHANGED
|
@@ -56,21 +56,28 @@ function applyHeaders(res, headers) {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
// src/translate.ts
|
|
59
|
-
function nextRequestToHttpLike(req) {
|
|
59
|
+
async function nextRequestToHttpLike(req, opts = {}) {
|
|
60
60
|
const url = new URL(req.url);
|
|
61
|
+
const body = await tryDrainJsonBody(req, opts);
|
|
61
62
|
return {
|
|
62
63
|
method: req.method,
|
|
63
64
|
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
64
65
|
url: url.pathname + url.search,
|
|
65
66
|
headers: headersToRecord(req.headers),
|
|
66
|
-
|
|
67
|
-
// The orchestrator routes to PlainHttp when body is falsy, which
|
|
68
|
-
// is the right call for streaming middlewares that don't want to
|
|
69
|
-
// buffer the request body just to detect agents.
|
|
70
|
-
body: null,
|
|
67
|
+
body,
|
|
71
68
|
remoteAddress: extractRemoteAddress(req)
|
|
72
69
|
};
|
|
73
70
|
}
|
|
71
|
+
async function tryDrainJsonBody(req, opts) {
|
|
72
|
+
if (opts.drainJsonBody === false) return null;
|
|
73
|
+
const contentType = req.headers.get("content-type") ?? "";
|
|
74
|
+
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
75
|
+
try {
|
|
76
|
+
return await req.clone().text();
|
|
77
|
+
} catch {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
74
81
|
function headersToRecord(headers) {
|
|
75
82
|
const out = {};
|
|
76
83
|
headers.forEach((value, key) => {
|
|
@@ -91,8 +98,9 @@ function extractRemoteAddress(req) {
|
|
|
91
98
|
// src/middleware-node.ts
|
|
92
99
|
function withCheckpoint(config) {
|
|
93
100
|
const opts = buildVerifyOpts(config);
|
|
101
|
+
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
94
102
|
return async function checkpointMiddleware(req) {
|
|
95
|
-
const httpLike = nextRequestToHttpLike(req);
|
|
103
|
+
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
96
104
|
const result = await orchestrator.verifyRequest(httpLike, opts);
|
|
97
105
|
await dispatchOnResult(config, result, req);
|
|
98
106
|
const rendered = orchestrator.renderDecisionAsResponse(result);
|
|
@@ -110,7 +118,8 @@ function buildVerifyOpts(config) {
|
|
|
110
118
|
tenantHost: config.tenantHost,
|
|
111
119
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
112
120
|
reputationBaseline: config.reputationBaseline,
|
|
113
|
-
argusUrl: config.argusUrl
|
|
121
|
+
argusUrl: config.argusUrl,
|
|
122
|
+
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
|
|
114
123
|
};
|
|
115
124
|
}
|
|
116
125
|
async function dispatchOnResult(config, result, req) {
|
package/dist/middleware-node.mjs
CHANGED
|
@@ -54,21 +54,28 @@ function applyHeaders(res, headers) {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// src/translate.ts
|
|
57
|
-
function nextRequestToHttpLike(req) {
|
|
57
|
+
async function nextRequestToHttpLike(req, opts = {}) {
|
|
58
58
|
const url = new URL(req.url);
|
|
59
|
+
const body = await tryDrainJsonBody(req, opts);
|
|
59
60
|
return {
|
|
60
61
|
method: req.method,
|
|
61
62
|
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
62
63
|
url: url.pathname + url.search,
|
|
63
64
|
headers: headersToRecord(req.headers),
|
|
64
|
-
|
|
65
|
-
// The orchestrator routes to PlainHttp when body is falsy, which
|
|
66
|
-
// is the right call for streaming middlewares that don't want to
|
|
67
|
-
// buffer the request body just to detect agents.
|
|
68
|
-
body: null,
|
|
65
|
+
body,
|
|
69
66
|
remoteAddress: extractRemoteAddress(req)
|
|
70
67
|
};
|
|
71
68
|
}
|
|
69
|
+
async function tryDrainJsonBody(req, opts) {
|
|
70
|
+
if (opts.drainJsonBody === false) return null;
|
|
71
|
+
const contentType = req.headers.get("content-type") ?? "";
|
|
72
|
+
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
73
|
+
try {
|
|
74
|
+
return await req.clone().text();
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
72
79
|
function headersToRecord(headers) {
|
|
73
80
|
const out = {};
|
|
74
81
|
headers.forEach((value, key) => {
|
|
@@ -89,8 +96,9 @@ function extractRemoteAddress(req) {
|
|
|
89
96
|
// src/middleware-node.ts
|
|
90
97
|
function withCheckpoint(config) {
|
|
91
98
|
const opts = buildVerifyOpts(config);
|
|
99
|
+
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
92
100
|
return async function checkpointMiddleware(req) {
|
|
93
|
-
const httpLike = nextRequestToHttpLike(req);
|
|
101
|
+
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
94
102
|
const result = await verifyRequest(httpLike, opts);
|
|
95
103
|
await dispatchOnResult(config, result, req);
|
|
96
104
|
const rendered = renderDecisionAsResponse(result);
|
|
@@ -108,7 +116,8 @@ function buildVerifyOpts(config) {
|
|
|
108
116
|
tenantHost: config.tenantHost,
|
|
109
117
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
110
118
|
reputationBaseline: config.reputationBaseline,
|
|
111
|
-
argusUrl: config.argusUrl
|
|
119
|
+
argusUrl: config.argusUrl,
|
|
120
|
+
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
|
|
112
121
|
};
|
|
113
122
|
}
|
|
114
123
|
async function dispatchOnResult(config, result, req) {
|
package/dist/policy.js
CHANGED
|
@@ -144,7 +144,13 @@ async function applyPolicy(request, detection, config) {
|
|
|
144
144
|
const context = createContextFromDetection(detection, request);
|
|
145
145
|
const decision = checkpointShared.evaluatePolicy(policy, context);
|
|
146
146
|
if (config.onPolicyDecision) {
|
|
147
|
-
|
|
147
|
+
try {
|
|
148
|
+
await config.onPolicyDecision(request, decision, context);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (config.debug) {
|
|
151
|
+
console.error("[Checkpoint] onPolicyDecision callback failed:", error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
148
154
|
}
|
|
149
155
|
return await handlePolicyDecision(request, decision, config, detection);
|
|
150
156
|
} catch (error) {
|
package/dist/policy.mjs
CHANGED
|
@@ -143,7 +143,13 @@ async function applyPolicy(request, detection, config) {
|
|
|
143
143
|
const context = createContextFromDetection(detection, request);
|
|
144
144
|
const decision = evaluatePolicy(policy, context);
|
|
145
145
|
if (config.onPolicyDecision) {
|
|
146
|
-
|
|
146
|
+
try {
|
|
147
|
+
await config.onPolicyDecision(request, decision, context);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (config.debug) {
|
|
150
|
+
console.error("[Checkpoint] onPolicyDecision callback failed:", error);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
147
153
|
}
|
|
148
154
|
return await handlePolicyDecision(request, decision, config, detection);
|
|
149
155
|
} catch (error) {
|
package/dist/translate.d.mts
CHANGED
|
@@ -16,18 +16,45 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
|
|
|
16
16
|
* `x-forwarded-for` first IP).
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Options controlling translator side effects.
|
|
21
|
+
*
|
|
22
|
+
* `drainJsonBody` (default `true`) controls whether the translator
|
|
23
|
+
* reads the request body so the orchestrator can extract an MCP-I
|
|
24
|
+
* envelope from `_meta.proof.jws` (the canonical spec-form transport).
|
|
25
|
+
*
|
|
26
|
+
* Trade-off — body draining consumes the request stream. We use
|
|
27
|
+
* `req.clone().text()` so the original `req.body` stream is preserved
|
|
28
|
+
* for downstream Next.js handlers. Cloning has memory overhead
|
|
29
|
+
* proportional to the body size; for large request bodies (>1 MB) or
|
|
30
|
+
* streaming-sensitive routes, set `drainJsonBody: false` and verify
|
|
31
|
+
* envelopes via the `KYA-Delegation` header transport instead (the
|
|
32
|
+
* legacy form, requires `legacyEnvelopeFallback: true` on
|
|
33
|
+
* `withCheckpoint`).
|
|
34
|
+
*/
|
|
35
|
+
interface NextRequestToHttpLikeOpts {
|
|
36
|
+
/**
|
|
37
|
+
* Read the request body when `content-type` is `application/json` so
|
|
38
|
+
* the orchestrator can extract an MCP-I envelope from
|
|
39
|
+
* `_meta.proof.jws`. Default `true`. Set `false` for streaming
|
|
40
|
+
* middlewares that can't tolerate the clone/read overhead.
|
|
41
|
+
*/
|
|
42
|
+
drainJsonBody?: boolean;
|
|
43
|
+
}
|
|
19
44
|
/**
|
|
20
45
|
* Translate a Next.js `NextRequest` into the orchestrator's
|
|
21
46
|
* `IncomingHttpLike` shape.
|
|
22
47
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
48
|
+
* When `opts.drainJsonBody !== false` and the request is JSON, the
|
|
49
|
+
* translator clones the request and reads the body into a string so
|
|
50
|
+
* the orchestrator's `buildAgentRequest` can extract a
|
|
51
|
+
* `_meta.proof.jws` envelope. The original `req.body` stream is
|
|
52
|
+
* preserved for downstream handlers via `req.clone()`.
|
|
53
|
+
*
|
|
54
|
+
* When the body can't be read (drain disabled, non-JSON, or a stream
|
|
55
|
+
* error) we pass `body: null` and the orchestrator routes to
|
|
56
|
+
* PlainHttp — same behavior as the pre-1.1.0 default.
|
|
30
57
|
*/
|
|
31
|
-
declare function nextRequestToHttpLike(req: NextRequest): IncomingHttpLike
|
|
58
|
+
declare function nextRequestToHttpLike(req: NextRequest, opts?: NextRequestToHttpLikeOpts): Promise<IncomingHttpLike>;
|
|
32
59
|
|
|
33
|
-
export { nextRequestToHttpLike };
|
|
60
|
+
export { type NextRequestToHttpLikeOpts, nextRequestToHttpLike };
|
package/dist/translate.d.ts
CHANGED
|
@@ -16,18 +16,45 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
|
|
|
16
16
|
* `x-forwarded-for` first IP).
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Options controlling translator side effects.
|
|
21
|
+
*
|
|
22
|
+
* `drainJsonBody` (default `true`) controls whether the translator
|
|
23
|
+
* reads the request body so the orchestrator can extract an MCP-I
|
|
24
|
+
* envelope from `_meta.proof.jws` (the canonical spec-form transport).
|
|
25
|
+
*
|
|
26
|
+
* Trade-off — body draining consumes the request stream. We use
|
|
27
|
+
* `req.clone().text()` so the original `req.body` stream is preserved
|
|
28
|
+
* for downstream Next.js handlers. Cloning has memory overhead
|
|
29
|
+
* proportional to the body size; for large request bodies (>1 MB) or
|
|
30
|
+
* streaming-sensitive routes, set `drainJsonBody: false` and verify
|
|
31
|
+
* envelopes via the `KYA-Delegation` header transport instead (the
|
|
32
|
+
* legacy form, requires `legacyEnvelopeFallback: true` on
|
|
33
|
+
* `withCheckpoint`).
|
|
34
|
+
*/
|
|
35
|
+
interface NextRequestToHttpLikeOpts {
|
|
36
|
+
/**
|
|
37
|
+
* Read the request body when `content-type` is `application/json` so
|
|
38
|
+
* the orchestrator can extract an MCP-I envelope from
|
|
39
|
+
* `_meta.proof.jws`. Default `true`. Set `false` for streaming
|
|
40
|
+
* middlewares that can't tolerate the clone/read overhead.
|
|
41
|
+
*/
|
|
42
|
+
drainJsonBody?: boolean;
|
|
43
|
+
}
|
|
19
44
|
/**
|
|
20
45
|
* Translate a Next.js `NextRequest` into the orchestrator's
|
|
21
46
|
* `IncomingHttpLike` shape.
|
|
22
47
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
48
|
+
* When `opts.drainJsonBody !== false` and the request is JSON, the
|
|
49
|
+
* translator clones the request and reads the body into a string so
|
|
50
|
+
* the orchestrator's `buildAgentRequest` can extract a
|
|
51
|
+
* `_meta.proof.jws` envelope. The original `req.body` stream is
|
|
52
|
+
* preserved for downstream handlers via `req.clone()`.
|
|
53
|
+
*
|
|
54
|
+
* When the body can't be read (drain disabled, non-JSON, or a stream
|
|
55
|
+
* error) we pass `body: null` and the orchestrator routes to
|
|
56
|
+
* PlainHttp — same behavior as the pre-1.1.0 default.
|
|
30
57
|
*/
|
|
31
|
-
declare function nextRequestToHttpLike(req: NextRequest): IncomingHttpLike
|
|
58
|
+
declare function nextRequestToHttpLike(req: NextRequest, opts?: NextRequestToHttpLikeOpts): Promise<IncomingHttpLike>;
|
|
32
59
|
|
|
33
|
-
export { nextRequestToHttpLike };
|
|
60
|
+
export { type NextRequestToHttpLikeOpts, nextRequestToHttpLike };
|
package/dist/translate.js
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/translate.ts
|
|
4
|
-
function nextRequestToHttpLike(req) {
|
|
4
|
+
async function nextRequestToHttpLike(req, opts = {}) {
|
|
5
5
|
const url = new URL(req.url);
|
|
6
|
+
const body = await tryDrainJsonBody(req, opts);
|
|
6
7
|
return {
|
|
7
8
|
method: req.method,
|
|
8
9
|
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
9
10
|
url: url.pathname + url.search,
|
|
10
11
|
headers: headersToRecord(req.headers),
|
|
11
|
-
|
|
12
|
-
// The orchestrator routes to PlainHttp when body is falsy, which
|
|
13
|
-
// is the right call for streaming middlewares that don't want to
|
|
14
|
-
// buffer the request body just to detect agents.
|
|
15
|
-
body: null,
|
|
12
|
+
body,
|
|
16
13
|
remoteAddress: extractRemoteAddress(req)
|
|
17
14
|
};
|
|
18
15
|
}
|
|
16
|
+
async function tryDrainJsonBody(req, opts) {
|
|
17
|
+
if (opts.drainJsonBody === false) return null;
|
|
18
|
+
const contentType = req.headers.get("content-type") ?? "";
|
|
19
|
+
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
20
|
+
try {
|
|
21
|
+
return await req.clone().text();
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
19
26
|
function headersToRecord(headers) {
|
|
20
27
|
const out = {};
|
|
21
28
|
headers.forEach((value, key) => {
|
package/dist/translate.mjs
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
// src/translate.ts
|
|
2
|
-
function nextRequestToHttpLike(req) {
|
|
2
|
+
async function nextRequestToHttpLike(req, opts = {}) {
|
|
3
3
|
const url = new URL(req.url);
|
|
4
|
+
const body = await tryDrainJsonBody(req, opts);
|
|
4
5
|
return {
|
|
5
6
|
method: req.method,
|
|
6
7
|
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
7
8
|
url: url.pathname + url.search,
|
|
8
9
|
headers: headersToRecord(req.headers),
|
|
9
|
-
|
|
10
|
-
// The orchestrator routes to PlainHttp when body is falsy, which
|
|
11
|
-
// is the right call for streaming middlewares that don't want to
|
|
12
|
-
// buffer the request body just to detect agents.
|
|
13
|
-
body: null,
|
|
10
|
+
body,
|
|
14
11
|
remoteAddress: extractRemoteAddress(req)
|
|
15
12
|
};
|
|
16
13
|
}
|
|
14
|
+
async function tryDrainJsonBody(req, opts) {
|
|
15
|
+
if (opts.drainJsonBody === false) return null;
|
|
16
|
+
const contentType = req.headers.get("content-type") ?? "";
|
|
17
|
+
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
18
|
+
try {
|
|
19
|
+
return await req.clone().text();
|
|
20
|
+
} catch {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
17
24
|
function headersToRecord(headers) {
|
|
18
25
|
const out = {};
|
|
19
26
|
headers.forEach((value, key) => {
|
|
@@ -26,22 +26,41 @@ interface AgentShieldConfig {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* Create a WASM-enabled
|
|
30
|
-
* This must be used with proper WASM module import at the top of middleware.ts
|
|
29
|
+
* Create a WASM-enabled Checkpoint middleware (**pattern-detection only**).
|
|
31
30
|
*
|
|
32
|
-
*
|
|
31
|
+
* **This factory runs UA/header pattern matching only.** It does NOT
|
|
32
|
+
* verify MCP-I signed envelopes — no JWS verification, no DID
|
|
33
|
+
* resolution, no orchestrator stages. Use it when your only enforcement
|
|
34
|
+
* concern is "is this request from a known bot pattern."
|
|
35
|
+
*
|
|
36
|
+
* **For envelope verification, use {@link withCheckpoint} instead** —
|
|
37
|
+
* exported from `@kya-os/checkpoint-nextjs` (Node runtime) or
|
|
38
|
+
* `@kya-os/checkpoint-nextjs/edge` (Edge runtime). `withCheckpoint`
|
|
39
|
+
* routes every request through the kya-os-engine via WASM and supports
|
|
40
|
+
* both `_meta.proof.jws` body envelopes (default) and the legacy
|
|
41
|
+
* `KYA-Delegation` header form (opt-in via `legacyEnvelopeFallback`).
|
|
42
|
+
* See SDK-Envelope-Plumbing-1 (#2594) for the migration context.
|
|
43
|
+
*
|
|
44
|
+
* @example pattern-only (this factory)
|
|
33
45
|
* ```typescript
|
|
34
|
-
* // middleware.ts
|
|
35
46
|
* import wasmModule from '@kya-os/checkpoint/wasm?module';
|
|
36
|
-
* import {
|
|
47
|
+
* import { createCheckpointWasmMiddleware } from '@kya-os/checkpoint-nextjs';
|
|
37
48
|
*
|
|
38
49
|
* const wasmInstance = await WebAssembly.instantiate(wasmModule);
|
|
39
|
-
*
|
|
40
|
-
* export const middleware = createWasmAgentShieldMiddleware({
|
|
50
|
+
* export const middleware = createCheckpointWasmMiddleware({
|
|
41
51
|
* wasmInstance,
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
52
|
+
* confidenceThreshold: 80,
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example envelope verification (use `withCheckpoint` instead)
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { withCheckpoint } from '@kya-os/checkpoint-nextjs';
|
|
59
|
+
*
|
|
60
|
+
* export default withCheckpoint({
|
|
61
|
+
* tenantHost: 'acme.checkpoint.example',
|
|
62
|
+
* legacyEnvelopeFallback: true, // accept `KYA-Delegation` header form
|
|
63
|
+
* // drainJsonBody defaults to true; spec-form `_meta.proof.jws` works out of the box
|
|
45
64
|
* });
|
|
46
65
|
* ```
|
|
47
66
|
*/
|
|
@@ -26,22 +26,41 @@ interface AgentShieldConfig {
|
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
* Create a WASM-enabled
|
|
30
|
-
* This must be used with proper WASM module import at the top of middleware.ts
|
|
29
|
+
* Create a WASM-enabled Checkpoint middleware (**pattern-detection only**).
|
|
31
30
|
*
|
|
32
|
-
*
|
|
31
|
+
* **This factory runs UA/header pattern matching only.** It does NOT
|
|
32
|
+
* verify MCP-I signed envelopes — no JWS verification, no DID
|
|
33
|
+
* resolution, no orchestrator stages. Use it when your only enforcement
|
|
34
|
+
* concern is "is this request from a known bot pattern."
|
|
35
|
+
*
|
|
36
|
+
* **For envelope verification, use {@link withCheckpoint} instead** —
|
|
37
|
+
* exported from `@kya-os/checkpoint-nextjs` (Node runtime) or
|
|
38
|
+
* `@kya-os/checkpoint-nextjs/edge` (Edge runtime). `withCheckpoint`
|
|
39
|
+
* routes every request through the kya-os-engine via WASM and supports
|
|
40
|
+
* both `_meta.proof.jws` body envelopes (default) and the legacy
|
|
41
|
+
* `KYA-Delegation` header form (opt-in via `legacyEnvelopeFallback`).
|
|
42
|
+
* See SDK-Envelope-Plumbing-1 (#2594) for the migration context.
|
|
43
|
+
*
|
|
44
|
+
* @example pattern-only (this factory)
|
|
33
45
|
* ```typescript
|
|
34
|
-
* // middleware.ts
|
|
35
46
|
* import wasmModule from '@kya-os/checkpoint/wasm?module';
|
|
36
|
-
* import {
|
|
47
|
+
* import { createCheckpointWasmMiddleware } from '@kya-os/checkpoint-nextjs';
|
|
37
48
|
*
|
|
38
49
|
* const wasmInstance = await WebAssembly.instantiate(wasmModule);
|
|
39
|
-
*
|
|
40
|
-
* export const middleware = createWasmAgentShieldMiddleware({
|
|
50
|
+
* export const middleware = createCheckpointWasmMiddleware({
|
|
41
51
|
* wasmInstance,
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
52
|
+
* confidenceThreshold: 80,
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @example envelope verification (use `withCheckpoint` instead)
|
|
57
|
+
* ```typescript
|
|
58
|
+
* import { withCheckpoint } from '@kya-os/checkpoint-nextjs';
|
|
59
|
+
*
|
|
60
|
+
* export default withCheckpoint({
|
|
61
|
+
* tenantHost: 'acme.checkpoint.example',
|
|
62
|
+
* legacyEnvelopeFallback: true, // accept `KYA-Delegation` header form
|
|
63
|
+
* // drainJsonBody defaults to true; spec-form `_meta.proof.jws` works out of the box
|
|
45
64
|
* });
|
|
46
65
|
* ```
|
|
47
66
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kya-os/checkpoint-nextjs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Checkpoint Next.js middleware for AI agent detection (formerly @kya-os/agentshield-nextjs)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nextjs",
|
|
@@ -135,8 +135,8 @@
|
|
|
135
135
|
"@noble/ed25519": "^2.2.3",
|
|
136
136
|
"@noble/hashes": "^2.0.1",
|
|
137
137
|
"@kya-os/checkpoint": "1.0.0",
|
|
138
|
-
"@kya-os/checkpoint-
|
|
139
|
-
"@kya-os/checkpoint-
|
|
138
|
+
"@kya-os/checkpoint-shared": "1.0.0",
|
|
139
|
+
"@kya-os/checkpoint-wasm-runtime": "^1.1.0"
|
|
140
140
|
},
|
|
141
141
|
"scripts": {
|
|
142
142
|
"build": "tsup",
|