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