@webdecoy/nextjs 0.11.0 → 0.12.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, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
2
+ import { ProtectOptions, TrustedProxies, 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
  /**
@@ -20,8 +20,28 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
20
20
  */
21
21
  mode?: 'monitor' | 'enforce';
22
22
  /**
23
- * Custom function to extract IP address from request
24
- * By default, uses x-forwarded-for or x-real-ip headers
23
+ * How many proxies sit between the client and this middleware, or which ones.
24
+ * Defaults to `1` — the hosting platform in front of you.
25
+ *
26
+ * THIS CHANGED IN 0.12.0, and it is a behaviour change worth reading.
27
+ *
28
+ * Before, the middleware read the LEFTMOST `X-Forwarded-For` value. That is
29
+ * the one value in the header the client writes itself, so a single
30
+ * `-H 'X-Forwarded-For: 1.2.3.4'` bought a fresh rate-limit bucket per forged
31
+ * address and put an address of the caller's choosing on every detection we
32
+ * reported. It now reads from the right, past the hops you say you have.
33
+ *
34
+ * Edge middleware has no socket to fall back on, so unlike the Express and
35
+ * Fastify adapters there is no safe "believe nothing" default here — `1` is
36
+ * correct on Vercel and on any single-proxy deployment. Behind a CDN in front
37
+ * of your platform, set `2`. Behind Cloudflare with the origin locked to it,
38
+ * `'cloudflare'` is stronger than counting.
39
+ */
40
+ trustProxy?: TrustedProxies;
41
+ /**
42
+ * Custom function to extract IP address from request.
43
+ *
44
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
25
45
  */
26
46
  getIP?: (req: NextRequest) => string;
27
47
  /**
@@ -225,10 +245,17 @@ declare function honeytokenLink(apiKey: string, options?: {
225
245
  * ```
226
246
  */
227
247
 
248
+ interface NextCaptchaOptions extends CaptchaEndpointsOptions {
249
+ /**
250
+ * How many proxies sit between the client and this handler, or which ones.
251
+ * Defaults to `1`. Same meaning as the middleware's option.
252
+ */
253
+ trustProxy?: TrustedProxies;
254
+ }
228
255
  interface NextCaptchaHandlers {
229
256
  GET: (req: Request) => Promise<Response>;
230
257
  POST: (req: Request) => Promise<Response>;
231
258
  }
232
- declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
259
+ declare function createCaptchaHandler(options?: NextCaptchaOptions): NextCaptchaHandlers;
233
260
 
234
- export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
261
+ export { type NextCaptchaHandlers, type NextCaptchaOptions, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
2
+ import { ProtectOptions, TrustedProxies, 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
  /**
@@ -20,8 +20,28 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
20
20
  */
21
21
  mode?: 'monitor' | 'enforce';
22
22
  /**
23
- * Custom function to extract IP address from request
24
- * By default, uses x-forwarded-for or x-real-ip headers
23
+ * How many proxies sit between the client and this middleware, or which ones.
24
+ * Defaults to `1` — the hosting platform in front of you.
25
+ *
26
+ * THIS CHANGED IN 0.12.0, and it is a behaviour change worth reading.
27
+ *
28
+ * Before, the middleware read the LEFTMOST `X-Forwarded-For` value. That is
29
+ * the one value in the header the client writes itself, so a single
30
+ * `-H 'X-Forwarded-For: 1.2.3.4'` bought a fresh rate-limit bucket per forged
31
+ * address and put an address of the caller's choosing on every detection we
32
+ * reported. It now reads from the right, past the hops you say you have.
33
+ *
34
+ * Edge middleware has no socket to fall back on, so unlike the Express and
35
+ * Fastify adapters there is no safe "believe nothing" default here — `1` is
36
+ * correct on Vercel and on any single-proxy deployment. Behind a CDN in front
37
+ * of your platform, set `2`. Behind Cloudflare with the origin locked to it,
38
+ * `'cloudflare'` is stronger than counting.
39
+ */
40
+ trustProxy?: TrustedProxies;
41
+ /**
42
+ * Custom function to extract IP address from request.
43
+ *
44
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
25
45
  */
26
46
  getIP?: (req: NextRequest) => string;
27
47
  /**
@@ -225,10 +245,17 @@ declare function honeytokenLink(apiKey: string, options?: {
225
245
  * ```
226
246
  */
227
247
 
248
+ interface NextCaptchaOptions extends CaptchaEndpointsOptions {
249
+ /**
250
+ * How many proxies sit between the client and this handler, or which ones.
251
+ * Defaults to `1`. Same meaning as the middleware's option.
252
+ */
253
+ trustProxy?: TrustedProxies;
254
+ }
228
255
  interface NextCaptchaHandlers {
229
256
  GET: (req: Request) => Promise<Response>;
230
257
  POST: (req: Request) => Promise<Response>;
231
258
  }
232
- declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
259
+ declare function createCaptchaHandler(options?: NextCaptchaOptions): NextCaptchaHandlers;
233
260
 
234
- export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
261
+ export { type NextCaptchaHandlers, type NextCaptchaOptions, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
package/dist/index.js CHANGED
@@ -31,20 +31,13 @@ module.exports = __toCommonJS(index_exports);
31
31
  // src/middleware.ts
32
32
  var import_server = require("next/server");
33
33
  var import_node = require("@webdecoy/node");
34
- function defaultGetIP(req) {
35
- const forwardedFor = req.headers.get("x-forwarded-for");
36
- if (forwardedFor) {
37
- return forwardedFor.split(",")[0].trim();
38
- }
39
- const realIP = req.headers.get("x-real-ip");
40
- if (realIP) {
41
- return realIP;
42
- }
43
- const vercelIP = req.headers.get("x-vercel-forwarded-for");
44
- if (vercelIP) {
45
- return vercelIP.split(",")[0].trim();
46
- }
47
- return "127.0.0.1";
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";
48
41
  }
49
42
  function defaultOnBlocked(req, detection) {
50
43
  return import_server.NextResponse.json(
@@ -73,7 +66,7 @@ function shouldSkipPath(path, skipPaths) {
73
66
  }
74
67
  function withWebDecoy(config) {
75
68
  const sdk = new import_node.WebDecoy(config);
76
- const getIP2 = config.getIP || defaultGetIP;
69
+ const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
77
70
  const onBlocked = config.onBlocked || defaultOnBlocked;
78
71
  const mode = config.mode ?? "monitor";
79
72
  const onError = config.onError || defaultOnError;
@@ -167,12 +160,12 @@ function withBotProtection(handler, config) {
167
160
  return (async (...args) => {
168
161
  const [req, res] = args;
169
162
  try {
170
- const forwardedFor = req.headers["x-forwarded-for"];
171
- const ip = forwardedFor ? (Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor).split(",")[0].trim() : req.headers["x-real-ip"] || req.socket?.remoteAddress || "127.0.0.1";
163
+ const peer = req.socket?.remoteAddress;
164
+ const ip = (0, import_node.resolveClientIp)({ headers: req.headers, peer, trustProxy: config.trustProxy }) ?? (0, import_node.normalizeIp)(peer) ?? "127.0.0.1";
172
165
  const metadata = {
173
166
  method: req.method || "GET",
174
167
  path: req.url || "/",
175
- ip: typeof ip === "string" ? ip : "127.0.0.1",
168
+ ip,
176
169
  user_agent: req.headers["user-agent"],
177
170
  headers: req.headers,
178
171
  timestamp: Date.now()
@@ -237,10 +230,10 @@ function honeytokenLink(apiKey, options = {}) {
237
230
 
238
231
  // src/captcha.ts
239
232
  var import_node4 = require("@webdecoy/node");
240
- function getIP(headers) {
241
- const forwarded = headers.get("x-forwarded-for");
242
- if (forwarded) return forwarded.split(",")[0].trim();
243
- return headers.get("x-real-ip") || headers.get("x-vercel-forwarded-for") || "127.0.0.1";
233
+ 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";
244
237
  }
245
238
  function headersToRecord(headers) {
246
239
  const out = {};
@@ -272,7 +265,7 @@ function createCaptchaHandler(options) {
272
265
  query,
273
266
  headers,
274
267
  body,
275
- ip: getIP(req.headers)
268
+ ip: getIP(req.headers, options?.trustProxy)
276
269
  });
277
270
  if (!result) {
278
271
  return new Response(JSON.stringify({ error: "not_found" }), {
package/dist/index.mjs CHANGED
@@ -1,20 +1,17 @@
1
1
  // src/middleware.ts
2
2
  import { NextResponse } from "next/server";
3
- import { WebDecoy } from "@webdecoy/node";
4
- function defaultGetIP(req) {
5
- const forwardedFor = req.headers.get("x-forwarded-for");
6
- if (forwardedFor) {
7
- return forwardedFor.split(",")[0].trim();
8
- }
9
- const realIP = req.headers.get("x-real-ip");
10
- if (realIP) {
11
- return realIP;
12
- }
13
- const vercelIP = req.headers.get("x-vercel-forwarded-for");
14
- if (vercelIP) {
15
- return vercelIP.split(",")[0].trim();
16
- }
17
- return "127.0.0.1";
3
+ import {
4
+ WebDecoy,
5
+ resolveClientIp,
6
+ normalizeIp
7
+ } from "@webdecoy/node";
8
+ 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";
18
15
  }
19
16
  function defaultOnBlocked(req, detection) {
20
17
  return NextResponse.json(
@@ -43,7 +40,7 @@ function shouldSkipPath(path, skipPaths) {
43
40
  }
44
41
  function withWebDecoy(config) {
45
42
  const sdk = new WebDecoy(config);
46
- const getIP2 = config.getIP || defaultGetIP;
43
+ const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
47
44
  const onBlocked = config.onBlocked || defaultOnBlocked;
48
45
  const mode = config.mode ?? "monitor";
49
46
  const onError = config.onError || defaultOnError;
@@ -137,12 +134,12 @@ function withBotProtection(handler, config) {
137
134
  return (async (...args) => {
138
135
  const [req, res] = args;
139
136
  try {
140
- const forwardedFor = req.headers["x-forwarded-for"];
141
- const ip = forwardedFor ? (Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor).split(",")[0].trim() : req.headers["x-real-ip"] || req.socket?.remoteAddress || "127.0.0.1";
137
+ const peer = req.socket?.remoteAddress;
138
+ const ip = resolveClientIp({ headers: req.headers, peer, trustProxy: config.trustProxy }) ?? normalizeIp(peer) ?? "127.0.0.1";
142
139
  const metadata = {
143
140
  method: req.method || "GET",
144
141
  path: req.url || "/",
145
- ip: typeof ip === "string" ? ip : "127.0.0.1",
142
+ ip,
146
143
  user_agent: req.headers["user-agent"],
147
144
  headers: req.headers,
148
145
  timestamp: Date.now()
@@ -206,11 +203,15 @@ function honeytokenLink(apiKey, options = {}) {
206
203
  }
207
204
 
208
205
  // src/captcha.ts
209
- import { createCaptchaEndpoints } from "@webdecoy/node";
210
- function getIP(headers) {
211
- const forwarded = headers.get("x-forwarded-for");
212
- if (forwarded) return forwarded.split(",")[0].trim();
213
- return headers.get("x-real-ip") || headers.get("x-vercel-forwarded-for") || "127.0.0.1";
206
+ import {
207
+ createCaptchaEndpoints,
208
+ resolveClientIp as resolveClientIp2,
209
+ normalizeIp as normalizeIp2
210
+ } from "@webdecoy/node";
211
+ 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";
214
215
  }
215
216
  function headersToRecord(headers) {
216
217
  const out = {};
@@ -242,7 +243,7 @@ function createCaptchaHandler(options) {
242
243
  query,
243
244
  headers,
244
245
  body,
245
- ip: getIP(req.headers)
246
+ ip: getIP(req.headers, options?.trustProxy)
246
247
  });
247
248
  if (!result) {
248
249
  return new Response(JSON.stringify({ error: "not_found" }), {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.11.0",
3
+ "version": "0.12.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/**/*.ts",
21
+ "lint": "eslint src --max-warnings 5",
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.11.0"
45
+ "@webdecoy/node": "^0.12.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "next": ">=13.0.0"
@@ -50,9 +50,6 @@
50
50
  "devDependencies": {
51
51
  "@types/jest": "^29.5.11",
52
52
  "@types/node": "^20.11.0",
53
- "@typescript-eslint/eslint-plugin": "^6.18.1",
54
- "@typescript-eslint/parser": "^6.18.1",
55
- "eslint": "^8.56.0",
56
53
  "jest": "^29.7.0",
57
54
  "next": "^14.0.0",
58
55
  "ts-jest": "^29.1.1",