@kya-os/agentshield-nextjs 0.1.30 → 0.1.31

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.
Files changed (43) hide show
  1. package/dist/create-middleware.js +283 -13
  2. package/dist/create-middleware.js.map +1 -1
  3. package/dist/create-middleware.mjs +283 -13
  4. package/dist/create-middleware.mjs.map +1 -1
  5. package/dist/edge-detector-wrapper.js +251 -12
  6. package/dist/edge-detector-wrapper.js.map +1 -1
  7. package/dist/edge-detector-wrapper.mjs +251 -12
  8. package/dist/edge-detector-wrapper.mjs.map +1 -1
  9. package/dist/index.js +283 -13
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +283 -13
  12. package/dist/index.mjs.map +1 -1
  13. package/dist/middleware.js +283 -13
  14. package/dist/middleware.js.map +1 -1
  15. package/dist/middleware.mjs +283 -13
  16. package/dist/middleware.mjs.map +1 -1
  17. package/dist/signature-verifier.js +220 -0
  18. package/dist/signature-verifier.js.map +1 -0
  19. package/dist/signature-verifier.mjs +216 -0
  20. package/dist/signature-verifier.mjs.map +1 -0
  21. package/package.json +2 -2
  22. package/dist/create-middleware.d.mts +0 -16
  23. package/dist/create-middleware.d.ts +0 -16
  24. package/dist/edge-detector-wrapper.d.mts +0 -42
  25. package/dist/edge-detector-wrapper.d.ts +0 -42
  26. package/dist/edge-runtime-loader.d.mts +0 -49
  27. package/dist/edge-runtime-loader.d.ts +0 -49
  28. package/dist/edge-wasm-middleware.d.mts +0 -58
  29. package/dist/edge-wasm-middleware.d.ts +0 -58
  30. package/dist/index.d.mts +0 -19
  31. package/dist/index.d.ts +0 -19
  32. package/dist/middleware.d.mts +0 -20
  33. package/dist/middleware.d.ts +0 -20
  34. package/dist/nodejs-wasm-loader.d.mts +0 -25
  35. package/dist/nodejs-wasm-loader.d.ts +0 -25
  36. package/dist/session-tracker.d.mts +0 -55
  37. package/dist/session-tracker.d.ts +0 -55
  38. package/dist/types-BJTEUa4T.d.mts +0 -88
  39. package/dist/types-BJTEUa4T.d.ts +0 -88
  40. package/dist/wasm-middleware.d.mts +0 -62
  41. package/dist/wasm-middleware.d.ts +0 -62
  42. package/dist/wasm-setup.d.mts +0 -46
  43. package/dist/wasm-setup.d.ts +0 -46
