@kya-os/checkpoint-nextjs 1.7.0 → 1.9.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.
Files changed (65) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/EDGE_RUNTIME_WASM_SETUP.md +12 -12
  3. package/README.md +144 -251
  4. package/bin/setup-edge-wasm.js +10 -2
  5. package/dist/adapt.js +9 -6
  6. package/dist/adapt.mjs +9 -6
  7. package/dist/api-client.d.mts +9 -0
  8. package/dist/api-client.d.ts +9 -0
  9. package/dist/api-middleware.d.mts +12 -2
  10. package/dist/api-middleware.d.ts +12 -2
  11. package/dist/api-middleware.js +28 -31
  12. package/dist/api-middleware.mjs +29 -32
  13. package/dist/composed-policy.d.mts +7 -0
  14. package/dist/composed-policy.d.ts +7 -0
  15. package/dist/composed-policy.js +14 -2
  16. package/dist/composed-policy.mjs +15 -3
  17. package/dist/{config-_nfPN3E3.d.mts → config-CleTeeoW.d.mts} +39 -11
  18. package/dist/{config-kxFihzR_.d.ts → config-XMOIkxFN.d.ts} +39 -11
  19. package/dist/create-middleware.d.mts +1 -1
  20. package/dist/create-middleware.d.ts +1 -1
  21. package/dist/edge/index.d.mts +3 -1
  22. package/dist/edge/index.d.ts +3 -1
  23. package/dist/edge/index.js +5 -16
  24. package/dist/edge/index.mjs +5 -16
  25. package/dist/edge-runtime-loader.d.mts +3 -1
  26. package/dist/edge-runtime-loader.d.ts +3 -1
  27. package/dist/edge-runtime-loader.js +4 -16
  28. package/dist/edge-runtime-loader.mjs +5 -17
  29. package/dist/edge-wasm-middleware.d.mts +1 -1
  30. package/dist/edge-wasm-middleware.d.ts +1 -1
  31. package/dist/edge-wasm-middleware.js +1 -1
  32. package/dist/edge-wasm-middleware.mjs +1 -1
  33. package/dist/index.d.mts +6 -6
  34. package/dist/index.d.ts +6 -6
  35. package/dist/index.js +151 -84
  36. package/dist/index.mjs +153 -86
  37. package/dist/middleware-bundler.d.mts +13 -0
  38. package/dist/middleware-bundler.d.ts +13 -0
  39. package/dist/middleware-bundler.js +354 -0
  40. package/dist/middleware-bundler.mjs +352 -0
  41. package/dist/middleware-edge.d.mts +5 -41
  42. package/dist/middleware-edge.d.ts +5 -41
  43. package/dist/middleware-edge.js +161 -97
  44. package/dist/middleware-edge.mjs +164 -100
  45. package/dist/middleware-node.d.mts +16 -52
  46. package/dist/middleware-node.d.ts +16 -52
  47. package/dist/middleware-node.js +115 -56
  48. package/dist/middleware-node.mjs +118 -55
  49. package/dist/middleware.d.mts +1 -1
  50. package/dist/middleware.d.ts +1 -1
  51. package/dist/policy.js +6 -1
  52. package/dist/policy.mjs +6 -1
  53. package/dist/session-tracker.js +2 -0
  54. package/dist/session-tracker.mjs +2 -0
  55. package/dist/translate.d.mts +7 -4
  56. package/dist/translate.d.ts +7 -4
  57. package/dist/translate.js +9 -8
  58. package/dist/translate.mjs +9 -8
  59. package/dist/{types-D9RQvPNy.d.mts → types-Chgi5TFC.d.mts} +2 -2
  60. package/dist/{types-D9RQvPNy.d.ts → types-Chgi5TFC.d.ts} +2 -2
  61. package/package.json +17 -10
  62. package/dist/signature-verifier.d.mts +0 -33
  63. package/dist/signature-verifier.d.ts +0 -33
  64. package/dist/signature-verifier.js +0 -384
  65. package/dist/signature-verifier.mjs +0 -360
