@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/middleware-node.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var orchestrator = require('@kya-os/checkpoint-wasm-runtime/orchestrator');
|
|
4
|
+
var browserPosture = require('@kya-os/checkpoint-shared/browser-posture');
|
|
5
|
+
var composedPolicy = require('@kya-os/checkpoint-wasm-runtime/composed-policy');
|
|
4
6
|
var adapters = require('@kya-os/checkpoint-wasm-runtime/adapters');
|
|
5
7
|
var reporter = require('@kya-os/checkpoint-wasm-runtime/reporter');
|
|
6
8
|
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
7
9
|
var server = require('next/server');
|
|
8
|
-
var composedPolicy = require('@kya-os/checkpoint-wasm-runtime/composed-policy');
|
|
9
10
|
|
|
10
11
|
// src/middleware-node.ts
|
|
11
12
|
function adaptToNextResponse(rendered, req) {
|
|
@@ -15,28 +16,28 @@ function adaptToNextResponse(rendered, req) {
|
|
|
15
16
|
switch (shape) {
|
|
16
17
|
case "pass-through": {
|
|
17
18
|
const res = server.NextResponse.next();
|
|
18
|
-
applyHeaders(res, rendered
|
|
19
|
+
applyHeaders(res, rendered);
|
|
19
20
|
setVerdictCookie(res, verdictCookie);
|
|
20
21
|
return res;
|
|
21
22
|
}
|
|
22
23
|
case "redirect": {
|
|
23
24
|
const target = new URL(rendered.headers.Location);
|
|
24
25
|
const res = server.NextResponse.redirect(target);
|
|
25
|
-
applyHeaders(res, rendered
|
|
26
|
+
applyHeaders(res, rendered);
|
|
26
27
|
setVerdictCookie(res, verdictCookie);
|
|
27
28
|
return res;
|
|
28
29
|
}
|
|
29
30
|
case "html-block": {
|
|
30
31
|
const blockedUrl = new URL(checkpointShared.BLOCKED_PATH, req.url);
|
|
31
32
|
const res = server.NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
32
|
-
applyHeaders(res, rendered
|
|
33
|
+
applyHeaders(res, rendered);
|
|
33
34
|
setVerdictCookie(res, verdictCookie);
|
|
34
35
|
return res;
|
|
35
36
|
}
|
|
36
37
|
case "json-block": {
|
|
37
38
|
const body = rendered.body ?? {};
|
|
38
39
|
const res = server.NextResponse.json(body, { status: rendered.status });
|
|
39
|
-
applyHeaders(res, rendered
|
|
40
|
+
applyHeaders(res, rendered);
|
|
40
41
|
setVerdictCookie(res, verdictCookie);
|
|
41
42
|
return res;
|
|
42
43
|
}
|
|
@@ -51,10 +52,13 @@ function setVerdictCookie(res, value) {
|
|
|
51
52
|
httpOnly: false
|
|
52
53
|
});
|
|
53
54
|
}
|
|
54
|
-
function applyHeaders(res,
|
|
55
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
55
|
+
function applyHeaders(res, rendered) {
|
|
56
|
+
for (const [key, value] of Object.entries(rendered.headers)) {
|
|
56
57
|
res.headers.set(key, value);
|
|
57
58
|
}
|
|
59
|
+
for (const [key, value] of rendered.headerEntries ?? []) {
|
|
60
|
+
res.headers.append(key, value);
|
|
61
|
+
}
|
|
58
62
|
}
|
|
59
63
|
var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
|
|
60
64
|
var NOOP_LOGGER = {
|
|
@@ -70,6 +74,7 @@ function makeComposedPolicyContext(opts) {
|
|
|
70
74
|
return {
|
|
71
75
|
async apply(result, path) {
|
|
72
76
|
const structured = { decision: result.decision, acted: false };
|
|
77
|
+
if (composedPolicy.isCredentialVerificationBlock(result.decision)) return structured;
|
|
73
78
|
const policy = await fetcher.getPolicy(projectId);
|
|
74
79
|
const outcome = await composedPolicy.evaluateComposedPolicy({
|
|
75
80
|
cache,
|
|
@@ -146,8 +151,6 @@ var consoleComposedPolicyLogger = {
|
|
|
146
151
|
);
|
|
147
152
|
}
|
|
148
153
|
};
|
|
149
|
-
|
|
150
|
-
// src/translate.ts
|
|
151
154
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
152
155
|
const url = new URL(req.url);
|
|
153
156
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -157,7 +160,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
157
160
|
url: url.pathname + url.search,
|
|
158
161
|
headers: headersToRecord(req.headers),
|
|
159
162
|
body,
|
|
160
|
-
remoteAddress: extractRemoteAddress(req)
|
|
163
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
161
164
|
};
|
|
162
165
|
}
|
|
163
166
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -177,52 +180,84 @@ function headersToRecord(headers) {
|
|
|
177
180
|
});
|
|
178
181
|
return out;
|
|
179
182
|
}
|
|
180
|
-
function extractRemoteAddress(req) {
|
|
181
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
182
|
-
if (xff) {
|
|
183
|
-
const first = xff.split(",")[0]?.trim();
|
|
184
|
-
if (first) return first;
|
|
185
|
-
}
|
|
183
|
+
function extractRemoteAddress(req, policy) {
|
|
186
184
|
const maybeIp = req.ip;
|
|
187
|
-
return
|
|
185
|
+
return checkpointShared.resolveClientIp({
|
|
186
|
+
headers: req.headers,
|
|
187
|
+
frameworkAddress: maybeIp,
|
|
188
|
+
policy
|
|
189
|
+
}).ip ?? void 0;
|
|
188
190
|
}
|
|
189
191
|
|
|
190
|
-
// src/middleware-
|
|
191
|
-
function
|
|
192
|
+
// src/middleware-core.ts
|
|
193
|
+
function makeNextMiddleware(verifyRequest2, renderDecisionAsResponse2, config, host) {
|
|
194
|
+
if (config.browserPosture) 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
|
+
}
|
|
192
200
|
const opts = buildVerifyOpts(config);
|
|
193
|
-
const reporter = buildReporter(config);
|
|
194
|
-
const composed = buildComposedContext(config);
|
|
201
|
+
const reporter = buildReporter(config, host.runtime);
|
|
202
|
+
const composed = host.buildComposedContext(config);
|
|
195
203
|
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
196
|
-
const translateOpts = {
|
|
204
|
+
const translateOpts = {
|
|
205
|
+
drainJsonBody: config.drainJsonBody,
|
|
206
|
+
clientIpPolicy: config.clientIpPolicy
|
|
207
|
+
};
|
|
197
208
|
return async function checkpointMiddleware(req, event) {
|
|
198
209
|
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
199
210
|
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
200
211
|
trustedRootsResolver,
|
|
201
212
|
req.headers
|
|
202
213
|
);
|
|
203
|
-
const result = await
|
|
214
|
+
const result = await verifyRequest2(
|
|
204
215
|
httpLike,
|
|
205
216
|
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
206
217
|
);
|
|
218
|
+
await composedPolicy.applyBrowserPosture(
|
|
219
|
+
result,
|
|
220
|
+
req.headers.get("cookie"),
|
|
221
|
+
config.browserPosture,
|
|
222
|
+
config.projectId
|
|
223
|
+
);
|
|
207
224
|
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
225
|
+
composedPolicy.applyBrowserIntegrityStepUp(result, config.browserPosture, req.nextUrl.pathname);
|
|
208
226
|
if (reporter) {
|
|
209
|
-
const reportPromise = reporter(
|
|
227
|
+
const reportPromise = reporter(
|
|
228
|
+
result,
|
|
229
|
+
extractReporterContext(req, config.clientIpPolicy ?? { platform: "direct" })
|
|
230
|
+
);
|
|
210
231
|
if (event) {
|
|
211
232
|
event.waitUntil(reportPromise);
|
|
212
233
|
}
|
|
213
234
|
}
|
|
214
235
|
await dispatchOnResult(config, result, req);
|
|
215
236
|
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
216
|
-
config.delegationChallengeMode ?? "
|
|
237
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
217
238
|
req.headers,
|
|
218
|
-
result.detectionDetail.detectionClass
|
|
239
|
+
result.detectionDetail.detectionClass,
|
|
240
|
+
result.detectionDetail.isAgent
|
|
219
241
|
);
|
|
220
|
-
const
|
|
242
|
+
const challengeContextRequested = result.enforcementMode === "enforce" && result.decision.kind === "Challenge" && config.resolveDelegationChallenge !== void 0;
|
|
243
|
+
const delegationChallenge = challengeContextRequested ? await checkpointShared.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: composedPolicy.validateBrowserIntegrityChallenge(result, delegationChallenge),
|
|
254
|
+
challengeContextRequested
|
|
255
|
+
});
|
|
221
256
|
return adaptToNextResponse(rendered, req);
|
|
222
257
|
};
|
|
223
258
|
}
|
|
224
259
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
225
|
-
var VERSION = "1.
|
|
260
|
+
var VERSION = "1.9.0";
|
|
226
261
|
function buildReporter(config, runtime = "node") {
|
|
227
262
|
if (!config.apiKey) return null;
|
|
228
263
|
return reporter.makeDetectionReporter({
|
|
@@ -236,6 +271,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
236
271
|
});
|
|
237
272
|
}
|
|
238
273
|
function buildTrustedRootsResolver(config, composed) {
|
|
274
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
239
275
|
if (composed?.trustedDelegationRoots) {
|
|
240
276
|
return () => composed.trustedDelegationRoots();
|
|
241
277
|
}
|
|
@@ -248,33 +284,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
248
284
|
const projectId = config.projectId;
|
|
249
285
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
250
286
|
}
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
fetcher: new checkpointShared.PolicyFetcher({
|
|
257
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
258
|
-
apiKey: config.apiKey,
|
|
259
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
260
|
-
}),
|
|
261
|
-
// LAZY dynamic import — NOT a top-level `import` — so the node-only
|
|
262
|
-
// `./policy` glue (`createRequire`/`fs` at module load) is never pulled into
|
|
263
|
-
// the Edge bundle. `middleware-edge.ts` imports helpers from this file, so a
|
|
264
|
-
// top-level `./policy` import would surface as a side-effect import in the
|
|
265
|
-
// edge bundle and boot-fail on Vercel edge. The import is cached after first
|
|
266
|
-
// call; the core's single-flight cache wraps the (now async) compile.
|
|
267
|
-
compile: async (_language, source) => {
|
|
268
|
-
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
269
|
-
return createPolicyEvaluator(source);
|
|
270
|
-
},
|
|
271
|
-
logger: config.debug ? consoleComposedPolicyLogger : void 0
|
|
287
|
+
function extractReporterContext(req, policy) {
|
|
288
|
+
const resolution = checkpointShared.resolveClientIp({
|
|
289
|
+
headers: req.headers,
|
|
290
|
+
frameworkAddress: req.ip,
|
|
291
|
+
policy
|
|
272
292
|
});
|
|
273
|
-
}
|
|
274
|
-
function extractReporterContext(req) {
|
|
275
293
|
return {
|
|
276
294
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
277
|
-
ipAddress:
|
|
295
|
+
ipAddress: resolution.ip ?? void 0,
|
|
278
296
|
path: req.nextUrl.pathname,
|
|
279
297
|
url: req.nextUrl.href,
|
|
280
298
|
method: req.method
|
|
@@ -289,6 +307,8 @@ function buildVerifyOpts(config) {
|
|
|
289
307
|
policyEvaluator: overrides.policyEvaluator ?? adapters.makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
290
308
|
clock: adapters.makeSystemClock(),
|
|
291
309
|
tenantHost: config.tenantHost,
|
|
310
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
311
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
292
312
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
293
313
|
reputationBaseline: config.reputationBaseline,
|
|
294
314
|
argusUrl: config.argusUrl,
|
|
@@ -303,10 +323,33 @@ async function dispatchOnResult(config, result, req) {
|
|
|
303
323
|
} catch {
|
|
304
324
|
}
|
|
305
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 checkpointShared.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(orchestrator.verifyRequest, orchestrator.renderDecisionAsResponse, config, {
|
|
349
|
+
runtime: "node",
|
|
350
|
+
buildComposedContext
|
|
351
|
+
});
|
|
352
|
+
}
|
|
306
353
|
|
|
307
354
|
exports.VERSION = VERSION;
|
|
308
|
-
exports._buildReporter = buildReporter;
|
|
309
|
-
exports._buildTrustedRootsResolver = buildTrustedRootsResolver;
|
|
310
|
-
exports._buildVerifyOpts = buildVerifyOpts;
|
|
311
|
-
exports._extractReporterContext = extractReporterContext;
|
|
312
355
|
exports.withCheckpoint = withCheckpoint;
|
package/dist/middleware-node.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
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 { selectBodyReadable200, PolicyFetcher, requestCarriesDelegationProof, acceptsHtml, encodeVerdictCookie, classifyResponseShape, BLOCKED_PATH, VERDICT_COOKIE_NAME } from '@kya-os/checkpoint-shared';
|
|
6
|
+
import { selectBodyReadable200, resolveChallengeContext, PolicyFetcher, requestCarriesDelegationProof, resolveClientIp, acceptsHtml, encodeVerdictCookie, classifyResponseShape, BLOCKED_PATH, VERDICT_COOKIE_NAME } from '@kya-os/checkpoint-shared';
|
|
5
7
|
import { NextResponse } from 'next/server';
|
|
6
|
-
import { makeComposedPolicyCache, evaluateComposedPolicy, verifyResultToAuthorizeInput } from '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
7
8
|
|
|
8
9
|
// src/middleware-node.ts
|
|
9
10
|
function adaptToNextResponse(rendered, req) {
|
|
@@ -13,28 +14,28 @@ function adaptToNextResponse(rendered, req) {
|
|
|
13
14
|
switch (shape) {
|
|
14
15
|
case "pass-through": {
|
|
15
16
|
const res = NextResponse.next();
|
|
16
|
-
applyHeaders(res, rendered
|
|
17
|
+
applyHeaders(res, rendered);
|
|
17
18
|
setVerdictCookie(res, verdictCookie);
|
|
18
19
|
return res;
|
|
19
20
|
}
|
|
20
21
|
case "redirect": {
|
|
21
22
|
const target = new URL(rendered.headers.Location);
|
|
22
23
|
const res = NextResponse.redirect(target);
|
|
23
|
-
applyHeaders(res, rendered
|
|
24
|
+
applyHeaders(res, rendered);
|
|
24
25
|
setVerdictCookie(res, verdictCookie);
|
|
25
26
|
return res;
|
|
26
27
|
}
|
|
27
28
|
case "html-block": {
|
|
28
29
|
const blockedUrl = new URL(BLOCKED_PATH, req.url);
|
|
29
30
|
const res = NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
30
|
-
applyHeaders(res, rendered
|
|
31
|
+
applyHeaders(res, rendered);
|
|
31
32
|
setVerdictCookie(res, verdictCookie);
|
|
32
33
|
return res;
|
|
33
34
|
}
|
|
34
35
|
case "json-block": {
|
|
35
36
|
const body = rendered.body ?? {};
|
|
36
37
|
const res = NextResponse.json(body, { status: rendered.status });
|
|
37
|
-
applyHeaders(res, rendered
|
|
38
|
+
applyHeaders(res, rendered);
|
|
38
39
|
setVerdictCookie(res, verdictCookie);
|
|
39
40
|
return res;
|
|
40
41
|
}
|
|
@@ -49,10 +50,13 @@ function setVerdictCookie(res, value) {
|
|
|
49
50
|
httpOnly: false
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
|
-
function applyHeaders(res,
|
|
53
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
53
|
+
function applyHeaders(res, rendered) {
|
|
54
|
+
for (const [key, value] of Object.entries(rendered.headers)) {
|
|
54
55
|
res.headers.set(key, value);
|
|
55
56
|
}
|
|
57
|
+
for (const [key, value] of rendered.headerEntries ?? []) {
|
|
58
|
+
res.headers.append(key, value);
|
|
59
|
+
}
|
|
56
60
|
}
|
|
57
61
|
var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
|
|
58
62
|
var NOOP_LOGGER = {
|
|
@@ -68,6 +72,7 @@ function makeComposedPolicyContext(opts) {
|
|
|
68
72
|
return {
|
|
69
73
|
async apply(result, path) {
|
|
70
74
|
const structured = { decision: result.decision, acted: false };
|
|
75
|
+
if (isCredentialVerificationBlock(result.decision)) return structured;
|
|
71
76
|
const policy = await fetcher.getPolicy(projectId);
|
|
72
77
|
const outcome = await evaluateComposedPolicy({
|
|
73
78
|
cache,
|
|
@@ -144,8 +149,6 @@ var consoleComposedPolicyLogger = {
|
|
|
144
149
|
);
|
|
145
150
|
}
|
|
146
151
|
};
|
|
147
|
-
|
|
148
|
-
// src/translate.ts
|
|
149
152
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
150
153
|
const url = new URL(req.url);
|
|
151
154
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -155,7 +158,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
155
158
|
url: url.pathname + url.search,
|
|
156
159
|
headers: headersToRecord(req.headers),
|
|
157
160
|
body,
|
|
158
|
-
remoteAddress: extractRemoteAddress(req)
|
|
161
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
159
162
|
};
|
|
160
163
|
}
|
|
161
164
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -175,52 +178,84 @@ function headersToRecord(headers) {
|
|
|
175
178
|
});
|
|
176
179
|
return out;
|
|
177
180
|
}
|
|
178
|
-
function extractRemoteAddress(req) {
|
|
179
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
180
|
-
if (xff) {
|
|
181
|
-
const first = xff.split(",")[0]?.trim();
|
|
182
|
-
if (first) return first;
|
|
183
|
-
}
|
|
181
|
+
function extractRemoteAddress(req, policy) {
|
|
184
182
|
const maybeIp = req.ip;
|
|
185
|
-
return
|
|
183
|
+
return resolveClientIp({
|
|
184
|
+
headers: req.headers,
|
|
185
|
+
frameworkAddress: maybeIp,
|
|
186
|
+
policy
|
|
187
|
+
}).ip ?? void 0;
|
|
186
188
|
}
|
|
187
189
|
|
|
188
|
-
// src/middleware-
|
|
189
|
-
function
|
|
190
|
+
// src/middleware-core.ts
|
|
191
|
+
function makeNextMiddleware(verifyRequest2, renderDecisionAsResponse2, config, host) {
|
|
192
|
+
if (config.browserPosture) validateBrowserPostureConfig(config.browserPosture, config.projectId);
|
|
193
|
+
if (config.browserPosture?.protectedRoutes?.length && !config.resolveDelegationChallenge) {
|
|
194
|
+
throw new Error(
|
|
195
|
+
"browserPosture.protectedRoutes requires resolveDelegationChallenge (see createBrowserIntegrityChallengeResolver)"
|
|
196
|
+
);
|
|
197
|
+
}
|
|
190
198
|
const opts = buildVerifyOpts(config);
|
|
191
|
-
const reporter = buildReporter(config);
|
|
192
|
-
const composed = buildComposedContext(config);
|
|
199
|
+
const reporter = buildReporter(config, host.runtime);
|
|
200
|
+
const composed = host.buildComposedContext(config);
|
|
193
201
|
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
194
|
-
const translateOpts = {
|
|
202
|
+
const translateOpts = {
|
|
203
|
+
drainJsonBody: config.drainJsonBody,
|
|
204
|
+
clientIpPolicy: config.clientIpPolicy
|
|
205
|
+
};
|
|
195
206
|
return async function checkpointMiddleware(req, event) {
|
|
196
207
|
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
197
208
|
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
198
209
|
trustedRootsResolver,
|
|
199
210
|
req.headers
|
|
200
211
|
);
|
|
201
|
-
const result = await
|
|
212
|
+
const result = await verifyRequest2(
|
|
202
213
|
httpLike,
|
|
203
214
|
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
204
215
|
);
|
|
216
|
+
await applyBrowserPosture(
|
|
217
|
+
result,
|
|
218
|
+
req.headers.get("cookie"),
|
|
219
|
+
config.browserPosture,
|
|
220
|
+
config.projectId
|
|
221
|
+
);
|
|
205
222
|
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
223
|
+
applyBrowserIntegrityStepUp(result, config.browserPosture, req.nextUrl.pathname);
|
|
206
224
|
if (reporter) {
|
|
207
|
-
const reportPromise = reporter(
|
|
225
|
+
const reportPromise = reporter(
|
|
226
|
+
result,
|
|
227
|
+
extractReporterContext(req, config.clientIpPolicy ?? { platform: "direct" })
|
|
228
|
+
);
|
|
208
229
|
if (event) {
|
|
209
230
|
event.waitUntil(reportPromise);
|
|
210
231
|
}
|
|
211
232
|
}
|
|
212
233
|
await dispatchOnResult(config, result, req);
|
|
213
234
|
const bodyReadable200 = selectBodyReadable200(
|
|
214
|
-
config.delegationChallengeMode ?? "
|
|
235
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
215
236
|
req.headers,
|
|
216
|
-
result.detectionDetail.detectionClass
|
|
237
|
+
result.detectionDetail.detectionClass,
|
|
238
|
+
result.detectionDetail.isAgent
|
|
217
239
|
);
|
|
218
|
-
const
|
|
240
|
+
const challengeContextRequested = result.enforcementMode === "enforce" && result.decision.kind === "Challenge" && config.resolveDelegationChallenge !== void 0;
|
|
241
|
+
const delegationChallenge = challengeContextRequested ? await resolveChallengeContext(
|
|
242
|
+
config.resolveDelegationChallenge,
|
|
243
|
+
{ result, request: req },
|
|
244
|
+
{
|
|
245
|
+
timeoutMs: config.delegationChallengeTimeoutMs,
|
|
246
|
+
onFailure: config.onDelegationChallengeFailure
|
|
247
|
+
}
|
|
248
|
+
) : void 0;
|
|
249
|
+
const rendered = renderDecisionAsResponse2(result, {
|
|
250
|
+
bodyReadable200,
|
|
251
|
+
delegationChallenge: validateBrowserIntegrityChallenge(result, delegationChallenge),
|
|
252
|
+
challengeContextRequested
|
|
253
|
+
});
|
|
219
254
|
return adaptToNextResponse(rendered, req);
|
|
220
255
|
};
|
|
221
256
|
}
|
|
222
257
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
223
|
-
var VERSION = "1.
|
|
258
|
+
var VERSION = "1.9.0";
|
|
224
259
|
function buildReporter(config, runtime = "node") {
|
|
225
260
|
if (!config.apiKey) return null;
|
|
226
261
|
return makeDetectionReporter({
|
|
@@ -234,6 +269,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
234
269
|
});
|
|
235
270
|
}
|
|
236
271
|
function buildTrustedRootsResolver(config, composed) {
|
|
272
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
237
273
|
if (composed?.trustedDelegationRoots) {
|
|
238
274
|
return () => composed.trustedDelegationRoots();
|
|
239
275
|
}
|
|
@@ -246,33 +282,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
246
282
|
const projectId = config.projectId;
|
|
247
283
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
248
284
|
}
|
|
249
|
-
function
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
fetcher: new PolicyFetcher({
|
|
255
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
256
|
-
apiKey: config.apiKey,
|
|
257
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
258
|
-
}),
|
|
259
|
-
// LAZY dynamic import — NOT a top-level `import` — so the node-only
|
|
260
|
-
// `./policy` glue (`createRequire`/`fs` at module load) is never pulled into
|
|
261
|
-
// the Edge bundle. `middleware-edge.ts` imports helpers from this file, so a
|
|
262
|
-
// top-level `./policy` import would surface as a side-effect import in the
|
|
263
|
-
// edge bundle and boot-fail on Vercel edge. The import is cached after first
|
|
264
|
-
// call; the core's single-flight cache wraps the (now async) compile.
|
|
265
|
-
compile: async (_language, source) => {
|
|
266
|
-
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
267
|
-
return createPolicyEvaluator(source);
|
|
268
|
-
},
|
|
269
|
-
logger: config.debug ? consoleComposedPolicyLogger : void 0
|
|
285
|
+
function extractReporterContext(req, policy) {
|
|
286
|
+
const resolution = resolveClientIp({
|
|
287
|
+
headers: req.headers,
|
|
288
|
+
frameworkAddress: req.ip,
|
|
289
|
+
policy
|
|
270
290
|
});
|
|
271
|
-
}
|
|
272
|
-
function extractReporterContext(req) {
|
|
273
291
|
return {
|
|
274
292
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
275
|
-
ipAddress:
|
|
293
|
+
ipAddress: resolution.ip ?? void 0,
|
|
276
294
|
path: req.nextUrl.pathname,
|
|
277
295
|
url: req.nextUrl.href,
|
|
278
296
|
method: req.method
|
|
@@ -287,6 +305,8 @@ function buildVerifyOpts(config) {
|
|
|
287
305
|
policyEvaluator: overrides.policyEvaluator ?? makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
288
306
|
clock: makeSystemClock(),
|
|
289
307
|
tenantHost: config.tenantHost,
|
|
308
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
309
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
290
310
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
291
311
|
reputationBaseline: config.reputationBaseline,
|
|
292
312
|
argusUrl: config.argusUrl,
|
|
@@ -301,5 +321,32 @@ async function dispatchOnResult(config, result, req) {
|
|
|
301
321
|
} catch {
|
|
302
322
|
}
|
|
303
323
|
}
|
|
324
|
+
function buildComposedContext(config) {
|
|
325
|
+
if (config.composedPolicyEnforcer) return config.composedPolicyEnforcer;
|
|
326
|
+
if (!config.projectId) return null;
|
|
327
|
+
return makeComposedPolicyContext({
|
|
328
|
+
projectId: config.projectId,
|
|
329
|
+
fetcher: new PolicyFetcher({
|
|
330
|
+
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
331
|
+
apiKey: config.apiKey,
|
|
332
|
+
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
333
|
+
}),
|
|
334
|
+
// Load the Node policy compiler only on first use. This module is imported
|
|
335
|
+
// exclusively by the Node and bundler hosts, never by the Edge pipeline.
|
|
336
|
+
compile: async (_language, source) => {
|
|
337
|
+
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
338
|
+
return createPolicyEvaluator(source);
|
|
339
|
+
},
|
|
340
|
+
logger: config.debug ? consoleComposedPolicyLogger : void 0
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// src/middleware-node.ts
|
|
345
|
+
function withCheckpoint(config) {
|
|
346
|
+
return makeNextMiddleware(verifyRequest, renderDecisionAsResponse, config, {
|
|
347
|
+
runtime: "node",
|
|
348
|
+
buildComposedContext
|
|
349
|
+
});
|
|
350
|
+
}
|
|
304
351
|
|
|
305
|
-
export { VERSION,
|
|
352
|
+
export { VERSION, withCheckpoint };
|
package/dist/middleware.d.mts
CHANGED
package/dist/middleware.d.ts
CHANGED
package/dist/policy.js
CHANGED
|
@@ -13,7 +13,12 @@ function createContextFromDetection(detection, request) {
|
|
|
13
13
|
riskLevel: detection.riskLevel,
|
|
14
14
|
path: request.nextUrl.pathname,
|
|
15
15
|
method: request.method,
|
|
16
|
-
|
|
16
|
+
// Cryptographic-signature methods only, mirroring the composed-policy
|
|
17
|
+
// VERIFIED_VERIFICATION_METHODS allowlist (checkpoint-wasm-runtime):
|
|
18
|
+
// 'signature' (bare RFC 9421 keyid) and 'tier1_rfc9421' (RFC 9421 vendor-key
|
|
19
|
+
// / Web Bot Auth, #4804) are the same crypto class; kya-http/kya-http-delegated
|
|
20
|
+
// are the draft-kya-http-02 §4.3 request-signature methods.
|
|
21
|
+
signatureVerified: detection.verificationMethod === "signature" || detection.verificationMethod === "tier1_rfc9421" || detection.verificationMethod === "kya-http" || detection.verificationMethod === "kya-http-delegated",
|
|
17
22
|
isAuthenticated: false,
|
|
18
23
|
// TODO: integrate with auth
|
|
19
24
|
userAgent: request.headers.get("user-agent") || void 0
|
package/dist/policy.mjs
CHANGED
|
@@ -12,7 +12,12 @@ function createContextFromDetection(detection, request) {
|
|
|
12
12
|
riskLevel: detection.riskLevel,
|
|
13
13
|
path: request.nextUrl.pathname,
|
|
14
14
|
method: request.method,
|
|
15
|
-
|
|
15
|
+
// Cryptographic-signature methods only, mirroring the composed-policy
|
|
16
|
+
// VERIFIED_VERIFICATION_METHODS allowlist (checkpoint-wasm-runtime):
|
|
17
|
+
// 'signature' (bare RFC 9421 keyid) and 'tier1_rfc9421' (RFC 9421 vendor-key
|
|
18
|
+
// / Web Bot Auth, #4804) are the same crypto class; kya-http/kya-http-delegated
|
|
19
|
+
// are the draft-kya-http-02 §4.3 request-signature methods.
|
|
20
|
+
signatureVerified: detection.verificationMethod === "signature" || detection.verificationMethod === "tier1_rfc9421" || detection.verificationMethod === "kya-http" || detection.verificationMethod === "kya-http-delegated",
|
|
16
21
|
isAuthenticated: false,
|
|
17
22
|
// TODO: integrate with auth
|
|
18
23
|
userAgent: request.headers.get("user-agent") || void 0
|
package/dist/session-tracker.js
CHANGED
|
@@ -150,6 +150,8 @@ var StatelessSessionChecker = class {
|
|
|
150
150
|
return null;
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- published
|
|
154
|
+
// signature; narrowing it is a breaking change, deferred to a major.
|
|
153
155
|
static setHeaders(response, session) {
|
|
154
156
|
try {
|
|
155
157
|
if (response.setHeader) {
|
package/dist/session-tracker.mjs
CHANGED
|
@@ -148,6 +148,8 @@ var StatelessSessionChecker = class {
|
|
|
148
148
|
return null;
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- published
|
|
152
|
+
// signature; narrowing it is a breaking change, deferred to a major.
|
|
151
153
|
static setHeaders(response, session) {
|
|
152
154
|
try {
|
|
153
155
|
if (response.setHeader) {
|
package/dist/translate.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { NextRequest } from 'next/server';
|
|
2
2
|
import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
|
|
3
|
+
import { ClientIpTrustPolicy } from '@kya-os/checkpoint-shared';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* D.2 — `NextRequest` → `IncomingHttpLike` translator.
|
|
@@ -12,15 +13,15 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
|
|
|
12
13
|
* Shared between the Node-runtime and Edge-runtime entries. Next.js
|
|
13
14
|
* `NextRequest` is the same shape in both runtimes — `req.headers` is
|
|
14
15
|
* a `Headers` instance, `req.body` is a `ReadableStream`, `req.ip` is
|
|
15
|
-
* a getter (only present in some deployment surfaces;
|
|
16
|
-
*
|
|
16
|
+
* a getter (only present in some deployment surfaces; forwarding headers
|
|
17
|
+
* are considered only when the caller selects a matching trust policy).
|
|
17
18
|
*/
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* Options controlling translator side effects.
|
|
21
22
|
*
|
|
22
23
|
* `drainJsonBody` (default `true`) controls whether the translator
|
|
23
|
-
* reads the request body so the orchestrator can extract
|
|
24
|
+
* reads the request body so the orchestrator can extract a KYA-OS
|
|
24
25
|
* envelope from `_meta.proof.jws` (the canonical spec-form transport).
|
|
25
26
|
*
|
|
26
27
|
* Trade-off — body draining consumes the request stream. We use
|
|
@@ -35,11 +36,13 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
|
|
|
35
36
|
interface NextRequestToHttpLikeOpts {
|
|
36
37
|
/**
|
|
37
38
|
* Read the request body when `content-type` is `application/json` so
|
|
38
|
-
* the orchestrator can extract
|
|
39
|
+
* the orchestrator can extract a KYA-OS envelope from
|
|
39
40
|
* `_meta.proof.jws`. Default `true`. Set `false` for streaming
|
|
40
41
|
* middlewares that can't tolerate the clone/read overhead.
|
|
41
42
|
*/
|
|
42
43
|
drainJsonBody?: boolean;
|
|
44
|
+
/** Deployment trust profile. Defaults to direct/peer-only fail-closed behavior. */
|
|
45
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
43
46
|
}
|
|
44
47
|
/**
|
|
45
48
|
* Translate a Next.js `NextRequest` into the orchestrator's
|