@kya-os/checkpoint-nextjs 1.1.1 → 1.2.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 +174 -0
- package/EDGE_RUNTIME_WASM_SETUP.md +4 -10
- package/README.md +13 -0
- package/bin/setup-edge-wasm.js +40 -32
- package/dist/api-client.d.mts +10 -10
- package/dist/api-client.d.ts +10 -10
- package/dist/create-middleware.d.mts +7 -2
- package/dist/create-middleware.d.ts +7 -2
- package/dist/edge/index.d.mts +3 -3
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +16 -3
- package/dist/edge/index.mjs +16 -3
- package/dist/edge-runtime-loader.d.mts +17 -28
- package/dist/edge-runtime-loader.d.ts +17 -28
- package/dist/edge-runtime-loader.js +43 -14
- package/dist/edge-runtime-loader.mjs +44 -15
- package/dist/edge-wasm-middleware.d.mts +28 -34
- package/dist/edge-wasm-middleware.d.ts +28 -34
- package/dist/edge-wasm-middleware.js +16 -306
- package/dist/edge-wasm-middleware.mjs +16 -307
- package/dist/index.js +5 -2
- package/dist/index.mjs +6 -3
- package/dist/middleware-edge.js +2 -1
- package/dist/middleware-edge.mjs +2 -1
- package/dist/middleware-node.d.mts +16 -1
- package/dist/middleware-node.d.ts +16 -1
- package/dist/middleware-node.js +2 -1
- package/dist/middleware-node.mjs +2 -1
- package/dist/nodejs-wasm-loader.d.mts +26 -9
- package/dist/nodejs-wasm-loader.d.ts +26 -9
- package/dist/nodejs-wasm-loader.js +21 -78
- package/dist/nodejs-wasm-loader.mjs +21 -74
- package/dist/session-tracker.d.mts +2 -2
- package/dist/session-tracker.d.ts +2 -2
- package/dist/session-tracker.js +3 -1
- package/dist/session-tracker.mjs +4 -2
- package/dist/wasm-middleware.d.mts +19 -3
- package/dist/wasm-middleware.d.ts +19 -3
- package/dist/wasm-middleware.js +32 -3
- package/dist/wasm-middleware.mjs +32 -4
- package/dist/wasm-setup.js +29 -81
- package/dist/wasm-setup.mjs +29 -76
- package/package.json +8 -6
- package/templates/middleware-wasm-100.ts +11 -3
|
@@ -1,316 +1,26 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var server = require('next/server');
|
|
4
|
-
var checkpointShared = require('@kya-os/checkpoint-shared');
|
|
5
|
-
|
|
6
3
|
// src/edge-wasm-middleware.ts
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const idx = heap_next;
|
|
15
|
-
heap_next = heap[idx];
|
|
16
|
-
heap[idx] = obj;
|
|
17
|
-
return idx;
|
|
18
|
-
}
|
|
19
|
-
function getObject(idx) {
|
|
20
|
-
return heap[idx];
|
|
21
|
-
}
|
|
22
|
-
function dropObject(idx) {
|
|
23
|
-
if (idx < 132) return;
|
|
24
|
-
heap[idx] = heap_next;
|
|
25
|
-
heap_next = idx;
|
|
26
|
-
}
|
|
27
|
-
function takeObject(idx) {
|
|
28
|
-
const ret = getObject(idx);
|
|
29
|
-
dropObject(idx);
|
|
30
|
-
return ret;
|
|
31
|
-
}
|
|
32
|
-
var cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
|
|
33
|
-
var cachedTextEncoder = new TextEncoder();
|
|
34
|
-
var cachedUint8ArrayMemory0 = null;
|
|
35
|
-
function getUint8ArrayMemory0() {
|
|
36
|
-
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
37
|
-
cachedUint8ArrayMemory0 = new Uint8Array(wasmExports.memory.buffer);
|
|
38
|
-
}
|
|
39
|
-
return cachedUint8ArrayMemory0;
|
|
40
|
-
}
|
|
41
|
-
function getStringFromWasm0(ptr, len) {
|
|
42
|
-
ptr = ptr >>> 0;
|
|
43
|
-
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
44
|
-
}
|
|
45
|
-
var WASM_VECTOR_LEN = 0;
|
|
46
|
-
function passStringToWasm0(arg, malloc, realloc) {
|
|
47
|
-
if (realloc === void 0) {
|
|
48
|
-
const buf = cachedTextEncoder.encode(arg);
|
|
49
|
-
const ptr2 = malloc(buf.length, 1) >>> 0;
|
|
50
|
-
getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
|
|
51
|
-
WASM_VECTOR_LEN = buf.length;
|
|
52
|
-
return ptr2;
|
|
53
|
-
}
|
|
54
|
-
let len = arg.length;
|
|
55
|
-
let ptr = malloc(len, 1) >>> 0;
|
|
56
|
-
const mem = getUint8ArrayMemory0();
|
|
57
|
-
let offset = 0;
|
|
58
|
-
for (; offset < len; offset++) {
|
|
59
|
-
const code = arg.charCodeAt(offset);
|
|
60
|
-
if (code > 127) break;
|
|
61
|
-
mem[ptr + offset] = code;
|
|
62
|
-
}
|
|
63
|
-
if (offset !== len) {
|
|
64
|
-
if (offset !== 0) {
|
|
65
|
-
arg = arg.slice(offset);
|
|
66
|
-
}
|
|
67
|
-
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
68
|
-
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
69
|
-
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
70
|
-
offset += ret.written;
|
|
71
|
-
}
|
|
72
|
-
WASM_VECTOR_LEN = offset;
|
|
73
|
-
return ptr;
|
|
74
|
-
}
|
|
75
|
-
var cachedDataViewMemory0 = null;
|
|
76
|
-
function getDataViewMemory0() {
|
|
77
|
-
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer !== wasmExports.memory.buffer) {
|
|
78
|
-
cachedDataViewMemory0 = new DataView(wasmExports.memory.buffer);
|
|
79
|
-
}
|
|
80
|
-
return cachedDataViewMemory0;
|
|
4
|
+
var MIGRATION_ERROR = "`@kya-os/checkpoint-nextjs/edge-wasm-middleware` was deprecated in Phase-D.9a (legacy `agentshield-wasm` Rust crate retirement). Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs/edge` \u2014 engine-backed via the Rust `kya-os-engine` crate (PDM-1 #2560), runs the full orchestrator including MCP-I envelope verification. See packages/checkpoint-nextjs/README.md for the canonical recipe.";
|
|
5
|
+
var _edgeWasmWarned = false;
|
|
6
|
+
function warnEdgeWasmDeprecated() {
|
|
7
|
+
if (_edgeWasmWarned) return;
|
|
8
|
+
_edgeWasmWarned = true;
|
|
9
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") return;
|
|
10
|
+
console.warn(`[Checkpoint] ${MIGRATION_ERROR}`);
|
|
81
11
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return wasmInitPromise;
|
|
85
|
-
}
|
|
86
|
-
wasmInitPromise = (async () => {
|
|
87
|
-
try {
|
|
88
|
-
const imports = {
|
|
89
|
-
"./agentshield_wasm_bg.js": {
|
|
90
|
-
__wbindgen_object_drop_ref: function(arg0) {
|
|
91
|
-
dropObject(arg0);
|
|
92
|
-
},
|
|
93
|
-
__wbindgen_string_new: function(arg0, arg1) {
|
|
94
|
-
const ret = getStringFromWasm0(arg0, arg1);
|
|
95
|
-
return addHeapObject(ret);
|
|
96
|
-
},
|
|
97
|
-
__wbindgen_string_get: function(arg0, arg1) {
|
|
98
|
-
const obj = getObject(arg1);
|
|
99
|
-
const ret = typeof obj === "string" ? obj : void 0;
|
|
100
|
-
const ptr1 = ret ? passStringToWasm0(
|
|
101
|
-
ret,
|
|
102
|
-
wasmExports.__wbindgen_malloc,
|
|
103
|
-
wasmExports.__wbindgen_realloc
|
|
104
|
-
) : 0;
|
|
105
|
-
const len1 = WASM_VECTOR_LEN;
|
|
106
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
107
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
108
|
-
},
|
|
109
|
-
__wbindgen_throw: function(arg0, arg1) {
|
|
110
|
-
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
wbg: {
|
|
114
|
-
__wbindgen_object_drop_ref: function(arg0) {
|
|
115
|
-
dropObject(arg0);
|
|
116
|
-
},
|
|
117
|
-
__wbindgen_string_new: function(arg0, arg1) {
|
|
118
|
-
const ret = getStringFromWasm0(arg0, arg1);
|
|
119
|
-
return addHeapObject(ret);
|
|
120
|
-
},
|
|
121
|
-
__wbindgen_string_get: function(arg0, arg1) {
|
|
122
|
-
const obj = getObject(arg1);
|
|
123
|
-
const ret = typeof obj === "string" ? obj : void 0;
|
|
124
|
-
const ptr1 = ret ? passStringToWasm0(
|
|
125
|
-
ret,
|
|
126
|
-
wasmExports.__wbindgen_malloc,
|
|
127
|
-
wasmExports.__wbindgen_realloc
|
|
128
|
-
) : 0;
|
|
129
|
-
const len1 = WASM_VECTOR_LEN;
|
|
130
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
131
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
132
|
-
},
|
|
133
|
-
__wbindgen_throw: function(arg0, arg1) {
|
|
134
|
-
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
const instance = await WebAssembly.instantiate(wasmModule, imports);
|
|
139
|
-
wasmExports = instance.exports;
|
|
140
|
-
if (wasmExports.__wbindgen_start) {
|
|
141
|
-
wasmExports.__wbindgen_start();
|
|
142
|
-
}
|
|
143
|
-
console.log(
|
|
144
|
-
"\u2705 AgentShield: WASM module loaded successfully in Edge Runtime (95-100% confidence enabled)"
|
|
145
|
-
);
|
|
146
|
-
} catch (error) {
|
|
147
|
-
console.error("\u274C AgentShield: Failed to initialize WASM in Edge Runtime:", error);
|
|
148
|
-
throw error;
|
|
149
|
-
}
|
|
150
|
-
})();
|
|
151
|
-
return wasmInitPromise;
|
|
12
|
+
function __resetEdgeWasmWarningForTests() {
|
|
13
|
+
_edgeWasmWarned = false;
|
|
152
14
|
}
|
|
153
|
-
async function
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
try {
|
|
158
|
-
const userAgent = metadata.userAgent || "";
|
|
159
|
-
const ipAddress = metadata.ipAddress || "";
|
|
160
|
-
const headers = JSON.stringify(metadata.headers);
|
|
161
|
-
const timestamp = metadata.timestamp;
|
|
162
|
-
const url = metadata.url || "";
|
|
163
|
-
const method = metadata.method || "";
|
|
164
|
-
const clientFingerprint = metadata.clientFingerprint || "";
|
|
165
|
-
const ptr0 = passStringToWasm0(
|
|
166
|
-
userAgent,
|
|
167
|
-
wasmExports.__wbindgen_malloc,
|
|
168
|
-
wasmExports.__wbindgen_realloc
|
|
169
|
-
);
|
|
170
|
-
const len0 = WASM_VECTOR_LEN;
|
|
171
|
-
const ptr1 = passStringToWasm0(
|
|
172
|
-
ipAddress,
|
|
173
|
-
wasmExports.__wbindgen_malloc,
|
|
174
|
-
wasmExports.__wbindgen_realloc
|
|
175
|
-
);
|
|
176
|
-
const len1 = WASM_VECTOR_LEN;
|
|
177
|
-
const ptr2 = passStringToWasm0(
|
|
178
|
-
headers,
|
|
179
|
-
wasmExports.__wbindgen_malloc,
|
|
180
|
-
wasmExports.__wbindgen_realloc
|
|
181
|
-
);
|
|
182
|
-
const len2 = WASM_VECTOR_LEN;
|
|
183
|
-
const ptr3 = passStringToWasm0(
|
|
184
|
-
timestamp,
|
|
185
|
-
wasmExports.__wbindgen_malloc,
|
|
186
|
-
wasmExports.__wbindgen_realloc
|
|
187
|
-
);
|
|
188
|
-
const len3 = WASM_VECTOR_LEN;
|
|
189
|
-
const ptr4 = passStringToWasm0(
|
|
190
|
-
url,
|
|
191
|
-
wasmExports.__wbindgen_malloc,
|
|
192
|
-
wasmExports.__wbindgen_realloc
|
|
193
|
-
);
|
|
194
|
-
const len4 = WASM_VECTOR_LEN;
|
|
195
|
-
const ptr5 = passStringToWasm0(
|
|
196
|
-
method,
|
|
197
|
-
wasmExports.__wbindgen_malloc,
|
|
198
|
-
wasmExports.__wbindgen_realloc
|
|
199
|
-
);
|
|
200
|
-
const len5 = WASM_VECTOR_LEN;
|
|
201
|
-
const ptr6 = passStringToWasm0(
|
|
202
|
-
clientFingerprint,
|
|
203
|
-
wasmExports.__wbindgen_malloc,
|
|
204
|
-
wasmExports.__wbindgen_realloc
|
|
205
|
-
);
|
|
206
|
-
const len6 = WASM_VECTOR_LEN;
|
|
207
|
-
const metadataPtr = wasmExports.jsrequestmetadata_new(
|
|
208
|
-
ptr0,
|
|
209
|
-
len0,
|
|
210
|
-
ptr1,
|
|
211
|
-
len1,
|
|
212
|
-
ptr2,
|
|
213
|
-
len2,
|
|
214
|
-
ptr3,
|
|
215
|
-
len3,
|
|
216
|
-
ptr4,
|
|
217
|
-
len4,
|
|
218
|
-
ptr5,
|
|
219
|
-
len5,
|
|
220
|
-
ptr6,
|
|
221
|
-
len6
|
|
222
|
-
);
|
|
223
|
-
const resultPtr = wasmExports.detect_agent(metadataPtr);
|
|
224
|
-
const result = takeObject(resultPtr);
|
|
225
|
-
wasmExports.__wbg_jsrequestmetadata_free(metadataPtr, 0);
|
|
226
|
-
const parsedResult = typeof result === "string" ? JSON.parse(result) : result;
|
|
227
|
-
return {
|
|
228
|
-
isAgent: parsedResult.is_agent || false,
|
|
229
|
-
isAiCrawler: parsedResult.is_ai_crawler || false,
|
|
230
|
-
confidence: parsedResult.confidence || 0,
|
|
231
|
-
agent: parsedResult.agent,
|
|
232
|
-
verificationMethod: parsedResult.verification_method || "wasm",
|
|
233
|
-
riskLevel: parsedResult.risk_level || "low",
|
|
234
|
-
timestamp: parsedResult.timestamp || metadata.timestamp,
|
|
235
|
-
reasons: parsedResult.reasons || []
|
|
236
|
-
};
|
|
237
|
-
} catch (error) {
|
|
238
|
-
console.error("WASM detection failed:", error);
|
|
239
|
-
return {
|
|
240
|
-
isAgent: false,
|
|
241
|
-
confidence: 0,
|
|
242
|
-
verificationMethod: "pattern",
|
|
243
|
-
riskLevel: "low",
|
|
244
|
-
timestamp: metadata.timestamp
|
|
245
|
-
};
|
|
246
|
-
}
|
|
15
|
+
async function initializeEdgeWasm(_wasmModule) {
|
|
16
|
+
warnEdgeWasmDeprecated();
|
|
17
|
+
throw new Error(MIGRATION_ERROR);
|
|
247
18
|
}
|
|
248
|
-
function createEdgeWasmMiddleware(
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
onAgentDetected,
|
|
252
|
-
blockOnHighConfidence = false,
|
|
253
|
-
confidenceThreshold: configThreshold = 0.9,
|
|
254
|
-
skipPaths = [],
|
|
255
|
-
blockedResponse = {
|
|
256
|
-
status: 403,
|
|
257
|
-
message: "AI agent access restricted",
|
|
258
|
-
headers: { "Content-Type": "application/json" }
|
|
259
|
-
}
|
|
260
|
-
} = config;
|
|
261
|
-
const confidenceThreshold = checkpointShared.normalizeConfidence(configThreshold, "confidenceThreshold");
|
|
262
|
-
const initPromise = initializeEdgeWasm(wasmModule);
|
|
263
|
-
return async function middleware(request) {
|
|
264
|
-
const path = request.nextUrl.pathname;
|
|
265
|
-
if (skipPaths.some((skip) => path.startsWith(skip))) {
|
|
266
|
-
return server.NextResponse.next();
|
|
267
|
-
}
|
|
268
|
-
try {
|
|
269
|
-
await initPromise;
|
|
270
|
-
const metadata = {
|
|
271
|
-
userAgent: request.headers.get("user-agent") || void 0,
|
|
272
|
-
ipAddress: request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || void 0,
|
|
273
|
-
headers: Object.fromEntries(request.headers.entries()),
|
|
274
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
275
|
-
};
|
|
276
|
-
const result = await detectWithWasm(metadata);
|
|
277
|
-
const decision = checkpointShared.evaluateEnforcement(
|
|
278
|
-
{ ...result, confidence: result.confidence * 100 },
|
|
279
|
-
{
|
|
280
|
-
confidenceThreshold: confidenceThreshold * 100,
|
|
281
|
-
defaultAction: blockOnHighConfidence ? "block" : "allow"
|
|
282
|
-
}
|
|
283
|
-
);
|
|
284
|
-
if (onAgentDetected && checkpointShared.shouldEnforce(result)) {
|
|
285
|
-
await onAgentDetected(result);
|
|
286
|
-
}
|
|
287
|
-
if (decision.action === "block") {
|
|
288
|
-
return server.NextResponse.json(
|
|
289
|
-
{
|
|
290
|
-
error: blockedResponse.message,
|
|
291
|
-
agent: result.agent,
|
|
292
|
-
confidence: checkpointShared.toPercent(result.confidence)
|
|
293
|
-
// Convert to 0-100 for display
|
|
294
|
-
},
|
|
295
|
-
{
|
|
296
|
-
status: blockedResponse.status || 403,
|
|
297
|
-
headers: blockedResponse.headers || {}
|
|
298
|
-
}
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
const response = server.NextResponse.next();
|
|
302
|
-
if (result.isAgent) {
|
|
303
|
-
response.headers.set("X-Agent-Detected", result.agent || "unknown");
|
|
304
|
-
response.headers.set("X-Agent-Confidence", String(checkpointShared.toPercent(result.confidence)));
|
|
305
|
-
response.headers.set("X-Agent-Verification", result.verificationMethod);
|
|
306
|
-
}
|
|
307
|
-
return response;
|
|
308
|
-
} catch (error) {
|
|
309
|
-
console.error("Edge WASM middleware error:", error);
|
|
310
|
-
return server.NextResponse.next();
|
|
311
|
-
}
|
|
312
|
-
};
|
|
19
|
+
function createEdgeWasmMiddleware(_config) {
|
|
20
|
+
warnEdgeWasmDeprecated();
|
|
21
|
+
throw new Error(MIGRATION_ERROR);
|
|
313
22
|
}
|
|
314
23
|
|
|
24
|
+
exports.__resetEdgeWasmWarningForTests = __resetEdgeWasmWarningForTests;
|
|
315
25
|
exports.createEdgeWasmMiddleware = createEdgeWasmMiddleware;
|
|
316
26
|
exports.initializeEdgeWasm = initializeEdgeWasm;
|
|
@@ -1,313 +1,22 @@
|
|
|
1
|
-
import { NextResponse } from 'next/server';
|
|
2
|
-
import { normalizeConfidence, evaluateEnforcement, shouldEnforce, toPercent } from '@kya-os/checkpoint-shared';
|
|
3
|
-
|
|
4
1
|
// src/edge-wasm-middleware.ts
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const idx = heap_next;
|
|
13
|
-
heap_next = heap[idx];
|
|
14
|
-
heap[idx] = obj;
|
|
15
|
-
return idx;
|
|
16
|
-
}
|
|
17
|
-
function getObject(idx) {
|
|
18
|
-
return heap[idx];
|
|
19
|
-
}
|
|
20
|
-
function dropObject(idx) {
|
|
21
|
-
if (idx < 132) return;
|
|
22
|
-
heap[idx] = heap_next;
|
|
23
|
-
heap_next = idx;
|
|
24
|
-
}
|
|
25
|
-
function takeObject(idx) {
|
|
26
|
-
const ret = getObject(idx);
|
|
27
|
-
dropObject(idx);
|
|
28
|
-
return ret;
|
|
29
|
-
}
|
|
30
|
-
var cachedTextDecoder = new TextDecoder("utf-8", { ignoreBOM: true, fatal: true });
|
|
31
|
-
var cachedTextEncoder = new TextEncoder();
|
|
32
|
-
var cachedUint8ArrayMemory0 = null;
|
|
33
|
-
function getUint8ArrayMemory0() {
|
|
34
|
-
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
35
|
-
cachedUint8ArrayMemory0 = new Uint8Array(wasmExports.memory.buffer);
|
|
36
|
-
}
|
|
37
|
-
return cachedUint8ArrayMemory0;
|
|
38
|
-
}
|
|
39
|
-
function getStringFromWasm0(ptr, len) {
|
|
40
|
-
ptr = ptr >>> 0;
|
|
41
|
-
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
42
|
-
}
|
|
43
|
-
var WASM_VECTOR_LEN = 0;
|
|
44
|
-
function passStringToWasm0(arg, malloc, realloc) {
|
|
45
|
-
if (realloc === void 0) {
|
|
46
|
-
const buf = cachedTextEncoder.encode(arg);
|
|
47
|
-
const ptr2 = malloc(buf.length, 1) >>> 0;
|
|
48
|
-
getUint8ArrayMemory0().subarray(ptr2, ptr2 + buf.length).set(buf);
|
|
49
|
-
WASM_VECTOR_LEN = buf.length;
|
|
50
|
-
return ptr2;
|
|
51
|
-
}
|
|
52
|
-
let len = arg.length;
|
|
53
|
-
let ptr = malloc(len, 1) >>> 0;
|
|
54
|
-
const mem = getUint8ArrayMemory0();
|
|
55
|
-
let offset = 0;
|
|
56
|
-
for (; offset < len; offset++) {
|
|
57
|
-
const code = arg.charCodeAt(offset);
|
|
58
|
-
if (code > 127) break;
|
|
59
|
-
mem[ptr + offset] = code;
|
|
60
|
-
}
|
|
61
|
-
if (offset !== len) {
|
|
62
|
-
if (offset !== 0) {
|
|
63
|
-
arg = arg.slice(offset);
|
|
64
|
-
}
|
|
65
|
-
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
66
|
-
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
67
|
-
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
68
|
-
offset += ret.written;
|
|
69
|
-
}
|
|
70
|
-
WASM_VECTOR_LEN = offset;
|
|
71
|
-
return ptr;
|
|
72
|
-
}
|
|
73
|
-
var cachedDataViewMemory0 = null;
|
|
74
|
-
function getDataViewMemory0() {
|
|
75
|
-
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer !== wasmExports.memory.buffer) {
|
|
76
|
-
cachedDataViewMemory0 = new DataView(wasmExports.memory.buffer);
|
|
77
|
-
}
|
|
78
|
-
return cachedDataViewMemory0;
|
|
2
|
+
var MIGRATION_ERROR = "`@kya-os/checkpoint-nextjs/edge-wasm-middleware` was deprecated in Phase-D.9a (legacy `agentshield-wasm` Rust crate retirement). Migrate to `withCheckpoint` from `@kya-os/checkpoint-nextjs/edge` \u2014 engine-backed via the Rust `kya-os-engine` crate (PDM-1 #2560), runs the full orchestrator including MCP-I envelope verification. See packages/checkpoint-nextjs/README.md for the canonical recipe.";
|
|
3
|
+
var _edgeWasmWarned = false;
|
|
4
|
+
function warnEdgeWasmDeprecated() {
|
|
5
|
+
if (_edgeWasmWarned) return;
|
|
6
|
+
_edgeWasmWarned = true;
|
|
7
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") return;
|
|
8
|
+
console.warn(`[Checkpoint] ${MIGRATION_ERROR}`);
|
|
79
9
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return wasmInitPromise;
|
|
83
|
-
}
|
|
84
|
-
wasmInitPromise = (async () => {
|
|
85
|
-
try {
|
|
86
|
-
const imports = {
|
|
87
|
-
"./agentshield_wasm_bg.js": {
|
|
88
|
-
__wbindgen_object_drop_ref: function(arg0) {
|
|
89
|
-
dropObject(arg0);
|
|
90
|
-
},
|
|
91
|
-
__wbindgen_string_new: function(arg0, arg1) {
|
|
92
|
-
const ret = getStringFromWasm0(arg0, arg1);
|
|
93
|
-
return addHeapObject(ret);
|
|
94
|
-
},
|
|
95
|
-
__wbindgen_string_get: function(arg0, arg1) {
|
|
96
|
-
const obj = getObject(arg1);
|
|
97
|
-
const ret = typeof obj === "string" ? obj : void 0;
|
|
98
|
-
const ptr1 = ret ? passStringToWasm0(
|
|
99
|
-
ret,
|
|
100
|
-
wasmExports.__wbindgen_malloc,
|
|
101
|
-
wasmExports.__wbindgen_realloc
|
|
102
|
-
) : 0;
|
|
103
|
-
const len1 = WASM_VECTOR_LEN;
|
|
104
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
105
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
106
|
-
},
|
|
107
|
-
__wbindgen_throw: function(arg0, arg1) {
|
|
108
|
-
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
109
|
-
}
|
|
110
|
-
},
|
|
111
|
-
wbg: {
|
|
112
|
-
__wbindgen_object_drop_ref: function(arg0) {
|
|
113
|
-
dropObject(arg0);
|
|
114
|
-
},
|
|
115
|
-
__wbindgen_string_new: function(arg0, arg1) {
|
|
116
|
-
const ret = getStringFromWasm0(arg0, arg1);
|
|
117
|
-
return addHeapObject(ret);
|
|
118
|
-
},
|
|
119
|
-
__wbindgen_string_get: function(arg0, arg1) {
|
|
120
|
-
const obj = getObject(arg1);
|
|
121
|
-
const ret = typeof obj === "string" ? obj : void 0;
|
|
122
|
-
const ptr1 = ret ? passStringToWasm0(
|
|
123
|
-
ret,
|
|
124
|
-
wasmExports.__wbindgen_malloc,
|
|
125
|
-
wasmExports.__wbindgen_realloc
|
|
126
|
-
) : 0;
|
|
127
|
-
const len1 = WASM_VECTOR_LEN;
|
|
128
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
129
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
130
|
-
},
|
|
131
|
-
__wbindgen_throw: function(arg0, arg1) {
|
|
132
|
-
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
const instance = await WebAssembly.instantiate(wasmModule, imports);
|
|
137
|
-
wasmExports = instance.exports;
|
|
138
|
-
if (wasmExports.__wbindgen_start) {
|
|
139
|
-
wasmExports.__wbindgen_start();
|
|
140
|
-
}
|
|
141
|
-
console.log(
|
|
142
|
-
"\u2705 AgentShield: WASM module loaded successfully in Edge Runtime (95-100% confidence enabled)"
|
|
143
|
-
);
|
|
144
|
-
} catch (error) {
|
|
145
|
-
console.error("\u274C AgentShield: Failed to initialize WASM in Edge Runtime:", error);
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
148
|
-
})();
|
|
149
|
-
return wasmInitPromise;
|
|
10
|
+
function __resetEdgeWasmWarningForTests() {
|
|
11
|
+
_edgeWasmWarned = false;
|
|
150
12
|
}
|
|
151
|
-
async function
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
155
|
-
try {
|
|
156
|
-
const userAgent = metadata.userAgent || "";
|
|
157
|
-
const ipAddress = metadata.ipAddress || "";
|
|
158
|
-
const headers = JSON.stringify(metadata.headers);
|
|
159
|
-
const timestamp = metadata.timestamp;
|
|
160
|
-
const url = metadata.url || "";
|
|
161
|
-
const method = metadata.method || "";
|
|
162
|
-
const clientFingerprint = metadata.clientFingerprint || "";
|
|
163
|
-
const ptr0 = passStringToWasm0(
|
|
164
|
-
userAgent,
|
|
165
|
-
wasmExports.__wbindgen_malloc,
|
|
166
|
-
wasmExports.__wbindgen_realloc
|
|
167
|
-
);
|
|
168
|
-
const len0 = WASM_VECTOR_LEN;
|
|
169
|
-
const ptr1 = passStringToWasm0(
|
|
170
|
-
ipAddress,
|
|
171
|
-
wasmExports.__wbindgen_malloc,
|
|
172
|
-
wasmExports.__wbindgen_realloc
|
|
173
|
-
);
|
|
174
|
-
const len1 = WASM_VECTOR_LEN;
|
|
175
|
-
const ptr2 = passStringToWasm0(
|
|
176
|
-
headers,
|
|
177
|
-
wasmExports.__wbindgen_malloc,
|
|
178
|
-
wasmExports.__wbindgen_realloc
|
|
179
|
-
);
|
|
180
|
-
const len2 = WASM_VECTOR_LEN;
|
|
181
|
-
const ptr3 = passStringToWasm0(
|
|
182
|
-
timestamp,
|
|
183
|
-
wasmExports.__wbindgen_malloc,
|
|
184
|
-
wasmExports.__wbindgen_realloc
|
|
185
|
-
);
|
|
186
|
-
const len3 = WASM_VECTOR_LEN;
|
|
187
|
-
const ptr4 = passStringToWasm0(
|
|
188
|
-
url,
|
|
189
|
-
wasmExports.__wbindgen_malloc,
|
|
190
|
-
wasmExports.__wbindgen_realloc
|
|
191
|
-
);
|
|
192
|
-
const len4 = WASM_VECTOR_LEN;
|
|
193
|
-
const ptr5 = passStringToWasm0(
|
|
194
|
-
method,
|
|
195
|
-
wasmExports.__wbindgen_malloc,
|
|
196
|
-
wasmExports.__wbindgen_realloc
|
|
197
|
-
);
|
|
198
|
-
const len5 = WASM_VECTOR_LEN;
|
|
199
|
-
const ptr6 = passStringToWasm0(
|
|
200
|
-
clientFingerprint,
|
|
201
|
-
wasmExports.__wbindgen_malloc,
|
|
202
|
-
wasmExports.__wbindgen_realloc
|
|
203
|
-
);
|
|
204
|
-
const len6 = WASM_VECTOR_LEN;
|
|
205
|
-
const metadataPtr = wasmExports.jsrequestmetadata_new(
|
|
206
|
-
ptr0,
|
|
207
|
-
len0,
|
|
208
|
-
ptr1,
|
|
209
|
-
len1,
|
|
210
|
-
ptr2,
|
|
211
|
-
len2,
|
|
212
|
-
ptr3,
|
|
213
|
-
len3,
|
|
214
|
-
ptr4,
|
|
215
|
-
len4,
|
|
216
|
-
ptr5,
|
|
217
|
-
len5,
|
|
218
|
-
ptr6,
|
|
219
|
-
len6
|
|
220
|
-
);
|
|
221
|
-
const resultPtr = wasmExports.detect_agent(metadataPtr);
|
|
222
|
-
const result = takeObject(resultPtr);
|
|
223
|
-
wasmExports.__wbg_jsrequestmetadata_free(metadataPtr, 0);
|
|
224
|
-
const parsedResult = typeof result === "string" ? JSON.parse(result) : result;
|
|
225
|
-
return {
|
|
226
|
-
isAgent: parsedResult.is_agent || false,
|
|
227
|
-
isAiCrawler: parsedResult.is_ai_crawler || false,
|
|
228
|
-
confidence: parsedResult.confidence || 0,
|
|
229
|
-
agent: parsedResult.agent,
|
|
230
|
-
verificationMethod: parsedResult.verification_method || "wasm",
|
|
231
|
-
riskLevel: parsedResult.risk_level || "low",
|
|
232
|
-
timestamp: parsedResult.timestamp || metadata.timestamp,
|
|
233
|
-
reasons: parsedResult.reasons || []
|
|
234
|
-
};
|
|
235
|
-
} catch (error) {
|
|
236
|
-
console.error("WASM detection failed:", error);
|
|
237
|
-
return {
|
|
238
|
-
isAgent: false,
|
|
239
|
-
confidence: 0,
|
|
240
|
-
verificationMethod: "pattern",
|
|
241
|
-
riskLevel: "low",
|
|
242
|
-
timestamp: metadata.timestamp
|
|
243
|
-
};
|
|
244
|
-
}
|
|
13
|
+
async function initializeEdgeWasm(_wasmModule) {
|
|
14
|
+
warnEdgeWasmDeprecated();
|
|
15
|
+
throw new Error(MIGRATION_ERROR);
|
|
245
16
|
}
|
|
246
|
-
function createEdgeWasmMiddleware(
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
onAgentDetected,
|
|
250
|
-
blockOnHighConfidence = false,
|
|
251
|
-
confidenceThreshold: configThreshold = 0.9,
|
|
252
|
-
skipPaths = [],
|
|
253
|
-
blockedResponse = {
|
|
254
|
-
status: 403,
|
|
255
|
-
message: "AI agent access restricted",
|
|
256
|
-
headers: { "Content-Type": "application/json" }
|
|
257
|
-
}
|
|
258
|
-
} = config;
|
|
259
|
-
const confidenceThreshold = normalizeConfidence(configThreshold, "confidenceThreshold");
|
|
260
|
-
const initPromise = initializeEdgeWasm(wasmModule);
|
|
261
|
-
return async function middleware(request) {
|
|
262
|
-
const path = request.nextUrl.pathname;
|
|
263
|
-
if (skipPaths.some((skip) => path.startsWith(skip))) {
|
|
264
|
-
return NextResponse.next();
|
|
265
|
-
}
|
|
266
|
-
try {
|
|
267
|
-
await initPromise;
|
|
268
|
-
const metadata = {
|
|
269
|
-
userAgent: request.headers.get("user-agent") || void 0,
|
|
270
|
-
ipAddress: request.headers.get("x-forwarded-for") || request.headers.get("x-real-ip") || void 0,
|
|
271
|
-
headers: Object.fromEntries(request.headers.entries()),
|
|
272
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
273
|
-
};
|
|
274
|
-
const result = await detectWithWasm(metadata);
|
|
275
|
-
const decision = evaluateEnforcement(
|
|
276
|
-
{ ...result, confidence: result.confidence * 100 },
|
|
277
|
-
{
|
|
278
|
-
confidenceThreshold: confidenceThreshold * 100,
|
|
279
|
-
defaultAction: blockOnHighConfidence ? "block" : "allow"
|
|
280
|
-
}
|
|
281
|
-
);
|
|
282
|
-
if (onAgentDetected && shouldEnforce(result)) {
|
|
283
|
-
await onAgentDetected(result);
|
|
284
|
-
}
|
|
285
|
-
if (decision.action === "block") {
|
|
286
|
-
return NextResponse.json(
|
|
287
|
-
{
|
|
288
|
-
error: blockedResponse.message,
|
|
289
|
-
agent: result.agent,
|
|
290
|
-
confidence: toPercent(result.confidence)
|
|
291
|
-
// Convert to 0-100 for display
|
|
292
|
-
},
|
|
293
|
-
{
|
|
294
|
-
status: blockedResponse.status || 403,
|
|
295
|
-
headers: blockedResponse.headers || {}
|
|
296
|
-
}
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
const response = NextResponse.next();
|
|
300
|
-
if (result.isAgent) {
|
|
301
|
-
response.headers.set("X-Agent-Detected", result.agent || "unknown");
|
|
302
|
-
response.headers.set("X-Agent-Confidence", String(toPercent(result.confidence)));
|
|
303
|
-
response.headers.set("X-Agent-Verification", result.verificationMethod);
|
|
304
|
-
}
|
|
305
|
-
return response;
|
|
306
|
-
} catch (error) {
|
|
307
|
-
console.error("Edge WASM middleware error:", error);
|
|
308
|
-
return NextResponse.next();
|
|
309
|
-
}
|
|
310
|
-
};
|
|
17
|
+
function createEdgeWasmMiddleware(_config) {
|
|
18
|
+
warnEdgeWasmDeprecated();
|
|
19
|
+
throw new Error(MIGRATION_ERROR);
|
|
311
20
|
}
|
|
312
21
|
|
|
313
|
-
export { createEdgeWasmMiddleware, initializeEdgeWasm };
|
|
22
|
+
export { __resetEdgeWasmWarningForTests, createEdgeWasmMiddleware, initializeEdgeWasm };
|
package/dist/index.js
CHANGED
|
@@ -119,7 +119,8 @@ function buildVerifyOpts(config) {
|
|
|
119
119
|
enforcementMode: config.enforcementMode ?? "enforce",
|
|
120
120
|
reputationBaseline: config.reputationBaseline,
|
|
121
121
|
argusUrl: config.argusUrl,
|
|
122
|
-
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
|
|
122
|
+
legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false,
|
|
123
|
+
engineConfig: config.engineConfig
|
|
123
124
|
};
|
|
124
125
|
}
|
|
125
126
|
async function dispatchOnResult(config, result, req) {
|
|
@@ -698,7 +699,9 @@ var EdgeSessionTracker = class {
|
|
|
698
699
|
*/
|
|
699
700
|
async track(_request, response, result) {
|
|
700
701
|
try {
|
|
701
|
-
|
|
702
|
+
const detectedName = result.detectedAgent?.name;
|
|
703
|
+
const isEnforceable = result.isAgent || result.isAiCrawler || checkpointShared.isKnownAiCrawler(detectedName);
|
|
704
|
+
if (!this.config.enabled || !isEnforceable) {
|
|
702
705
|
return response;
|
|
703
706
|
}
|
|
704
707
|
const sessionData = {
|