@@ -1,20 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { N as NextJSMiddlewareConfig } from './types-BJTEUa4T.js';
3
- import '@kya-os/agentshield';
4
-
5
- /**
6
- * Next.js middleware for AgentShield
7
- *
8
- * Uses edge-safe imports to avoid WASM in Edge Runtime
9
- */
10
-
11
- /**
12
- * Create AgentShield middleware for Next.js
13
- */
14
- declare function createAgentShieldMiddleware(config?: Partial<NextJSMiddlewareConfig>): (request: NextRequest) => Promise<NextResponse>;
15
- /**
16
- * Convenience function for basic setup
17
- */
18
- declare function agentShield(config?: Partial<NextJSMiddlewareConfig>): (request: NextRequest) => Promise<NextResponse>;
19
-
20
- export { agentShield, createAgentShieldMiddleware };
@@ -1,25 +0,0 @@
1
- /**
2
- * Node.js Runtime WASM Loader for AgentShield
3
- *
4
- * This loader uses fs.readFileSync to load WASM in Node.js runtime.
5
- * It provides full cryptographic verification capabilities.
6
- */
7
- /**
8
- * Load WASM module using Node.js fs module
9
- * This only works in Node.js runtime, not Edge Runtime
10
- */
11
- declare function loadWasmNodejs(): Promise<boolean>;
12
- /**
13
- * Check if we're in Node.js runtime
14
- */
15
- declare function isNodejsRuntime(): boolean;
16
- /**
17
- * Get the loaded WASM module
18
- */
19
- declare function getWasmModule(): WebAssembly.Module | null;
20
- /**
21
- * Check if WASM is initialized
22
- */
23
- declare function isWasmInitialized(): boolean;
24
-
25
- export { getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
@@ -1,25 +0,0 @@
1
- /**
2
- * Node.js Runtime WASM Loader for AgentShield
3
- *
4
- * This loader uses fs.readFileSync to load WASM in Node.js runtime.
5
- * It provides full cryptographic verification capabilities.
6
- */
7
- /**
8
- * Load WASM module using Node.js fs module
9
- * This only works in Node.js runtime, not Edge Runtime
10
- */
11
- declare function loadWasmNodejs(): Promise<boolean>;
12
- /**
13
- * Check if we're in Node.js runtime
14
- */
15
- declare function isNodejsRuntime(): boolean;
16
- /**
17
- * Get the loaded WASM module
18
- */
19
- declare function getWasmModule(): WebAssembly.Module | null;
20
- /**
21
- * Check if WASM is initialized
22
- */
23
- declare function isWasmInitialized(): boolean;
24
-
25
- export { getWasmModule, isNodejsRuntime, isWasmInitialized, loadWasmNodejs };
@@ -1,55 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { DetectionResult } from '@kya-os/agentshield';
3
-
4
- /**
5
- * Edge-compatible session tracking for AI agents
6
- * Uses cookie-based storage to work in Edge Runtime
7
- */
8
-
9
- interface SessionData {
10
- id: string;
11
- agent: string;
12
- confidence: number;
13
- detectedAt: number;
14
- expires: number;
15
- }
16
- interface SessionTrackingConfig {
17
- enabled: boolean;
18
- cookieName?: string;
19
- cookieMaxAge?: number;
20
- encryptionKey?: string;
21
- }
22
- declare class EdgeSessionTracker {
23
- private readonly config;
24
- constructor(config: SessionTrackingConfig);
25
- /**
26
- * Track a new AI agent session
27
- */
28
- track(_request: NextRequest, response: NextResponse, result: DetectionResult): Promise<NextResponse>;
29
- /**
30
- * Check for existing AI agent session
31
- */
32
- check(request: NextRequest): Promise<SessionData | null>;
33
- /**
34
- * Clear an existing session
35
- */
36
- clear(response: NextResponse): NextResponse;
37
- /**
38
- * Simple encryption using Web Crypto API (Edge-compatible)
39
- */
40
- private encrypt;
41
- /**
42
- * Simple decryption (Edge-compatible)
43
- */
44
- private decrypt;
45
- }
46
- /**
47
- * Stateless session checker for non-Next.js environments (Express, etc.)
48
- * Uses a combination of headers to identify continued sessions
49
- */
50
- declare class StatelessSessionChecker {
51
- static check(headers: Record<string, string>): SessionData | null;
52
- static setHeaders(response: any, session: SessionData): void;
53
- }
54
-
55
- export { EdgeSessionTracker, type SessionData, type SessionTrackingConfig, StatelessSessionChecker };
@@ -1,55 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { DetectionResult } from '@kya-os/agentshield';
3
-
4
- /**
5
- * Edge-compatible session tracking for AI agents
6
- * Uses cookie-based storage to work in Edge Runtime
7
- */
8
-
9
- interface SessionData {
10
- id: string;
11
- agent: string;
12
- confidence: number;
13
- detectedAt: number;
14
- expires: number;
15
- }
16
- interface SessionTrackingConfig {
17
- enabled: boolean;
18
- cookieName?: string;
19
- cookieMaxAge?: number;
20
- encryptionKey?: string;
21
- }
22
- declare class EdgeSessionTracker {
23
- private readonly config;
24
- constructor(config: SessionTrackingConfig);
25
- /**
26
- * Track a new AI agent session
27
- */
28
- track(_request: NextRequest, response: NextResponse, result: DetectionResult): Promise<NextResponse>;
29
- /**
30
- * Check for existing AI agent session
31
- */
32
- check(request: NextRequest): Promise<SessionData | null>;
33
- /**
34
- * Clear an existing session
35
- */
36
- clear(response: NextResponse): NextResponse;
37
- /**
38
- * Simple encryption using Web Crypto API (Edge-compatible)
39
- */
40
- private encrypt;
41
- /**
42
- * Simple decryption (Edge-compatible)
43
- */
44
- private decrypt;
45
- }
46
- /**
47
- * Stateless session checker for non-Next.js environments (Express, etc.)
48
- * Uses a combination of headers to identify continued sessions
49
- */
50
- declare class StatelessSessionChecker {
51
- static check(headers: Record<string, string>): SessionData | null;
52
- static setHeaders(response: any, session: SessionData): void;
53
- }
54
-
55
- export { EdgeSessionTracker, type SessionData, type SessionTrackingConfig, StatelessSessionChecker };
@@ -1,88 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { AgentShieldConfig, DetectionResult, AgentShieldEvents } from '@kya-os/agentshield';
3
-
4
- /**
5
- * Next.js-specific type definitions
6
- */
7
-
8
- /**
9
- * Next.js middleware configuration
10
- */
11
- interface NextJSMiddlewareConfig extends Partial<AgentShieldConfig> {
12
- /**
13
- * Action to take when an agent is detected
14
- */
15
- onAgentDetected?: 'block' | 'redirect' | 'rewrite' | 'allow' | 'log';
16
- /**
17
- * Custom handler for agent detection
18
- * @deprecated Use 'events' instead. Will be removed in v1.0.0
19
- */
20
- onDetection?: (req: NextRequest, result: DetectionResult) => NextResponse | Promise<NextResponse> | void | Promise<void>;
21
- /**
22
- * Event handlers for detection events
23
- */
24
- events?: Partial<AgentShieldEvents>;
25
- /**
26
- * Path patterns to skip detection
27
- */
28
- skipPaths?: string[] | RegExp[];
29
- /**
30
- * Response when blocking agents
31
- */
32
- blockedResponse?: {
33
- status: number;
34
- message: string;
35
- headers?: Record<string, string>;
36
- };
37
- /**
38
- * Redirect URL when redirecting detected agents
39
- */
40
- redirectUrl?: string;
41
- /**
42
- * Rewrite URL when rewriting requests from detected agents
43
- */
44
- rewriteUrl?: string;
45
- /**
46
- * Confidence threshold for agent detection
47
- */
48
- confidenceThreshold?: number;
49
- /**
50
- * Enable WASM for enhanced detection
51
- */
52
- enableWasm?: boolean;
53
- /**
54
- * Session tracking configuration
55
- */
56
- sessionTracking?: {
57
- /**
58
- * Enable session tracking
59
- */
60
- enabled: boolean;
61
- /**
62
- * Cookie name for session storage
63
- * Default: '__agentshield_session'
64
- */
65
- cookieName?: string;
66
- /**
67
- * Cookie max age in seconds
68
- * Default: 3600 (1 hour)
69
- */
70
- cookieMaxAge?: number;
71
- /**
72
- * Encryption key for session data
73
- * Default: Uses AGENTSHIELD_SECRET env var or default key
74
- */
75
- encryptionKey?: string;
76
- };
77
- }
78
- /**
79
- * Detection context for hooks
80
- */
81
- interface DetectionContext {
82
- result: DetectionResult;
83
- request: NextRequest;
84
- userAgent?: string;
85
- ip?: string;
86
- }
87
-
88
- export type { DetectionContext as D, NextJSMiddlewareConfig as N };
@@ -1,88 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { AgentShieldConfig, DetectionResult, AgentShieldEvents } from '@kya-os/agentshield';
3
-
4
- /**
5
- * Next.js-specific type definitions
6
- */
7
-
8
- /**
9
- * Next.js middleware configuration
10
- */
11
- interface NextJSMiddlewareConfig extends Partial<AgentShieldConfig> {
12
- /**
13
- * Action to take when an agent is detected
14
- */
15
- onAgentDetected?: 'block' | 'redirect' | 'rewrite' | 'allow' | 'log';
16
- /**
17
- * Custom handler for agent detection
18
- * @deprecated Use 'events' instead. Will be removed in v1.0.0
19
- */
20
- onDetection?: (req: NextRequest, result: DetectionResult) => NextResponse | Promise<NextResponse> | void | Promise<void>;
21
- /**
22
- * Event handlers for detection events
23
- */
24
- events?: Partial<AgentShieldEvents>;
25
- /**
26
- * Path patterns to skip detection
27
- */
28
- skipPaths?: string[] | RegExp[];
29
- /**
30
- * Response when blocking agents
31
- */
32
- blockedResponse?: {
33
- status: number;
34
- message: string;
35
- headers?: Record<string, string>;
36
- };
37
- /**
38
- * Redirect URL when redirecting detected agents
39
- */
40
- redirectUrl?: string;
41
- /**
42
- * Rewrite URL when rewriting requests from detected agents
43
- */
44
- rewriteUrl?: string;
45
- /**
46
- * Confidence threshold for agent detection
47
- */
48
- confidenceThreshold?: number;
49
- /**
50
- * Enable WASM for enhanced detection
51
- */
52
- enableWasm?: boolean;
53
- /**
54
- * Session tracking configuration
55
- */
56
- sessionTracking?: {
57
- /**
58
- * Enable session tracking
59
- */
60
- enabled: boolean;
61
- /**
62
- * Cookie name for session storage
63
- * Default: '__agentshield_session'
64
- */
65
- cookieName?: string;
66
- /**
67
- * Cookie max age in seconds
68
- * Default: 3600 (1 hour)
69
- */
70
- cookieMaxAge?: number;
71
- /**
72
- * Encryption key for session data
73
- * Default: Uses AGENTSHIELD_SECRET env var or default key
74
- */
75
- encryptionKey?: string;
76
- };
77
- }
78
- /**
79
- * Detection context for hooks
80
- */
81
- interface DetectionContext {
82
- result: DetectionResult;
83
- request: NextRequest;
84
- userAgent?: string;
85
- ip?: string;
86
- }
87
-
88
- export type { DetectionContext as D, NextJSMiddlewareConfig as N };
@@ -1,62 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
-
3
- /**
4
- * WASM-enabled middleware for Next.js with AgentShield
5
- * Following official Next.js documentation for WebAssembly in Edge Runtime
6
- */
7
-
8
- interface WasmDetectionResult {
9
- isAgent: boolean;
10
- confidence: number;
11
- agent?: string | undefined;
12
- verificationMethod: 'signature' | 'pattern' | 'none';
13
- riskLevel: 'low' | 'medium' | 'high';
14
- timestamp: string;
15
- }
16
- interface AgentShieldConfig {
17
- onAgentDetected?: (result: WasmDetectionResult) => void | Promise<void>;
18
- blockOnHighConfidence?: boolean;
19
- confidenceThreshold?: number;
20
- skipPaths?: string[];
21
- blockedResponse?: {
22
- status?: number;
23
- message?: string;
24
- headers?: Record<string, string>;
25
- };
26
- }
27
- /**
28
- * Create a WASM-enabled AgentShield middleware
29
- * This must be used with proper WASM module import at the top of middleware.ts
30
- *
31
- * @example
32
- * ```typescript
33
- * // middleware.ts
34
- * import wasmModule from '@kya-os/agentshield/wasm?module';
35
- * import { createWasmAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';
36
- *
37
- * const wasmInstance = await WebAssembly.instantiate(wasmModule);
38
- *
39
- * export const middleware = createWasmAgentShieldMiddleware({
40
- * wasmInstance,
41
- * onAgentDetected: (result) => {
42
- * console.log(`Detected ${result.agent} with ${result.confidence * 100}% confidence`);
43
- * }
44
- * });
45
- * ```
46
- */
47
- declare function createWasmAgentShieldMiddleware(config: AgentShieldConfig & {
48
- wasmInstance?: WebAssembly.Instance;
49
- }): (request: NextRequest) => Promise<NextResponse<unknown>>;
50
- /**
51
- * Helper to load and instantiate WASM module
52
- * This should be called at the top of your middleware.ts file
53
- *
54
- * @example
55
- * ```typescript
56
- * import wasmModule from '@kya-os/agentshield/wasm?module';
57
- * const wasmInstance = await instantiateWasm(wasmModule);
58
- * ```
59
- */
60
- declare function instantiateWasm(wasmModule: WebAssembly.Module): Promise<WebAssembly.Instance>;
61
-
62
- export { type AgentShieldConfig, type WasmDetectionResult, createWasmAgentShieldMiddleware, instantiateWasm };
@@ -1,62 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
-
3
- /**
4
- * WASM-enabled middleware for Next.js with AgentShield
5
- * Following official Next.js documentation for WebAssembly in Edge Runtime
6
- */
7
-
8
- interface WasmDetectionResult {
9
- isAgent: boolean;
10
- confidence: number;
11
- agent?: string | undefined;
12
- verificationMethod: 'signature' | 'pattern' | 'none';
13
- riskLevel: 'low' | 'medium' | 'high';
14
- timestamp: string;
15
- }
16
- interface AgentShieldConfig {
17
- onAgentDetected?: (result: WasmDetectionResult) => void | Promise<void>;
18
- blockOnHighConfidence?: boolean;
19
- confidenceThreshold?: number;
20
- skipPaths?: string[];
21
- blockedResponse?: {
22
- status?: number;
23
- message?: string;
24
- headers?: Record<string, string>;
25
- };
26
- }
27
- /**
28
- * Create a WASM-enabled AgentShield middleware
29
- * This must be used with proper WASM module import at the top of middleware.ts
30
- *
31
- * @example
32
- * ```typescript
33
- * // middleware.ts
34
- * import wasmModule from '@kya-os/agentshield/wasm?module';
35
- * import { createWasmAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';
36
- *
37
- * const wasmInstance = await WebAssembly.instantiate(wasmModule);
38
- *
39
- * export const middleware = createWasmAgentShieldMiddleware({
40
- * wasmInstance,
41
- * onAgentDetected: (result) => {
42
- * console.log(`Detected ${result.agent} with ${result.confidence * 100}% confidence`);
43
- * }
44
- * });
45
- * ```
46
- */
47
- declare function createWasmAgentShieldMiddleware(config: AgentShieldConfig & {
48
- wasmInstance?: WebAssembly.Instance;
49
- }): (request: NextRequest) => Promise<NextResponse<unknown>>;
50
- /**
51
- * Helper to load and instantiate WASM module
52
- * This should be called at the top of your middleware.ts file
53
- *
54
- * @example
55
- * ```typescript
56
- * import wasmModule from '@kya-os/agentshield/wasm?module';
57
- * const wasmInstance = await instantiateWasm(wasmModule);
58
- * ```
59
- */
60
- declare function instantiateWasm(wasmModule: WebAssembly.Module): Promise<WebAssembly.Instance>;
61
-
62
- export { type AgentShieldConfig, type WasmDetectionResult, createWasmAgentShieldMiddleware, instantiateWasm };
@@ -1,46 +0,0 @@
1
- /**
2
- * WASM Setup for AgentShield in Next.js Edge Runtime
3
- *
4
- * This module handles WASM initialization for cryptographic signature verification.
5
- * Designed to work without top-level await to avoid Next.js middleware issues.
6
- *
7
- * Usage in middleware.ts:
8
- * ```typescript
9
- * import { setupWasm } from '@kya-os/agentshield-nextjs/wasm-setup';
10
- * import { createAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';
11
- *
12
- * export async function middleware(request: NextRequest) {
13
- * // Initialize WASM inside the middleware function
14
- * await setupWasm();
15
- *
16
- * const agentShieldMiddleware = createAgentShieldMiddleware({...});
17
- * return agentShieldMiddleware(request);
18
- * }
19
- * ```
20
- */
21
- /**
22
- * Initialize WASM module for AgentShield
23
- *
24
- * This function:
25
- * - Loads WASM in production/Edge Runtime for cryptographic verification
26
- * - Skips WASM in test environments (Jest) automatically
27
- * - Is safe to call multiple times (idempotent)
28
- * - Handles errors gracefully with fallback to pattern detection
29
- *
30
- * @returns Promise that resolves when initialization is complete
31
- */
32
- declare function setupWasm(): Promise<void>;
33
- /**
34
- * Check if WASM has been initialized
35
- *
36
- * @returns true if WASM setup has been attempted (success or failure)
37
- */
38
- declare function isWasmInitialized(): boolean;
39
- /**
40
- * Reset WASM initialization state (mainly for testing)
41
- *
42
- * @internal
43
- */
44
- declare function resetWasmState(): void;
45
-
46
- export { isWasmInitialized, resetWasmState, setupWasm };
@@ -1,46 +0,0 @@
1
- /**
2
- * WASM Setup for AgentShield in Next.js Edge Runtime
3
- *
4
- * This module handles WASM initialization for cryptographic signature verification.
5
- * Designed to work without top-level await to avoid Next.js middleware issues.
6
- *
7
- * Usage in middleware.ts:
8
- * ```typescript
9
- * import { setupWasm } from '@kya-os/agentshield-nextjs/wasm-setup';
10
- * import { createAgentShieldMiddleware } from '@kya-os/agentshield-nextjs';
11
- *
12
- * export async function middleware(request: NextRequest) {
13
- * // Initialize WASM inside the middleware function
14
- * await setupWasm();
15
- *
16
- * const agentShieldMiddleware = createAgentShieldMiddleware({...});
17
- * return agentShieldMiddleware(request);
18
- * }
19
- * ```
20
- */
21
- /**
22
- * Initialize WASM module for AgentShield
23
- *
24
- * This function:
25
- * - Loads WASM in production/Edge Runtime for cryptographic verification
26
- * - Skips WASM in test environments (Jest) automatically
27
- * - Is safe to call multiple times (idempotent)
28
- * - Handles errors gracefully with fallback to pattern detection
29
- *
30
- * @returns Promise that resolves when initialization is complete
31
- */
32
- declare function setupWasm(): Promise<void>;
33
- /**
34
- * Check if WASM has been initialized
35
- *
36
- * @returns true if WASM setup has been attempted (success or failure)
37
- */
38
- declare function isWasmInitialized(): boolean;
39
- /**
40
- * Reset WASM initialization state (mainly for testing)
41
- *
42
- * @internal
43
- */
44
- declare function resetWasmState(): void;
45
-
46
- export { isWasmInitialized, resetWasmState, setupWasm };