@webdecoy/nextjs 0.12.0 → 0.14.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/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, TrustedProxies, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
2
+ import { ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
3
3
  export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
@@ -48,7 +48,15 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
48
48
  * Custom function to handle blocked requests
49
49
  * By default, returns 403 Forbidden JSON response
50
50
  */
51
- onBlocked?: (req: NextRequest, detection: any) => NextResponse;
51
+ /**
52
+ * Called when a request would be blocked.
53
+ *
54
+ * `detection` is the detection response, as before. `decision` is the full
55
+ * typed verdict — `conclusion`, every rule's outcome including the ones that
56
+ * dry-ran or never ran, and `deniedBy('tripwire')` — for handlers that need to
57
+ * know WHY rather than just THAT.
58
+ */
59
+ onBlocked?: (req: NextRequest, detection: SDKDetectionResponse, decision: ProtectResult) => NextResponse;
52
60
  /**
53
61
  * Custom function to handle errors
54
62
  * By default, logs error and allows request (fail open)
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, TrustedProxies, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
2
+ import { ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
3
3
  export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
@@ -48,7 +48,15 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
48
48
  * Custom function to handle blocked requests
49
49
  * By default, returns 403 Forbidden JSON response
50
50
  */
51
- onBlocked?: (req: NextRequest, detection: any) => NextResponse;
51
+ /**
52
+ * Called when a request would be blocked.
53
+ *
54
+ * `detection` is the detection response, as before. `decision` is the full
55
+ * typed verdict — `conclusion`, every rule's outcome including the ones that
56
+ * dry-ran or never ran, and `deniedBy('tripwire')` — for handlers that need to
57
+ * know WHY rather than just THAT.
58
+ */
59
+ onBlocked?: (req: NextRequest, detection: SDKDetectionResponse, decision: ProtectResult) => NextResponse;
52
60
  /**
53
61
  * Custom function to handle errors
54
62
  * By default, logs error and allows request (fail open)
package/dist/index.js CHANGED
@@ -32,12 +32,7 @@ module.exports = __toCommonJS(index_exports);
32
32
  var import_server = require("next/server");
33
33
  var import_node = require("@webdecoy/node");
34
34
  function resolveIP(req, trustProxy) {
35
- const fromChain = (0, import_node.resolveClientIp)({
36
- headers: req.headers,
37
- trustProxy: trustProxy ?? 1
38
- });
39
- if (fromChain) return fromChain;
40
- return (0, import_node.normalizeIp)(req.headers.get("x-real-ip")) ?? (0, import_node.normalizeIp)(req.headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
35
+ return (0, import_node.resolveClientIp)({ headers: req.headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
41
36
  }
42
37
  function defaultOnBlocked(req, detection) {
43
38
  return import_server.NextResponse.json(
@@ -53,17 +48,6 @@ function defaultOnError(req, error) {
53
48
  console.error("[WebDecoy] Middleware error:", error);
54
49
  return null;
55
50
  }
56
- function shouldSkipPath(path, skipPaths) {
57
- if (!skipPaths || skipPaths.length === 0) {
58
- return false;
59
- }
60
- return skipPaths.some((pattern) => {
61
- if (typeof pattern === "string") {
62
- return path === pattern || path.startsWith(pattern);
63
- }
64
- return pattern.test(path);
65
- });
66
- }
67
51
  function withWebDecoy(config) {
68
52
  const sdk = new import_node.WebDecoy(config);
69
53
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
@@ -74,7 +58,7 @@ function withWebDecoy(config) {
74
58
  return async (req) => {
75
59
  try {
76
60
  const pathname = req.nextUrl.pathname;
77
- if (shouldSkipPath(pathname, skipPaths)) {
61
+ if ((0, import_node.shouldSkipPath)(pathname, skipPaths)) {
78
62
  return import_server.NextResponse.next();
79
63
  }
80
64
  const headers = {};
@@ -87,6 +71,9 @@ function withWebDecoy(config) {
87
71
  ip: getIP2(req),
88
72
  user_agent: req.headers.get("user-agent") || void 0,
89
73
  headers,
74
+ // The query is where injection payloads live, and it is not part of
75
+ // the routed path, so attackSignatures() cannot see it otherwise.
76
+ query: req.nextUrl.search ? req.nextUrl.search.slice(1) : void 0,
90
77
  timestamp: Date.now()
91
78
  };
92
79
  const result = await sdk.protect(metadata, {
@@ -106,32 +93,12 @@ function withWebDecoy(config) {
106
93
  monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
107
94
  return import_server.NextResponse.next({ request: { headers: monitorHeaders } });
108
95
  }
109
- if (!result.allowed && result.ruleResult) {
110
- const rr = result.ruleResult;
111
- if (rr.action === "THROTTLE") {
112
- const retryAfter = rr.metadata?.retryAfter ?? 60;
113
- return import_server.NextResponse.json(
114
- {
115
- error: "Too Many Requests",
116
- message: rr.reason || "Rate limit exceeded",
117
- retry_after: retryAfter
118
- },
119
- {
120
- status: 429,
121
- headers: { "Retry-After": String(retryAfter) }
122
- }
123
- );
124
- }
125
- if (rr.action === "DENY") {
126
- return import_server.NextResponse.json(
127
- {
128
- error: "Forbidden",
129
- message: rr.reason || "Access denied by rule",
130
- rule: rr.rule
131
- },
132
- { status: 403 }
133
- );
134
- }
96
+ const block = (0, import_node.ruleBlockResponse)(result);
97
+ if (block) {
98
+ return import_server.NextResponse.json(block.body, {
99
+ status: block.status,
100
+ headers: block.headers
101
+ });
135
102
  }
136
103
  if (result.allowed) {
137
104
  const requestHeaders = new Headers(req.headers);
@@ -143,7 +110,7 @@ function withWebDecoy(config) {
143
110
  }
144
111
  return import_server.NextResponse.next({ request: { headers: requestHeaders } });
145
112
  } else {
146
- return onBlocked(req, result.detection);
113
+ return onBlocked(req, result.detection, result);
147
114
  }
148
115
  } catch (error) {
149
116
  const errorResponse = onError(req, error);
@@ -168,6 +135,9 @@ function withBotProtection(handler, config) {
168
135
  ip,
169
136
  user_agent: req.headers["user-agent"],
170
137
  headers: req.headers,
138
+ // The query is where injection payloads live, and it is not part of
139
+ // the routed path, so attackSignatures() cannot see it otherwise.
140
+ query: req.url?.includes("?") ? req.url.slice(req.url.indexOf("?") + 1) : void 0,
171
141
  timestamp: Date.now()
172
142
  };
173
143
  const result = await sdk.protect(metadata, {
@@ -176,14 +146,12 @@ function withBotProtection(handler, config) {
176
146
  metadata: config.metadata
177
147
  });
178
148
  if (!result.allowed) {
179
- if (result.ruleResult?.action === "THROTTLE") {
180
- const retryAfter = result.ruleResult.metadata?.retryAfter ?? 60;
181
- res.setHeader("Retry-After", String(retryAfter));
182
- return res.status(429).json({
183
- error: "Too Many Requests",
184
- message: result.ruleResult.reason || "Rate limit exceeded",
185
- retry_after: retryAfter
186
- });
149
+ const block = (0, import_node.ruleBlockResponse)(result);
150
+ if (block) {
151
+ for (const [name, value] of Object.entries(block.headers)) {
152
+ res.setHeader(name, value);
153
+ }
154
+ return res.status(block.status).json(block.body);
187
155
  }
188
156
  return res.status(403).json({
189
157
  error: "Forbidden",
@@ -191,7 +159,9 @@ function withBotProtection(handler, config) {
191
159
  detection_id: result.detection?.detection_id
192
160
  });
193
161
  }
194
- req.webdecoy = result.detection;
162
+ const annotated = req;
163
+ annotated.webdecoy = result.detection;
164
+ annotated.webdecoyDecision = result;
195
165
  } catch (error) {
196
166
  console.error("[WebDecoy] Protection error:", error);
197
167
  }
@@ -231,9 +201,7 @@ function honeytokenLink(apiKey, options = {}) {
231
201
  // src/captcha.ts
232
202
  var import_node4 = require("@webdecoy/node");
233
203
  function getIP(headers, trustProxy) {
234
- const fromChain = (0, import_node4.resolveClientIp)({ headers, trustProxy: trustProxy ?? 1 });
235
- if (fromChain) return fromChain;
236
- return (0, import_node4.normalizeIp)(headers.get("x-real-ip")) ?? (0, import_node4.normalizeIp)(headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
204
+ return (0, import_node4.resolveClientIp)({ headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
237
205
  }
238
206
  function headersToRecord(headers) {
239
207
  const out = {};
package/dist/index.mjs CHANGED
@@ -3,15 +3,12 @@ import { NextResponse } from "next/server";
3
3
  import {
4
4
  WebDecoy,
5
5
  resolveClientIp,
6
- normalizeIp
6
+ normalizeIp,
7
+ shouldSkipPath,
8
+ ruleBlockResponse
7
9
  } from "@webdecoy/node";
8
10
  function resolveIP(req, trustProxy) {
9
- const fromChain = resolveClientIp({
10
- headers: req.headers,
11
- trustProxy: trustProxy ?? 1
12
- });
13
- if (fromChain) return fromChain;
14
- return normalizeIp(req.headers.get("x-real-ip")) ?? normalizeIp(req.headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
11
+ return resolveClientIp({ headers: req.headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
15
12
  }
16
13
  function defaultOnBlocked(req, detection) {
17
14
  return NextResponse.json(
@@ -27,17 +24,6 @@ function defaultOnError(req, error) {
27
24
  console.error("[WebDecoy] Middleware error:", error);
28
25
  return null;
29
26
  }
30
- function shouldSkipPath(path, skipPaths) {
31
- if (!skipPaths || skipPaths.length === 0) {
32
- return false;
33
- }
34
- return skipPaths.some((pattern) => {
35
- if (typeof pattern === "string") {
36
- return path === pattern || path.startsWith(pattern);
37
- }
38
- return pattern.test(path);
39
- });
40
- }
41
27
  function withWebDecoy(config) {
42
28
  const sdk = new WebDecoy(config);
43
29
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
@@ -61,6 +47,9 @@ function withWebDecoy(config) {
61
47
  ip: getIP2(req),
62
48
  user_agent: req.headers.get("user-agent") || void 0,
63
49
  headers,
50
+ // The query is where injection payloads live, and it is not part of
51
+ // the routed path, so attackSignatures() cannot see it otherwise.
52
+ query: req.nextUrl.search ? req.nextUrl.search.slice(1) : void 0,
64
53
  timestamp: Date.now()
65
54
  };
66
55
  const result = await sdk.protect(metadata, {
@@ -80,32 +69,12 @@ function withWebDecoy(config) {
80
69
  monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
81
70
  return NextResponse.next({ request: { headers: monitorHeaders } });
82
71
  }
83
- if (!result.allowed && result.ruleResult) {
84
- const rr = result.ruleResult;
85
- if (rr.action === "THROTTLE") {
86
- const retryAfter = rr.metadata?.retryAfter ?? 60;
87
- return NextResponse.json(
88
- {
89
- error: "Too Many Requests",
90
- message: rr.reason || "Rate limit exceeded",
91
- retry_after: retryAfter
92
- },
93
- {
94
- status: 429,
95
- headers: { "Retry-After": String(retryAfter) }
96
- }
97
- );
98
- }
99
- if (rr.action === "DENY") {
100
- return NextResponse.json(
101
- {
102
- error: "Forbidden",
103
- message: rr.reason || "Access denied by rule",
104
- rule: rr.rule
105
- },
106
- { status: 403 }
107
- );
108
- }
72
+ const block = ruleBlockResponse(result);
73
+ if (block) {
74
+ return NextResponse.json(block.body, {
75
+ status: block.status,
76
+ headers: block.headers
77
+ });
109
78
  }
110
79
  if (result.allowed) {
111
80
  const requestHeaders = new Headers(req.headers);
@@ -117,7 +86,7 @@ function withWebDecoy(config) {
117
86
  }
118
87
  return NextResponse.next({ request: { headers: requestHeaders } });
119
88
  } else {
120
- return onBlocked(req, result.detection);
89
+ return onBlocked(req, result.detection, result);
121
90
  }
122
91
  } catch (error) {
123
92
  const errorResponse = onError(req, error);
@@ -142,6 +111,9 @@ function withBotProtection(handler, config) {
142
111
  ip,
143
112
  user_agent: req.headers["user-agent"],
144
113
  headers: req.headers,
114
+ // The query is where injection payloads live, and it is not part of
115
+ // the routed path, so attackSignatures() cannot see it otherwise.
116
+ query: req.url?.includes("?") ? req.url.slice(req.url.indexOf("?") + 1) : void 0,
145
117
  timestamp: Date.now()
146
118
  };
147
119
  const result = await sdk.protect(metadata, {
@@ -150,14 +122,12 @@ function withBotProtection(handler, config) {
150
122
  metadata: config.metadata
151
123
  });
152
124
  if (!result.allowed) {
153
- if (result.ruleResult?.action === "THROTTLE") {
154
- const retryAfter = result.ruleResult.metadata?.retryAfter ?? 60;
155
- res.setHeader("Retry-After", String(retryAfter));
156
- return res.status(429).json({
157
- error: "Too Many Requests",
158
- message: result.ruleResult.reason || "Rate limit exceeded",
159
- retry_after: retryAfter
160
- });
125
+ const block = ruleBlockResponse(result);
126
+ if (block) {
127
+ for (const [name, value] of Object.entries(block.headers)) {
128
+ res.setHeader(name, value);
129
+ }
130
+ return res.status(block.status).json(block.body);
161
131
  }
162
132
  return res.status(403).json({
163
133
  error: "Forbidden",
@@ -165,7 +135,9 @@ function withBotProtection(handler, config) {
165
135
  detection_id: result.detection?.detection_id
166
136
  });
167
137
  }
168
- req.webdecoy = result.detection;
138
+ const annotated = req;
139
+ annotated.webdecoy = result.detection;
140
+ annotated.webdecoyDecision = result;
169
141
  } catch (error) {
170
142
  console.error("[WebDecoy] Protection error:", error);
171
143
  }
@@ -205,13 +177,10 @@ function honeytokenLink(apiKey, options = {}) {
205
177
  // src/captcha.ts
206
178
  import {
207
179
  createCaptchaEndpoints,
208
- resolveClientIp as resolveClientIp2,
209
- normalizeIp as normalizeIp2
180
+ resolveClientIp as resolveClientIp2
210
181
  } from "@webdecoy/node";
211
182
  function getIP(headers, trustProxy) {
212
- const fromChain = resolveClientIp2({ headers, trustProxy: trustProxy ?? 1 });
213
- if (fromChain) return fromChain;
214
- return normalizeIp2(headers.get("x-real-ip")) ?? normalizeIp2(headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
183
+ return resolveClientIp2({ headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
215
184
  }
216
185
  function headersToRecord(headers) {
217
186
  const out = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Web Decoy middleware for Next.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "build": "tsup src/index.ts --format cjs,esm --dts --clean",
19
19
  "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
20
20
  "test": "jest --passWithNoTests",
21
- "lint": "eslint src --max-warnings 5",
21
+ "lint": "eslint src --max-warnings 2",
22
22
  "clean": "rm -rf dist",
23
23
  "check:edge": "node ../../scripts/check-edge.mjs src/index.ts"
24
24
  },
@@ -42,7 +42,7 @@
42
42
  "url": "https://github.com/WebDecoy/node/issues"
43
43
  },
44
44
  "dependencies": {
45
- "@webdecoy/node": "^0.12.0"
45
+ "@webdecoy/node": "^0.14.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "next": ">=13.0.0"