@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
|
@@ -1,125 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* API-based AgentShield Middleware for Next.js
|
|
7
|
-
*
|
|
8
|
-
* This middleware uses the AgentShield API for detection and enforcement,
|
|
9
|
-
* instead of running detection locally. This approach:
|
|
10
|
-
*
|
|
11
|
-
* 1. Works reliably in Edge Runtime (no WASM loading issues)
|
|
12
|
-
* 2. Ensures consistent detection across all platforms
|
|
13
|
-
* 3. Applies centralized policies from the dashboard
|
|
14
|
-
* 4. Supports deny lists, thresholds, and path rules
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* // middleware.ts
|
|
19
|
-
* import { withCheckpointApi } from '@kya-os/checkpoint-nextjs/api-middleware';
|
|
20
|
-
*
|
|
21
|
-
* export default withCheckpointApi({
|
|
22
|
-
* apiKey: process.env.CHECKPOINT_API_KEY!,
|
|
23
|
-
* // Optional overrides:
|
|
24
|
-
* onBlock: 'redirect', // 'block' | 'redirect' | 'challenge'
|
|
25
|
-
* redirectUrl: '/blocked',
|
|
26
|
-
* skipPaths: ['/api/health', '/_next/*'],
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* export const config = {
|
|
30
|
-
* matcher: ['/((?!_next/static|favicon.ico).*)'],
|
|
31
|
-
* };
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
|
|
1
|
+
import { EnforcementDecision } from "./api-client.mjs";
|
|
2
|
+
import { ChallengeEnvelopeMode, ClientIpTrustPolicy } from "@kya-os/checkpoint-shared";
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
//#region src/api-middleware.d.ts
|
|
35
5
|
/**
|
|
36
6
|
* Middleware configuration
|
|
37
7
|
*/
|
|
38
|
-
interface CheckpointApiMiddlewareConfig {
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
8
|
+
export interface CheckpointApiMiddlewareConfig {
|
|
9
|
+
/** Explicit deployment/proxy trust profile for client-IP resolution. */
|
|
10
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
11
|
+
/** API key (or use CHECKPOINT_API_KEY env var) */
|
|
12
|
+
apiKey?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Gateway base URL. Override for staging or self-hosted deployments.
|
|
15
|
+
* With `useEdge: false` the default becomes `https://kya.vouched.id`.
|
|
16
|
+
*
|
|
17
|
+
* @default 'https://detect.checkpoint-gateway.ai'
|
|
18
|
+
*/
|
|
19
|
+
apiUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Use edge detection for lower latency (~30-50ms vs ~150ms) and better coverage.
|
|
22
|
+
* Edge detection can identify non-JS clients (curl, Python, Claude Code WebFetch)
|
|
23
|
+
* that the pixel cannot detect since they don't execute JavaScript.
|
|
24
|
+
* Set to false to use the Vercel API instead.
|
|
25
|
+
* @default true
|
|
26
|
+
*/
|
|
27
|
+
useEdge?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Request timeout in milliseconds for the gateway call.
|
|
30
|
+
*
|
|
31
|
+
* @default 5000
|
|
32
|
+
*/
|
|
33
|
+
timeout?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Action to take when an agent should be blocked
|
|
36
|
+
* - 'block': Return 403 response
|
|
37
|
+
* - 'redirect': Redirect to redirectUrl
|
|
38
|
+
* - 'challenge': Show a challenge page (future)
|
|
39
|
+
* Default: uses policy from dashboard
|
|
40
|
+
*/
|
|
41
|
+
onBlock?: 'block' | 'redirect' | 'challenge';
|
|
42
|
+
/**
|
|
43
|
+
* URL to redirect to when blocking (if onBlock is 'redirect')
|
|
44
|
+
* Default: uses redirectUrl from dashboard policy
|
|
45
|
+
*/
|
|
46
|
+
redirectUrl?: string;
|
|
47
|
+
/**
|
|
48
|
+
* How the middleware handles a `redirect` / `instruct` action.
|
|
49
|
+
*
|
|
50
|
+
* - `'instruct'` (default): return HTTP 401 with a KYA-OS Link header + JSON
|
|
51
|
+
* body pointing the agent at the redirect URL. LLMs surface the URL as a
|
|
52
|
+
* clickable link for the user. Matches the Cloudflare Gateway contract.
|
|
53
|
+
* - `'http'`: legacy behavior — return HTTP 302 with `Location`. Most LLM
|
|
54
|
+
* fetchers won't follow the redirect, so this is only useful when your
|
|
55
|
+
* traffic is real browsers.
|
|
56
|
+
*
|
|
57
|
+
* @default 'instruct'
|
|
58
|
+
*/
|
|
59
|
+
redirectMode?: 'instruct' | 'http';
|
|
60
|
+
/**
|
|
61
|
+
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
62
|
+
* `negotiated` serves a body-readable HTTP 200 instruct response to
|
|
63
|
+
* cooperative agents (so fetchers that drop 4xx bodies can read it),
|
|
64
|
+
* keeping the spec 401 for everyone else. Defaults to `negotiated` (#3530).
|
|
65
|
+
* A cooperative-UX bridge, NOT an access control.
|
|
66
|
+
*
|
|
67
|
+
* @default 'negotiated'
|
|
68
|
+
*/
|
|
69
|
+
delegationChallengeMode?: ChallengeEnvelopeMode;
|
|
70
|
+
/**
|
|
71
|
+
* Customize the blocked response's status, message, and extra headers.
|
|
72
|
+
*
|
|
73
|
+
* @default { status: 403, message: 'Access denied' }
|
|
74
|
+
*/
|
|
75
|
+
blockedResponse?: {
|
|
76
|
+
status?: number;
|
|
77
|
+
message?: string;
|
|
78
|
+
headers?: Record<string, string>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Paths to skip (in addition to dashboard policy)
|
|
82
|
+
* Supports glob patterns: '/api/*', '/_next/*'
|
|
83
|
+
*/
|
|
84
|
+
skipPaths?: string[];
|
|
85
|
+
/**
|
|
86
|
+
* Only enforce on these paths (overrides dashboard policy)
|
|
87
|
+
*/
|
|
88
|
+
includePaths?: string[];
|
|
89
|
+
/**
|
|
90
|
+
* Callback when an agent is detected
|
|
91
|
+
*/
|
|
92
|
+
onAgentDetected?: (request: NextRequest, decision: EnforcementDecision) => void | Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Callback to customize the blocked response
|
|
95
|
+
*/
|
|
96
|
+
customBlockedResponse?: (request: NextRequest, decision: EnforcementDecision) => NextResponse | Promise<NextResponse>;
|
|
97
|
+
/**
|
|
98
|
+
* Whether to fail open (allow) on API errors. Recommended for production.
|
|
99
|
+
*
|
|
100
|
+
* @default true
|
|
101
|
+
*/
|
|
102
|
+
failOpen?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Log detection decisions and errors to the console.
|
|
105
|
+
*
|
|
106
|
+
* @default false
|
|
107
|
+
*/
|
|
108
|
+
debug?: boolean;
|
|
123
109
|
}
|
|
124
110
|
/**
|
|
125
111
|
* Create AgentShield middleware with API-based detection
|
|
@@ -135,32 +121,32 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
135
121
|
* });
|
|
136
122
|
* ```
|
|
137
123
|
*/
|
|
138
|
-
declare function withCheckpointApi(config?: CheckpointApiMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
124
|
+
export declare function withCheckpointApi(config?: CheckpointApiMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
139
125
|
/** @deprecated Renamed to {@link withCheckpointApi}. The behaviour is identical. */
|
|
140
|
-
declare const withAgentShield: typeof withCheckpointApi;
|
|
126
|
+
export declare const withAgentShield: typeof withCheckpointApi;
|
|
141
127
|
/** @deprecated Renamed to {@link CheckpointApiMiddlewareConfig}. */
|
|
142
|
-
type AgentShieldMiddlewareConfig = CheckpointApiMiddlewareConfig;
|
|
128
|
+
export type AgentShieldMiddlewareConfig = CheckpointApiMiddlewareConfig;
|
|
143
129
|
/**
|
|
144
130
|
* @deprecated Module-load-time invocation of the legacy `withAgentShield()`.
|
|
145
131
|
* Construct the middleware explicitly via `withCheckpointApi({ apiKey })`
|
|
146
132
|
* instead of relying on a default-constructed singleton at import time.
|
|
147
133
|
*/
|
|
148
|
-
declare function agentShieldMiddleware(_request: NextRequest): Promise<NextResponse>;
|
|
134
|
+
export declare function agentShieldMiddleware(_request: NextRequest): Promise<NextResponse>;
|
|
149
135
|
/**
|
|
150
136
|
* @deprecated The "enhanced middleware" combined detection + storage +
|
|
151
137
|
* enforcement into a single legacy path that no longer exists. Migrate
|
|
152
138
|
* to `withCheckpoint` (local-engine) or `withCheckpointApi` (SaaS-gateway).
|
|
153
139
|
*/
|
|
154
|
-
declare function createEnhancedAgentShieldMiddleware(_config?: EnhancedMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
140
|
+
export declare function createEnhancedAgentShieldMiddleware(_config?: EnhancedMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
155
141
|
/** @deprecated The enhanced-middleware path is gone. Use `CheckpointConfig` (local-engine) or `CheckpointApiMiddlewareConfig` (SaaS). */
|
|
156
|
-
type EnhancedMiddlewareConfig = Record<string, unknown>;
|
|
142
|
+
export type EnhancedMiddlewareConfig = Record<string, unknown>;
|
|
157
143
|
/** @deprecated Storage was tied to the legacy enhanced-middleware path. */
|
|
158
|
-
type StorageAdapter = Record<string, unknown>;
|
|
144
|
+
export type StorageAdapter = Record<string, unknown>;
|
|
159
145
|
/** @deprecated Storage was tied to the legacy enhanced-middleware path. */
|
|
160
|
-
type StorageConfig = Record<string, unknown>;
|
|
146
|
+
export type StorageConfig = Record<string, unknown>;
|
|
161
147
|
/** @deprecated Detection events now flow through the engine; the legacy event shape no longer applies. */
|
|
162
|
-
type AgentDetectionEvent = Record<string, unknown>;
|
|
148
|
+
export type AgentDetectionEvent = Record<string, unknown>;
|
|
163
149
|
/** @deprecated Use `EdgeSessionTracker` / `StatelessSessionChecker` from `./session-tracker`. */
|
|
164
|
-
type AgentSession = Record<string, unknown>;
|
|
165
|
-
|
|
166
|
-
|
|
150
|
+
export type AgentSession = Record<string, unknown>;
|
|
151
|
+
//#endregion
|
|
152
|
+
//# sourceMappingURL=api-middleware.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-middleware.d.mts","names":[],"sources":["../src/api-middleware.ts"],"mappings":";;;;;;;iBAyEiB;;EAEf,iBAAiB;;EAEjB;;;;;;;EAOA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;;EASA;;;;;EAMA;;;;;;;;;;;;;EAcA;;;;;;;;;;EAWA,0BAA0B;;;;;;EAO1B;IACE;IACA;IACA,UAAU;;;;;;EAOZ;;;;EAKA;;;;EAKA,mBAAmB,SAAS,aAAa,UAAU,+BAA+B;;;;EAKlF,yBACE,SAAS,aACT,UAAU,wBACP,eAAe,QAAQ;;;;;;EAO5B;;;;;;EAOA;;;;;;;;;;;;;;;;wBAwIc,kBAAkB,SAAQ,iCAsCP,SAAS,gBAAc,QAAQ;;qBAiOrD,wBAAe;;YAGhB,8BAA8B;;;;;;wBAoB1B,sBAAsB,UAAU,cAAc,QAAQ;;;;;;wBAStD,oCACd,UAAS,4BACP,SAAS,gBAAgB,QAAQ;;YAQzB,2BAA2B;;YAG3B,iBAAiB;;YAGjB,gBAAgB;;YAGhB,sBAAsB;;YAGtB,eAAe"}
|
package/dist/api-middleware.d.ts
CHANGED
|
@@ -1,125 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* API-based AgentShield Middleware for Next.js
|
|
7
|
-
*
|
|
8
|
-
* This middleware uses the AgentShield API for detection and enforcement,
|
|
9
|
-
* instead of running detection locally. This approach:
|
|
10
|
-
*
|
|
11
|
-
* 1. Works reliably in Edge Runtime (no WASM loading issues)
|
|
12
|
-
* 2. Ensures consistent detection across all platforms
|
|
13
|
-
* 3. Applies centralized policies from the dashboard
|
|
14
|
-
* 4. Supports deny lists, thresholds, and path rules
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* // middleware.ts
|
|
19
|
-
* import { withCheckpointApi } from '@kya-os/checkpoint-nextjs/api-middleware';
|
|
20
|
-
*
|
|
21
|
-
* export default withCheckpointApi({
|
|
22
|
-
* apiKey: process.env.CHECKPOINT_API_KEY!,
|
|
23
|
-
* // Optional overrides:
|
|
24
|
-
* onBlock: 'redirect', // 'block' | 'redirect' | 'challenge'
|
|
25
|
-
* redirectUrl: '/blocked',
|
|
26
|
-
* skipPaths: ['/api/health', '/_next/*'],
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* export const config = {
|
|
30
|
-
* matcher: ['/((?!_next/static|favicon.ico).*)'],
|
|
31
|
-
* };
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
|
|
1
|
+
import { EnforcementDecision } from "./api-client.js";
|
|
2
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
3
|
+
import { ChallengeEnvelopeMode, ClientIpTrustPolicy } from "@kya-os/checkpoint-shared";
|
|
4
|
+
//#region src/api-middleware.d.ts
|
|
35
5
|
/**
|
|
36
6
|
* Middleware configuration
|
|
37
7
|
*/
|
|
38
|
-
interface CheckpointApiMiddlewareConfig {
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
8
|
+
export interface CheckpointApiMiddlewareConfig {
|
|
9
|
+
/** Explicit deployment/proxy trust profile for client-IP resolution. */
|
|
10
|
+
clientIpPolicy?: ClientIpTrustPolicy;
|
|
11
|
+
/** API key (or use CHECKPOINT_API_KEY env var) */
|
|
12
|
+
apiKey?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Gateway base URL. Override for staging or self-hosted deployments.
|
|
15
|
+
* With `useEdge: false` the default becomes `https://kya.vouched.id`.
|
|
16
|
+
*
|
|
17
|
+
* @default 'https://detect.checkpoint-gateway.ai'
|
|
18
|
+
*/
|
|
19
|
+
apiUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Use edge detection for lower latency (~30-50ms vs ~150ms) and better coverage.
|
|
22
|
+
* Edge detection can identify non-JS clients (curl, Python, Claude Code WebFetch)
|
|
23
|
+
* that the pixel cannot detect since they don't execute JavaScript.
|
|
24
|
+
* Set to false to use the Vercel API instead.
|
|
25
|
+
* @default true
|
|
26
|
+
*/
|
|
27
|
+
useEdge?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Request timeout in milliseconds for the gateway call.
|
|
30
|
+
*
|
|
31
|
+
* @default 5000
|
|
32
|
+
*/
|
|
33
|
+
timeout?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Action to take when an agent should be blocked
|
|
36
|
+
* - 'block': Return 403 response
|
|
37
|
+
* - 'redirect': Redirect to redirectUrl
|
|
38
|
+
* - 'challenge': Show a challenge page (future)
|
|
39
|
+
* Default: uses policy from dashboard
|
|
40
|
+
*/
|
|
41
|
+
onBlock?: 'block' | 'redirect' | 'challenge';
|
|
42
|
+
/**
|
|
43
|
+
* URL to redirect to when blocking (if onBlock is 'redirect')
|
|
44
|
+
* Default: uses redirectUrl from dashboard policy
|
|
45
|
+
*/
|
|
46
|
+
redirectUrl?: string;
|
|
47
|
+
/**
|
|
48
|
+
* How the middleware handles a `redirect` / `instruct` action.
|
|
49
|
+
*
|
|
50
|
+
* - `'instruct'` (default): return HTTP 401 with a KYA-OS Link header + JSON
|
|
51
|
+
* body pointing the agent at the redirect URL. LLMs surface the URL as a
|
|
52
|
+
* clickable link for the user. Matches the Cloudflare Gateway contract.
|
|
53
|
+
* - `'http'`: legacy behavior — return HTTP 302 with `Location`. Most LLM
|
|
54
|
+
* fetchers won't follow the redirect, so this is only useful when your
|
|
55
|
+
* traffic is real browsers.
|
|
56
|
+
*
|
|
57
|
+
* @default 'instruct'
|
|
58
|
+
*/
|
|
59
|
+
redirectMode?: 'instruct' | 'http';
|
|
60
|
+
/**
|
|
61
|
+
* Challenge-emission envelope mode (draft-kya-http-02 §8.1 fetcher-200).
|
|
62
|
+
* `negotiated` serves a body-readable HTTP 200 instruct response to
|
|
63
|
+
* cooperative agents (so fetchers that drop 4xx bodies can read it),
|
|
64
|
+
* keeping the spec 401 for everyone else. Defaults to `negotiated` (#3530).
|
|
65
|
+
* A cooperative-UX bridge, NOT an access control.
|
|
66
|
+
*
|
|
67
|
+
* @default 'negotiated'
|
|
68
|
+
*/
|
|
69
|
+
delegationChallengeMode?: ChallengeEnvelopeMode;
|
|
70
|
+
/**
|
|
71
|
+
* Customize the blocked response's status, message, and extra headers.
|
|
72
|
+
*
|
|
73
|
+
* @default { status: 403, message: 'Access denied' }
|
|
74
|
+
*/
|
|
75
|
+
blockedResponse?: {
|
|
76
|
+
status?: number;
|
|
77
|
+
message?: string;
|
|
78
|
+
headers?: Record<string, string>;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Paths to skip (in addition to dashboard policy)
|
|
82
|
+
* Supports glob patterns: '/api/*', '/_next/*'
|
|
83
|
+
*/
|
|
84
|
+
skipPaths?: string[];
|
|
85
|
+
/**
|
|
86
|
+
* Only enforce on these paths (overrides dashboard policy)
|
|
87
|
+
*/
|
|
88
|
+
includePaths?: string[];
|
|
89
|
+
/**
|
|
90
|
+
* Callback when an agent is detected
|
|
91
|
+
*/
|
|
92
|
+
onAgentDetected?: (request: NextRequest, decision: EnforcementDecision) => void | Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Callback to customize the blocked response
|
|
95
|
+
*/
|
|
96
|
+
customBlockedResponse?: (request: NextRequest, decision: EnforcementDecision) => NextResponse | Promise<NextResponse>;
|
|
97
|
+
/**
|
|
98
|
+
* Whether to fail open (allow) on API errors. Recommended for production.
|
|
99
|
+
*
|
|
100
|
+
* @default true
|
|
101
|
+
*/
|
|
102
|
+
failOpen?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* Log detection decisions and errors to the console.
|
|
105
|
+
*
|
|
106
|
+
* @default false
|
|
107
|
+
*/
|
|
108
|
+
debug?: boolean;
|
|
123
109
|
}
|
|
124
110
|
/**
|
|
125
111
|
* Create AgentShield middleware with API-based detection
|
|
@@ -135,32 +121,32 @@ interface CheckpointApiMiddlewareConfig {
|
|
|
135
121
|
* });
|
|
136
122
|
* ```
|
|
137
123
|
*/
|
|
138
|
-
declare function withCheckpointApi(config?: CheckpointApiMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
124
|
+
export declare function withCheckpointApi(config?: CheckpointApiMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
139
125
|
/** @deprecated Renamed to {@link withCheckpointApi}. The behaviour is identical. */
|
|
140
|
-
declare const withAgentShield: typeof withCheckpointApi;
|
|
126
|
+
export declare const withAgentShield: typeof withCheckpointApi;
|
|
141
127
|
/** @deprecated Renamed to {@link CheckpointApiMiddlewareConfig}. */
|
|
142
|
-
type AgentShieldMiddlewareConfig = CheckpointApiMiddlewareConfig;
|
|
128
|
+
export type AgentShieldMiddlewareConfig = CheckpointApiMiddlewareConfig;
|
|
143
129
|
/**
|
|
144
130
|
* @deprecated Module-load-time invocation of the legacy `withAgentShield()`.
|
|
145
131
|
* Construct the middleware explicitly via `withCheckpointApi({ apiKey })`
|
|
146
132
|
* instead of relying on a default-constructed singleton at import time.
|
|
147
133
|
*/
|
|
148
|
-
declare function agentShieldMiddleware(_request: NextRequest): Promise<NextResponse>;
|
|
134
|
+
export declare function agentShieldMiddleware(_request: NextRequest): Promise<NextResponse>;
|
|
149
135
|
/**
|
|
150
136
|
* @deprecated The "enhanced middleware" combined detection + storage +
|
|
151
137
|
* enforcement into a single legacy path that no longer exists. Migrate
|
|
152
138
|
* to `withCheckpoint` (local-engine) or `withCheckpointApi` (SaaS-gateway).
|
|
153
139
|
*/
|
|
154
|
-
declare function createEnhancedAgentShieldMiddleware(_config?: EnhancedMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
140
|
+
export declare function createEnhancedAgentShieldMiddleware(_config?: EnhancedMiddlewareConfig): (request: NextRequest) => Promise<NextResponse>;
|
|
155
141
|
/** @deprecated The enhanced-middleware path is gone. Use `CheckpointConfig` (local-engine) or `CheckpointApiMiddlewareConfig` (SaaS). */
|
|
156
|
-
type EnhancedMiddlewareConfig = Record<string, unknown>;
|
|
142
|
+
export type EnhancedMiddlewareConfig = Record<string, unknown>;
|
|
157
143
|
/** @deprecated Storage was tied to the legacy enhanced-middleware path. */
|
|
158
|
-
type StorageAdapter = Record<string, unknown>;
|
|
144
|
+
export type StorageAdapter = Record<string, unknown>;
|
|
159
145
|
/** @deprecated Storage was tied to the legacy enhanced-middleware path. */
|
|
160
|
-
type StorageConfig = Record<string, unknown>;
|
|
146
|
+
export type StorageConfig = Record<string, unknown>;
|
|
161
147
|
/** @deprecated Detection events now flow through the engine; the legacy event shape no longer applies. */
|
|
162
|
-
type AgentDetectionEvent = Record<string, unknown>;
|
|
148
|
+
export type AgentDetectionEvent = Record<string, unknown>;
|
|
163
149
|
/** @deprecated Use `EdgeSessionTracker` / `StatelessSessionChecker` from `./session-tracker`. */
|
|
164
|
-
type AgentSession = Record<string, unknown>;
|
|
165
|
-
|
|
166
|
-
|
|
150
|
+
export type AgentSession = Record<string, unknown>;
|
|
151
|
+
//#endregion
|
|
152
|
+
//# sourceMappingURL=api-middleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-middleware.d.ts","names":[],"sources":["../src/api-middleware.ts"],"mappings":";;;;;;;iBAyEiB;;EAEf,iBAAiB;;EAEjB;;;;;;;EAOA;;;;;;;;EAQA;;;;;;EAMA;;;;;;;;EASA;;;;;EAMA;;;;;;;;;;;;;EAcA;;;;;;;;;;EAWA,0BAA0B;;;;;;EAO1B;IACE;IACA;IACA,UAAU;;;;;;EAOZ;;;;EAKA;;;;EAKA,mBAAmB,SAAS,aAAa,UAAU,+BAA+B;;;;EAKlF,yBACE,SAAS,aACT,UAAU,wBACP,eAAe,QAAQ;;;;;;EAO5B;;;;;;EAOA;;;;;;;;;;;;;;;;wBAwIc,kBAAkB,SAAQ,iCAsCP,SAAS,gBAAc,QAAQ;;qBAiOrD,wBAAe;;YAGhB,8BAA8B;;;;;;wBAoB1B,sBAAsB,UAAU,cAAc,QAAQ;;;;;;wBAStD,oCACd,UAAS,4BACP,SAAS,gBAAgB,QAAQ;;YAQzB,2BAA2B;;YAG3B,iBAAiB;;YAGjB,gBAAgB;;YAGhB,sBAAsB;;YAGtB,eAAe"}
|