@kya-os/agentshield-nextjs 0.1.25 → 0.1.28
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/README.md +24 -36
- package/dist/create-middleware.d.mts +1 -1
- package/dist/create-middleware.d.ts +1 -1
- package/dist/create-middleware.js +171 -2
- package/dist/create-middleware.js.map +1 -1
- package/dist/create-middleware.mjs +171 -2
- package/dist/create-middleware.mjs.map +1 -1
- package/dist/edge-detector-wrapper.js +8 -1
- package/dist/edge-detector-wrapper.js.map +1 -1
- package/dist/edge-detector-wrapper.mjs +8 -1
- package/dist/edge-detector-wrapper.mjs.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +226 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +225 -3
- package/dist/index.mjs.map +1 -1
- package/dist/middleware.d.mts +1 -1
- package/dist/middleware.d.ts +1 -1
- package/dist/middleware.js +171 -2
- package/dist/middleware.js.map +1 -1
- package/dist/middleware.mjs +171 -2
- package/dist/middleware.mjs.map +1 -1
- package/dist/session-tracker.d.mts +55 -0
- package/dist/session-tracker.d.ts +55 -0
- package/dist/session-tracker.js +176 -0
- package/dist/session-tracker.js.map +1 -0
- package/dist/session-tracker.mjs +173 -0
- package/dist/session-tracker.mjs.map +1 -0
- package/dist/{types-Hsgc8Gry.d.mts → types-BJTEUa4T.d.mts} +28 -0
- package/dist/{types-Hsgc8Gry.d.ts → types-BJTEUa4T.d.ts} +28 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -28,12 +28,15 @@ import { agentShield } from '@kya-os/agentshield-nextjs';
|
|
|
28
28
|
export default agentShield({
|
|
29
29
|
onAgentDetected: 'block',
|
|
30
30
|
confidenceThreshold: 0.8,
|
|
31
|
+
|
|
32
|
+
// NEW: Session tracking (v0.1.27+)
|
|
33
|
+
sessionTracking: {
|
|
34
|
+
enabled: true, // Track continued sessions from AI agents
|
|
35
|
+
},
|
|
31
36
|
});
|
|
32
37
|
|
|
33
38
|
export const config = {
|
|
34
|
-
matcher: [
|
|
35
|
-
'/((?!api|_next/static|_next/image|favicon.ico).*)',
|
|
36
|
-
],
|
|
39
|
+
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
|
|
37
40
|
};
|
|
38
41
|
```
|
|
39
42
|
|
|
@@ -61,7 +64,7 @@ export default function SecurityMonitor() {
|
|
|
61
64
|
<button onClick={handleCheck} disabled={isDetecting}>
|
|
62
65
|
{isDetecting ? 'Checking...' : 'Check for Agents'}
|
|
63
66
|
</button>
|
|
64
|
-
|
|
67
|
+
|
|
65
68
|
{lastResult && (
|
|
66
69
|
<div>
|
|
67
70
|
<p>Is Agent: {lastResult.isAgent ? 'Yes' : 'No'}</p>
|
|
@@ -83,13 +86,13 @@ export default agentShield({
|
|
|
83
86
|
confidenceThreshold: 0.7,
|
|
84
87
|
enablePatternMatching: true,
|
|
85
88
|
enableBehaviorAnalysis: true,
|
|
86
|
-
|
|
89
|
+
|
|
87
90
|
// Action when agent is detected
|
|
88
91
|
onAgentDetected: 'block', // 'block' | 'redirect' | 'rewrite' | 'allow' | 'log'
|
|
89
|
-
|
|
92
|
+
|
|
90
93
|
// Skip detection for paths
|
|
91
94
|
skipPaths: ['/api/webhooks', /^\/admin/],
|
|
92
|
-
|
|
95
|
+
|
|
93
96
|
// Custom responses
|
|
94
97
|
blockedResponse: {
|
|
95
98
|
status: 403,
|
|
@@ -97,7 +100,7 @@ export default agentShield({
|
|
|
97
100
|
},
|
|
98
101
|
redirectUrl: '/blocked',
|
|
99
102
|
rewriteUrl: '/blocked',
|
|
100
|
-
|
|
103
|
+
|
|
101
104
|
// Custom handler
|
|
102
105
|
onDetection: async (request, result) => {
|
|
103
106
|
console.log('Agent detected:', result);
|
|
@@ -151,15 +154,10 @@ export default agentShield({
|
|
|
151
154
|
onDetection: async (request, result) => {
|
|
152
155
|
if (result.confidence > 0.9) {
|
|
153
156
|
// High confidence - block
|
|
154
|
-
return NextResponse.json(
|
|
155
|
-
{ error: 'Blocked' },
|
|
156
|
-
{ status: 403 }
|
|
157
|
-
);
|
|
157
|
+
return NextResponse.json({ error: 'Blocked' }, { status: 403 });
|
|
158
158
|
} else if (result.confidence > 0.5) {
|
|
159
159
|
// Medium confidence - redirect to captcha
|
|
160
|
-
return NextResponse.redirect(
|
|
161
|
-
new URL('/verify', request.url)
|
|
162
|
-
);
|
|
160
|
+
return NextResponse.redirect(new URL('/verify', request.url));
|
|
163
161
|
}
|
|
164
162
|
// Low confidence - continue
|
|
165
163
|
},
|
|
@@ -188,9 +186,7 @@ function SecurityComponent() {
|
|
|
188
186
|
return (
|
|
189
187
|
<div>
|
|
190
188
|
{lastResult?.isAgent && (
|
|
191
|
-
<div className=
|
|
192
|
-
Agent detected with {lastResult.confidence} confidence
|
|
193
|
-
</div>
|
|
189
|
+
<div className='alert'>Agent detected with {lastResult.confidence} confidence</div>
|
|
194
190
|
)}
|
|
195
191
|
</div>
|
|
196
192
|
);
|
|
@@ -205,12 +201,10 @@ Monitor and track detection events:
|
|
|
205
201
|
import { useDetectionMonitor } from '@kya-os/agentshield-nextjs';
|
|
206
202
|
|
|
207
203
|
function AnalyticsDashboard() {
|
|
208
|
-
const { detectionHistory, getStats, clearHistory } = useDetectionMonitor(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
);
|
|
204
|
+
const { detectionHistory, getStats, clearHistory } = useDetectionMonitor(context => {
|
|
205
|
+
// Handle each detection
|
|
206
|
+
console.log('Detection event:', context);
|
|
207
|
+
});
|
|
214
208
|
|
|
215
209
|
const stats = getStats();
|
|
216
210
|
|
|
@@ -221,7 +215,7 @@ function AnalyticsDashboard() {
|
|
|
221
215
|
<p>Agents Detected: {stats.detected}</p>
|
|
222
216
|
<p>Detection Rate: {(stats.detectionRate * 100).toFixed(1)}%</p>
|
|
223
217
|
<p>Average Confidence: {(stats.avgConfidence * 100).toFixed(1)}%</p>
|
|
224
|
-
|
|
218
|
+
|
|
225
219
|
<button onClick={clearHistory}>Clear History</button>
|
|
226
220
|
</div>
|
|
227
221
|
);
|
|
@@ -248,10 +242,7 @@ export async function GET(request) {
|
|
|
248
242
|
const result = await detector.analyze(context);
|
|
249
243
|
|
|
250
244
|
if (result.isAgent && result.confidence > 0.7) {
|
|
251
|
-
return NextResponse.json(
|
|
252
|
-
{ error: 'Automated access detected' },
|
|
253
|
-
{ status: 403 }
|
|
254
|
-
);
|
|
245
|
+
return NextResponse.json({ error: 'Automated access detected' }, { status: 403 });
|
|
255
246
|
}
|
|
256
247
|
|
|
257
248
|
return NextResponse.json({ data: 'Protected content' });
|
|
@@ -370,18 +361,15 @@ export const config = {
|
|
|
370
361
|
export default agentShield({
|
|
371
362
|
onDetection: async (request, result) => {
|
|
372
363
|
const userAgent = request.headers.get('user-agent') || '';
|
|
373
|
-
|
|
364
|
+
|
|
374
365
|
// Allow known search engines
|
|
375
366
|
if (/googlebot|bingbot|slurp/i.test(userAgent)) {
|
|
376
367
|
return; // Continue
|
|
377
368
|
}
|
|
378
|
-
|
|
369
|
+
|
|
379
370
|
// Block other agents
|
|
380
371
|
if (result.isAgent && result.confidence > 0.5) {
|
|
381
|
-
return NextResponse.json(
|
|
382
|
-
{ error: 'Bot access restricted' },
|
|
383
|
-
{ status: 403 }
|
|
384
|
-
);
|
|
372
|
+
return NextResponse.json({ error: 'Bot access restricted' }, { status: 403 });
|
|
385
373
|
}
|
|
386
374
|
},
|
|
387
375
|
});
|
|
@@ -389,4 +377,4 @@ export default agentShield({
|
|
|
389
377
|
|
|
390
378
|
## License
|
|
391
379
|
|
|
392
|
-
MIT OR Apache-2.0
|
|
380
|
+
MIT OR Apache-2.0
|
|
@@ -47,7 +47,14 @@ var EdgeAgentDetector = class {
|
|
|
47
47
|
if (userAgent) {
|
|
48
48
|
for (const { pattern, type, name } of AI_AGENT_PATTERNS) {
|
|
49
49
|
if (pattern.test(userAgent)) {
|
|
50
|
-
|
|
50
|
+
const highConfidenceAgents = [
|
|
51
|
+
"chatgpt",
|
|
52
|
+
"claude",
|
|
53
|
+
"perplexity",
|
|
54
|
+
"anthropic"
|
|
55
|
+
];
|
|
56
|
+
const patternConfidence = highConfidenceAgents.includes(type) ? 0.85 : 0.5;
|
|
57
|
+
confidence = Math.max(confidence, patternConfidence);
|
|
51
58
|
reasons.push(`known_pattern:${type}`);
|
|
52
59
|
if (!detectedAgent) {
|
|
53
60
|
detectedAgent = { type, name };
|
|
@@ -124,9 +131,130 @@ var EdgeAgentDetectorWrapper = class {
|
|
|
124
131
|
}
|
|
125
132
|
};
|
|
126
133
|
|
|
134
|
+
// src/session-tracker.ts
|
|
135
|
+
var EdgeSessionTracker = class {
|
|
136
|
+
config;
|
|
137
|
+
constructor(config) {
|
|
138
|
+
this.config = {
|
|
139
|
+
enabled: config.enabled,
|
|
140
|
+
cookieName: config.cookieName || "__agentshield_session",
|
|
141
|
+
cookieMaxAge: config.cookieMaxAge || 3600,
|
|
142
|
+
// 1 hour default
|
|
143
|
+
encryptionKey: config.encryptionKey || process.env.AGENTSHIELD_SECRET || "agentshield-default-key"
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Track a new AI agent session
|
|
148
|
+
*/
|
|
149
|
+
async track(_request, response, result) {
|
|
150
|
+
try {
|
|
151
|
+
if (!this.config.enabled || !result.isAgent) {
|
|
152
|
+
return response;
|
|
153
|
+
}
|
|
154
|
+
const sessionData = {
|
|
155
|
+
id: crypto.randomUUID(),
|
|
156
|
+
agent: result.detectedAgent?.name || "unknown",
|
|
157
|
+
confidence: result.confidence,
|
|
158
|
+
detectedAt: Date.now(),
|
|
159
|
+
expires: Date.now() + this.config.cookieMaxAge * 1e3
|
|
160
|
+
};
|
|
161
|
+
const encrypted = await this.encrypt(JSON.stringify(sessionData));
|
|
162
|
+
response.cookies.set(this.config.cookieName, encrypted, {
|
|
163
|
+
httpOnly: true,
|
|
164
|
+
secure: process.env.NODE_ENV === "production",
|
|
165
|
+
sameSite: "lax",
|
|
166
|
+
maxAge: this.config.cookieMaxAge,
|
|
167
|
+
path: "/"
|
|
168
|
+
});
|
|
169
|
+
return response;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (process.env.DEBUG_AGENTSHIELD) {
|
|
172
|
+
console.warn("AgentShield: Failed to track session:", error);
|
|
173
|
+
}
|
|
174
|
+
return response;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Check for existing AI agent session
|
|
179
|
+
*/
|
|
180
|
+
async check(request) {
|
|
181
|
+
try {
|
|
182
|
+
if (!this.config.enabled) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
const cookie = request.cookies.get(this.config.cookieName);
|
|
186
|
+
if (!cookie?.value) {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const decrypted = await this.decrypt(cookie.value);
|
|
190
|
+
const session = JSON.parse(decrypted);
|
|
191
|
+
if (session.expires < Date.now()) {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
return session;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
if (process.env.DEBUG_AGENTSHIELD) {
|
|
197
|
+
console.warn("AgentShield: Failed to check session:", error);
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Clear an existing session
|
|
204
|
+
*/
|
|
205
|
+
clear(response) {
|
|
206
|
+
try {
|
|
207
|
+
response.cookies.delete(this.config.cookieName);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
if (process.env.DEBUG_AGENTSHIELD) {
|
|
210
|
+
console.warn("AgentShield: Failed to clear session:", error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return response;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Simple encryption using Web Crypto API (Edge-compatible)
|
|
217
|
+
*/
|
|
218
|
+
async encrypt(data) {
|
|
219
|
+
try {
|
|
220
|
+
const key = this.config.encryptionKey;
|
|
221
|
+
const encoded = new TextEncoder().encode(data);
|
|
222
|
+
const obfuscated = new Uint8Array(encoded.length);
|
|
223
|
+
for (let i = 0; i < encoded.length; i++) {
|
|
224
|
+
obfuscated[i] = (encoded[i] || 0) ^ key.charCodeAt(i % key.length);
|
|
225
|
+
}
|
|
226
|
+
return btoa(
|
|
227
|
+
Array.from(obfuscated, (byte) => String.fromCharCode(byte)).join("")
|
|
228
|
+
);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
return btoa(data);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Simple decryption (Edge-compatible)
|
|
235
|
+
*/
|
|
236
|
+
async decrypt(data) {
|
|
237
|
+
try {
|
|
238
|
+
const key = this.config.encryptionKey;
|
|
239
|
+
const decoded = Uint8Array.from(atob(data), (c) => c.charCodeAt(0));
|
|
240
|
+
const deobfuscated = new Uint8Array(decoded.length);
|
|
241
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
242
|
+
deobfuscated[i] = (decoded[i] || 0) ^ key.charCodeAt(i % key.length);
|
|
243
|
+
}
|
|
244
|
+
return new TextDecoder().decode(deobfuscated);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
return atob(data);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
127
251
|
// src/middleware.ts
|
|
128
252
|
function createAgentShieldMiddleware(config = {}) {
|
|
129
253
|
const detector = new EdgeAgentDetectorWrapper(config);
|
|
254
|
+
const sessionTracker = config.sessionTracking?.enabled || config.enableWasm ? new EdgeSessionTracker({
|
|
255
|
+
enabled: true,
|
|
256
|
+
...config.sessionTracking
|
|
257
|
+
}) : null;
|
|
130
258
|
if (config.events) {
|
|
131
259
|
Object.entries(config.events).forEach(([event, handler]) => {
|
|
132
260
|
detector.on(event, handler);
|
|
@@ -156,6 +284,39 @@ function createAgentShieldMiddleware(config = {}) {
|
|
|
156
284
|
request.agentShield = { skipped: true };
|
|
157
285
|
return server.NextResponse.next();
|
|
158
286
|
}
|
|
287
|
+
const existingSession = sessionTracker ? await sessionTracker.check(request) : null;
|
|
288
|
+
if (existingSession) {
|
|
289
|
+
const response2 = server.NextResponse.next();
|
|
290
|
+
response2.headers.set("x-agentshield-detected", "true");
|
|
291
|
+
response2.headers.set("x-agentshield-agent", existingSession.agent);
|
|
292
|
+
response2.headers.set(
|
|
293
|
+
"x-agentshield-confidence",
|
|
294
|
+
existingSession.confidence.toString()
|
|
295
|
+
);
|
|
296
|
+
response2.headers.set("x-agentshield-session", "continued");
|
|
297
|
+
response2.headers.set("x-agentshield-session-id", existingSession.id);
|
|
298
|
+
request.agentShield = {
|
|
299
|
+
result: {
|
|
300
|
+
isAgent: true,
|
|
301
|
+
confidence: existingSession.confidence,
|
|
302
|
+
detectedAgent: { name: existingSession.agent },
|
|
303
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
304
|
+
verificationMethod: "session"
|
|
305
|
+
},
|
|
306
|
+
session: existingSession,
|
|
307
|
+
skipped: false
|
|
308
|
+
};
|
|
309
|
+
const context2 = {
|
|
310
|
+
userAgent: request.headers.get("user-agent") || "",
|
|
311
|
+
ipAddress: (request.ip ?? request.headers.get("x-forwarded-for")) || "",
|
|
312
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
313
|
+
url: request.url,
|
|
314
|
+
method: request.method,
|
|
315
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
316
|
+
};
|
|
317
|
+
detector.emit("agent.session.continued", existingSession, context2);
|
|
318
|
+
return response2;
|
|
319
|
+
}
|
|
159
320
|
const userAgent = request.headers.get("user-agent");
|
|
160
321
|
const ipAddress = request.ip ?? request.headers.get("x-forwarded-for");
|
|
161
322
|
const context = {
|
|
@@ -220,12 +381,20 @@ function createAgentShieldMiddleware(config = {}) {
|
|
|
220
381
|
result,
|
|
221
382
|
skipped: false
|
|
222
383
|
};
|
|
223
|
-
|
|
384
|
+
let response = server.NextResponse.next();
|
|
224
385
|
response.headers.set("x-agentshield-detected", result.isAgent.toString());
|
|
225
386
|
response.headers.set(
|
|
226
387
|
"x-agentshield-confidence",
|
|
227
388
|
result.confidence.toString()
|
|
228
389
|
);
|
|
390
|
+
if (result.detectedAgent?.name) {
|
|
391
|
+
response.headers.set("x-agentshield-agent", result.detectedAgent.name);
|
|
392
|
+
}
|
|
393
|
+
if (sessionTracker && result.isAgent && result.confidence >= (config.confidenceThreshold ?? 0.7)) {
|
|
394
|
+
response = await sessionTracker.track(request, response, result);
|
|
395
|
+
response.headers.set("x-agentshield-session", "new");
|
|
396
|
+
detector.emit("agent.session.started", result, context);
|
|
397
|
+
}
|
|
229
398
|
return response;
|
|
230
399
|
} catch (error) {
|
|
231
400
|
console.error("AgentShield middleware error:", error);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/edge-detector-wrapper.ts","../src/middleware.ts","../src/create-middleware.ts"],"names":["NextResponse","response","createAgentShieldMiddleware"],"mappings":";;;;;;;AAiCA,IAAM,iBAAA,GAAoB;AAAA,EACxB,EAAE,OAAA,EAAS,eAAA,EAAiB,IAAA,EAAM,SAAA,EAAW,MAAM,SAAA,EAAU;AAAA,EAC7D,EAAE,OAAA,EAAS,aAAA,EAAe,IAAA,EAAM,QAAA,EAAU,MAAM,QAAA,EAAS;AAAA,EACzD,EAAE,OAAA,EAAS,gBAAA,EAAkB,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAAA,EACpE,EAAE,OAAA,EAAS,kBAAA,EAAoB,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAAA,EACtE,EAAE,OAAA,EAAS,gBAAA,EAAkB,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAAA,EACpE,EAAE,OAAA,EAAS,aAAA,EAAe,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAAA;AAAA,EACjE,EAAE,OAAA,EAAS,UAAA,EAAY,IAAA,EAAM,MAAA,EA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A,IACT,SAAS,KAAA,EAAO;AACd,MAAA,OAAA,CAAQ,KAAA,CAAM,iCAAiC,KAAK,CAAA;AACpD,MAAA,OAAOA,oBAAa,IAAA,EAAK;AAAA,IAC3B;AAAA,EACF,CAAA;AACF;;;ACpJA,IAAI,kBAAA,GAA0B,IAAA;AAC9B,IAAI,cAAA,GAAiB,KAAA;AACrB,IAAI,WAAA,GAAmC,IAAA;AAMhC,SAASE,6BAA4B,MAAA,EAAgC;AAC1E,EAAA,OAAO,eAAe,sBACpB,OAAA,EACuB;AAEvB,IAAA,IAAI,CAAC,kBAAA,EAAoB;AACvB,MAAA,IAAI,CAAC,cAAA,EAAgB;AACnB,QAAA,cAAA,GAAiB,IAAA;AACjB,QAAA,WAAA,GAAA,CAAe,YAAY;AAIzB,UAAA,kBAAA,GAAqB,4BAAqB,MAAM,CAAA;AAChD,UAAA,OAAO,kBAAA;AAAA,QACT,CAAA,GAAG;AAAA,MACL;AAGA,MAAA,IAAI,WAAA,EAAa;AACf,QAAA,kBAAA,GAAqB,MAAM,WAAA;AAAA,MAC7B;AAAA,IACF;AAGA,IAAA,OAAO,kBAAA,GACH,kBAAA,CAAmB,OAAO,CAAA,GAC1BF,oBAAa,IAAA,EAAK;AAAA,EACxB,CAAA;AACF","file":"create-middleware.js","sourcesContent":["/**\n * Wrapper for EdgeAgentDetector to match AgentDetector interface\n * This allows the middleware to work with EdgeAgentDetector in Edge Runtime\n *\n * This is a self-contained implementation to avoid import resolution issues\n */\n\ntype DetectionInput = {\n userAgent?: string;\n ip?: string;\n ipAddress?: string;\n headers?: Record<string, string>;\n url?: string;\n method?: string;\n timestamp?: Date;\n};\n\ntype DetectionResult = {\n isAgent: boolean;\n confidence: number;\n detectedAgent?: {\n type: string;\n name: string;\n };\n reasons: string[];\n verificationMethod?: string;\n forgeabilityRisk?: 'low' | 'medium' | 'high';\n timestamp: Date;\n};\n\ntype EventHandler = (...args: any[]) => void;\n\n// Known AI agent patterns\nconst AI_AGENT_PATTERNS = [\n { pattern: /chatgpt-user/i, type: 'chatgpt', name: 'ChatGPT' },\n { pattern: /claude-web/i, type: 'claude', name: 'Claude' },\n { pattern: /perplexitybot/i, type: 'perplexity', name: 'Perplexity' },\n { pattern: /perplexity-user/i, type: 'perplexity', name: 'Perplexity' },\n { pattern: /perplexity-ai/i, type: 'perplexity', name: 'Perplexity' },\n { pattern: /perplexity/i, type: 'perplexity', name: 'Perplexity' }, // Fallback\n { pattern: /bingbot/i, type: 'bing', name: 'Bing AI' },\n { pattern: /anthropic-ai/i, type: 'anthropic', name: 'Anthropic' },\n];\n\n// Known cloud provider IP ranges (simplified)\nconst CLOUD_PROVIDERS = {\n aws: ['54.', '52.', '35.', '18.', '3.'],\n gcp: ['35.', '34.', '104.', '107.', '108.'],\n azure: ['13.', '20.', '40.', '52.', '104.'],\n};\n\n/**\n * Self-contained EdgeAgentDetector implementation\n */\nclass EdgeAgentDetector {\n async analyze(input: DetectionInput): Promise<DetectionResult> {\n const reasons: string[] = [];\n let detectedAgent: { type: string; name: string } | undefined;\n let verificationMethod: string | undefined;\n let confidence = 0;\n\n const headers = input.headers || {};\n const normalizedHeaders: Record<string, string> = {};\n\n // Normalize header names to lowercase\n for (const [key, value] of Object.entries(headers)) {\n normalizedHeaders[key.toLowerCase()] = value;\n }\n\n // Check for HTTP Message Signatures (highest confidence)\n const signaturePresent = !!(\n normalizedHeaders['signature'] || normalizedHeaders['signature-input']\n );\n const signatureAgent = normalizedHeaders['signature-agent'];\n\n if (signatureAgent?.includes('chatgpt.com')) {\n confidence = 0.85;\n reasons.push('signature_agent:chatgpt');\n detectedAgent = { type: 'chatgpt', name: 'ChatGPT' };\n verificationMethod = 'signature';\n } else if (signaturePresent) {\n confidence = Math.max(confidence, 0.4);\n reasons.push('signature_present');\n }\n\n // Check User-Agent patterns\n const userAgent = input.userAgent || input.headers?.['user-agent'] || '';\n if (userAgent) {\n for (const { pattern, type, name } of AI_AGENT_PATTERNS) {\n if (pattern.test(userAgent)) {\n confidence = Math.max(confidence, 0.5);\n reasons.push(`known_pattern:${type}`);\n if (!detectedAgent) {\n detectedAgent = { type, name };\n verificationMethod = 'pattern';\n }\n break;\n }\n }\n }\n\n // Check AI-specific headers\n const aiHeaders = [\n 'openai-conversation-id',\n 'openai-ephemeral-user-id',\n 'anthropic-client-id',\n 'x-goog-api-client',\n 'x-ms-copilot-id',\n ];\n\n const foundAiHeaders = aiHeaders.filter(\n header => normalizedHeaders[header]\n );\n if (foundAiHeaders.length > 0) {\n confidence = Math.max(confidence, 0.6);\n reasons.push(`ai_headers:${foundAiHeaders.length}`);\n }\n\n // Check cloud provider IPs\n const ip = input.ip || input.ipAddress;\n if (\n ip &&\n !normalizedHeaders['x-forwarded-for'] &&\n !normalizedHeaders['x-real-ip']\n ) {\n for (const [provider, prefixes] of Object.entries(CLOUD_PROVIDERS)) {\n if (prefixes.some(prefix => ip.startsWith(prefix))) {\n confidence = Math.max(confidence, 0.4);\n reasons.push(`cloud_provider:${provider}`);\n break;\n }\n }\n }\n\n // Boost confidence for combinations\n if (reasons.length > 2) {\n confidence = Math.min(confidence * 1.2, 0.95);\n }\n\n return {\n isAgent: confidence > 0.3,\n confidence,\n ...(detectedAgent && { detectedAgent }),\n reasons,\n ...(verificationMethod && { verificationMethod }),\n forgeabilityRisk: confidence > 0.8 ? 'medium' : 'high',\n timestamp: new Date(),\n };\n }\n}\n\n/**\n * Wrapper that provides event emitter functionality\n */\nexport class EdgeAgentDetectorWrapper {\n private detector: EdgeAgentDetector;\n private events: Map<string, EventHandler[]> = new Map();\n\n constructor(_config?: any) {\n // Config is accepted but not used by EdgeAgentDetector\n this.detector = new EdgeAgentDetector();\n }\n\n async analyze(input: DetectionInput): Promise<DetectionResult> {\n const result = await this.detector.analyze(input);\n\n // Emit events if there are listeners\n if (result.isAgent && this.events.has('agent.detected')) {\n const handlers = this.events.get('agent.detected') || [];\n handlers.forEach(handler => handler(result, input));\n }\n\n return result;\n }\n\n on(event: string, handler: EventHandler): void {\n if (!this.events.has(event)) {\n this.events.set(event, []);\n }\n this.events.get(event)!.push(handler);\n }\n\n emit(event: string, ...args: any[]): void {\n const handlers = this.events.get(event) || [];\n handlers.forEach(handler => handler(...args));\n }\n\n async init(): Promise<void> {\n // EdgeAgentDetector doesn't need initialization\n return;\n }\n}\n","/**\n * Next.js middleware for AgentShield\n * \n * Uses edge-safe imports to avoid WASM in Edge Runtime\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\n// Use wrapper for EdgeAgentDetector to match expected interface\nimport { EdgeAgentDetectorWrapper as AgentDetector } from './edge-detector-wrapper';\nimport type { NextJSMiddlewareConfig } from './types';\n\n/**\n * Create AgentShield middleware for Next.js\n */\nexport function createAgentShieldMiddleware(\n config: Partial<NextJSMiddlewareConfig> = {}\n) {\n const detector = new AgentDetector(config);\n \n // Wire up event handlers if provided\n if (config.events) {\n Object.entries(config.events).forEach(([event, handler]) => {\n detector.on(event as any, handler as any);\n });\n }\n\n const {\n onAgentDetected = 'log',\n onDetection,\n skipPaths = [],\n blockedResponse = {\n status: 403,\n message: 'Access denied: Automated agent detected',\n headers: { 'Content-Type': 'application/json' },\n },\n redirectUrl = '/blocked',\n rewriteUrl = '/blocked',\n } = config;\n\n return async (request: NextRequest): Promise<NextResponse> => {\n try {\n // Check if path should be skipped\n const shouldSkip = skipPaths.some(pattern => {\n if (typeof pattern === 'string') {\n return request.nextUrl.pathname.startsWith(pattern);\n }\n return pattern.test(request.nextUrl.pathname);\n });\n\n if (shouldSkip) {\n // Mark as skipped in request\n (request as any).agentShield = { skipped: true };\n return NextResponse.next();\n }\n\n // Prepare request context - ensure all optional fields are properly typed\n const userAgent = request.headers.get('user-agent');\n const ipAddress = request.ip ?? request.headers.get('x-forwarded-for');\n \n const context = {\n ...(userAgent && { userAgent }),\n ...(ipAddress && { ipAddress }),\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n // Analyze request\n const result = await detector.analyze(context);\n\n // Handle detection result\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n // Call custom detection handler if provided\n if (onDetection) {\n const customResponse = await onDetection(request, result);\n if (customResponse) {\n return customResponse;\n }\n }\n\n // Handle based on configuration\n switch (onAgentDetected) {\n case 'block': {\n const response = NextResponse.json(\n {\n error: blockedResponse.message,\n detected: true,\n confidence: result.confidence,\n timestamp: result.timestamp,\n },\n { status: blockedResponse.status }\n );\n\n if (blockedResponse.headers) {\n Object.entries(blockedResponse.headers).forEach(\n ([key, value]) => {\n response.headers.set(key, value);\n }\n );\n }\n \n // Emit blocked event\n detector.emit('agent.blocked', result, context);\n\n return response;\n }\n\n case 'redirect':\n return NextResponse.redirect(new URL(redirectUrl, request.url));\n\n case 'rewrite':\n return NextResponse.rewrite(new URL(rewriteUrl, request.url));\n\n case 'log':\n console.warn('AgentShield: Agent detected', {\n ipAddress: context.ipAddress,\n userAgent: context.userAgent,\n confidence: result.confidence,\n reasons: result.reasons,\n pathname: request.nextUrl.pathname,\n });\n break;\n\n case 'allow':\n default:\n // Emit allowed event for high-confidence agents\n if (result.isAgent && result.confidence >= (config.confidenceThreshold ?? 0.7)) {\n detector.emit('agent.allowed', result, context);\n }\n // Continue processing\n break;\n }\n }\n\n // Add detection result to request for API routes\n (request as any).agentShield = {\n result,\n skipped: false,\n };\n\n // Add detection result to response headers for debugging\n const response = NextResponse.next();\n response.headers.set('x-agentshield-detected', result.isAgent.toString());\n response.headers.set(\n 'x-agentshield-confidence',\n result.confidence.toString()\n );\n\n return response;\n } catch (error) {\n console.error('AgentShield middleware error:', error);\n return NextResponse.next(); // Continue on error\n }\n };\n}\n\n/**\n * Convenience function for basic setup\n */\nexport function agentShield(config: Partial<NextJSMiddlewareConfig> = {}) {\n return createAgentShieldMiddleware(config);\n}\n","/**\n * Enhanced middleware creator for Edge Runtime\n * Uses EdgeAgentDetector which doesn't require WASM\n */\n\nimport { NextResponse } from 'next/server';\nimport type { NextRequest } from 'next/server';\nimport { createAgentShieldMiddleware as createBaseMiddleware } from './middleware';\nimport type { NextJSMiddlewareConfig } from './types';\n\nlet middlewareInstance: any = null;\nlet isInitializing = false;\nlet initPromise: Promise<any> | null = null;\n\n/**\n * Create an AgentShield middleware with automatic WASM initialization\n * This version handles initialization internally to avoid top-level await\n */\nexport function createAgentShieldMiddleware(config: NextJSMiddlewareConfig) {\n return async function agentShieldMiddleware(\n request: NextRequest\n ): Promise<NextResponse> {\n // Initialize middleware on first request (no WASM needed in Edge Runtime)\n if (!middlewareInstance) {\n if (!isInitializing) {\n isInitializing = true;\n initPromise = (async () => {\n // No WASM initialization needed - EdgeAgentDetector works without it\n\n // Create the actual middleware instance\n middlewareInstance = createBaseMiddleware(config);\n return middlewareInstance;\n })();\n }\n\n // Wait for initialization to complete\n if (initPromise) {\n middlewareInstance = await initPromise;\n }\n }\n\n // Run the middleware\n return middlewareInstance\n ? middlewareInstance(request)\n : NextResponse.next();\n };\n}\n\n/**\n * Export the original function as well for backward compatibility\n */\nexport { createAgentShieldMiddleware as createMiddleware };\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/edge-detector-wrapper.ts","../src/session-tracker.ts","../src/middleware.ts","../src/create-middleware.ts"],"names":["NextResponse","response","context","createAgentShieldMiddleware"],"mappings":";;;;;;;AAiCA,IAAM,iBAAA,GAAoB;AAAA,EACxB,EAAE,OAAA,EAAS,eAAA,EAAiB,IAAA,EAAM,SAAA,EAAW,MAAM,SAAA,EAAU;AAAA,EAC7D,EAAE,OAAA,EAAS,aAAA,EAAe,IAAA,EAAM,QAAA,EAAU,MAAM,QAAA,EAAS;AAAA,EACzD,EAAE,OAAA,EAAS,gBAAA,EAAkB,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAAA,EACpE,EAAE,OAAA,EAAS,kBAAA,EAAoB,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAAA,EACtE,EAAE,OAAA,EAAS,gBAAA,EAAkB,IAAA,EAAM,YAAA,EAAc,MAAM,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AM,WAAA;AAAA,MAC7B;AAAA,IACF;AAGA,IAAA,OAAO,kBAAA,GACH,kBAAA,CAAmB,OAAO,CAAA,GAC1BH,oBAAa,IAAA,EAAK;AAAA,EACxB,CAAA;AACF","file":"create-middleware.js","sourcesContent":["/**\n * Wrapper for EdgeAgentDetector to match AgentDetector interface\n * This allows the middleware to work with EdgeAgentDetector in Edge Runtime\n *\n * This is a self-contained implementation to avoid import resolution issues\n */\n\ntype DetectionInput = {\n userAgent?: string;\n ip?: string;\n ipAddress?: string;\n headers?: Record<string, string>;\n url?: string;\n method?: string;\n timestamp?: Date;\n};\n\ntype DetectionResult = {\n isAgent: boolean;\n confidence: number;\n detectedAgent?: {\n type: string;\n name: string;\n };\n reasons: string[];\n verificationMethod?: string;\n forgeabilityRisk?: 'low' | 'medium' | 'high';\n timestamp: Date;\n};\n\ntype EventHandler = (...args: any[]) => void;\n\n// Known AI agent patterns\nconst AI_AGENT_PATTERNS = [\n { pattern: /chatgpt-user/i, type: 'chatgpt', name: 'ChatGPT' },\n { pattern: /claude-web/i, type: 'claude', name: 'Claude' },\n { pattern: /perplexitybot/i, type: 'perplexity', name: 'Perplexity' },\n { pattern: /perplexity-user/i, type: 'perplexity', name: 'Perplexity' },\n { pattern: /perplexity-ai/i, type: 'perplexity', name: 'Perplexity' },\n { pattern: /perplexity/i, type: 'perplexity', name: 'Perplexity' }, // Fallback\n { pattern: /bingbot/i, type: 'bing', name: 'Bing AI' },\n { pattern: /anthropic-ai/i, type: 'anthropic', name: 'Anthropic' },\n];\n\n// Known cloud provider IP ranges (simplified)\nconst CLOUD_PROVIDERS = {\n aws: ['54.', '52.', '35.', '18.', '3.'],\n gcp: ['35.', '34.', '104.', '107.', '108.'],\n azure: ['13.', '20.', '40.', '52.', '104.'],\n};\n\n/**\n * Self-contained EdgeAgentDetector implementation\n */\nclass EdgeAgentDetector {\n async analyze(input: DetectionInput): Promise<DetectionResult> {\n const reasons: string[] = [];\n let detectedAgent: { type: string; name: string } | undefined;\n let verificationMethod: string | undefined;\n let confidence = 0;\n\n const headers = input.headers || {};\n const normalizedHeaders: Record<string, string> = {};\n\n // Normalize header names to lowercase\n for (const [key, value] of Object.entries(headers)) {\n normalizedHeaders[key.toLowerCase()] = value;\n }\n\n // Check for HTTP Message Signatures (highest confidence)\n const signaturePresent = !!(\n normalizedHeaders['signature'] || normalizedHeaders['signature-input']\n );\n const signatureAgent = normalizedHeaders['signature-agent'];\n\n if (signatureAgent?.includes('chatgpt.com')) {\n confidence = 0.85;\n reasons.push('signature_agent:chatgpt');\n detectedAgent = { type: 'chatgpt', name: 'ChatGPT' };\n verificationMethod = 'signature';\n } else if (signaturePresent) {\n confidence = Math.max(confidence, 0.4);\n reasons.push('signature_present');\n }\n\n // Check User-Agent patterns\n const userAgent = input.userAgent || input.headers?.['user-agent'] || '';\n if (userAgent) {\n for (const { pattern, type, name } of AI_AGENT_PATTERNS) {\n if (pattern.test(userAgent)) {\n // Give higher confidence to well-known AI agents that properly identify themselves\n const highConfidenceAgents = [\n 'chatgpt',\n 'claude',\n 'perplexity',\n 'anthropic',\n ];\n const patternConfidence = highConfidenceAgents.includes(type)\n ? 0.85\n : 0.5;\n\n confidence = Math.max(confidence, patternConfidence);\n reasons.push(`known_pattern:${type}`);\n if (!detectedAgent) {\n detectedAgent = { type, name };\n verificationMethod = 'pattern';\n }\n break;\n }\n }\n }\n\n // Check AI-specific headers\n const aiHeaders = [\n 'openai-conversation-id',\n 'openai-ephemeral-user-id',\n 'anthropic-client-id',\n 'x-goog-api-client',\n 'x-ms-copilot-id',\n ];\n\n const foundAiHeaders = aiHeaders.filter(\n header => normalizedHeaders[header]\n );\n if (foundAiHeaders.length > 0) {\n confidence = Math.max(confidence, 0.6);\n reasons.push(`ai_headers:${foundAiHeaders.length}`);\n }\n\n // Check cloud provider IPs\n const ip = input.ip || input.ipAddress;\n if (\n ip &&\n !normalizedHeaders['x-forwarded-for'] &&\n !normalizedHeaders['x-real-ip']\n ) {\n for (const [provider, prefixes] of Object.entries(CLOUD_PROVIDERS)) {\n if (prefixes.some(prefix => ip.startsWith(prefix))) {\n confidence = Math.max(confidence, 0.4);\n reasons.push(`cloud_provider:${provider}`);\n break;\n }\n }\n }\n\n // Boost confidence for combinations\n if (reasons.length > 2) {\n confidence = Math.min(confidence * 1.2, 0.95);\n }\n\n return {\n isAgent: confidence > 0.3,\n confidence,\n ...(detectedAgent && { detectedAgent }),\n reasons,\n ...(verificationMethod && { verificationMethod }),\n forgeabilityRisk: confidence > 0.8 ? 'medium' : 'high',\n timestamp: new Date(),\n };\n }\n}\n\n/**\n * Wrapper that provides event emitter functionality\n */\nexport class EdgeAgentDetectorWrapper {\n private detector: EdgeAgentDetector;\n private events: Map<string, EventHandler[]> = new Map();\n\n constructor(_config?: any) {\n // Config is accepted but not used by EdgeAgentDetector\n this.detector = new EdgeAgentDetector();\n }\n\n async analyze(input: DetectionInput): Promise<DetectionResult> {\n const result = await this.detector.analyze(input);\n\n // Emit events if there are listeners\n if (result.isAgent && this.events.has('agent.detected')) {\n const handlers = this.events.get('agent.detected') || [];\n handlers.forEach(handler => handler(result, input));\n }\n\n return result;\n }\n\n on(event: string, handler: EventHandler): void {\n if (!this.events.has(event)) {\n this.events.set(event, []);\n }\n this.events.get(event)!.push(handler);\n }\n\n emit(event: string, ...args: any[]): void {\n const handlers = this.events.get(event) || [];\n handlers.forEach(handler => handler(...args));\n }\n\n async init(): Promise<void> {\n // EdgeAgentDetector doesn't need initialization\n return;\n }\n}\n","/**\n * Edge-compatible session tracking for AI agents\n * Uses cookie-based storage to work in Edge Runtime\n */\n\nimport type { NextRequest, NextResponse } from 'next/server';\nimport type { DetectionResult } from '@kya-os/agentshield';\n\nexport interface SessionData {\n id: string;\n agent: string;\n confidence: number;\n detectedAt: number;\n expires: number;\n}\n\nexport interface SessionTrackingConfig {\n enabled: boolean;\n cookieName?: string;\n cookieMaxAge?: number; // in seconds\n encryptionKey?: string;\n}\n\nexport class EdgeSessionTracker {\n private readonly config: Required<SessionTrackingConfig>;\n\n constructor(config: SessionTrackingConfig) {\n this.config = {\n enabled: config.enabled,\n cookieName: config.cookieName || '__agentshield_session',\n cookieMaxAge: config.cookieMaxAge || 3600, // 1 hour default\n encryptionKey:\n config.encryptionKey ||\n process.env.AGENTSHIELD_SECRET ||\n 'agentshield-default-key',\n };\n }\n\n /**\n * Track a new AI agent session\n */\n async track(\n _request: NextRequest,\n response: NextResponse,\n result: DetectionResult\n ): Promise<NextResponse> {\n try {\n if (!this.config.enabled || !result.isAgent) {\n return response;\n }\n\n const sessionData: SessionData = {\n id: crypto.randomUUID(),\n agent: result.detectedAgent?.name || 'unknown',\n confidence: result.confidence,\n detectedAt: Date.now(),\n expires: Date.now() + this.config.cookieMaxAge * 1000,\n };\n\n // Encrypt session data for security\n const encrypted = await this.encrypt(JSON.stringify(sessionData));\n\n // Set secure httpOnly cookie\n response.cookies.set(this.config.cookieName, encrypted, {\n httpOnly: true,\n secure: process.env.NODE_ENV === 'production',\n sameSite: 'lax',\n maxAge: this.config.cookieMaxAge,\n path: '/',\n });\n\n return response;\n } catch (error) {\n // Fail gracefully - log error but don't break request\n if (process.env.DEBUG_AGENTSHIELD) {\n console.warn('AgentShield: Failed to track session:', error);\n }\n return response;\n }\n }\n\n /**\n * Check for existing AI agent session\n */\n async check(request: NextRequest): Promise<SessionData | null> {\n try {\n if (!this.config.enabled) {\n return null;\n }\n\n const cookie = request.cookies.get(this.config.cookieName);\n if (!cookie?.value) {\n return null;\n }\n\n // Decrypt and parse session data\n const decrypted = await this.decrypt(cookie.value);\n const session: SessionData = JSON.parse(decrypted);\n\n // Check if session is expired\n if (session.expires < Date.now()) {\n return null;\n }\n\n return session;\n } catch (error) {\n // Fail gracefully - invalid or corrupted session\n if (process.env.DEBUG_AGENTSHIELD) {\n console.warn('AgentShield: Failed to check session:', error);\n }\n return null;\n }\n }\n\n /**\n * Clear an existing session\n */\n clear(response: NextResponse): NextResponse {\n try {\n response.cookies.delete(this.config.cookieName);\n } catch (error) {\n // Fail gracefully\n if (process.env.DEBUG_AGENTSHIELD) {\n console.warn('AgentShield: Failed to clear session:', error);\n }\n }\n return response;\n }\n\n /**\n * Simple encryption using Web Crypto API (Edge-compatible)\n */\n private async encrypt(data: string): Promise<string> {\n try {\n // For Edge Runtime, use simple base64 encoding with obfuscation\n // In production, consider using Web Crypto API subtle.encrypt()\n const key = this.config.encryptionKey;\n const encoded = new TextEncoder().encode(data);\n\n // Simple XOR obfuscation\n const obfuscated = new Uint8Array(encoded.length);\n for (let i = 0; i < encoded.length; i++) {\n obfuscated[i] = (encoded[i] || 0) ^ key.charCodeAt(i % key.length);\n }\n\n // Convert to base64\n return btoa(\n Array.from(obfuscated, byte => String.fromCharCode(byte)).join('')\n );\n } catch (error) {\n // Fallback to simple base64 if encryption fails\n return btoa(data);\n }\n }\n\n /**\n * Simple decryption (Edge-compatible)\n */\n private async decrypt(data: string): Promise<string> {\n try {\n const key = this.config.encryptionKey;\n const decoded = Uint8Array.from(atob(data), c => c.charCodeAt(0));\n\n // Reverse XOR obfuscation\n const deobfuscated = new Uint8Array(decoded.length);\n for (let i = 0; i < decoded.length; i++) {\n deobfuscated[i] = (decoded[i] || 0) ^ key.charCodeAt(i % key.length);\n }\n\n return new TextDecoder().decode(deobfuscated);\n } catch (error) {\n // Fallback to simple base64 if decryption fails\n return atob(data);\n }\n }\n}\n\n/**\n * Stateless session checker for non-Next.js environments (Express, etc.)\n * Uses a combination of headers to identify continued sessions\n */\nexport class StatelessSessionChecker {\n static check(headers: Record<string, string>): SessionData | null {\n try {\n // Check for session headers (set by previous response)\n const agent = headers['x-agentshield-session-agent'];\n const confidence = headers['x-agentshield-session-confidence'];\n const sessionId = headers['x-agentshield-session-id'];\n\n if (agent && confidence && sessionId) {\n return {\n id: sessionId,\n agent,\n confidence: parseFloat(confidence),\n detectedAt: Date.now(),\n expires: Date.now() + 3600000, // 1 hour\n };\n }\n\n // Check for cookie-based session (if cookies are parsed)\n const cookieHeader = headers['cookie'];\n if (cookieHeader && cookieHeader.includes('__agentshield_session=')) {\n // Simple cookie parsing\n const match = cookieHeader.match(/__agentshield_session=([^;]+)/);\n if (match && match[1]) {\n try {\n const decoded = atob(match[1]);\n return JSON.parse(decoded);\n } catch {\n // Invalid session data\n }\n }\n }\n\n return null;\n } catch {\n return null;\n }\n }\n\n static setHeaders(response: any, session: SessionData): void {\n try {\n // Set session headers for stateless tracking\n if (response.setHeader) {\n response.setHeader('X-AgentShield-Session-Agent', session.agent);\n response.setHeader(\n 'X-AgentShield-Session-Confidence',\n session.confidence.toString()\n );\n response.setHeader('X-AgentShield-Session-Id', session.id);\n } else if (response.headers && response.headers.set) {\n response.headers.set('x-agentshield-session-agent', session.agent);\n response.headers.set(\n 'x-agentshield-session-confidence',\n session.confidence.toString()\n );\n response.headers.set('x-agentshield-session-id', session.id);\n }\n } catch {\n // Fail gracefully\n }\n }\n}\n","/**\n * Next.js middleware for AgentShield\n *\n * Uses edge-safe imports to avoid WASM in Edge Runtime\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\n// Use wrapper for EdgeAgentDetector to match expected interface\nimport { EdgeAgentDetectorWrapper as AgentDetector } from './edge-detector-wrapper';\nimport { EdgeSessionTracker } from './session-tracker';\nimport type { NextJSMiddlewareConfig } from './types';\n\n/**\n * Create AgentShield middleware for Next.js\n */\nexport function createAgentShieldMiddleware(\n config: Partial<NextJSMiddlewareConfig> = {}\n) {\n const detector = new AgentDetector(config);\n\n // Initialize session tracker if enabled (or if WASM is enabled)\n const sessionTracker =\n config.sessionTracking?.enabled || config.enableWasm\n ? new EdgeSessionTracker({\n enabled: true,\n ...config.sessionTracking,\n })\n : null;\n\n // Wire up event handlers if provided\n if (config.events) {\n Object.entries(config.events).forEach(([event, handler]) => {\n detector.on(event as any, handler as any);\n });\n }\n\n const {\n onAgentDetected = 'log',\n onDetection,\n skipPaths = [],\n blockedResponse = {\n status: 403,\n message: 'Access denied: Automated agent detected',\n headers: { 'Content-Type': 'application/json' },\n },\n redirectUrl = '/blocked',\n rewriteUrl = '/blocked',\n } = config;\n\n return async (request: NextRequest): Promise<NextResponse> => {\n try {\n // Check if path should be skipped\n const shouldSkip = skipPaths.some(pattern => {\n if (typeof pattern === 'string') {\n return request.nextUrl.pathname.startsWith(pattern);\n }\n return pattern.test(request.nextUrl.pathname);\n });\n\n if (shouldSkip) {\n // Mark as skipped in request\n (request as any).agentShield = { skipped: true };\n return NextResponse.next();\n }\n\n // Check for existing session first\n const existingSession = sessionTracker\n ? await sessionTracker.check(request)\n : null;\n\n if (existingSession) {\n // This is a continued AI agent session\n const response = NextResponse.next();\n\n // Set headers to indicate continued session\n response.headers.set('x-agentshield-detected', 'true');\n response.headers.set('x-agentshield-agent', existingSession.agent);\n response.headers.set(\n 'x-agentshield-confidence',\n existingSession.confidence.toString()\n );\n response.headers.set('x-agentshield-session', 'continued');\n response.headers.set('x-agentshield-session-id', existingSession.id);\n\n // Mark request with session info\n (request as any).agentShield = {\n result: {\n isAgent: true,\n confidence: existingSession.confidence,\n detectedAgent: { name: existingSession.agent },\n timestamp: new Date(),\n verificationMethod: 'session',\n },\n session: existingSession,\n skipped: false,\n };\n\n // Emit session continued event\n const context = {\n userAgent: request.headers.get('user-agent') || '',\n ipAddress:\n (request.ip ?? request.headers.get('x-forwarded-for')) || '',\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n detector.emit('agent.session.continued', existingSession, context);\n\n return response;\n }\n\n // Prepare request context for new detection\n const userAgent = request.headers.get('user-agent');\n const ipAddress = request.ip ?? request.headers.get('x-forwarded-for');\n\n const context = {\n ...(userAgent && { userAgent }),\n ...(ipAddress && { ipAddress }),\n headers: Object.fromEntries(request.headers.entries()),\n url: request.url,\n method: request.method,\n timestamp: new Date(),\n };\n\n // Analyze request\n const result = await detector.analyze(context);\n\n // Handle detection result\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n // Call custom detection handler if provided\n if (onDetection) {\n const customResponse = await onDetection(request, result);\n if (customResponse) {\n return customResponse;\n }\n }\n\n // Handle based on configuration\n switch (onAgentDetected) {\n case 'block': {\n const response = NextResponse.json(\n {\n error: blockedResponse.message,\n detected: true,\n confidence: result.confidence,\n timestamp: result.timestamp,\n },\n { status: blockedResponse.status }\n );\n\n if (blockedResponse.headers) {\n Object.entries(blockedResponse.headers).forEach(\n ([key, value]) => {\n response.headers.set(key, value);\n }\n );\n }\n\n // Emit blocked event\n detector.emit('agent.blocked', result, context);\n\n return response;\n }\n\n case 'redirect':\n return NextResponse.redirect(new URL(redirectUrl, request.url));\n\n case 'rewrite':\n return NextResponse.rewrite(new URL(rewriteUrl, request.url));\n\n case 'log':\n console.warn('AgentShield: Agent detected', {\n ipAddress: context.ipAddress,\n userAgent: context.userAgent,\n confidence: result.confidence,\n reasons: result.reasons,\n pathname: request.nextUrl.pathname,\n });\n break;\n\n case 'allow':\n default:\n // Emit allowed event for high-confidence agents\n if (\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n detector.emit('agent.allowed', result, context);\n }\n // Continue processing\n break;\n }\n }\n\n // Add detection result to request for API routes\n (request as any).agentShield = {\n result,\n skipped: false,\n };\n\n // Add detection result to response headers for debugging\n let response = NextResponse.next();\n response.headers.set('x-agentshield-detected', result.isAgent.toString());\n response.headers.set(\n 'x-agentshield-confidence',\n result.confidence.toString()\n );\n\n // Add agent name if detected\n if (result.detectedAgent?.name) {\n response.headers.set('x-agentshield-agent', result.detectedAgent.name);\n }\n\n // Track new AI agent sessions\n if (\n sessionTracker &&\n result.isAgent &&\n result.confidence >= (config.confidenceThreshold ?? 0.7)\n ) {\n response = await sessionTracker.track(request, response, result);\n response.headers.set('x-agentshield-session', 'new');\n\n // Emit new session event\n detector.emit('agent.session.started', result, context);\n }\n\n return response;\n } catch (error) {\n console.error('AgentShield middleware error:', error);\n return NextResponse.next(); // Continue on error\n }\n };\n}\n\n/**\n * Convenience function for basic setup\n */\nexport function agentShield(config: Partial<NextJSMiddlewareConfig> = {}) {\n return createAgentShieldMiddleware(config);\n}\n","/**\n * Enhanced middleware creator for Edge Runtime\n * Uses EdgeAgentDetector which doesn't require WASM\n */\n\nimport { NextResponse } from 'next/server';\nimport type { NextRequest } from 'next/server';\nimport { createAgentShieldMiddleware as createBaseMiddleware } from './middleware';\nimport type { NextJSMiddlewareConfig } from './types';\n\nlet middlewareInstance: any = null;\nlet isInitializing = false;\nlet initPromise: Promise<any> | null = null;\n\n/**\n * Create an AgentShield middleware with automatic WASM initialization\n * This version handles initialization internally to avoid top-level await\n */\nexport function createAgentShieldMiddleware(config: NextJSMiddlewareConfig) {\n return async function agentShieldMiddleware(\n request: NextRequest\n ): Promise<NextResponse> {\n // Initialize middleware on first request (no WASM needed in Edge Runtime)\n if (!middlewareInstance) {\n if (!isInitializing) {\n isInitializing = true;\n initPromise = (async () => {\n // No WASM initialization needed - EdgeAgentDetector works without it\n\n // Create the actual middleware instance\n middlewareInstance = createBaseMiddleware(config);\n return middlewareInstance;\n })();\n }\n\n // Wait for initialization to complete\n if (initPromise) {\n middlewareInstance = await initPromise;\n }\n }\n\n // Run the middleware\n return middlewareInstance\n ? middlewareInstance(request)\n : NextResponse.next();\n };\n}\n\n/**\n * Export the original function as well for backward compatibility\n */\nexport { createAgentShieldMiddleware as createMiddleware };\n"]}
|