@kya-os/checkpoint-nextjs 1.7.1 → 1.10.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 +107 -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.d.mts +6 -34
- package/dist/adapt.d.mts.map +1 -0
- package/dist/adapt.d.ts +6 -34
- package/dist/adapt.d.ts.map +1 -0
- package/dist/adapt.js +79 -49
- package/dist/adapt.mjs +78 -46
- package/dist/adapt.mjs.map +1 -0
- package/dist/api-client.d.mts +150 -150
- package/dist/api-client.d.mts.map +1 -0
- package/dist/api-client.d.ts +150 -150
- package/dist/api-client.d.ts.map +1 -0
- package/dist/api-client.js +218 -204
- package/dist/api-client.mjs +217 -201
- package/dist/api-client.mjs.map +1 -0
- package/dist/api-middleware-CDSh3TSE.js +379 -0
- package/dist/api-middleware-CMDRoQik.mjs +357 -0
- package/dist/api-middleware-CMDRoQik.mjs.map +1 -0
- package/dist/api-middleware.d.mts +117 -129
- package/dist/api-middleware.d.mts.map +1 -0
- package/dist/api-middleware.d.ts +117 -129
- package/dist/api-middleware.d.ts.map +1 -0
- package/dist/api-middleware.js +8 -540
- package/dist/api-middleware.mjs +3 -534
- package/dist/composed-policy-BCaNiVHv.d.mts +98 -0
- package/dist/composed-policy-BCaNiVHv.d.mts.map +1 -0
- package/dist/composed-policy-zzJYNFTb.d.ts +98 -0
- package/dist/composed-policy-zzJYNFTb.d.ts.map +1 -0
- package/dist/composed-policy.d.mts +2 -115
- package/dist/composed-policy.d.ts +2 -115
- package/dist/composed-policy.js +122 -88
- package/dist/composed-policy.mjs +121 -85
- package/dist/composed-policy.mjs.map +1 -0
- package/dist/config-CbAg2mze.d.ts +255 -0
- package/dist/config-CbAg2mze.d.ts.map +1 -0
- package/dist/config-Doe0Tn53.d.mts +255 -0
- package/dist/config-Doe0Tn53.d.mts.map +1 -0
- package/dist/create-middleware.d.mts +7 -18
- package/dist/create-middleware.d.mts.map +1 -0
- package/dist/create-middleware.d.ts +7 -18
- package/dist/create-middleware.d.ts.map +1 -0
- package/dist/create-middleware.js +36 -31
- package/dist/create-middleware.mjs +35 -28
- package/dist/create-middleware.mjs.map +1 -0
- package/dist/edge/index.d.mts +64 -86
- package/dist/edge/index.d.mts.map +1 -0
- package/dist/edge/index.d.ts +64 -86
- package/dist/edge/index.d.ts.map +1 -0
- package/dist/edge/index.js +349 -275
- package/dist/edge/index.mjs +348 -272
- package/dist/edge/index.mjs.map +1 -0
- package/dist/edge-runtime-loader.d.mts +37 -63
- package/dist/edge-runtime-loader.d.mts.map +1 -0
- package/dist/edge-runtime-loader.d.ts +37 -63
- package/dist/edge-runtime-loader.d.ts.map +1 -0
- package/dist/edge-runtime-loader.js +268 -225
- package/dist/edge-runtime-loader.mjs +267 -222
- package/dist/edge-runtime-loader.mjs.map +1 -0
- package/dist/edge-wasm-middleware.d.mts +27 -46
- package/dist/edge-wasm-middleware.d.mts.map +1 -0
- package/dist/edge-wasm-middleware.d.ts +27 -46
- package/dist/edge-wasm-middleware.d.ts.map +1 -0
- package/dist/edge-wasm-middleware.js +26 -15
- package/dist/edge-wasm-middleware.mjs +25 -12
- package/dist/edge-wasm-middleware.mjs.map +1 -0
- package/dist/index.d.mts +10 -17
- package/dist/index.d.ts +10 -17
- package/dist/index.js +55 -1224
- package/dist/index.mjs +16 -1193
- package/dist/index.mjs.map +1 -0
- package/dist/middleware-bundler.d.mts +7 -0
- package/dist/middleware-bundler.d.mts.map +1 -0
- package/dist/middleware-bundler.d.ts +7 -0
- package/dist/middleware-bundler.d.ts.map +1 -0
- package/dist/middleware-bundler.js +16 -0
- package/dist/middleware-bundler.mjs +16 -0
- package/dist/middleware-bundler.mjs.map +1 -0
- package/dist/middleware-core-BYQP9FND.mjs +165 -0
- package/dist/middleware-core-BYQP9FND.mjs.map +1 -0
- package/dist/middleware-core-D6TijE6P.js +175 -0
- package/dist/middleware-edge.d.mts +8 -50
- package/dist/middleware-edge.d.mts.map +1 -0
- package/dist/middleware-edge.d.ts +8 -50
- package/dist/middleware-edge.d.ts.map +1 -0
- package/dist/middleware-edge.js +41 -307
- package/dist/middleware-edge.mjs +39 -299
- package/dist/middleware-edge.mjs.map +1 -0
- package/dist/middleware-node-CE4GevYs.d.ts +20 -0
- package/dist/middleware-node-CE4GevYs.d.ts.map +1 -0
- package/dist/middleware-node-DtyOuPta.d.mts +20 -0
- package/dist/middleware-node-DtyOuPta.d.mts.map +1 -0
- package/dist/middleware-node-context-BqPbf-fI.mjs +34 -0
- package/dist/middleware-node-context-BqPbf-fI.mjs.map +1 -0
- package/dist/middleware-node-context-rgnc7UNR.js +38 -0
- package/dist/middleware-node.d.mts +3 -63
- package/dist/middleware-node.d.ts +3 -63
- package/dist/middleware-node.js +13 -308
- package/dist/middleware-node.mjs +12 -301
- package/dist/middleware-node.mjs.map +1 -0
- package/dist/middleware.d.mts +8 -28
- package/dist/middleware.d.mts.map +1 -0
- package/dist/middleware.d.ts +8 -28
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +43 -9
- package/dist/middleware.mjs +42 -6
- package/dist/middleware.mjs.map +1 -0
- package/dist/nodejs-wasm-loader.d.mts +8 -7
- package/dist/nodejs-wasm-loader.d.mts.map +1 -0
- package/dist/nodejs-wasm-loader.d.ts +8 -7
- package/dist/nodejs-wasm-loader.d.ts.map +1 -0
- package/dist/nodejs-wasm-loader.js +53 -19
- package/dist/nodejs-wasm-loader.mjs +52 -16
- package/dist/nodejs-wasm-loader.mjs.map +1 -0
- package/dist/policy.d.mts +86 -110
- package/dist/policy.d.mts.map +1 -0
- package/dist/policy.d.ts +86 -110
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +203 -160
- package/dist/policy.mjs +183 -147
- package/dist/policy.mjs.map +1 -0
- package/dist/session-tracker.d.mts +42 -47
- package/dist/session-tracker.d.mts.map +1 -0
- package/dist/session-tracker.d.ts +42 -47
- package/dist/session-tracker.d.ts.map +1 -0
- package/dist/session-tracker.js +141 -163
- package/dist/session-tracker.mjs +140 -160
- package/dist/session-tracker.mjs.map +1 -0
- package/dist/translate.d.mts +18 -30
- package/dist/translate.d.mts.map +1 -0
- package/dist/translate.d.ts +18 -30
- package/dist/translate.d.ts.map +1 -0
- package/dist/translate.js +70 -34
- package/dist/translate.mjs +70 -32
- package/dist/translate.mjs.map +1 -0
- package/dist/types-C0SkqLMM.d.mts +102 -0
- package/dist/types-C0SkqLMM.d.mts.map +1 -0
- package/dist/types-C7DNSCTO.d.ts +102 -0
- package/dist/types-C7DNSCTO.d.ts.map +1 -0
- package/dist/utils-Cj4XNhIY.mjs +35 -0
- package/dist/utils-Cj4XNhIY.mjs.map +1 -0
- package/dist/utils-DAisnYnI.js +45 -0
- package/dist/wasm-setup.d.mts +6 -5
- package/dist/wasm-setup.d.mts.map +1 -0
- package/dist/wasm-setup.d.ts +6 -5
- package/dist/wasm-setup.d.ts.map +1 -0
- package/dist/wasm-setup.js +82 -113
- package/dist/wasm-setup.mjs +85 -108
- package/dist/wasm-setup.mjs.map +1 -0
- package/package.json +22 -15
- package/dist/config-DAwIA4DB.d.mts +0 -214
- package/dist/config-DyU4l5er.d.ts +0 -214
- package/dist/types-D9RQvPNy.d.mts +0 -105
- package/dist/types-D9RQvPNy.d.ts +0 -105
package/dist/middleware-node.js
CHANGED
|
@@ -1,312 +1,17 @@
|
|
|
1
|
-
'
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
const require_middleware_core = require('./middleware-core-D6TijE6P.js');
|
|
3
|
+
const require_middleware_node_context = require('./middleware-node-context-rgnc7UNR.js');
|
|
4
|
+
let _kya_os_checkpoint_wasm_runtime_orchestrator = require("@kya-os/checkpoint-wasm-runtime/orchestrator");
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var reporter = require('@kya-os/checkpoint-wasm-runtime/reporter');
|
|
6
|
-
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
7
|
-
var server = require('next/server');
|
|
8
|
-
var composedPolicy = require('@kya-os/checkpoint-wasm-runtime/composed-policy');
|
|
9
|
-
|
|
10
|
-
// src/middleware-node.ts
|
|
11
|
-
function adaptToNextResponse(rendered, req) {
|
|
12
|
-
const clientAcceptsHtml = checkpointShared.acceptsHtml(req.headers);
|
|
13
|
-
const verdictCookie = checkpointShared.encodeVerdictCookie(rendered);
|
|
14
|
-
const shape = checkpointShared.classifyResponseShape(rendered, clientAcceptsHtml);
|
|
15
|
-
switch (shape) {
|
|
16
|
-
case "pass-through": {
|
|
17
|
-
const res = server.NextResponse.next();
|
|
18
|
-
applyHeaders(res, rendered.headers);
|
|
19
|
-
setVerdictCookie(res, verdictCookie);
|
|
20
|
-
return res;
|
|
21
|
-
}
|
|
22
|
-
case "redirect": {
|
|
23
|
-
const target = new URL(rendered.headers.Location);
|
|
24
|
-
const res = server.NextResponse.redirect(target);
|
|
25
|
-
applyHeaders(res, rendered.headers);
|
|
26
|
-
setVerdictCookie(res, verdictCookie);
|
|
27
|
-
return res;
|
|
28
|
-
}
|
|
29
|
-
case "html-block": {
|
|
30
|
-
const blockedUrl = new URL(checkpointShared.BLOCKED_PATH, req.url);
|
|
31
|
-
const res = server.NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
32
|
-
applyHeaders(res, rendered.headers);
|
|
33
|
-
setVerdictCookie(res, verdictCookie);
|
|
34
|
-
return res;
|
|
35
|
-
}
|
|
36
|
-
case "json-block": {
|
|
37
|
-
const body = rendered.body ?? {};
|
|
38
|
-
const res = server.NextResponse.json(body, { status: rendered.status });
|
|
39
|
-
applyHeaders(res, rendered.headers);
|
|
40
|
-
setVerdictCookie(res, verdictCookie);
|
|
41
|
-
return res;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function setVerdictCookie(res, value) {
|
|
46
|
-
res.cookies.set({
|
|
47
|
-
name: checkpointShared.VERDICT_COOKIE_NAME,
|
|
48
|
-
value,
|
|
49
|
-
path: "/",
|
|
50
|
-
sameSite: "lax",
|
|
51
|
-
httpOnly: false
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
function applyHeaders(res, headers) {
|
|
55
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
56
|
-
res.headers.set(key, value);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
|
|
60
|
-
var NOOP_LOGGER = {
|
|
61
|
-
shadowDivergence: () => {
|
|
62
|
-
},
|
|
63
|
-
evaluationError: () => {
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
function makeComposedPolicyContext(opts) {
|
|
67
|
-
const { projectId, fetcher } = opts;
|
|
68
|
-
const cache = composedPolicy.makeComposedPolicyCache({ compile: opts.compile, cacheMax: opts.cacheMax });
|
|
69
|
-
const logger = opts.logger ?? NOOP_LOGGER;
|
|
70
|
-
return {
|
|
71
|
-
async apply(result, path) {
|
|
72
|
-
const structured = { decision: result.decision, acted: false };
|
|
73
|
-
const policy = await fetcher.getPolicy(projectId);
|
|
74
|
-
const outcome = await composedPolicy.evaluateComposedPolicy({
|
|
75
|
-
cache,
|
|
76
|
-
projectId,
|
|
77
|
-
flags: {
|
|
78
|
-
policyLanguage: policy.policyLanguage,
|
|
79
|
-
policySourceText: policy.policySourceText,
|
|
80
|
-
engineEnforcementEnabled: policy.engineEnforcementEnabled,
|
|
81
|
-
enabled: policy.enabled
|
|
82
|
-
},
|
|
83
|
-
authorizeInput: composedPolicy.verifyResultToAuthorizeInput(result, { tenantId: projectId, path }),
|
|
84
|
-
baselineDecisionKind: result.decision.kind
|
|
85
|
-
});
|
|
86
|
-
if ((outcome.status === "acting" || outcome.status === "shadow") && outcome.diverged) {
|
|
87
|
-
logger.shadowDivergence({
|
|
88
|
-
projectId,
|
|
89
|
-
path,
|
|
90
|
-
engineDecision: outcome.engineDecision.kind,
|
|
91
|
-
structuredDecision: result.decision.kind,
|
|
92
|
-
detectionClass: result.detectionDetail.detectionClass.type,
|
|
93
|
-
verificationMethod: result.detectionDetail.verificationMethod,
|
|
94
|
-
confidence: result.detectionDetail.confidence,
|
|
95
|
-
agentName: result.detectionDetail.detectedAgent?.name
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
if (outcome.status === "error") {
|
|
99
|
-
logger.evaluationError(projectId, outcome.error);
|
|
100
|
-
return structured;
|
|
101
|
-
}
|
|
102
|
-
if (outcome.status === "acting") {
|
|
103
|
-
return {
|
|
104
|
-
decision: outcome.engineDecision,
|
|
105
|
-
acted: true,
|
|
106
|
-
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
107
|
-
// PR5). Conditional spread: absent upstream stays absent here.
|
|
108
|
-
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
return structured;
|
|
112
|
-
},
|
|
113
|
-
async trustedDelegationRoots() {
|
|
114
|
-
const policy = await fetcher.getPolicy(projectId);
|
|
115
|
-
return policy.trustedDelegationRoots ?? [];
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
async function resolveTrustedDelegationRootsForRequest(resolver, headers) {
|
|
120
|
-
if (!resolver) return void 0;
|
|
121
|
-
if (!checkpointShared.requestCarriesDelegationProof(headers)) return void 0;
|
|
122
|
-
const roots = await resolver();
|
|
123
|
-
return roots.length > 0 ? roots : void 0;
|
|
124
|
-
}
|
|
125
|
-
async function applyComposedPolicy(context, result, path) {
|
|
126
|
-
if (!context) return;
|
|
127
|
-
try {
|
|
128
|
-
const outcome = await context.apply(result, path);
|
|
129
|
-
if (outcome.acted) {
|
|
130
|
-
result.decision = outcome.decision;
|
|
131
|
-
if (outcome.composedBlobHash) {
|
|
132
|
-
result.composedBlobHash = outcome.composedBlobHash;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
} catch {
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
var consoleComposedPolicyLogger = {
|
|
139
|
-
shadowDivergence(info) {
|
|
140
|
-
console.warn("[checkpoint/composed-policy] shadow-divergence", info);
|
|
141
|
-
},
|
|
142
|
-
evaluationError(projectId, error) {
|
|
143
|
-
console.error(
|
|
144
|
-
`[checkpoint/composed-policy] evaluation failed for ${projectId}; using structured decision:`,
|
|
145
|
-
error
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
// src/translate.ts
|
|
151
|
-
async function nextRequestToHttpLike(req, opts = {}) {
|
|
152
|
-
const url = new URL(req.url);
|
|
153
|
-
const body = await tryDrainJsonBody(req, opts);
|
|
154
|
-
return {
|
|
155
|
-
method: req.method,
|
|
156
|
-
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
157
|
-
url: url.pathname + url.search,
|
|
158
|
-
headers: headersToRecord(req.headers),
|
|
159
|
-
body,
|
|
160
|
-
remoteAddress: extractRemoteAddress(req)
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
async function tryDrainJsonBody(req, opts) {
|
|
164
|
-
if (opts.drainJsonBody === false) return null;
|
|
165
|
-
const contentType = req.headers.get("content-type") ?? "";
|
|
166
|
-
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
167
|
-
try {
|
|
168
|
-
return await req.clone().text();
|
|
169
|
-
} catch {
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
function headersToRecord(headers) {
|
|
174
|
-
const out = {};
|
|
175
|
-
headers.forEach((value, key) => {
|
|
176
|
-
out[key.toLowerCase()] = value;
|
|
177
|
-
});
|
|
178
|
-
return out;
|
|
179
|
-
}
|
|
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
|
-
}
|
|
186
|
-
const maybeIp = req.ip;
|
|
187
|
-
return maybeIp;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// src/middleware-node.ts
|
|
6
|
+
//#region src/middleware-node.ts
|
|
7
|
+
/** Node host with native filesystem WASM loading. */
|
|
191
8
|
function withCheckpoint(config) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
197
|
-
return async function checkpointMiddleware(req, event) {
|
|
198
|
-
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
199
|
-
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
200
|
-
trustedRootsResolver,
|
|
201
|
-
req.headers
|
|
202
|
-
);
|
|
203
|
-
const result = await orchestrator.verifyRequest(
|
|
204
|
-
httpLike,
|
|
205
|
-
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
206
|
-
);
|
|
207
|
-
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
208
|
-
if (reporter) {
|
|
209
|
-
const reportPromise = reporter(result, extractReporterContext(req));
|
|
210
|
-
if (event) {
|
|
211
|
-
event.waitUntil(reportPromise);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
await dispatchOnResult(config, result, req);
|
|
215
|
-
const bodyReadable200 = checkpointShared.selectBodyReadable200(
|
|
216
|
-
config.delegationChallengeMode ?? "spec-401",
|
|
217
|
-
req.headers,
|
|
218
|
-
result.detectionDetail.detectionClass
|
|
219
|
-
);
|
|
220
|
-
const rendered = orchestrator.renderDecisionAsResponse(result, { bodyReadable200 });
|
|
221
|
-
return adaptToNextResponse(rendered, req);
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
225
|
-
var VERSION = "1.7.1";
|
|
226
|
-
function buildReporter(config, runtime = "node") {
|
|
227
|
-
if (!config.apiKey) return null;
|
|
228
|
-
return reporter.makeDetectionReporter({
|
|
229
|
-
apiKey: config.apiKey,
|
|
230
|
-
baseUrl: config.baseUrl,
|
|
231
|
-
debug: config.debug,
|
|
232
|
-
// Self-identify (incl. node-vs-edge) so the dashboard can version-gate
|
|
233
|
-
// enforcement. Next.js EDGE composed enforcement is opt-in (needs
|
|
234
|
-
// `cedarWasmModule`), so the dashboard shows it as opt-in, never "Enforcing".
|
|
235
|
-
sdk: { name: SDK_NAME, version: VERSION, runtime }
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
function buildTrustedRootsResolver(config, composed) {
|
|
239
|
-
if (composed?.trustedDelegationRoots) {
|
|
240
|
-
return () => composed.trustedDelegationRoots();
|
|
241
|
-
}
|
|
242
|
-
if (!config.projectId) return null;
|
|
243
|
-
const fetcher = new checkpointShared.PolicyFetcher({
|
|
244
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
245
|
-
apiKey: config.apiKey,
|
|
246
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
247
|
-
});
|
|
248
|
-
const projectId = config.projectId;
|
|
249
|
-
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
250
|
-
}
|
|
251
|
-
function buildComposedContext(config) {
|
|
252
|
-
if (config.composedPolicyEnforcer) return config.composedPolicyEnforcer;
|
|
253
|
-
if (!config.projectId) return null;
|
|
254
|
-
return makeComposedPolicyContext({
|
|
255
|
-
projectId: config.projectId,
|
|
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
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
function extractReporterContext(req) {
|
|
275
|
-
return {
|
|
276
|
-
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
277
|
-
ipAddress: req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? req.headers.get("x-real-ip") ?? void 0,
|
|
278
|
-
path: req.nextUrl.pathname,
|
|
279
|
-
url: req.nextUrl.href,
|
|
280
|
-
method: req.method
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
function buildVerifyOpts(config) {
|
|
284
|
-
const overrides = config.adapters ?? {};
|
|
285
|
-
return {
|
|
286
|
-
didResolver: overrides.didResolver ?? adapters.makeDidResolver(),
|
|
287
|
-
statusListCache: overrides.statusListCache ?? adapters.makeStatusListCache(),
|
|
288
|
-
reputationOracle: overrides.reputationOracle ?? adapters.makeReputationOracle({ argusUrl: config.argusUrl }),
|
|
289
|
-
policyEvaluator: overrides.policyEvaluator ?? adapters.makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
290
|
-
clock: adapters.makeSystemClock(),
|
|
291
|
-
tenantHost: config.tenantHost,
|
|
292
|
-
enforcementMode: config.enforcementMode ?? "enforce",
|
|
293
|
-
reputationBaseline: config.reputationBaseline,
|
|
294
|
-
argusUrl: config.argusUrl,
|
|
295
|
-
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false,
|
|
296
|
-
engineConfig: config.engineConfig
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
async function dispatchOnResult(config, result, req) {
|
|
300
|
-
if (!config.onResult) return;
|
|
301
|
-
try {
|
|
302
|
-
await config.onResult(result, req);
|
|
303
|
-
} catch {
|
|
304
|
-
}
|
|
9
|
+
return require_middleware_core.makeNextMiddleware(_kya_os_checkpoint_wasm_runtime_orchestrator.verifyRequest, _kya_os_checkpoint_wasm_runtime_orchestrator.renderDecisionAsResponse, config, {
|
|
10
|
+
runtime: "node",
|
|
11
|
+
buildComposedContext: require_middleware_node_context.buildComposedContext
|
|
12
|
+
});
|
|
305
13
|
}
|
|
306
14
|
|
|
307
|
-
|
|
308
|
-
exports.
|
|
309
|
-
exports.
|
|
310
|
-
exports._buildVerifyOpts = buildVerifyOpts;
|
|
311
|
-
exports._extractReporterContext = extractReporterContext;
|
|
312
|
-
exports.withCheckpoint = withCheckpoint;
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.VERSION = require_middleware_core.VERSION;
|
|
17
|
+
exports.withCheckpoint = withCheckpoint;
|
package/dist/middleware-node.mjs
CHANGED
|
@@ -1,305 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { selectBodyReadable200, PolicyFetcher, requestCarriesDelegationProof, acceptsHtml, encodeVerdictCookie, classifyResponseShape, BLOCKED_PATH, VERDICT_COOKIE_NAME } from '@kya-os/checkpoint-shared';
|
|
5
|
-
import { NextResponse } from 'next/server';
|
|
6
|
-
import { makeComposedPolicyCache, evaluateComposedPolicy, verifyResultToAuthorizeInput } from '@kya-os/checkpoint-wasm-runtime/composed-policy';
|
|
1
|
+
import { n as makeNextMiddleware, t as VERSION } from "./middleware-core-BYQP9FND.mjs";
|
|
2
|
+
import { t as buildComposedContext } from "./middleware-node-context-BqPbf-fI.mjs";
|
|
3
|
+
import { renderDecisionAsResponse, verifyRequest } from "@kya-os/checkpoint-wasm-runtime/orchestrator";
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const clientAcceptsHtml = acceptsHtml(req.headers);
|
|
11
|
-
const verdictCookie = encodeVerdictCookie(rendered);
|
|
12
|
-
const shape = classifyResponseShape(rendered, clientAcceptsHtml);
|
|
13
|
-
switch (shape) {
|
|
14
|
-
case "pass-through": {
|
|
15
|
-
const res = NextResponse.next();
|
|
16
|
-
applyHeaders(res, rendered.headers);
|
|
17
|
-
setVerdictCookie(res, verdictCookie);
|
|
18
|
-
return res;
|
|
19
|
-
}
|
|
20
|
-
case "redirect": {
|
|
21
|
-
const target = new URL(rendered.headers.Location);
|
|
22
|
-
const res = NextResponse.redirect(target);
|
|
23
|
-
applyHeaders(res, rendered.headers);
|
|
24
|
-
setVerdictCookie(res, verdictCookie);
|
|
25
|
-
return res;
|
|
26
|
-
}
|
|
27
|
-
case "html-block": {
|
|
28
|
-
const blockedUrl = new URL(BLOCKED_PATH, req.url);
|
|
29
|
-
const res = NextResponse.rewrite(blockedUrl, { status: 200 });
|
|
30
|
-
applyHeaders(res, rendered.headers);
|
|
31
|
-
setVerdictCookie(res, verdictCookie);
|
|
32
|
-
return res;
|
|
33
|
-
}
|
|
34
|
-
case "json-block": {
|
|
35
|
-
const body = rendered.body ?? {};
|
|
36
|
-
const res = NextResponse.json(body, { status: rendered.status });
|
|
37
|
-
applyHeaders(res, rendered.headers);
|
|
38
|
-
setVerdictCookie(res, verdictCookie);
|
|
39
|
-
return res;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function setVerdictCookie(res, value) {
|
|
44
|
-
res.cookies.set({
|
|
45
|
-
name: VERDICT_COOKIE_NAME,
|
|
46
|
-
value,
|
|
47
|
-
path: "/",
|
|
48
|
-
sameSite: "lax",
|
|
49
|
-
httpOnly: false
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function applyHeaders(res, headers) {
|
|
53
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
54
|
-
res.headers.set(key, value);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
|
|
58
|
-
var NOOP_LOGGER = {
|
|
59
|
-
shadowDivergence: () => {
|
|
60
|
-
},
|
|
61
|
-
evaluationError: () => {
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
function makeComposedPolicyContext(opts) {
|
|
65
|
-
const { projectId, fetcher } = opts;
|
|
66
|
-
const cache = makeComposedPolicyCache({ compile: opts.compile, cacheMax: opts.cacheMax });
|
|
67
|
-
const logger = opts.logger ?? NOOP_LOGGER;
|
|
68
|
-
return {
|
|
69
|
-
async apply(result, path) {
|
|
70
|
-
const structured = { decision: result.decision, acted: false };
|
|
71
|
-
const policy = await fetcher.getPolicy(projectId);
|
|
72
|
-
const outcome = await evaluateComposedPolicy({
|
|
73
|
-
cache,
|
|
74
|
-
projectId,
|
|
75
|
-
flags: {
|
|
76
|
-
policyLanguage: policy.policyLanguage,
|
|
77
|
-
policySourceText: policy.policySourceText,
|
|
78
|
-
engineEnforcementEnabled: policy.engineEnforcementEnabled,
|
|
79
|
-
enabled: policy.enabled
|
|
80
|
-
},
|
|
81
|
-
authorizeInput: verifyResultToAuthorizeInput(result, { tenantId: projectId, path }),
|
|
82
|
-
baselineDecisionKind: result.decision.kind
|
|
83
|
-
});
|
|
84
|
-
if ((outcome.status === "acting" || outcome.status === "shadow") && outcome.diverged) {
|
|
85
|
-
logger.shadowDivergence({
|
|
86
|
-
projectId,
|
|
87
|
-
path,
|
|
88
|
-
engineDecision: outcome.engineDecision.kind,
|
|
89
|
-
structuredDecision: result.decision.kind,
|
|
90
|
-
detectionClass: result.detectionDetail.detectionClass.type,
|
|
91
|
-
verificationMethod: result.detectionDetail.verificationMethod,
|
|
92
|
-
confidence: result.detectionDetail.confidence,
|
|
93
|
-
agentName: result.detectionDetail.detectedAgent?.name
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
if (outcome.status === "error") {
|
|
97
|
-
logger.evaluationError(projectId, outcome.error);
|
|
98
|
-
return structured;
|
|
99
|
-
}
|
|
100
|
-
if (outcome.status === "acting") {
|
|
101
|
-
return {
|
|
102
|
-
decision: outcome.engineDecision,
|
|
103
|
-
acted: true,
|
|
104
|
-
// Echo the SSOT revision pin with the decision it produced (#3246
|
|
105
|
-
// PR5). Conditional spread: absent upstream stays absent here.
|
|
106
|
-
...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
return structured;
|
|
110
|
-
},
|
|
111
|
-
async trustedDelegationRoots() {
|
|
112
|
-
const policy = await fetcher.getPolicy(projectId);
|
|
113
|
-
return policy.trustedDelegationRoots ?? [];
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
async function resolveTrustedDelegationRootsForRequest(resolver, headers) {
|
|
118
|
-
if (!resolver) return void 0;
|
|
119
|
-
if (!requestCarriesDelegationProof(headers)) return void 0;
|
|
120
|
-
const roots = await resolver();
|
|
121
|
-
return roots.length > 0 ? roots : void 0;
|
|
122
|
-
}
|
|
123
|
-
async function applyComposedPolicy(context, result, path) {
|
|
124
|
-
if (!context) return;
|
|
125
|
-
try {
|
|
126
|
-
const outcome = await context.apply(result, path);
|
|
127
|
-
if (outcome.acted) {
|
|
128
|
-
result.decision = outcome.decision;
|
|
129
|
-
if (outcome.composedBlobHash) {
|
|
130
|
-
result.composedBlobHash = outcome.composedBlobHash;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
} catch {
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
var consoleComposedPolicyLogger = {
|
|
137
|
-
shadowDivergence(info) {
|
|
138
|
-
console.warn("[checkpoint/composed-policy] shadow-divergence", info);
|
|
139
|
-
},
|
|
140
|
-
evaluationError(projectId, error) {
|
|
141
|
-
console.error(
|
|
142
|
-
`[checkpoint/composed-policy] evaluation failed for ${projectId}; using structured decision:`,
|
|
143
|
-
error
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
// src/translate.ts
|
|
149
|
-
async function nextRequestToHttpLike(req, opts = {}) {
|
|
150
|
-
const url = new URL(req.url);
|
|
151
|
-
const body = await tryDrainJsonBody(req, opts);
|
|
152
|
-
return {
|
|
153
|
-
method: req.method,
|
|
154
|
-
// Path + query only — orchestrator's URL parsing expects no scheme/host.
|
|
155
|
-
url: url.pathname + url.search,
|
|
156
|
-
headers: headersToRecord(req.headers),
|
|
157
|
-
body,
|
|
158
|
-
remoteAddress: extractRemoteAddress(req)
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
async function tryDrainJsonBody(req, opts) {
|
|
162
|
-
if (opts.drainJsonBody === false) return null;
|
|
163
|
-
const contentType = req.headers.get("content-type") ?? "";
|
|
164
|
-
if (!contentType.toLowerCase().includes("application/json")) return null;
|
|
165
|
-
try {
|
|
166
|
-
return await req.clone().text();
|
|
167
|
-
} catch {
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
function headersToRecord(headers) {
|
|
172
|
-
const out = {};
|
|
173
|
-
headers.forEach((value, key) => {
|
|
174
|
-
out[key.toLowerCase()] = value;
|
|
175
|
-
});
|
|
176
|
-
return out;
|
|
177
|
-
}
|
|
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
|
-
}
|
|
184
|
-
const maybeIp = req.ip;
|
|
185
|
-
return maybeIp;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// src/middleware-node.ts
|
|
5
|
+
//#region src/middleware-node.ts
|
|
6
|
+
/** Node host with native filesystem WASM loading. */
|
|
189
7
|
function withCheckpoint(config) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const translateOpts = { drainJsonBody: config.drainJsonBody };
|
|
195
|
-
return async function checkpointMiddleware(req, event) {
|
|
196
|
-
const httpLike = await nextRequestToHttpLike(req, translateOpts);
|
|
197
|
-
const trustedDelegationRoots = await resolveTrustedDelegationRootsForRequest(
|
|
198
|
-
trustedRootsResolver,
|
|
199
|
-
req.headers
|
|
200
|
-
);
|
|
201
|
-
const result = await verifyRequest(
|
|
202
|
-
httpLike,
|
|
203
|
-
trustedDelegationRoots ? { ...opts, trustedDelegationRoots } : opts
|
|
204
|
-
);
|
|
205
|
-
await applyComposedPolicy(composed, result, req.nextUrl.pathname);
|
|
206
|
-
if (reporter) {
|
|
207
|
-
const reportPromise = reporter(result, extractReporterContext(req));
|
|
208
|
-
if (event) {
|
|
209
|
-
event.waitUntil(reportPromise);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
await dispatchOnResult(config, result, req);
|
|
213
|
-
const bodyReadable200 = selectBodyReadable200(
|
|
214
|
-
config.delegationChallengeMode ?? "spec-401",
|
|
215
|
-
req.headers,
|
|
216
|
-
result.detectionDetail.detectionClass
|
|
217
|
-
);
|
|
218
|
-
const rendered = renderDecisionAsResponse(result, { bodyReadable200 });
|
|
219
|
-
return adaptToNextResponse(rendered, req);
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
var SDK_NAME = "@kya-os/checkpoint-nextjs";
|
|
223
|
-
var VERSION = "1.7.1";
|
|
224
|
-
function buildReporter(config, runtime = "node") {
|
|
225
|
-
if (!config.apiKey) return null;
|
|
226
|
-
return makeDetectionReporter({
|
|
227
|
-
apiKey: config.apiKey,
|
|
228
|
-
baseUrl: config.baseUrl,
|
|
229
|
-
debug: config.debug,
|
|
230
|
-
// Self-identify (incl. node-vs-edge) so the dashboard can version-gate
|
|
231
|
-
// enforcement. Next.js EDGE composed enforcement is opt-in (needs
|
|
232
|
-
// `cedarWasmModule`), so the dashboard shows it as opt-in, never "Enforcing".
|
|
233
|
-
sdk: { name: SDK_NAME, version: VERSION, runtime }
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
function buildTrustedRootsResolver(config, composed) {
|
|
237
|
-
if (composed?.trustedDelegationRoots) {
|
|
238
|
-
return () => composed.trustedDelegationRoots();
|
|
239
|
-
}
|
|
240
|
-
if (!config.projectId) return null;
|
|
241
|
-
const fetcher = new PolicyFetcher({
|
|
242
|
-
apiBaseUrl: config.dashboardUrl ?? config.baseUrl ?? DEFAULT_DASHBOARD_URL,
|
|
243
|
-
apiKey: config.apiKey,
|
|
244
|
-
cacheTtlSeconds: config.policyCacheTtlSeconds
|
|
245
|
-
});
|
|
246
|
-
const projectId = config.projectId;
|
|
247
|
-
return async () => (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
|
|
248
|
-
}
|
|
249
|
-
function buildComposedContext(config) {
|
|
250
|
-
if (config.composedPolicyEnforcer) return config.composedPolicyEnforcer;
|
|
251
|
-
if (!config.projectId) return null;
|
|
252
|
-
return makeComposedPolicyContext({
|
|
253
|
-
projectId: config.projectId,
|
|
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
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
function extractReporterContext(req) {
|
|
273
|
-
return {
|
|
274
|
-
userAgent: req.headers.get("user-agent") ?? void 0,
|
|
275
|
-
ipAddress: req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? req.headers.get("x-real-ip") ?? void 0,
|
|
276
|
-
path: req.nextUrl.pathname,
|
|
277
|
-
url: req.nextUrl.href,
|
|
278
|
-
method: req.method
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
function buildVerifyOpts(config) {
|
|
282
|
-
const overrides = config.adapters ?? {};
|
|
283
|
-
return {
|
|
284
|
-
didResolver: overrides.didResolver ?? makeDidResolver(),
|
|
285
|
-
statusListCache: overrides.statusListCache ?? makeStatusListCache(),
|
|
286
|
-
reputationOracle: overrides.reputationOracle ?? makeReputationOracle({ argusUrl: config.argusUrl }),
|
|
287
|
-
policyEvaluator: overrides.policyEvaluator ?? makePolicyEvaluator({ dashboardUrl: config.dashboardUrl }),
|
|
288
|
-
clock: makeSystemClock(),
|
|
289
|
-
tenantHost: config.tenantHost,
|
|
290
|
-
enforcementMode: config.enforcementMode ?? "enforce",
|
|
291
|
-
reputationBaseline: config.reputationBaseline,
|
|
292
|
-
argusUrl: config.argusUrl,
|
|
293
|
-
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false,
|
|
294
|
-
engineConfig: config.engineConfig
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
async function dispatchOnResult(config, result, req) {
|
|
298
|
-
if (!config.onResult) return;
|
|
299
|
-
try {
|
|
300
|
-
await config.onResult(result, req);
|
|
301
|
-
} catch {
|
|
302
|
-
}
|
|
8
|
+
return makeNextMiddleware(verifyRequest, renderDecisionAsResponse, config, {
|
|
9
|
+
runtime: "node",
|
|
10
|
+
buildComposedContext
|
|
11
|
+
});
|
|
303
12
|
}
|
|
304
13
|
|
|
305
|
-
|
|
14
|
+
//#endregion
|
|
15
|
+
export { VERSION, withCheckpoint };
|
|
16
|
+
//# sourceMappingURL=middleware-node.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware-node.mjs","names":[],"sources":["../src/middleware-node.ts"],"sourcesContent":["import {\n verifyRequest,\n renderDecisionAsResponse,\n} from '@kya-os/checkpoint-wasm-runtime/orchestrator';\nimport { makeNextMiddleware } from './middleware-core';\nimport { buildComposedContext } from './middleware-node-context';\nimport type { CheckpointConfig } from './config';\nexport type { CheckpointConfig } from './config';\n\n/** Node host with native filesystem WASM loading. */\nexport function withCheckpoint(config: CheckpointConfig) {\n return makeNextMiddleware(verifyRequest, renderDecisionAsResponse, config, {\n runtime: 'node',\n buildComposedContext,\n });\n}\n\nexport { VERSION } from './middleware-core';\n"],"mappings":";;;;;;AAUA,SAAgB,eAAe,QAA0B;CACvD,OAAO,mBAAmB,eAAe,0BAA0B,QAAQ;EACzE,SAAS;EACT;CACF,CAAC;AACH"}
|