@kya-os/agentshield-nextjs 0.2.7 → 0.2.8

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.
@@ -0,0 +1,150 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ import { DetectionResult, PolicyConfig, PolicyEvaluationResult, PolicyEvaluationContext } from '@kya-os/agentshield-shared';
3
+ export { DEFAULT_POLICY, ENFORCEMENT_ACTIONS, PolicyConfig, PolicyEvaluationContext, PolicyEvaluationResult, createEvaluationContext, evaluatePolicy } from '@kya-os/agentshield-shared';
4
+
5
+ /**
6
+ * Policy Integration for agentshield-nextjs
7
+ *
8
+ * This module provides policy evaluation support for the Next.js middleware.
9
+ * It can use:
10
+ * - Local policy configuration (static)
11
+ * - Fetched policy from AgentShield API (dynamic with caching)
12
+ * - Fallback/default policies
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { createPolicyMiddleware } from '@kya-os/agentshield-nextjs/policy';
17
+ *
18
+ * export default createPolicyMiddleware({
19
+ * policy: {
20
+ * enabled: true,
21
+ * defaultAction: 'allow',
22
+ * thresholds: { confidenceThreshold: 80, confidenceAction: 'block' },
23
+ * allowList: [{ clientName: 'ChatGPT' }],
24
+ * },
25
+ * });
26
+ * ```
27
+ */
28
+
29
+ /**
30
+ * Policy middleware configuration
31
+ */
32
+ interface PolicyMiddlewareConfig {
33
+ /**
34
+ * Local policy configuration (static)
35
+ * If provided, this policy is used instead of fetching from API
36
+ */
37
+ policy?: Partial<PolicyConfig>;
38
+ /**
39
+ * Fetch policy from AgentShield API
40
+ * Requires projectId and optionally an apiKey
41
+ */
42
+ fetchPolicy?: {
43
+ /** Project ID to fetch policy for */
44
+ projectId: string;
45
+ /** API base URL (defaults to production) */
46
+ apiUrl?: string;
47
+ /** API key for authentication */
48
+ apiKey?: string;
49
+ /** Cache TTL in seconds (default: 300) */
50
+ cacheTtlSeconds?: number;
51
+ };
52
+ /**
53
+ * Fallback policy to use when fetch fails
54
+ * Defaults to DEFAULT_POLICY (allow all)
55
+ */
56
+ fallbackPolicy?: Partial<PolicyConfig>;
57
+ /**
58
+ * Custom blocked response
59
+ */
60
+ blockedResponse?: {
61
+ status?: number;
62
+ message?: string;
63
+ headers?: Record<string, string>;
64
+ };
65
+ /**
66
+ * Default redirect URL for redirect actions
67
+ */
68
+ redirectUrl?: string;
69
+ /**
70
+ * Callback when policy decision is made
71
+ */
72
+ onPolicyDecision?: (request: NextRequest, decision: PolicyEvaluationResult, context: PolicyEvaluationContext) => void | Promise<void>;
73
+ /**
74
+ * Custom response builder for blocked requests
75
+ */
76
+ customBlockedResponse?: (request: NextRequest, decision: PolicyEvaluationResult) => NextResponse | Promise<NextResponse>;
77
+ /**
78
+ * Whether to fail open (allow) on policy evaluation errors
79
+ * Default: true (recommended for production)
80
+ */
81
+ failOpen?: boolean;
82
+ /**
83
+ * Enable debug logging
84
+ */
85
+ debug?: boolean;
86
+ }
87
+ /**
88
+ * Combined middleware configuration with policy support
89
+ */
90
+ interface NextJSPolicyMiddlewareConfig extends PolicyMiddlewareConfig {
91
+ /**
92
+ * Paths to skip (in addition to policy excludedPaths)
93
+ */
94
+ skipPaths?: string[];
95
+ /**
96
+ * Only enforce on these paths (overrides policy includedPaths)
97
+ */
98
+ includePaths?: string[];
99
+ }
100
+ /**
101
+ * Create policy evaluation context from detection result and request
102
+ */
103
+ declare function createContextFromDetection(detection: DetectionResult, request: NextRequest): PolicyEvaluationContext;
104
+ /**
105
+ * Evaluate policy for a detection result
106
+ */
107
+ declare function evaluatePolicyForDetection(detection: DetectionResult, request: NextRequest, policy: PolicyConfig): PolicyEvaluationResult;
108
+ /**
109
+ * Build blocked response based on policy decision
110
+ */
111
+ declare function buildBlockedResponse(decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
112
+ /**
113
+ * Build redirect response based on policy decision
114
+ */
115
+ declare function buildRedirectResponse(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
116
+ /**
117
+ * Build challenge response (placeholder - future implementation)
118
+ */
119
+ declare function buildChallengeResponse(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
120
+ /**
121
+ * Handle policy decision and return appropriate response
122
+ */
123
+ declare function handlePolicyDecision(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): Promise<NextResponse | null>;
124
+ /**
125
+ * Get policy (local, fetched, or fallback)
126
+ */
127
+ declare function getPolicy(config: PolicyMiddlewareConfig): Promise<PolicyConfig>;
128
+ /**
129
+ * Apply policy to a detection result
130
+ *
131
+ * This function can be used standalone to evaluate policy after detection.
132
+ * Supports extended config with skipPaths and includePaths for path-based filtering.
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const result = await detector.analyze(context);
137
+ * const response = await applyPolicy(request, result, {
138
+ * policy: { thresholds: { confidenceThreshold: 80 } },
139
+ * skipPaths: ['/health', '/api/public/*'],
140
+ * includePaths: ['/api/*'],
141
+ * });
142
+ *
143
+ * if (response) {
144
+ * return response; // Policy blocked the request
145
+ * }
146
+ * ```
147
+ */
148
+ declare function applyPolicy(request: NextRequest, detection: DetectionResult, config: NextJSPolicyMiddlewareConfig): Promise<NextResponse | null>;
149
+
150
+ export { type NextJSPolicyMiddlewareConfig, type PolicyMiddlewareConfig, applyPolicy, buildBlockedResponse, buildChallengeResponse, buildRedirectResponse, createContextFromDetection, evaluatePolicyForDetection, getPolicy, handlePolicyDecision };
@@ -0,0 +1,150 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ import { DetectionResult, PolicyConfig, PolicyEvaluationResult, PolicyEvaluationContext } from '@kya-os/agentshield-shared';
3
+ export { DEFAULT_POLICY, ENFORCEMENT_ACTIONS, PolicyConfig, PolicyEvaluationContext, PolicyEvaluationResult, createEvaluationContext, evaluatePolicy } from '@kya-os/agentshield-shared';
4
+
5
+ /**
6
+ * Policy Integration for agentshield-nextjs
7
+ *
8
+ * This module provides policy evaluation support for the Next.js middleware.
9
+ * It can use:
10
+ * - Local policy configuration (static)
11
+ * - Fetched policy from AgentShield API (dynamic with caching)
12
+ * - Fallback/default policies
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { createPolicyMiddleware } from '@kya-os/agentshield-nextjs/policy';
17
+ *
18
+ * export default createPolicyMiddleware({
19
+ * policy: {
20
+ * enabled: true,
21
+ * defaultAction: 'allow',
22
+ * thresholds: { confidenceThreshold: 80, confidenceAction: 'block' },
23
+ * allowList: [{ clientName: 'ChatGPT' }],
24
+ * },
25
+ * });
26
+ * ```
27
+ */
28
+
29
+ /**
30
+ * Policy middleware configuration
31
+ */
32
+ interface PolicyMiddlewareConfig {
33
+ /**
34
+ * Local policy configuration (static)
35
+ * If provided, this policy is used instead of fetching from API
36
+ */
37
+ policy?: Partial<PolicyConfig>;
38
+ /**
39
+ * Fetch policy from AgentShield API
40
+ * Requires projectId and optionally an apiKey
41
+ */
42
+ fetchPolicy?: {
43
+ /** Project ID to fetch policy for */
44
+ projectId: string;
45
+ /** API base URL (defaults to production) */
46
+ apiUrl?: string;
47
+ /** API key for authentication */
48
+ apiKey?: string;
49
+ /** Cache TTL in seconds (default: 300) */
50
+ cacheTtlSeconds?: number;
51
+ };
52
+ /**
53
+ * Fallback policy to use when fetch fails
54
+ * Defaults to DEFAULT_POLICY (allow all)
55
+ */
56
+ fallbackPolicy?: Partial<PolicyConfig>;
57
+ /**
58
+ * Custom blocked response
59
+ */
60
+ blockedResponse?: {
61
+ status?: number;
62
+ message?: string;
63
+ headers?: Record<string, string>;
64
+ };
65
+ /**
66
+ * Default redirect URL for redirect actions
67
+ */
68
+ redirectUrl?: string;
69
+ /**
70
+ * Callback when policy decision is made
71
+ */
72
+ onPolicyDecision?: (request: NextRequest, decision: PolicyEvaluationResult, context: PolicyEvaluationContext) => void | Promise<void>;
73
+ /**
74
+ * Custom response builder for blocked requests
75
+ */
76
+ customBlockedResponse?: (request: NextRequest, decision: PolicyEvaluationResult) => NextResponse | Promise<NextResponse>;
77
+ /**
78
+ * Whether to fail open (allow) on policy evaluation errors
79
+ * Default: true (recommended for production)
80
+ */
81
+ failOpen?: boolean;
82
+ /**
83
+ * Enable debug logging
84
+ */
85
+ debug?: boolean;
86
+ }
87
+ /**
88
+ * Combined middleware configuration with policy support
89
+ */
90
+ interface NextJSPolicyMiddlewareConfig extends PolicyMiddlewareConfig {
91
+ /**
92
+ * Paths to skip (in addition to policy excludedPaths)
93
+ */
94
+ skipPaths?: string[];
95
+ /**
96
+ * Only enforce on these paths (overrides policy includedPaths)
97
+ */
98
+ includePaths?: string[];
99
+ }
100
+ /**
101
+ * Create policy evaluation context from detection result and request
102
+ */
103
+ declare function createContextFromDetection(detection: DetectionResult, request: NextRequest): PolicyEvaluationContext;
104
+ /**
105
+ * Evaluate policy for a detection result
106
+ */
107
+ declare function evaluatePolicyForDetection(detection: DetectionResult, request: NextRequest, policy: PolicyConfig): PolicyEvaluationResult;
108
+ /**
109
+ * Build blocked response based on policy decision
110
+ */
111
+ declare function buildBlockedResponse(decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
112
+ /**
113
+ * Build redirect response based on policy decision
114
+ */
115
+ declare function buildRedirectResponse(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
116
+ /**
117
+ * Build challenge response (placeholder - future implementation)
118
+ */
119
+ declare function buildChallengeResponse(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
120
+ /**
121
+ * Handle policy decision and return appropriate response
122
+ */
123
+ declare function handlePolicyDecision(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): Promise<NextResponse | null>;
124
+ /**
125
+ * Get policy (local, fetched, or fallback)
126
+ */
127
+ declare function getPolicy(config: PolicyMiddlewareConfig): Promise<PolicyConfig>;
128
+ /**
129
+ * Apply policy to a detection result
130
+ *
131
+ * This function can be used standalone to evaluate policy after detection.
132
+ * Supports extended config with skipPaths and includePaths for path-based filtering.
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const result = await detector.analyze(context);
137
+ * const response = await applyPolicy(request, result, {
138
+ * policy: { thresholds: { confidenceThreshold: 80 } },
139
+ * skipPaths: ['/health', '/api/public/*'],
140
+ * includePaths: ['/api/*'],
141
+ * });
142
+ *
143
+ * if (response) {
144
+ * return response; // Policy blocked the request
145
+ * }
146
+ * ```
147
+ */
148
+ declare function applyPolicy(request: NextRequest, detection: DetectionResult, config: NextJSPolicyMiddlewareConfig): Promise<NextResponse | null>;
149
+
150
+ export { type NextJSPolicyMiddlewareConfig, type PolicyMiddlewareConfig, applyPolicy, buildBlockedResponse, buildChallengeResponse, buildRedirectResponse, createContextFromDetection, evaluatePolicyForDetection, getPolicy, handlePolicyDecision };
package/dist/policy.js ADDED
@@ -0,0 +1,185 @@
1
+ 'use strict';
2
+
3
+ var server = require('next/server');
4
+ var agentshieldShared = require('@kya-os/agentshield-shared');
5
+
6
+ // src/policy.ts
7
+ function createContextFromDetection(detection, request) {
8
+ return agentshieldShared.createEvaluationContext({
9
+ agentType: detection.detectedAgent?.type,
10
+ agentName: detection.detectedAgent?.name,
11
+ agentVendor: detection.detectedAgent?.vendor,
12
+ confidence: detection.confidence,
13
+ riskLevel: detection.riskLevel,
14
+ path: request.nextUrl.pathname,
15
+ method: request.method,
16
+ signatureVerified: detection.verificationMethod === "signature",
17
+ isAuthenticated: false,
18
+ // TODO: integrate with auth
19
+ userAgent: request.headers.get("user-agent") || void 0
20
+ });
21
+ }
22
+ function evaluatePolicyForDetection(detection, request, policy) {
23
+ const context = createContextFromDetection(detection, request);
24
+ return agentshieldShared.evaluatePolicy(policy, context);
25
+ }
26
+ function buildBlockedResponse(decision, config) {
27
+ const status = config.blockedResponse?.status ?? 403;
28
+ const message = config.blockedResponse?.message ?? decision.message ?? "Access denied";
29
+ const response = server.NextResponse.json(
30
+ {
31
+ error: message,
32
+ code: "POLICY_BLOCKED",
33
+ reason: decision.reason,
34
+ ruleId: decision.ruleId,
35
+ matchType: decision.matchType
36
+ },
37
+ { status }
38
+ );
39
+ if (config.blockedResponse?.headers) {
40
+ for (const [key, value] of Object.entries(config.blockedResponse.headers)) {
41
+ response.headers.set(key, value);
42
+ }
43
+ }
44
+ response.headers.set("X-AgentShield-Action", decision.action);
45
+ response.headers.set("X-AgentShield-Reason", decision.reason);
46
+ response.headers.set("X-AgentShield-MatchType", decision.matchType);
47
+ return response;
48
+ }
49
+ function buildRedirectResponse(request, decision, config) {
50
+ const redirectUrl = decision.redirectUrl || config.redirectUrl || "/blocked";
51
+ const url = new URL(redirectUrl, request.url);
52
+ url.searchParams.set("reason", decision.reason);
53
+ if (decision.ruleId) {
54
+ url.searchParams.set("ruleId", decision.ruleId);
55
+ }
56
+ return server.NextResponse.redirect(url);
57
+ }
58
+ function buildChallengeResponse(request, decision, config) {
59
+ return buildRedirectResponse(request, decision, config);
60
+ }
61
+ async function handlePolicyDecision(request, decision, config) {
62
+ switch (decision.action) {
63
+ case agentshieldShared.ENFORCEMENT_ACTIONS.BLOCK:
64
+ if (config.customBlockedResponse) {
65
+ return await config.customBlockedResponse(request, decision);
66
+ }
67
+ return buildBlockedResponse(decision, config);
68
+ case agentshieldShared.ENFORCEMENT_ACTIONS.REDIRECT:
69
+ return buildRedirectResponse(request, decision, config);
70
+ case agentshieldShared.ENFORCEMENT_ACTIONS.CHALLENGE:
71
+ return buildChallengeResponse(request, decision, config);
72
+ case agentshieldShared.ENFORCEMENT_ACTIONS.LOG:
73
+ console.log("[AgentShield] Policy decision (log):", {
74
+ path: request.nextUrl.pathname,
75
+ action: decision.action,
76
+ reason: decision.reason,
77
+ matchType: decision.matchType,
78
+ ruleId: decision.ruleId
79
+ });
80
+ return null;
81
+ // Continue to allow
82
+ case agentshieldShared.ENFORCEMENT_ACTIONS.ALLOW:
83
+ default:
84
+ return null;
85
+ }
86
+ }
87
+ var fetcherCache = /* @__PURE__ */ new Map();
88
+ function getFetcherCacheKey(config) {
89
+ return `${config.apiUrl ?? "default"}:${config.apiKey ?? ""}:${config.cacheTtlSeconds ?? "default"}`;
90
+ }
91
+ function getPolicyFetcher(config) {
92
+ if (!config) {
93
+ throw new Error("fetchPolicy config required");
94
+ }
95
+ const cacheKey = getFetcherCacheKey(config);
96
+ let fetcher = fetcherCache.get(cacheKey);
97
+ if (!fetcher) {
98
+ const fetcherConfig = {
99
+ apiBaseUrl: config.apiUrl || "https://kya.vouched.id",
100
+ apiKey: config.apiKey,
101
+ cacheTtlSeconds: config.cacheTtlSeconds
102
+ };
103
+ fetcher = agentshieldShared.createPolicyFetcher(fetcherConfig);
104
+ fetcherCache.set(cacheKey, fetcher);
105
+ }
106
+ return fetcher;
107
+ }
108
+ async function getPolicy(config) {
109
+ if (config.policy) {
110
+ return agentshieldShared.PolicyConfigSchema.parse({ ...agentshieldShared.DEFAULT_POLICY, ...config.policy });
111
+ }
112
+ if (config.fetchPolicy) {
113
+ try {
114
+ const fetcher = getPolicyFetcher(config.fetchPolicy);
115
+ return await fetcher.getPolicy(config.fetchPolicy.projectId);
116
+ } catch (error) {
117
+ if (config.debug) {
118
+ console.warn("[AgentShield] Policy fetch failed, using fallback:", error);
119
+ }
120
+ return agentshieldShared.PolicyConfigSchema.parse({
121
+ ...agentshieldShared.DEFAULT_POLICY,
122
+ ...config.fallbackPolicy || {}
123
+ });
124
+ }
125
+ }
126
+ return agentshieldShared.PolicyConfigSchema.parse(agentshieldShared.DEFAULT_POLICY);
127
+ }
128
+ async function applyPolicy(request, detection, config) {
129
+ try {
130
+ const path = request.nextUrl.pathname;
131
+ if (config.skipPaths?.some((pattern) => agentshieldShared.matchPath(path, pattern))) {
132
+ return null;
133
+ }
134
+ if (config.includePaths && config.includePaths.length > 0) {
135
+ if (!config.includePaths.some((pattern) => agentshieldShared.matchPath(path, pattern))) {
136
+ return null;
137
+ }
138
+ }
139
+ const policy = await getPolicy(config);
140
+ const context = createContextFromDetection(detection, request);
141
+ const decision = agentshieldShared.evaluatePolicy(policy, context);
142
+ if (config.onPolicyDecision) {
143
+ await config.onPolicyDecision(request, decision, context);
144
+ }
145
+ return await handlePolicyDecision(request, decision, config);
146
+ } catch (error) {
147
+ if (config.debug) {
148
+ console.error("[AgentShield] Policy evaluation error:", error);
149
+ }
150
+ if (config.failOpen !== false) {
151
+ return null;
152
+ }
153
+ return server.NextResponse.json(
154
+ { error: "Security check failed", code: "POLICY_ERROR" },
155
+ { status: 503 }
156
+ );
157
+ }
158
+ }
159
+
160
+ Object.defineProperty(exports, "DEFAULT_POLICY", {
161
+ enumerable: true,
162
+ get: function () { return agentshieldShared.DEFAULT_POLICY; }
163
+ });
164
+ Object.defineProperty(exports, "ENFORCEMENT_ACTIONS", {
165
+ enumerable: true,
166
+ get: function () { return agentshieldShared.ENFORCEMENT_ACTIONS; }
167
+ });
168
+ Object.defineProperty(exports, "createEvaluationContext", {
169
+ enumerable: true,
170
+ get: function () { return agentshieldShared.createEvaluationContext; }
171
+ });
172
+ Object.defineProperty(exports, "evaluatePolicy", {
173
+ enumerable: true,
174
+ get: function () { return agentshieldShared.evaluatePolicy; }
175
+ });
176
+ exports.applyPolicy = applyPolicy;
177
+ exports.buildBlockedResponse = buildBlockedResponse;
178
+ exports.buildChallengeResponse = buildChallengeResponse;
179
+ exports.buildRedirectResponse = buildRedirectResponse;
180
+ exports.createContextFromDetection = createContextFromDetection;
181
+ exports.evaluatePolicyForDetection = evaluatePolicyForDetection;
182
+ exports.getPolicy = getPolicy;
183
+ exports.handlePolicyDecision = handlePolicyDecision;
184
+ //# sourceMappingURL=policy.js.map
185
+ //# sourceMappingURL=policy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/policy.ts"],"names":["createEvaluationContext","evaluatePolicy","NextResponse","ENFORCEMENT_ACTIONS","createPolicyFetcher","PolicyConfigSchema","DEFAULT_POLICY","matchPath"],"mappings":";;;;;;AAwJO,SAAS,0BAAA,CACd,WACA,OAAA,EACyB;AACzB,EAAA,OAAOA,yCAAA,CAAwB;AAAA,IAC7B,SAAA,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":"policy.js","sourcesContent":["/**\n * Policy Integration for agentshield-nextjs\n *\n * This module provides policy evaluation support for the Next.js middleware.\n * It can use:\n * - Local policy configuration (static)\n * - Fetched policy from AgentShield API (dynamic with caching)\n * - Fallback/default policies\n *\n * @example\n * ```typescript\n * import { createPolicyMiddleware } from '@kya-os/agentshield-nextjs/policy';\n *\n * export default createPolicyMiddleware({\n * policy: {\n * enabled: true,\n * defaultAction: 'allow',\n * thresholds: { confidenceThreshold: 80, confidenceAction: 'block' },\n * allowList: [{ clientName: 'ChatGPT' }],\n * },\n * });\n * ```\n */\n\nimport { NextRequest, NextResponse } from 'next/server';\nimport {\n evaluatePolicy,\n createEvaluationContext,\n createPolicyFetcher,\n matchPath,\n PolicyFetcher,\n PolicyConfigSchema,\n ENFORCEMENT_ACTIONS,\n DEFAULT_POLICY,\n type PolicyConfig,\n type PolicyEvaluationContext,\n type PolicyEvaluationResult,\n type PolicyFetcherConfig,\n type DetectionResult,\n} from '@kya-os/agentshield-shared';\n\n// Re-export shared policy types for convenience\nexport {\n evaluatePolicy,\n createEvaluationContext,\n type PolicyConfig,\n type PolicyEvaluationContext,\n type PolicyEvaluationResult,\n ENFORCEMENT_ACTIONS,\n DEFAULT_POLICY,\n} from '@kya-os/agentshield-shared';\n\n// ============================================================================\n// Types\n// ============================================================================\n\n/**\n * Policy middleware configuration\n */\nexport interface PolicyMiddlewareConfig {\n /**\n * Local policy configuration (static)\n * If provided, this policy is used instead of fetching from API\n */\n policy?: Partial<PolicyConfig>;\n\n /**\n * Fetch policy from AgentShield API\n * Requires projectId and optionally an apiKey\n */\n fetchPolicy?: {\n /** Project ID to fetch policy for */\n projectId: string;\n /** API base URL (defaults to production) */\n apiUrl?: string;\n /** API key for authentication */\n apiKey?: string;\n /** Cache TTL in seconds (default: 300) */\n cacheTtlSeconds?: number;\n };\n\n /**\n * Fallback policy to use when fetch fails\n * Defaults to DEFAULT_POLICY (allow all)\n */\n fallbackPolicy?: Partial<PolicyConfig>;\n\n /**\n * Custom blocked response\n */\n blockedResponse?: {\n status?: number;\n message?: string;\n headers?: Record<string, string>;\n };\n\n /**\n * Default redirect URL for redirect actions\n */\n redirectUrl?: string;\n\n /**\n * Callback when policy decision is made\n */\n onPolicyDecision?: (\n request: NextRequest,\n decision: PolicyEvaluationResult,\n context: PolicyEvaluationContext\n ) => void | Promise<void>;\n\n /**\n * Custom response builder for blocked requests\n */\n customBlockedResponse?: (\n request: NextRequest,\n decision: PolicyEvaluationResult\n ) => NextResponse | Promise<NextResponse>;\n\n /**\n * Whether to fail open (allow) on policy evaluation errors\n * Default: true (recommended for production)\n */\n failOpen?: boolean;\n\n /**\n * Enable debug logging\n */\n debug?: boolean;\n}\n\n/**\n * Combined middleware configuration with policy support\n */\nexport interface NextJSPolicyMiddlewareConfig extends PolicyMiddlewareConfig {\n /**\n * Paths to skip (in addition to policy excludedPaths)\n */\n skipPaths?: string[];\n\n /**\n * Only enforce on these paths (overrides policy includedPaths)\n */\n includePaths?: string[];\n}\n\n// ============================================================================\n// Policy Evaluation Helper\n// ============================================================================\n\n/**\n * Create policy evaluation context from detection result and request\n */\nexport function createContextFromDetection(\n detection: DetectionResult,\n request: NextRequest\n): PolicyEvaluationContext {\n return createEvaluationContext({\n agentType: detection.detectedAgent?.type,\n agentName: detection.detectedAgent?.name,\n agentVendor: detection.detectedAgent?.vendor,\n confidence: detection.confidence,\n riskLevel: detection.riskLevel,\n path: request.nextUrl.pathname,\n method: request.method,\n signatureVerified: detection.verificationMethod === 'signature',\n isAuthenticated: false, // TODO: integrate with auth\n userAgent: request.headers.get('user-agent') || undefined,\n });\n}\n\n/**\n * Evaluate policy for a detection result\n */\nexport function evaluatePolicyForDetection(\n detection: DetectionResult,\n request: NextRequest,\n policy: PolicyConfig\n): PolicyEvaluationResult {\n const context = createContextFromDetection(detection, request);\n return evaluatePolicy(policy, context);\n}\n\n// ============================================================================\n// Response Builders\n// ============================================================================\n\n/**\n * Build blocked response based on policy decision\n */\nexport function buildBlockedResponse(\n decision: PolicyEvaluationResult,\n config: PolicyMiddlewareConfig\n): NextResponse {\n const status = config.blockedResponse?.status ?? 403;\n const message = config.blockedResponse?.message ?? decision.message ?? 'Access denied';\n\n const response = NextResponse.json(\n {\n error: message,\n code: 'POLICY_BLOCKED',\n reason: decision.reason,\n ruleId: decision.ruleId,\n matchType: decision.matchType,\n },\n { status }\n );\n\n // Add custom headers\n if (config.blockedResponse?.headers) {\n for (const [key, value] of Object.entries(config.blockedResponse.headers)) {\n response.headers.set(key, value);\n }\n }\n\n // Add AgentShield headers\n response.headers.set('X-AgentShield-Action', decision.action);\n response.headers.set('X-AgentShield-Reason', decision.reason);\n response.headers.set('X-AgentShield-MatchType', decision.matchType);\n\n return response;\n}\n\n/**\n * Build redirect response based on policy decision\n */\nexport function buildRedirectResponse(\n request: NextRequest,\n decision: PolicyEvaluationResult,\n config: PolicyMiddlewareConfig\n): NextResponse {\n const redirectUrl = decision.redirectUrl || config.redirectUrl || '/blocked';\n const url = new URL(redirectUrl, request.url);\n\n // Add query params with policy info\n url.searchParams.set('reason', decision.reason);\n if (decision.ruleId) {\n url.searchParams.set('ruleId', decision.ruleId);\n }\n\n return NextResponse.redirect(url);\n}\n\n/**\n * Build challenge response (placeholder - future implementation)\n */\nexport function buildChallengeResponse(\n request: NextRequest,\n decision: PolicyEvaluationResult,\n config: PolicyMiddlewareConfig\n): NextResponse {\n // For now, treat challenge as redirect\n // Future: implement CAPTCHA, proof-of-work, etc.\n return buildRedirectResponse(request, decision, config);\n}\n\n// ============================================================================\n// Policy Handler\n// ============================================================================\n\n/**\n * Handle policy decision and return appropriate response\n */\nexport async function handlePolicyDecision(\n request: NextRequest,\n decision: PolicyEvaluationResult,\n config: PolicyMiddlewareConfig\n): Promise<NextResponse | null> {\n switch (decision.action) {\n case ENFORCEMENT_ACTIONS.BLOCK:\n if (config.customBlockedResponse) {\n return await config.customBlockedResponse(request, decision);\n }\n return buildBlockedResponse(decision, config);\n\n case ENFORCEMENT_ACTIONS.REDIRECT:\n return buildRedirectResponse(request, decision, config);\n\n case ENFORCEMENT_ACTIONS.CHALLENGE:\n return buildChallengeResponse(request, decision, config);\n\n case ENFORCEMENT_ACTIONS.LOG:\n // LOG action always logs - that's its purpose\n // (debug flag controls verbose debugging output, not LOG action behavior)\n console.log('[AgentShield] Policy decision (log):', {\n path: request.nextUrl.pathname,\n action: decision.action,\n reason: decision.reason,\n matchType: decision.matchType,\n ruleId: decision.ruleId,\n });\n return null; // Continue to allow\n\n case ENFORCEMENT_ACTIONS.ALLOW:\n default:\n return null; // Continue\n }\n}\n\n// ============================================================================\n// Policy Fetcher Integration\n// ============================================================================\n\n// Cache fetchers by config to avoid recreating them, but also support\n// different configurations (different apiUrl, apiKey, etc.)\nconst fetcherCache = new Map<string, PolicyFetcher>();\n\n/**\n * Generate a cache key for fetcher config.\n * Uses ?? to distinguish between explicit 0 and undefined values.\n */\nfunction getFetcherCacheKey(config: NonNullable<PolicyMiddlewareConfig['fetchPolicy']>): string {\n return `${config.apiUrl ?? 'default'}:${config.apiKey ?? ''}:${config.cacheTtlSeconds ?? 'default'}`;\n}\n\n/**\n * Get or create policy fetcher for the given config\n */\nfunction getPolicyFetcher(config: PolicyMiddlewareConfig['fetchPolicy']): PolicyFetcher {\n if (!config) {\n throw new Error('fetchPolicy config required');\n }\n\n const cacheKey = getFetcherCacheKey(config);\n let fetcher = fetcherCache.get(cacheKey);\n\n if (!fetcher) {\n const fetcherConfig: PolicyFetcherConfig = {\n apiBaseUrl: config.apiUrl || 'https://kya.vouched.id',\n apiKey: config.apiKey,\n cacheTtlSeconds: config.cacheTtlSeconds,\n };\n fetcher = createPolicyFetcher(fetcherConfig);\n fetcherCache.set(cacheKey, fetcher);\n }\n\n return fetcher;\n}\n\n/**\n * Get policy (local, fetched, or fallback)\n */\nexport async function getPolicy(config: PolicyMiddlewareConfig): Promise<PolicyConfig> {\n // Use local policy if provided\n if (config.policy) {\n return PolicyConfigSchema.parse({ ...DEFAULT_POLICY, ...config.policy });\n }\n\n // Fetch from API if configured\n if (config.fetchPolicy) {\n try {\n const fetcher = getPolicyFetcher(config.fetchPolicy);\n return await fetcher.getPolicy(config.fetchPolicy.projectId);\n } catch (error) {\n if (config.debug) {\n console.warn('[AgentShield] Policy fetch failed, using fallback:', error);\n }\n // Return fallback policy\n return PolicyConfigSchema.parse({\n ...DEFAULT_POLICY,\n ...(config.fallbackPolicy || {}),\n });\n }\n }\n\n // No policy configured - return default (allow all)\n return PolicyConfigSchema.parse(DEFAULT_POLICY);\n}\n\n// ============================================================================\n// Standalone Policy Middleware\n// ============================================================================\n\n/**\n * Apply policy to a detection result\n *\n * This function can be used standalone to evaluate policy after detection.\n * Supports extended config with skipPaths and includePaths for path-based filtering.\n *\n * @example\n * ```typescript\n * const result = await detector.analyze(context);\n * const response = await applyPolicy(request, result, {\n * policy: { thresholds: { confidenceThreshold: 80 } },\n * skipPaths: ['/health', '/api/public/*'],\n * includePaths: ['/api/*'],\n * });\n *\n * if (response) {\n * return response; // Policy blocked the request\n * }\n * ```\n */\nexport async function applyPolicy(\n request: NextRequest,\n detection: DetectionResult,\n config: NextJSPolicyMiddlewareConfig\n): Promise<NextResponse | null> {\n try {\n const path = request.nextUrl.pathname;\n\n // Check skipPaths - if path matches any skip pattern, allow through\n if (config.skipPaths?.some((pattern) => matchPath(path, pattern))) {\n return null; // Skip policy enforcement for this path\n }\n\n // Check includePaths - if defined, path must match at least one pattern\n if (config.includePaths && config.includePaths.length > 0) {\n if (!config.includePaths.some((pattern) => matchPath(path, pattern))) {\n return null; // Path not in included paths, skip policy enforcement\n }\n }\n\n // Get policy\n const policy = await getPolicy(config);\n\n // Create context and evaluate\n const context = createContextFromDetection(detection, request);\n const decision = evaluatePolicy(policy, context);\n\n // Call decision callback if provided\n if (config.onPolicyDecision) {\n await config.onPolicyDecision(request, decision, context);\n }\n\n // Handle decision\n return await handlePolicyDecision(request, decision, config);\n } catch (error) {\n if (config.debug) {\n console.error('[AgentShield] Policy evaluation error:', error);\n }\n\n if (config.failOpen !== false) {\n return null; // Allow on error\n }\n\n // Fail closed\n return NextResponse.json(\n { error: 'Security check failed', code: 'POLICY_ERROR' },\n { status: 503 }\n );\n }\n}\n"]}