@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/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,
|
|
@@ -100,7 +105,13 @@ function makeComposedPolicyContext(opts) {
|
|
|
100
105
|
return structured;
|
|
101
106
|
}
|
|
102
107
|
if (outcome.status === "acting") {
|
|
103
|
-
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
|
+
};
|
|
104
115
|
}
|
|
105
116
|
return structured;
|
|
106
117
|
},
|
|
@@ -120,7 +131,12 @@ async function applyComposedPolicy(context, result, path) {
|
|
|
120
131
|
if (!context) return;
|
|
121
132
|
try {
|
|
122
133
|
const outcome = await context.apply(result, path);
|
|
123
|
-
if (outcome.acted)
|
|
134
|
+
if (outcome.acted) {
|
|
135
|
+
result.decision = outcome.decision;
|
|
136
|
+
if (outcome.composedBlobHash) {
|
|
137
|
+
result.composedBlobHash = outcome.composedBlobHash;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
124
140
|
} catch {
|
|
125
141
|
}
|
|
126
142
|
}
|
|
@@ -135,8 +151,6 @@ var consoleComposedPolicyLogger = {
|
|
|
135
151
|
);
|
|
136
152
|
}
|
|
137
153
|
};
|
|
138
|
-
|
|
139
|
-
// src/translate.ts
|
|
140
154
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
141
155
|
const url = new URL(req.url);
|
|
142
156
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -146,7 +160,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
146
160
|
url: url.pathname + url.search,
|
|
147
161
|
headers: headersToRecord(req.headers),
|
|
148
162
|
body,
|
|
149
|
-
remoteAddress: extractRemoteAddress(req)
|
|
163
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
150
164
|
};
|
|
151
165
|
}
|
|
152
166
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -166,47 +180,84 @@ function headersToRecord(headers) {
|
|
|
166
180
|
});
|
|
167
181
|
return out;
|
|
168
182
|
}
|
|
169
|
-
function extractRemoteAddress(req) {
|
|
170
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
171
|
-
if (xff) {
|
|
172
|
-
const first = xff.split(",")[0]?.trim();
|
|
173
|
-
if (first) return first;
|
|
174
|
-
}
|
|
183
|
+
function extractRemoteAddress(req, policy) {
|
|
175
184
|
const maybeIp = req.ip;
|
|
176
|
-
return
|
|
185
|
+
return checkpointShared.resolveClientIp({
|
|
186
|
+
headers: req.headers,
|
|
187
|
+
frameworkAddress: maybeIp,
|
|
188
|
+
policy
|
|
189
|
+
}).ip ?? void 0;
|
|
177
190
|
}
|
|
178
191
|
|
|
179
|
-
// src/middleware-
|
|
180
|
-
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
|
+
}
|
|
181
200
|
const opts = buildVerifyOpts(config);
|
|
182
|
-
const reporter = buildReporter(config);
|
|
183
|
-
const composed = buildComposedContext(config);
|
|
201
|
+
const reporter = buildReporter(config, host.runtime);
|
|
202
|
+
const composed = host.buildComposedContext(config);
|
|
184
203
|
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
185
|
-
const translateOpts = {
|
|
204
|
+
const translateOpts = {
|
|
205
|
+
drainJsonBody: config.drainJsonBody,
|
|
206
|
+
clientIpPolicy: config.clientIpPolicy
|
|
207
|
+
};
|
|
186
208
|
return async function checkpointMiddleware(req, event) {
|
|
187
209
|
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
188
210
|
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
189
211
|
trustedRootsResolver,
|
|
190
212
|
req.headers
|
|
191
213
|
);
|
|
192
|
-
const result = await
|
|
214
|
+
const result = await verifyRequest2(
|
|
193
215
|
httpLike,
|
|
194
216
|
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
195
217
|
);
|
|
218
|
+
await composedPolicy.applyBrowserPosture(
|
|
219
|
+
result,
|
|
220
|
+
req.headers.get("cookie"),
|
|
221
|
+
config.browserPosture,
|
|
222
|
+
config.projectId
|
|
223
|
+
);
|
|
196
224
|
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
225
|
+
composedPolicy.applyBrowserIntegrityStepUp(result, config.browserPosture, req.nextUrl.pathname);
|
|
197
226
|
if (reporter) {
|
|
198
|
-
const reportPromise = reporter(
|
|
227
|
+
const reportPromise = reporter(
|
|
228
|
+
result,
|
|
229
|
+
extractReporterContext(req, config.clientIpPolicy ?? { platform: "direct" })
|
|
230
|
+
);
|
|
199
231
|
if (event) {
|
|
200
232
|
event.waitUntil(reportPromise);
|
|
201
233
|
}
|
|
202
234
|
}
|
|
203
235
|
await dispatchOnResult(config, result, req);
|
|
204
|
-
const
|
|
236
|
+
const bodyReadable200 = checkpointShared.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 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
|
+
});
|
|
205
256
|
return adaptToNextResponse(rendered, req);
|
|
206
257
|
};
|
|
207
258
|
}
|
|
208
259
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
209
|
-
var VERSION = "1.
|
|
260
|
+
var VERSION = "1.9.0";
|
|
210
261
|
function buildReporter(config, runtime = "node") {
|
|
211
262
|
if (!config.apiKey) return null;
|
|
212
263
|
return reporter.makeDetectionReporter({
|
|
@@ -220,6 +271,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
220
271
|
});
|
|
221
272
|
}
|
|
222
273
|
function buildTrustedRootsResolver(config, composed) {
|
|
274
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
223
275
|
if (composed?.trustedDelegationRoots) {
|
|
224
276
|
return () => composed.trustedDelegationRoots();
|
|
225
277
|
}
|
|
@@ -232,33 +284,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
232
284
|
const projectId = config.projectId;
|
|
233
285
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
234
286
|
}
|
|
235
|
-
function
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
fetcher: new checkpointShared.PolicyFetcher({
|
|
241
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
242
|
-
apiKey: config.apiKey,
|
|
243
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
244
|
-
}),
|
|
245
|
-
// LAZY dynamic import — NOT a top-level `import` — so the node-only
|
|
246
|
-
// `./policy` glue (`createRequire`/`fs` at module load) is never pulled into
|
|
247
|
-
// the Edge bundle. `middleware-edge.ts` imports helpers from this file, so a
|
|
248
|
-
// top-level `./policy` import would surface as a side-effect import in the
|
|
249
|
-
// edge bundle and boot-fail on Vercel edge. The import is cached after first
|
|
250
|
-
// call; the core's single-flight cache wraps the (now async) compile.
|
|
251
|
-
compile: async (_language, source) => {
|
|
252
|
-
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
253
|
-
return createPolicyEvaluator(source);
|
|
254
|
-
},
|
|
255
|
-
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
|
|
256
292
|
});
|
|
257
|
-
}
|
|
258
|
-
function extractReporterContext(req) {
|
|
259
293
|
return {
|
|
260
294
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
261
|
-
ipAddress:
|
|
295
|
+
ipAddress: resolution.ip ?? void 0,
|
|
262
296
|
path: req.nextUrl.pathname,
|
|
263
297
|
url: req.nextUrl.href,
|
|
264
298
|
method: req.method
|
|
@@ -273,6 +307,8 @@ function buildVerifyOpts(config) {
|
|
|
273
307
|
policyEvaluator: overrides.policyEvaluator ?? adapters.makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
274
308
|
clock: adapters.makeSystemClock(),
|
|
275
309
|
tenantHost: config.tenantHost,
|
|
310
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
311
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
276
312
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
277
313
|
reputationBaseline: config.reputationBaseline,
|
|
278
314
|
argusUrl: config.argusUrl,
|
|
@@ -287,10 +323,33 @@ async function dispatchOnResult(config, result, req) {
|
|
|
287
323
|
} catch {
|
|
288
324
|
}
|
|
289
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
|
+
}
|
|
290
353
|
|
|
291
354
|
exports.VERSION = VERSION;
|
|
292
|
-
exports._buildReporter = buildReporter;
|
|
293
|
-
exports._buildTrustedRootsResolver = buildTrustedRootsResolver;
|
|
294
|
-
exports._buildVerifyOpts = buildVerifyOpts;
|
|
295
|
-
exports._extractReporterContext = extractReporterContext;
|
|
296
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 { 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,
|
|
@@ -98,7 +103,13 @@ function makeComposedPolicyContext(opts) {
|
|
|
98
103
|
return structured;
|
|
99
104
|
}
|
|
100
105
|
if (outcome.status === "acting") {
|
|
101
|
-
return {
|
|
106
|
+
return {
|
|
107
|
+
decision: outcome.engineDecision,
|
|
108
|
+
acted: true,
|
|
109
|
+
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
110
|
+
// PR5). Conditional spread: absent upstream stays absent here.
|
|
111
|
+
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
112
|
+
};
|
|
102
113
|
}
|
|
103
114
|
return structured;
|
|
104
115
|
},
|
|
@@ -118,7 +129,12 @@ async function applyComposedPolicy(context, result, path) {
|
|
|
118
129
|
if (!context) return;
|
|
119
130
|
try {
|
|
120
131
|
const outcome = await context.apply(result, path);
|
|
121
|
-
if (outcome.acted)
|
|
132
|
+
if (outcome.acted) {
|
|
133
|
+
result.decision = outcome.decision;
|
|
134
|
+
if (outcome.composedBlobHash) {
|
|
135
|
+
result.composedBlobHash = outcome.composedBlobHash;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
122
138
|
} catch {
|
|
123
139
|
}
|
|
124
140
|
}
|
|
@@ -133,8 +149,6 @@ var consoleComposedPolicyLogger = {
|
|
|
133
149
|
);
|
|
134
150
|
}
|
|
135
151
|
};
|
|
136
|
-
|
|
137
|
-
// src/translate.ts
|
|
138
152
|
async function nextRequestToHttpLike(req, opts = {}) {
|
|
139
153
|
const url = new URL(req.url);
|
|
140
154
|
const body = await tryDrainJsonBody(req, opts);
|
|
@@ -144,7 +158,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
|
|
|
144
158
|
url: url.pathname + url.search,
|
|
145
159
|
headers: headersToRecord(req.headers),
|
|
146
160
|
body,
|
|
147
|
-
remoteAddress: extractRemoteAddress(req)
|
|
161
|
+
remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
|
|
148
162
|
};
|
|
149
163
|
}
|
|
150
164
|
async function tryDrainJsonBody(req, opts) {
|
|
@@ -164,47 +178,84 @@ function headersToRecord(headers) {
|
|
|
164
178
|
});
|
|
165
179
|
return out;
|
|
166
180
|
}
|
|
167
|
-
function extractRemoteAddress(req) {
|
|
168
|
-
const xff = req.headers.get("x-forwarded-for");
|
|
169
|
-
if (xff) {
|
|
170
|
-
const first = xff.split(",")[0]?.trim();
|
|
171
|
-
if (first) return first;
|
|
172
|
-
}
|
|
181
|
+
function extractRemoteAddress(req, policy) {
|
|
173
182
|
const maybeIp = req.ip;
|
|
174
|
-
return
|
|
183
|
+
return resolveClientIp({
|
|
184
|
+
headers: req.headers,
|
|
185
|
+
frameworkAddress: maybeIp,
|
|
186
|
+
policy
|
|
187
|
+
}).ip ?? void 0;
|
|
175
188
|
}
|
|
176
189
|
|
|
177
|
-
// src/middleware-
|
|
178
|
-
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
|
+
}
|
|
179
198
|
const opts = buildVerifyOpts(config);
|
|
180
|
-
const reporter = buildReporter(config);
|
|
181
|
-
const composed = buildComposedContext(config);
|
|
199
|
+
const reporter = buildReporter(config, host.runtime);
|
|
200
|
+
const composed = host.buildComposedContext(config);
|
|
182
201
|
const trustedRootsResolver = buildTrustedRootsResolver(config, composed);
|
|
183
|
-
const translateOpts = {
|
|
202
|
+
const translateOpts = {
|
|
203
|
+
drainJsonBody: config.drainJsonBody,
|
|
204
|
+
clientIpPolicy: config.clientIpPolicy
|
|
205
|
+
};
|
|
184
206
|
return async function checkpointMiddleware(req, event) {
|
|
185
207
|
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
186
208
|
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
187
209
|
trustedRootsResolver,
|
|
188
210
|
req.headers
|
|
189
211
|
);
|
|
190
|
-
const result = await
|
|
212
|
+
const result = await verifyRequest2(
|
|
191
213
|
httpLike,
|
|
192
214
|
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
193
215
|
);
|
|
216
|
+
await applyBrowserPosture(
|
|
217
|
+
result,
|
|
218
|
+
req.headers.get("cookie"),
|
|
219
|
+
config.browserPosture,
|
|
220
|
+
config.projectId
|
|
221
|
+
);
|
|
194
222
|
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
223
|
+
applyBrowserIntegrityStepUp(result, config.browserPosture, req.nextUrl.pathname);
|
|
195
224
|
if (reporter) {
|
|
196
|
-
const reportPromise = reporter(
|
|
225
|
+
const reportPromise = reporter(
|
|
226
|
+
result,
|
|
227
|
+
extractReporterContext(req, config.clientIpPolicy ?? { platform: "direct" })
|
|
228
|
+
);
|
|
197
229
|
if (event) {
|
|
198
230
|
event.waitUntil(reportPromise);
|
|
199
231
|
}
|
|
200
232
|
}
|
|
201
233
|
await dispatchOnResult(config, result, req);
|
|
202
|
-
const
|
|
234
|
+
const bodyReadable200 = selectBodyReadable200(
|
|
235
|
+
config.delegationChallengeMode ?? "negotiated",
|
|
236
|
+
req.headers,
|
|
237
|
+
result.detectionDetail.detectionClass,
|
|
238
|
+
result.detectionDetail.isAgent
|
|
239
|
+
);
|
|
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
|
+
});
|
|
203
254
|
return adaptToNextResponse(rendered, req);
|
|
204
255
|
};
|
|
205
256
|
}
|
|
206
257
|
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
207
|
-
var VERSION = "1.
|
|
258
|
+
var VERSION = "1.9.0";
|
|
208
259
|
function buildReporter(config, runtime = "node") {
|
|
209
260
|
if (!config.apiKey) return null;
|
|
210
261
|
return makeDetectionReporter({
|
|
@@ -218,6 +269,7 @@ function buildReporter(config, runtime = "node") {
|
|
|
218
269
|
});
|
|
219
270
|
}
|
|
220
271
|
function buildTrustedRootsResolver(config, composed) {
|
|
272
|
+
if (config.trustedDelegationRoots) return () => Promise.resolve(config.trustedDelegationRoots);
|
|
221
273
|
if (composed?.trustedDelegationRoots) {
|
|
222
274
|
return () => composed.trustedDelegationRoots();
|
|
223
275
|
}
|
|
@@ -230,33 +282,15 @@ function buildTrustedRootsResolver(config, composed) {
|
|
|
230
282
|
const projectId = config.projectId;
|
|
231
283
|
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
232
284
|
}
|
|
233
|
-
function
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
fetcher: new PolicyFetcher({
|
|
239
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
240
|
-
apiKey: config.apiKey,
|
|
241
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
242
|
-
}),
|
|
243
|
-
// LAZY dynamic import — NOT a top-level `import` — so the node-only
|
|
244
|
-
// `./policy` glue (`createRequire`/`fs` at module load) is never pulled into
|
|
245
|
-
// the Edge bundle. `middleware-edge.ts` imports helpers from this file, so a
|
|
246
|
-
// top-level `./policy` import would surface as a side-effect import in the
|
|
247
|
-
// edge bundle and boot-fail on Vercel edge. The import is cached after first
|
|
248
|
-
// call; the core's single-flight cache wraps the (now async) compile.
|
|
249
|
-
compile: async (_language, source) => {
|
|
250
|
-
const { createPolicyEvaluator } = await import('@kya-os/checkpoint-wasm-runtime/policy');
|
|
251
|
-
return createPolicyEvaluator(source);
|
|
252
|
-
},
|
|
253
|
-
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
|
|
254
290
|
});
|
|
255
|
-
}
|
|
256
|
-
function extractReporterContext(req) {
|
|
257
291
|
return {
|
|
258
292
|
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
259
|
-
ipAddress:
|
|
293
|
+
ipAddress: resolution.ip ?? void 0,
|
|
260
294
|
path: req.nextUrl.pathname,
|
|
261
295
|
url: req.nextUrl.href,
|
|
262
296
|
method: req.method
|
|
@@ -271,6 +305,8 @@ function buildVerifyOpts(config) {
|
|
|
271
305
|
policyEvaluator: overrides.policyEvaluator ?? makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
272
306
|
clock: makeSystemClock(),
|
|
273
307
|
tenantHost: config.tenantHost,
|
|
308
|
+
resourceOrgDid: config.resourceOrgDid,
|
|
309
|
+
trustedDelegationRoots: config.trustedDelegationRoots,
|
|
274
310
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
275
311
|
reputationBaseline: config.reputationBaseline,
|
|
276
312
|
argusUrl: config.argusUrl,
|
|
@@ -285,5 +321,32 @@ async function dispatchOnResult(config, result, req) {
|
|
|
285
321
|
} catch {
|
|
286
322
|
}
|
|
287
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
|
+
}
|
|
288
351
|
|
|
289
|
-
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) {
|