@@ -148,6 +148,8 @@ var StatelessSessionChecker = class {
148
148
  return null;
149
149
  }
150
150
  }
151
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- published
152
+ // signature; narrowing it is a breaking change, deferred to a major.
151
153
  static setHeaders(response, session) {
152
154
  try {
153
155
  if (response.setHeader) {
@@ -1,5 +1,6 @@
1
1
  import { NextRequest } from 'next/server';
2
2
  import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
3
+ import { ClientIpTrustPolicy } from '@kya-os/checkpoint-shared';
3
4
 
4
5
  /**
5
6
  * D.2 — `NextRequest` → `IncomingHttpLike` translator.
@@ -12,15 +13,15 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
12
13
  * Shared between the Node-runtime and Edge-runtime entries. Next.js
13
14
  * `NextRequest` is the same shape in both runtimes — `req.headers` is
14
15
  * a `Headers` instance, `req.body` is a `ReadableStream`, `req.ip` is
15
- * a getter (only present in some deployment surfaces; fall back to
16
- * `x-forwarded-for` first IP).
16
+ * a getter (only present in some deployment surfaces; forwarding headers
17
+ * are considered only when the caller selects a matching trust policy).
17
18
  */
18
19
 
19
20
  /**
20
21
  * Options controlling translator side effects.
21
22
  *
22
23
  * `drainJsonBody` (default `true`) controls whether the translator
23
- * reads the request body so the orchestrator can extract an MCP-I
24
+ * reads the request body so the orchestrator can extract a KYA-OS
24
25
  * envelope from `_meta.proof.jws` (the canonical spec-form transport).
25
26
  *
26
27
  * Trade-off — body draining consumes the request stream. We use
@@ -35,11 +36,13 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
35
36
  interface NextRequestToHttpLikeOpts {
36
37
  /**
37
38
  * Read the request body when `content-type` is `application/json` so
38
- * the orchestrator can extract an MCP-I envelope from
39
+ * the orchestrator can extract a KYA-OS envelope from
39
40
  * `_meta.proof.jws`. Default `true`. Set `false` for streaming
40
41
  * middlewares that can't tolerate the clone/read overhead.
41
42
  */
42
43
  drainJsonBody?: boolean;
44
+ /** Deployment trust profile. Defaults to direct/peer-only fail-closed behavior. */
45
+ clientIpPolicy?: ClientIpTrustPolicy;
43
46
  }
44
47
  /**
45
48
  * Translate a Next.js `NextRequest` into the orchestrator's
@@ -1,5 +1,6 @@
1
1
  import { NextRequest } from 'next/server';
2
2
  import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
3
+ import { ClientIpTrustPolicy } from '@kya-os/checkpoint-shared';
3
4
 
4
5
  /**
5
6
  * D.2 — `NextRequest` → `IncomingHttpLike` translator.
@@ -12,15 +13,15 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
12
13
  * Shared between the Node-runtime and Edge-runtime entries. Next.js
13
14
  * `NextRequest` is the same shape in both runtimes — `req.headers` is
14
15
  * a `Headers` instance, `req.body` is a `ReadableStream`, `req.ip` is
15
- * a getter (only present in some deployment surfaces; fall back to
16
- * `x-forwarded-for` first IP).
16
+ * a getter (only present in some deployment surfaces; forwarding headers
17
+ * are considered only when the caller selects a matching trust policy).
17
18
  */
18
19
 
19
20
  /**
20
21
  * Options controlling translator side effects.
21
22
  *
22
23
  * `drainJsonBody` (default `true`) controls whether the translator
23
- * reads the request body so the orchestrator can extract an MCP-I
24
+ * reads the request body so the orchestrator can extract a KYA-OS
24
25
  * envelope from `_meta.proof.jws` (the canonical spec-form transport).
25
26
  *
26
27
  * Trade-off — body draining consumes the request stream. We use
@@ -35,11 +36,13 @@ import { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';
35
36
  interface NextRequestToHttpLikeOpts {
36
37
  /**
37
38
  * Read the request body when `content-type` is `application/json` so
38
- * the orchestrator can extract an MCP-I envelope from
39
+ * the orchestrator can extract a KYA-OS envelope from
39
40
  * `_meta.proof.jws`. Default `true`. Set `false` for streaming
40
41
  * middlewares that can't tolerate the clone/read overhead.
41
42
  */
42
43
  drainJsonBody?: boolean;
44
+ /** Deployment trust profile. Defaults to direct/peer-only fail-closed behavior. */
45
+ clientIpPolicy?: ClientIpTrustPolicy;
43
46
  }
44
47
  /**
45
48
  * Translate a Next.js `NextRequest` into the orchestrator's
package/dist/translate.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var checkpointShared = require('@kya-os/checkpoint-shared');
4
+
3
5
  // src/translate.ts
4
6
  async function nextRequestToHttpLike(req, opts = {}) {
5
7
  const url = new URL(req.url);
@@ -10,7 +12,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
10
12
  url: url.pathname + url.search,
11
13
  headers: headersToRecord(req.headers),
12
14
  body,
13
- remoteAddress: extractRemoteAddress(req)
15
+ remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
14
16
  };
15
17
  }
16
18
  async function tryDrainJsonBody(req, opts) {
@@ -30,14 +32,13 @@ function headersToRecord(headers) {
30
32
  });
31
33
  return out;
32
34
  }
33
- function extractRemoteAddress(req) {
34
- const xff = req.headers.get("x-forwarded-for");
35
- if (xff) {
36
- const first = xff.split(",")[0]?.trim();
37
- if (first) return first;
38
- }
35
+ function extractRemoteAddress(req, policy) {
39
36
  const maybeIp = req.ip;
40
- return maybeIp;
37
+ return checkpointShared.resolveClientIp({
38
+ headers: req.headers,
39
+ frameworkAddress: maybeIp,
40
+ policy
41
+ }).ip ?? void 0;
41
42
  }
42
43
 
43
44
  exports.nextRequestToHttpLike = nextRequestToHttpLike;
@@ -1,3 +1,5 @@
1
+ import { resolveClientIp } from '@kya-os/checkpoint-shared';
2
+
1
3
  // src/translate.ts
2
4
  async function nextRequestToHttpLike(req, opts = {}) {
3
5
  const url = new URL(req.url);
@@ -8,7 +10,7 @@ async function nextRequestToHttpLike(req, opts = {}) {
8
10
  url: url.pathname + url.search,
9
11
  headers: headersToRecord(req.headers),
10
12
  body,
11
- remoteAddress: extractRemoteAddress(req)
13
+ remoteAddress: extractRemoteAddress(req, opts.clientIpPolicy ?? { platform: "direct" })
12
14
  };
13
15
  }
14
16
  async function tryDrainJsonBody(req, opts) {
@@ -28,14 +30,13 @@ function headersToRecord(headers) {
28
30
  });
29
31
  return out;
30
32
  }
31
- function extractRemoteAddress(req) {
32
- const xff = req.headers.get("x-forwarded-for");
33
- if (xff) {
34
- const first = xff.split(",")[0]?.trim();
35
- if (first) return first;
36
- }
33
+ function extractRemoteAddress(req, policy) {
37
34
  const maybeIp = req.ip;
38
- return maybeIp;
35
+ return resolveClientIp({
36
+ headers: req.headers,
37
+ frameworkAddress: maybeIp,
38
+ policy
39
+ }).ip ?? void 0;
39
40
  }
40
41
 
41
42
  export { nextRequestToHttpLike };
@@ -1,6 +1,6 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
2
  import { AgentShieldConfig, DetectionResult } from '@kya-os/checkpoint-shared';
3
- import { AgentShieldEvents } from '@kya-os/checkpoint';
3
+ import { CheckpointEvents } from '@kya-os/checkpoint';
4
4
 
5
5
  /**
6
6
  * Next.js-specific type definitions
@@ -22,7 +22,7 @@ interface NextJSMiddlewareConfig extends Partial<AgentShieldConfig> {
22
22
  /**
23
23
  * Event handlers for detection events
24
24
  */
25
- events?: Partial<AgentShieldEvents>;
25
+ events?: Partial<CheckpointEvents>;
26
26
  /**
27
27
  * Path patterns to skip detection
28
28
  */
@@ -1,6 +1,6 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
2
  import { AgentShieldConfig, DetectionResult } from '@kya-os/checkpoint-shared';
3
- import { AgentShieldEvents } from '@kya-os/checkpoint';
3
+ import { CheckpointEvents } from '@kya-os/checkpoint';
4
4
 
5
5
  /**
6
6
  * Next.js-specific type definitions
@@ -22,7 +22,7 @@ interface NextJSMiddlewareConfig extends Partial<AgentShieldConfig> {
22
22
  /**
23
23
  * Event handlers for detection events
24
24
  */
25
- events?: Partial<AgentShieldEvents>;
25
+ events?: Partial<CheckpointEvents>;
26
26
  /**
27
27
  * Path patterns to skip detection
28
28
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kya-os/checkpoint-nextjs",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "Checkpoint Next.js middleware for AI agent detection (formerly @kya-os/agentshield-nextjs)",
5
5
  "keywords": [
6
6
  "nextjs",
@@ -14,14 +14,14 @@
14
14
  ],
15
15
  "license": "MIT OR Apache-2.0",
16
16
  "author": "KnowThat.ai",
17
- "homepage": "https://github.com/Know-That-Ai/agent-shield#readme",
17
+ "homepage": "https://github.com/Know-That-Ai/checkpoint#readme",
18
18
  "repository": {
19
19
  "type": "git",
20
- "url": "https://github.com/Know-That-Ai/agent-shield.git",
20
+ "url": "https://github.com/Know-That-Ai/checkpoint.git",
21
21
  "directory": "packages/checkpoint-nextjs"
22
22
  },
23
23
  "bugs": {
24
- "url": "https://github.com/Know-That-Ai/agent-shield/issues"
24
+ "url": "https://github.com/Know-That-Ai/checkpoint/issues"
25
25
  },
26
26
  "main": "./dist/index.js",
27
27
  "module": "./dist/index.mjs",
@@ -82,7 +82,12 @@
82
82
  "import": "./dist/policy.mjs",
83
83
  "require": "./dist/policy.js"
84
84
  },
85
- "./package.json": "./package.json"
85
+ "./package.json": "./package.json",
86
+ "./bundler": {
87
+ "types": "./dist/middleware-bundler.d.ts",
88
+ "import": "./dist/middleware-bundler.mjs",
89
+ "require": "./dist/middleware-bundler.js"
90
+ }
86
91
  },
87
92
  "files": [
88
93
  "dist",
@@ -102,9 +107,9 @@
102
107
  "@testing-library/react-hooks": "^8.0.1",
103
108
  "@types/node": "^20.11.24",
104
109
  "@types/react": "^18.2.61",
105
- "@vitest/coverage-v8": "^1.3.1",
110
+ "@vitest/coverage-v8": "^3.2.6",
106
111
  "jsdom": "^24.0.0",
107
- "next": "^16.2.2",
112
+ "next": "^16.2.6",
108
113
  "react": "^18.2.0",
109
114
  "react-dom": "^18.2.0",
110
115
  "rimraf": "^5.0.5",
@@ -112,7 +117,7 @@
112
117
  "typescript": "^5.4.2",
113
118
  "vite-plugin-top-level-await": "^1.6.0",
114
119
  "vite-plugin-wasm": "^3.6.0",
115
- "vitest": "^1.3.1"
120
+ "vitest": "^3.2.6"
116
121
  },
117
122
  "peerDependencies": {
118
123
  "@upstash/redis": ">=1.0.0",
@@ -131,9 +136,10 @@
131
136
  "dependencies": {
132
137
  "@noble/ed25519": "^2.2.3",
133
138
  "@noble/hashes": "^2.0.1",
139
+ "@vercel/functions": "^3.9.3",
140
+ "@kya-os/checkpoint-shared": "^1.3.0",
134
141
  "@kya-os/checkpoint": "1.2.0",
135
- "@kya-os/checkpoint-shared": "1.2.0",
136
- "@kya-os/checkpoint-wasm-runtime": "^1.8.0"
142
+ "@kya-os/checkpoint-wasm-runtime": "^1.8.2"
137
143
  },
138
144
  "scripts": {
139
145
  "build": "tsup",
@@ -142,6 +148,7 @@
142
148
  "dev": "tsup --watch",
143
149
  "clean": "rimraf dist .tsbuildinfo",
144
150
  "test": "vitest run",
151
+ "test:unit": "vitest run",
145
152
  "test:watch": "vitest",
146
153
  "test:coverage": "vitest run --coverage",
147
154
  "type-check": "tsc --noEmit",
@@ -1,33 +0,0 @@
1
- /**
2
- * Ed25519 Signature Verification for HTTP Message Signatures
3
- * Implements proper cryptographic verification for ChatGPT and other agents
4
- *
5
- * Based on RFC 9421 (HTTP Message Signatures) and ChatGPT's implementation
6
- * Reference: https://help.openai.com/en/articles/9785974-chatgpt-user-allowlisting
7
- */
8
- /**
9
- * Signature verification result
10
- */
11
- interface SignatureVerificationResult {
12
- isValid: boolean;
13
- agent?: string;
14
- keyid?: string;
15
- confidence: number;
16
- reason?: string;
17
- verificationMethod: 'signature' | 'none';
18
- }
19
- /**
20
- * Verify HTTP Message Signature for AI agents
21
- */
22
- declare function verifyAgentSignature(method: string, path: string, headers: Record<string, string>): Promise<SignatureVerificationResult>;
23
- /**
24
- * Quick check if signature headers are present (for performance)
25
- */
26
- declare function hasSignatureHeaders(headers: Record<string, string>): boolean;
27
- /**
28
- * Check if this is a ChatGPT signature based on headers
29
- * Uses secure URL parsing to prevent spoofing attacks
30
- */
31
- declare function isChatGPTSignature(headers: Record<string, string>): boolean;
32
-
33
- export { type SignatureVerificationResult, hasSignatureHeaders, isChatGPTSignature, verifyAgentSignature };
@@ -1,33 +0,0 @@
1
- /**
2
- * Ed25519 Signature Verification for HTTP Message Signatures
3
- * Implements proper cryptographic verification for ChatGPT and other agents
4
- *
5
- * Based on RFC 9421 (HTTP Message Signatures) and ChatGPT's implementation
6
- * Reference: https://help.openai.com/en/articles/9785974-chatgpt-user-allowlisting
7
- */
8
- /**
9
- * Signature verification result
10
- */
11
- interface SignatureVerificationResult {
12
- isValid: boolean;
13
- agent?: string;
14
- keyid?: string;
15
- confidence: number;
16
- reason?: string;
17
- verificationMethod: 'signature' | 'none';
18
- }
19
- /**
20
- * Verify HTTP Message Signature for AI agents
21
- */
22
- declare function verifyAgentSignature(method: string, path: string, headers: Record<string, string>): Promise<SignatureVerificationResult>;
23
- /**
24
- * Quick check if signature headers are present (for performance)
25
- */
26
- declare function hasSignatureHeaders(headers: Record<string, string>): boolean;
27
- /**
28
- * Check if this is a ChatGPT signature based on headers
29
- * Uses secure URL parsing to prevent spoofing attacks
30
- */
31
- declare function isChatGPTSignature(headers: Record<string, string>): boolean;
32
-
33
- export { type SignatureVerificationResult, hasSignatureHeaders, isChatGPTSignature, verifyAgentSignature };