@kya-os/checkpoint-nextjs 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +77 -0
- package/dist/adapt.d.mts +6 -34
- package/dist/adapt.d.mts.map +1 -0
- package/dist/adapt.d.ts +6 -34
- package/dist/adapt.d.ts.map +1 -0
- package/dist/adapt.js +78 -51
- package/dist/adapt.mjs +77 -48
- package/dist/adapt.mjs.map +1 -0
- package/dist/api-client.d.mts +150 -159
- package/dist/api-client.d.mts.map +1 -0
- package/dist/api-client.d.ts +150 -159
- package/dist/api-client.d.ts.map +1 -0
- package/dist/api-client.js +218 -204
- package/dist/api-client.mjs +217 -201
- package/dist/api-client.mjs.map +1 -0
- package/dist/api-middleware-CDSh3TSE.js +379 -0
- package/dist/api-middleware-CMDRoQik.mjs +357 -0
- package/dist/api-middleware-CMDRoQik.mjs.map +1 -0
- package/dist/api-middleware.d.mts +117 -131
- package/dist/api-middleware.d.mts.map +1 -0
- package/dist/api-middleware.d.ts +117 -131
- package/dist/api-middleware.d.ts.map +1 -0
- package/dist/api-middleware.js +8 -527
- package/dist/api-middleware.mjs +3 -521
- package/dist/composed-policy-BCaNiVHv.d.mts +98 -0
- package/dist/composed-policy-BCaNiVHv.d.mts.map +1 -0
- package/dist/composed-policy-zzJYNFTb.d.ts +98 -0
- package/dist/composed-policy-zzJYNFTb.d.ts.map +1 -0
- package/dist/composed-policy.d.mts +2 -115
- package/dist/composed-policy.d.ts +2 -115
- package/dist/composed-policy.js +122 -89
- package/dist/composed-policy.mjs +121 -86
- package/dist/composed-policy.mjs.map +1 -0
- package/dist/config-CbAg2mze.d.ts +255 -0
- package/dist/config-CbAg2mze.d.ts.map +1 -0
- package/dist/config-Doe0Tn53.d.mts +255 -0
- package/dist/config-Doe0Tn53.d.mts.map +1 -0
- package/dist/create-middleware.d.mts +7 -18
- package/dist/create-middleware.d.mts.map +1 -0
- package/dist/create-middleware.d.ts +7 -18
- package/dist/create-middleware.d.ts.map +1 -0
- package/dist/create-middleware.js +36 -31
- package/dist/create-middleware.mjs +35 -28
- package/dist/create-middleware.mjs.map +1 -0
- package/dist/edge/index.d.mts +64 -88
- package/dist/edge/index.d.mts.map +1 -0
- package/dist/edge/index.d.ts +64 -88
- package/dist/edge/index.d.ts.map +1 -0
- package/dist/edge/index.js +349 -264
- package/dist/edge/index.mjs +348 -261
- package/dist/edge/index.mjs.map +1 -0
- package/dist/edge-runtime-loader.d.mts +37 -65
- package/dist/edge-runtime-loader.d.mts.map +1 -0
- package/dist/edge-runtime-loader.d.ts +37 -65
- package/dist/edge-runtime-loader.d.ts.map +1 -0
- package/dist/edge-runtime-loader.js +268 -213
- package/dist/edge-runtime-loader.mjs +267 -210
- package/dist/edge-runtime-loader.mjs.map +1 -0
- package/dist/edge-wasm-middleware.d.mts +27 -46
- package/dist/edge-wasm-middleware.d.mts.map +1 -0
- package/dist/edge-wasm-middleware.d.ts +27 -46
- package/dist/edge-wasm-middleware.d.ts.map +1 -0
- package/dist/edge-wasm-middleware.js +26 -15
- package/dist/edge-wasm-middleware.mjs +25 -12
- package/dist/edge-wasm-middleware.mjs.map +1 -0
- package/dist/index.d.mts +10 -17
- package/dist/index.d.ts +10 -17
- package/dist/index.js +55 -1265
- package/dist/index.mjs +16 -1234
- package/dist/index.mjs.map +1 -0
- package/dist/middleware-bundler.d.mts +6 -12
- package/dist/middleware-bundler.d.mts.map +1 -0
- package/dist/middleware-bundler.d.ts +6 -12
- package/dist/middleware-bundler.d.ts.map +1 -0
- package/dist/middleware-bundler.js +12 -350
- package/dist/middleware-bundler.mjs +11 -347
- package/dist/middleware-bundler.mjs.map +1 -0
- package/dist/middleware-core-BYQP9FND.mjs +165 -0
- package/dist/middleware-core-BYQP9FND.mjs.map +1 -0
- package/dist/middleware-core-D6TijE6P.js +175 -0
- package/dist/middleware-edge.d.mts +8 -14
- package/dist/middleware-edge.d.mts.map +1 -0
- package/dist/middleware-edge.d.ts +8 -14
- package/dist/middleware-edge.d.ts.map +1 -0
- package/dist/middleware-edge.js +41 -355
- package/dist/middleware-edge.mjs +39 -347
- package/dist/middleware-edge.mjs.map +1 -0
- package/dist/middleware-node-CE4GevYs.d.ts +20 -0
- package/dist/middleware-node-CE4GevYs.d.ts.map +1 -0
- package/dist/middleware-node-DtyOuPta.d.mts +20 -0
- package/dist/middleware-node-DtyOuPta.d.mts.map +1 -0
- package/dist/middleware-node-context-BqPbf-fI.mjs +34 -0
- package/dist/middleware-node-context-BqPbf-fI.mjs.map +1 -0
- package/dist/middleware-node-context-rgnc7UNR.js +38 -0
- package/dist/middleware-node.d.mts +3 -27
- package/dist/middleware-node.d.ts +3 -27
- package/dist/middleware-node.js +13 -351
- package/dist/middleware-node.mjs +11 -347
- package/dist/middleware-node.mjs.map +1 -0
- package/dist/middleware.d.mts +8 -28
- package/dist/middleware.d.mts.map +1 -0
- package/dist/middleware.d.ts +8 -28
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +43 -9
- package/dist/middleware.mjs +42 -6
- package/dist/middleware.mjs.map +1 -0
- package/dist/nodejs-wasm-loader.d.mts +8 -7
- package/dist/nodejs-wasm-loader.d.mts.map +1 -0
- package/dist/nodejs-wasm-loader.d.ts +8 -7
- package/dist/nodejs-wasm-loader.d.ts.map +1 -0
- package/dist/nodejs-wasm-loader.js +53 -19
- package/dist/nodejs-wasm-loader.mjs +52 -16
- package/dist/nodejs-wasm-loader.mjs.map +1 -0
- package/dist/policy.d.mts +86 -110
- package/dist/policy.d.mts.map +1 -0
- package/dist/policy.d.ts +86 -110
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +203 -165
- package/dist/policy.mjs +183 -152
- package/dist/policy.mjs.map +1 -0
- package/dist/session-tracker.d.mts +42 -47
- package/dist/session-tracker.d.mts.map +1 -0
- package/dist/session-tracker.d.ts +42 -47
- package/dist/session-tracker.d.ts.map +1 -0
- package/dist/session-tracker.js +141 -165
- package/dist/session-tracker.mjs +140 -162
- package/dist/session-tracker.mjs.map +1 -0
- package/dist/translate.d.mts +17 -32
- package/dist/translate.d.mts.map +1 -0
- package/dist/translate.d.ts +17 -32
- package/dist/translate.d.ts.map +1 -0
- package/dist/translate.js +69 -34
- package/dist/translate.mjs +68 -31
- package/dist/translate.mjs.map +1 -0
- package/dist/types-C0SkqLMM.d.mts +102 -0
- package/dist/types-C0SkqLMM.d.mts.map +1 -0
- package/dist/types-C7DNSCTO.d.ts +102 -0
- package/dist/types-C7DNSCTO.d.ts.map +1 -0
- package/dist/utils-Cj4XNhIY.mjs +35 -0
- package/dist/utils-Cj4XNhIY.mjs.map +1 -0
- package/dist/utils-DAisnYnI.js +45 -0
- package/dist/wasm-setup.d.mts +6 -5
- package/dist/wasm-setup.d.mts.map +1 -0
- package/dist/wasm-setup.d.ts +6 -5
- package/dist/wasm-setup.d.ts.map +1 -0
- package/dist/wasm-setup.js +82 -113
- package/dist/wasm-setup.mjs +85 -108
- package/dist/wasm-setup.mjs.map +1 -0
- package/package.json +8 -8
- package/dist/config-CleTeeoW.d.mts +0 -233
- package/dist/config-XMOIkxFN.d.ts +0 -233
- package/dist/types-Chgi5TFC.d.mts +0 -105
- package/dist/types-Chgi5TFC.d.ts +0 -105
package/dist/policy.d.ts
CHANGED
|
@@ -1,142 +1,117 @@
|
|
|
1
|
-
import { NextRequest, NextResponse } from
|
|
2
|
-
import { PolicyConfig,
|
|
3
|
-
|
|
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/checkpoint-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
|
-
|
|
1
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
import { DEFAULT_POLICY, DetectionResult, ENFORCEMENT_ACTIONS, PolicyConfig, PolicyConfig as PolicyConfig$1, PolicyEvaluationContext, PolicyEvaluationContext as PolicyEvaluationContext$1, PolicyEvaluationResult, PolicyEvaluationResult as PolicyEvaluationResult$1, createEvaluationContext, evaluatePolicy } from "@kya-os/checkpoint-shared";
|
|
3
|
+
//#region src/policy.d.ts
|
|
29
4
|
/**
|
|
30
5
|
* Policy middleware configuration
|
|
31
6
|
*/
|
|
32
|
-
interface PolicyMiddlewareConfig {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
7
|
+
export interface PolicyMiddlewareConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Local policy configuration (static)
|
|
10
|
+
* If provided, this policy is used instead of fetching from API
|
|
11
|
+
*/
|
|
12
|
+
policy?: Partial<PolicyConfig$1>;
|
|
13
|
+
/**
|
|
14
|
+
* Fetch policy from AgentShield API
|
|
15
|
+
* Requires projectId and optionally an apiKey
|
|
16
|
+
*/
|
|
17
|
+
fetchPolicy?: {
|
|
18
|
+
/** Project ID to fetch policy for */
|
|
19
|
+
projectId: string;
|
|
20
|
+
/** API base URL (defaults to production) */
|
|
21
|
+
apiUrl?: string;
|
|
22
|
+
/** API key for authentication */
|
|
23
|
+
apiKey?: string;
|
|
24
|
+
/** Cache TTL in seconds (default: 300) */
|
|
25
|
+
cacheTtlSeconds?: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Fallback policy to use when fetch fails
|
|
29
|
+
* Defaults to DEFAULT_POLICY (allow all)
|
|
30
|
+
*/
|
|
31
|
+
fallbackPolicy?: Partial<PolicyConfig$1>;
|
|
32
|
+
/**
|
|
33
|
+
* Custom blocked response
|
|
34
|
+
*/
|
|
35
|
+
blockedResponse?: {
|
|
36
|
+
status?: number;
|
|
37
|
+
message?: string;
|
|
38
|
+
headers?: Record<string, string>;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Default redirect URL for redirect actions
|
|
42
|
+
*/
|
|
43
|
+
redirectUrl?: string;
|
|
44
|
+
/**
|
|
45
|
+
* Callback when policy decision is made
|
|
46
|
+
*/
|
|
47
|
+
onPolicyDecision?: (request: NextRequest, decision: PolicyEvaluationResult$1, context: PolicyEvaluationContext$1) => void | Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Custom response builder for blocked requests
|
|
50
|
+
*/
|
|
51
|
+
customBlockedResponse?: (request: NextRequest, decision: PolicyEvaluationResult$1) => NextResponse | Promise<NextResponse>;
|
|
52
|
+
/**
|
|
53
|
+
* Whether to fail open (allow) on policy evaluation errors
|
|
54
|
+
* Default: true (recommended for production)
|
|
55
|
+
*/
|
|
56
|
+
failOpen?: boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Enable debug logging
|
|
59
|
+
*/
|
|
60
|
+
debug?: boolean;
|
|
86
61
|
}
|
|
87
62
|
/**
|
|
88
63
|
* Combined middleware configuration with policy support
|
|
89
64
|
*/
|
|
90
|
-
interface NextJSPolicyMiddlewareConfig extends PolicyMiddlewareConfig {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
65
|
+
export interface NextJSPolicyMiddlewareConfig extends PolicyMiddlewareConfig {
|
|
66
|
+
/**
|
|
67
|
+
* Paths to skip (in addition to policy excludedPaths)
|
|
68
|
+
*/
|
|
69
|
+
skipPaths?: string[];
|
|
70
|
+
/**
|
|
71
|
+
* Only enforce on these paths (overrides policy includedPaths)
|
|
72
|
+
*/
|
|
73
|
+
includePaths?: string[];
|
|
99
74
|
}
|
|
100
75
|
/**
|
|
101
76
|
* Create policy evaluation context from detection result and request
|
|
102
77
|
*/
|
|
103
|
-
declare function createContextFromDetection(detection: DetectionResult, request: NextRequest): PolicyEvaluationContext;
|
|
78
|
+
export declare function createContextFromDetection(detection: DetectionResult, request: NextRequest): PolicyEvaluationContext$1;
|
|
104
79
|
/**
|
|
105
80
|
* Evaluate policy for a detection result
|
|
106
81
|
*/
|
|
107
|
-
declare function evaluatePolicyForDetection(detection: DetectionResult, request: NextRequest, policy: PolicyConfig): PolicyEvaluationResult;
|
|
82
|
+
export declare function evaluatePolicyForDetection(detection: DetectionResult, request: NextRequest, policy: PolicyConfig$1): PolicyEvaluationResult$1;
|
|
108
83
|
/**
|
|
109
84
|
* Build blocked response based on policy decision
|
|
110
85
|
*/
|
|
111
|
-
declare function buildBlockedResponse(decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig): NextResponse;
|
|
86
|
+
export declare function buildBlockedResponse(decision: PolicyEvaluationResult$1, config: PolicyMiddlewareConfig): NextResponse;
|
|
112
87
|
/**
|
|
113
88
|
* Build redirect response based on policy decision
|
|
114
89
|
*/
|
|
115
|
-
declare function buildRedirectResponse(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig, detection?: {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
90
|
+
export declare function buildRedirectResponse(request: NextRequest, decision: PolicyEvaluationResult$1, config: PolicyMiddlewareConfig, detection?: {
|
|
91
|
+
detectedAgent?: {
|
|
92
|
+
name?: string;
|
|
93
|
+
};
|
|
119
94
|
}): NextResponse;
|
|
120
95
|
/**
|
|
121
96
|
* Build challenge response (placeholder - future implementation)
|
|
122
97
|
*/
|
|
123
|
-
declare function buildChallengeResponse(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig, detection?: {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
98
|
+
export declare function buildChallengeResponse(request: NextRequest, decision: PolicyEvaluationResult$1, config: PolicyMiddlewareConfig, detection?: {
|
|
99
|
+
detectedAgent?: {
|
|
100
|
+
name?: string;
|
|
101
|
+
};
|
|
127
102
|
}): NextResponse;
|
|
128
103
|
/**
|
|
129
104
|
* Handle policy decision and return appropriate response
|
|
130
105
|
*/
|
|
131
|
-
declare function handlePolicyDecision(request: NextRequest, decision: PolicyEvaluationResult, config: PolicyMiddlewareConfig, detection?: {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
106
|
+
export declare function handlePolicyDecision(request: NextRequest, decision: PolicyEvaluationResult$1, config: PolicyMiddlewareConfig, detection?: {
|
|
107
|
+
detectedAgent?: {
|
|
108
|
+
name?: string;
|
|
109
|
+
};
|
|
135
110
|
}): Promise<NextResponse | null>;
|
|
136
111
|
/**
|
|
137
112
|
* Get policy (local, fetched, or fallback)
|
|
138
113
|
*/
|
|
139
|
-
declare function getPolicy(config: PolicyMiddlewareConfig): Promise<PolicyConfig>;
|
|
114
|
+
export declare function getPolicy(config: PolicyMiddlewareConfig): Promise<PolicyConfig$1>;
|
|
140
115
|
/**
|
|
141
116
|
* Apply policy to a detection result
|
|
142
117
|
*
|
|
@@ -157,6 +132,7 @@ declare function getPolicy(config: PolicyMiddlewareConfig): Promise<PolicyConfig
|
|
|
157
132
|
* }
|
|
158
133
|
* ```
|
|
159
134
|
*/
|
|
160
|
-
declare function applyPolicy(request: NextRequest, detection: DetectionResult, config: NextJSPolicyMiddlewareConfig): Promise<NextResponse | null>;
|
|
161
|
-
|
|
162
|
-
export {
|
|
135
|
+
export declare function applyPolicy(request: NextRequest, detection: DetectionResult, config: NextJSPolicyMiddlewareConfig): Promise<NextResponse | null>;
|
|
136
|
+
//#endregion
|
|
137
|
+
export { DEFAULT_POLICY, ENFORCEMENT_ACTIONS, type PolicyConfig, type PolicyEvaluationContext, type PolicyEvaluationResult, createEvaluationContext, evaluatePolicy };
|
|
138
|
+
//# sourceMappingURL=policy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.d.ts","names":[],"sources":["../src/policy.ts"],"mappings":";;;;;;iBA2DiB;;;;;EAKf,SAAS,QAAQ;;;;;EAMjB;;IAEE;;IAEA;;IAEA;;IAEA;;;;;;EAOF,iBAAiB,QAAQ;;;;EAKzB;IACE;IACA;IACA,UAAU;;;;;EAMZ;;;;EAKA,oBACE,SAAS,aACT,UAAU,0BACV,SAAS,qCACC;;;;EAKZ,yBACE,SAAS,aACT,UAAU,6BACP,eAAe,QAAQ;;;;;EAM5B;;;;EAKA;;;;;iBAMe,qCAAqC;;;;EAIpD;;;;EAKA;;;;;wBAUc,2BACd,WAAW,iBACX,SAAS,cACR;;;;wBA8Ba,2BACd,WAAW,iBACX,SAAS,aACT,QAAQ,iBACP;;;;wBAYa,qBACd,UAAU,0BACV,QAAQ,yBACP;;;;wBAiCa,sBACd,SAAS,aACT,UAAU,0BACV,QAAQ,wBACR;EAAc;IAAkB;;IAC/B;;;;wBAoBa,uBACd,SAAS,aACT,UAAU,0BACV,QAAQ,wBACR;EAAc;IAAkB;;IAC/B;;;;wBAWmB,qBACpB,SAAS,aACT,UAAU,0BACV,QAAQ,wBACR;EAAc;IAAkB;;IAC/B,QAAQ;;;;wBA2EW,UAAU,QAAQ,yBAAyB,QAAQ;;;;;;;;;;;;;;;;;;;;;wBAmDnD,YACpB,SAAS,aACT,WAAW,iBACX,QAAQ,+BACP,QAAQ"}
|
package/dist/policy.js
CHANGED
|
@@ -1,198 +1,236 @@
|
|
|
1
|
-
'
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _kya_os_checkpoint_shared = require("@kya-os/checkpoint-shared");
|
|
3
|
+
let next_server = require("next/server");
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
//#region src/policy.ts
|
|
6
|
+
/**
|
|
7
|
+
* Policy Integration for agentshield-nextjs
|
|
8
|
+
*
|
|
9
|
+
* This module provides policy evaluation support for the Next.js middleware.
|
|
10
|
+
* It can use:
|
|
11
|
+
* - Local policy configuration (static)
|
|
12
|
+
* - Fetched policy from AgentShield API (dynamic with caching)
|
|
13
|
+
* - Fallback/default policies
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { createPolicyMiddleware } from '@kya-os/checkpoint-nextjs/policy';
|
|
18
|
+
*
|
|
19
|
+
* export default createPolicyMiddleware({
|
|
20
|
+
* policy: {
|
|
21
|
+
* enabled: true,
|
|
22
|
+
* defaultAction: 'allow',
|
|
23
|
+
* thresholds: { confidenceThreshold: 80, confidenceAction: 'block' },
|
|
24
|
+
* allowList: [{ clientName: 'ChatGPT' }],
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Create policy evaluation context from detection result and request
|
|
31
|
+
*/
|
|
7
32
|
function createContextFromDetection(detection, request) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
// are the draft-kya-http-02 §4.3 request-signature methods.
|
|
21
|
-
signatureVerified: detection.verificationMethod === "signature" || detection.verificationMethod === "tier1_rfc9421" || detection.verificationMethod === "kya-http" || detection.verificationMethod === "kya-http-delegated",
|
|
22
|
-
isAuthenticated: false,
|
|
23
|
-
// TODO: integrate with auth
|
|
24
|
-
userAgent: request.headers.get("user-agent") || void 0
|
|
25
|
-
});
|
|
33
|
+
return (0, _kya_os_checkpoint_shared.createEvaluationContext)({
|
|
34
|
+
agentType: detection.detectedAgent?.type,
|
|
35
|
+
agentName: detection.detectedAgent?.name,
|
|
36
|
+
agentVendor: detection.detectedAgent?.vendor,
|
|
37
|
+
confidence: detection.confidence,
|
|
38
|
+
riskLevel: detection.riskLevel,
|
|
39
|
+
path: request.nextUrl.pathname,
|
|
40
|
+
method: request.method,
|
|
41
|
+
signatureVerified: detection.verificationMethod === "signature" || detection.verificationMethod === "tier1_rfc9421" || detection.verificationMethod === "kya-http" || detection.verificationMethod === "kya-http-delegated" || detection.verificationMethod === "a2a",
|
|
42
|
+
isAuthenticated: false,
|
|
43
|
+
userAgent: request.headers.get("user-agent") || void 0
|
|
44
|
+
});
|
|
26
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Evaluate policy for a detection result
|
|
48
|
+
*/
|
|
27
49
|
function evaluatePolicyForDetection(detection, request, policy) {
|
|
28
|
-
|
|
29
|
-
|
|
50
|
+
const context = createContextFromDetection(detection, request);
|
|
51
|
+
return (0, _kya_os_checkpoint_shared.evaluatePolicy)(policy, context);
|
|
30
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Build blocked response based on policy decision
|
|
55
|
+
*/
|
|
31
56
|
function buildBlockedResponse(decision, config) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
response.headers.set(key, value);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
response.headers.set("KYA-Action", decision.action);
|
|
50
|
-
response.headers.set("KYA-Reason", decision.reason);
|
|
51
|
-
response.headers.set("KYA-Match-Type", decision.matchType);
|
|
52
|
-
return response;
|
|
57
|
+
const status = config.blockedResponse?.status ?? 403;
|
|
58
|
+
const message = config.blockedResponse?.message ?? decision.message ?? "Access denied";
|
|
59
|
+
const response = next_server.NextResponse.json({
|
|
60
|
+
error: message,
|
|
61
|
+
code: "POLICY_BLOCKED",
|
|
62
|
+
reason: decision.reason,
|
|
63
|
+
ruleId: decision.ruleId,
|
|
64
|
+
matchType: decision.matchType
|
|
65
|
+
}, { status });
|
|
66
|
+
if (config.blockedResponse?.headers) for (const [key, value] of Object.entries(config.blockedResponse.headers)) response.headers.set(key, value);
|
|
67
|
+
response.headers.set("KYA-Action", decision.action);
|
|
68
|
+
response.headers.set("KYA-Reason", decision.reason);
|
|
69
|
+
response.headers.set("KYA-Match-Type", decision.matchType);
|
|
70
|
+
return response;
|
|
53
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Build redirect response based on policy decision
|
|
74
|
+
*/
|
|
54
75
|
function buildRedirectResponse(request, decision, config, detection) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (agentName && !url.searchParams.has("agent")) {
|
|
63
|
-
url.searchParams.set("agent", agentName.toLowerCase());
|
|
64
|
-
}
|
|
65
|
-
return server.NextResponse.redirect(url);
|
|
76
|
+
const redirectUrl = decision.redirectUrl || config.redirectUrl || "/blocked";
|
|
77
|
+
const url = new URL(redirectUrl, request.url);
|
|
78
|
+
url.searchParams.set("reason", decision.reason);
|
|
79
|
+
if (decision.ruleId) url.searchParams.set("ruleId", decision.ruleId);
|
|
80
|
+
const agentName = detection?.detectedAgent?.name;
|
|
81
|
+
if (agentName && !url.searchParams.has("agent")) url.searchParams.set("agent", agentName.toLowerCase());
|
|
82
|
+
return next_server.NextResponse.redirect(url);
|
|
66
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Build challenge response (placeholder - future implementation)
|
|
86
|
+
*/
|
|
67
87
|
function buildChallengeResponse(request, decision, config, detection) {
|
|
68
|
-
|
|
88
|
+
return buildRedirectResponse(request, decision, config, detection);
|
|
69
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Handle policy decision and return appropriate response
|
|
92
|
+
*/
|
|
70
93
|
async function handlePolicyDecision(request, decision, config, detection) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return null;
|
|
90
|
-
// Continue to allow
|
|
91
|
-
case checkpointShared.ENFORCEMENT_ACTIONS.ALLOW:
|
|
92
|
-
default:
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
94
|
+
switch (decision.action) {
|
|
95
|
+
case _kya_os_checkpoint_shared.ENFORCEMENT_ACTIONS.BLOCK:
|
|
96
|
+
if (config.customBlockedResponse) return await config.customBlockedResponse(request, decision);
|
|
97
|
+
return buildBlockedResponse(decision, config);
|
|
98
|
+
case _kya_os_checkpoint_shared.ENFORCEMENT_ACTIONS.REDIRECT: return buildRedirectResponse(request, decision, config, detection);
|
|
99
|
+
case _kya_os_checkpoint_shared.ENFORCEMENT_ACTIONS.CHALLENGE: return buildChallengeResponse(request, decision, config, detection);
|
|
100
|
+
case _kya_os_checkpoint_shared.ENFORCEMENT_ACTIONS.LOG:
|
|
101
|
+
console.log("[Checkpoint] Policy decision (log):", {
|
|
102
|
+
path: request.nextUrl.pathname,
|
|
103
|
+
action: decision.action,
|
|
104
|
+
reason: decision.reason,
|
|
105
|
+
matchType: decision.matchType,
|
|
106
|
+
ruleId: decision.ruleId
|
|
107
|
+
});
|
|
108
|
+
return null;
|
|
109
|
+
case _kya_os_checkpoint_shared.ENFORCEMENT_ACTIONS.ALLOW:
|
|
110
|
+
default: return null;
|
|
111
|
+
}
|
|
95
112
|
}
|
|
96
|
-
|
|
113
|
+
const fetcherCache = /* @__PURE__ */ new Map();
|
|
114
|
+
/**
|
|
115
|
+
* Generate a cache key for fetcher config.
|
|
116
|
+
* Uses ?? to distinguish between explicit 0 and undefined values.
|
|
117
|
+
*/
|
|
97
118
|
function getFetcherCacheKey(config) {
|
|
98
|
-
|
|
119
|
+
return `${config.apiUrl ?? "default"}:${config.apiKey ?? ""}:${config.cacheTtlSeconds ?? "default"}`;
|
|
99
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Get or create policy fetcher for the given config
|
|
123
|
+
*/
|
|
100
124
|
function getPolicyFetcher(config) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
return fetcher;
|
|
125
|
+
if (!config) throw new Error("fetchPolicy config required");
|
|
126
|
+
const cacheKey = getFetcherCacheKey(config);
|
|
127
|
+
let fetcher = fetcherCache.get(cacheKey);
|
|
128
|
+
if (!fetcher) {
|
|
129
|
+
const fetcherConfig = {
|
|
130
|
+
apiBaseUrl: config.apiUrl || "https://kya.vouched.id",
|
|
131
|
+
apiKey: config.apiKey,
|
|
132
|
+
cacheTtlSeconds: config.cacheTtlSeconds
|
|
133
|
+
};
|
|
134
|
+
fetcher = (0, _kya_os_checkpoint_shared.createPolicyFetcher)(fetcherConfig);
|
|
135
|
+
fetcherCache.set(cacheKey, fetcher);
|
|
136
|
+
}
|
|
137
|
+
return fetcher;
|
|
116
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Get policy (local, fetched, or fallback)
|
|
141
|
+
*/
|
|
117
142
|
async function getPolicy(config) {
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return checkpointShared.PolicyConfigSchema.parse(checkpointShared.DEFAULT_POLICY);
|
|
143
|
+
if (config.policy) return _kya_os_checkpoint_shared.PolicyConfigSchema.parse({
|
|
144
|
+
..._kya_os_checkpoint_shared.DEFAULT_POLICY,
|
|
145
|
+
...config.policy
|
|
146
|
+
});
|
|
147
|
+
if (config.fetchPolicy) try {
|
|
148
|
+
return await getPolicyFetcher(config.fetchPolicy).getPolicy(config.fetchPolicy.projectId);
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (config.debug) console.warn("[Checkpoint] Policy fetch failed, using fallback:", error);
|
|
151
|
+
return _kya_os_checkpoint_shared.PolicyConfigSchema.parse({
|
|
152
|
+
..._kya_os_checkpoint_shared.DEFAULT_POLICY,
|
|
153
|
+
...config.fallbackPolicy || {}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return _kya_os_checkpoint_shared.PolicyConfigSchema.parse(_kya_os_checkpoint_shared.DEFAULT_POLICY);
|
|
136
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* Apply policy to a detection result
|
|
160
|
+
*
|
|
161
|
+
* This function can be used standalone to evaluate policy after detection.
|
|
162
|
+
* Supports extended config with skipPaths and includePaths for path-based filtering.
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```typescript
|
|
166
|
+
* const result = await detector.analyze(context);
|
|
167
|
+
* const response = await applyPolicy(request, result, {
|
|
168
|
+
* policy: { thresholds: { confidenceThreshold: 80 } },
|
|
169
|
+
* skipPaths: ['/health', '/api/public/*'],
|
|
170
|
+
* includePaths: ['/api/*'],
|
|
171
|
+
* });
|
|
172
|
+
*
|
|
173
|
+
* if (response) {
|
|
174
|
+
* return response; // Policy blocked the request
|
|
175
|
+
* }
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
137
178
|
async function applyPolicy(request, detection, config) {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
} catch (error) {
|
|
162
|
-
if (config.debug) {
|
|
163
|
-
console.error("[Checkpoint] Policy evaluation error:", error);
|
|
164
|
-
}
|
|
165
|
-
if (config.failOpen !== false) {
|
|
166
|
-
return null;
|
|
167
|
-
}
|
|
168
|
-
return server.NextResponse.json(
|
|
169
|
-
{ error: "Security check failed", code: "POLICY_ERROR" },
|
|
170
|
-
{ status: 503 }
|
|
171
|
-
);
|
|
172
|
-
}
|
|
179
|
+
try {
|
|
180
|
+
const path = request.nextUrl.pathname;
|
|
181
|
+
if (config.skipPaths?.some((pattern) => (0, _kya_os_checkpoint_shared.matchPath)(path, pattern))) return null;
|
|
182
|
+
if (config.includePaths && config.includePaths.length > 0) {
|
|
183
|
+
if (!config.includePaths.some((pattern) => (0, _kya_os_checkpoint_shared.matchPath)(path, pattern))) return null;
|
|
184
|
+
}
|
|
185
|
+
const policy = await getPolicy(config);
|
|
186
|
+
const context = createContextFromDetection(detection, request);
|
|
187
|
+
const decision = (0, _kya_os_checkpoint_shared.evaluatePolicy)(policy, context);
|
|
188
|
+
if (config.onPolicyDecision) try {
|
|
189
|
+
await config.onPolicyDecision(request, decision, context);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (config.debug) console.error("[Checkpoint] onPolicyDecision callback failed:", error);
|
|
192
|
+
}
|
|
193
|
+
return await handlePolicyDecision(request, decision, config, detection);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
if (config.debug) console.error("[Checkpoint] Policy evaluation error:", error);
|
|
196
|
+
if (config.failOpen !== false) return null;
|
|
197
|
+
return next_server.NextResponse.json({
|
|
198
|
+
error: "Security check failed",
|
|
199
|
+
code: "POLICY_ERROR"
|
|
200
|
+
}, { status: 503 });
|
|
201
|
+
}
|
|
173
202
|
}
|
|
174
203
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
get: function () { return checkpointShared.DEFAULT_POLICY; }
|
|
178
|
-
});
|
|
179
|
-
Object.defineProperty(exports, "ENFORCEMENT_ACTIONS", {
|
|
180
|
-
enumerable: true,
|
|
181
|
-
get: function () { return checkpointShared.ENFORCEMENT_ACTIONS; }
|
|
182
|
-
});
|
|
183
|
-
Object.defineProperty(exports, "createEvaluationContext", {
|
|
204
|
+
//#endregion
|
|
205
|
+
Object.defineProperty(exports, 'DEFAULT_POLICY', {
|
|
184
206
|
enumerable: true,
|
|
185
|
-
get: function () {
|
|
207
|
+
get: function () {
|
|
208
|
+
return _kya_os_checkpoint_shared.DEFAULT_POLICY;
|
|
209
|
+
}
|
|
186
210
|
});
|
|
187
|
-
Object.defineProperty(exports,
|
|
211
|
+
Object.defineProperty(exports, 'ENFORCEMENT_ACTIONS', {
|
|
188
212
|
enumerable: true,
|
|
189
|
-
get: function () {
|
|
213
|
+
get: function () {
|
|
214
|
+
return _kya_os_checkpoint_shared.ENFORCEMENT_ACTIONS;
|
|
215
|
+
}
|
|
190
216
|
});
|
|
191
217
|
exports.applyPolicy = applyPolicy;
|
|
192
218
|
exports.buildBlockedResponse = buildBlockedResponse;
|
|
193
219
|
exports.buildChallengeResponse = buildChallengeResponse;
|
|
194
220
|
exports.buildRedirectResponse = buildRedirectResponse;
|
|
195
221
|
exports.createContextFromDetection = createContextFromDetection;
|
|
222
|
+
Object.defineProperty(exports, 'createEvaluationContext', {
|
|
223
|
+
enumerable: true,
|
|
224
|
+
get: function () {
|
|
225
|
+
return _kya_os_checkpoint_shared.createEvaluationContext;
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
Object.defineProperty(exports, 'evaluatePolicy', {
|
|
229
|
+
enumerable: true,
|
|
230
|
+
get: function () {
|
|
231
|
+
return _kya_os_checkpoint_shared.evaluatePolicy;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
196
234
|
exports.evaluatePolicyForDetection = evaluatePolicyForDetection;
|
|
197
235
|
exports.getPolicy = getPolicy;
|
|
198
|
-
exports.handlePolicyDecision = handlePolicyDecision;
|
|
236
|
+
exports.handlePolicyDecision = handlePolicyDecision;
|