@webdecoy/express 0.11.1 → 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 { Request, Response, NextFunction, RequestHandler } from 'express';
2
- import { EdgeVerdict, ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
2
+ import { EdgeVerdict, ProtectOptions, TrustedProxies, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
3
  export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
@@ -38,8 +38,29 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
38
38
  */
39
39
  honeytoken?: boolean;
40
40
  /**
41
- * Custom function to extract IP address from request
42
- * By default, uses req.ip or x-forwarded-for header
41
+ * How much of the `X-Forwarded-For` chain to believe.
42
+ *
43
+ * Leave this unset and Express decides: `req.ip` already honours the app's own
44
+ * `trust proxy` setting, which defaults to the socket address. Set it to
45
+ * override that for WebDecoy alone — a number of trusted hops, `'cloudflare'`,
46
+ * or CIDRs of your proxies.
47
+ *
48
+ * THIS CHANGED IN 0.12.0, and it is a behaviour change worth reading.
49
+ *
50
+ * Before, the middleware read the leftmost `X-Forwarded-For` value and called
51
+ * it the client — the one value in that header the client writes itself. A
52
+ * single `-H 'X-Forwarded-For: 1.2.3.4'` bought a fresh rate-limit bucket per
53
+ * forged address and put an address of the caller's choosing on every
54
+ * detection we reported.
55
+ *
56
+ * If your app is behind a proxy and does not already set `trust proxy`, set
57
+ * this, or every request will be attributed to the proxy.
58
+ */
59
+ trustProxy?: TrustedProxies;
60
+ /**
61
+ * Custom function to extract IP address from request.
62
+ *
63
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
43
64
  */
44
65
  getIP?: (req: Request) => string;
45
66
  /**
@@ -134,6 +155,14 @@ declare global {
134
155
  * ```
135
156
  */
136
157
 
137
- declare function webdecoyCaptcha(options?: CaptchaEndpointsOptions): RequestHandler;
158
+ interface ExpressCaptchaOptions extends CaptchaEndpointsOptions {
159
+ /**
160
+ * How much of the `X-Forwarded-For` chain to believe. Same meaning and same
161
+ * default as the middleware's option: unset defers to Express's own
162
+ * `trust proxy` setting.
163
+ */
164
+ trustProxy?: TrustedProxies;
165
+ }
166
+ declare function webdecoyCaptcha(options?: ExpressCaptchaOptions): RequestHandler;
138
167
 
139
- export { type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
168
+ export { type ExpressCaptchaOptions, type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Request, Response, NextFunction, RequestHandler } from 'express';
2
- import { EdgeVerdict, ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
2
+ import { EdgeVerdict, ProtectOptions, TrustedProxies, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
3
  export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
@@ -38,8 +38,29 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
38
38
  */
39
39
  honeytoken?: boolean;
40
40
  /**
41
- * Custom function to extract IP address from request
42
- * By default, uses req.ip or x-forwarded-for header
41
+ * How much of the `X-Forwarded-For` chain to believe.
42
+ *
43
+ * Leave this unset and Express decides: `req.ip` already honours the app's own
44
+ * `trust proxy` setting, which defaults to the socket address. Set it to
45
+ * override that for WebDecoy alone — a number of trusted hops, `'cloudflare'`,
46
+ * or CIDRs of your proxies.
47
+ *
48
+ * THIS CHANGED IN 0.12.0, and it is a behaviour change worth reading.
49
+ *
50
+ * Before, the middleware read the leftmost `X-Forwarded-For` value and called
51
+ * it the client — the one value in that header the client writes itself. A
52
+ * single `-H 'X-Forwarded-For: 1.2.3.4'` bought a fresh rate-limit bucket per
53
+ * forged address and put an address of the caller's choosing on every
54
+ * detection we reported.
55
+ *
56
+ * If your app is behind a proxy and does not already set `trust proxy`, set
57
+ * this, or every request will be attributed to the proxy.
58
+ */
59
+ trustProxy?: TrustedProxies;
60
+ /**
61
+ * Custom function to extract IP address from request.
62
+ *
63
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
43
64
  */
44
65
  getIP?: (req: Request) => string;
45
66
  /**
@@ -134,6 +155,14 @@ declare global {
134
155
  * ```
135
156
  */
136
157
 
137
- declare function webdecoyCaptcha(options?: CaptchaEndpointsOptions): RequestHandler;
158
+ interface ExpressCaptchaOptions extends CaptchaEndpointsOptions {
159
+ /**
160
+ * How much of the `X-Forwarded-For` chain to believe. Same meaning and same
161
+ * default as the middleware's option: unset defers to Express's own
162
+ * `trust proxy` setting.
163
+ */
164
+ trustProxy?: TrustedProxies;
165
+ }
166
+ declare function webdecoyCaptcha(options?: ExpressCaptchaOptions): RequestHandler;
138
167
 
139
- export { type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
168
+ export { type ExpressCaptchaOptions, type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
package/dist/index.js CHANGED
@@ -28,17 +28,12 @@ module.exports = __toCommonJS(index_exports);
28
28
  // src/middleware.ts
29
29
  var import_node = require("@webdecoy/node");
30
30
  var import_node2 = require("@webdecoy/node");
31
- function defaultGetIP(req) {
32
- const forwardedFor = req.headers["x-forwarded-for"];
33
- if (forwardedFor) {
34
- const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
35
- return ips.split(",")[0].trim();
31
+ function resolveIP(req, trustProxy) {
32
+ const peer = req.socket?.remoteAddress;
33
+ if (trustProxy === void 0) {
34
+ return (0, import_node2.normalizeIp)(req.ip) ?? (0, import_node2.normalizeIp)(peer) ?? "127.0.0.1";
36
35
  }
37
- const realIP = req.headers["x-real-ip"];
38
- if (realIP) {
39
- return Array.isArray(realIP) ? realIP[0] : realIP;
40
- }
41
- return req.ip || req.socket.remoteAddress || "127.0.0.1";
36
+ return (0, import_node2.resolveClientIp)({ headers: req.headers, peer, trustProxy }) ?? (0, import_node2.normalizeIp)(peer) ?? "127.0.0.1";
42
37
  }
43
38
  function defaultOnBlocked(req, res, detection, _next) {
44
39
  res.status(403).json({
@@ -63,7 +58,7 @@ function shouldSkipPath(path, skipPaths) {
63
58
  }
64
59
  function webdecoy(config) {
65
60
  const sdk = new import_node.WebDecoy(config);
66
- const getIP2 = config.getIP || defaultGetIP;
61
+ const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
67
62
  const onBlocked = config.onBlocked || defaultOnBlocked;
68
63
  const mode = config.mode ?? "monitor";
69
64
  const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
@@ -172,15 +167,12 @@ function webdecoy(config) {
172
167
 
173
168
  // src/captcha.ts
174
169
  var import_node3 = require("@webdecoy/node");
175
- function getIP(req) {
176
- const forwardedFor = req.headers["x-forwarded-for"];
177
- if (forwardedFor) {
178
- const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
179
- return ips.split(",")[0].trim();
170
+ function getIP(req, trustProxy) {
171
+ const peer = req.socket?.remoteAddress;
172
+ if (trustProxy === void 0) {
173
+ return (0, import_node3.normalizeIp)(req.ip) ?? (0, import_node3.normalizeIp)(peer) ?? "127.0.0.1";
180
174
  }
181
- const realIP = req.headers["x-real-ip"];
182
- if (realIP) return Array.isArray(realIP) ? realIP[0] : realIP;
183
- return req.ip || req.socket.remoteAddress || "127.0.0.1";
175
+ return (0, import_node3.resolveClientIp)({ headers: req.headers, peer, trustProxy }) ?? (0, import_node3.normalizeIp)(peer) ?? "127.0.0.1";
184
176
  }
185
177
  function normalizeQuery(query) {
186
178
  const out = {};
@@ -198,7 +190,7 @@ function webdecoyCaptcha(options) {
198
190
  query: normalizeQuery(req.query),
199
191
  headers: req.headers,
200
192
  body: req.body,
201
- ip: getIP(req)
193
+ ip: getIP(req, options?.trustProxy)
202
194
  });
203
195
  if (!result) {
204
196
  next();
package/dist/index.mjs CHANGED
@@ -4,19 +4,16 @@ import {
4
4
  siteHoneytoken,
5
5
  injectHoneytokenLink,
6
6
  isInjectableHtml,
7
- tripwire
7
+ tripwire,
8
+ resolveClientIp,
9
+ normalizeIp
8
10
  } from "@webdecoy/node";
9
- function defaultGetIP(req) {
10
- const forwardedFor = req.headers["x-forwarded-for"];
11
- if (forwardedFor) {
12
- const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
13
- return ips.split(",")[0].trim();
11
+ function resolveIP(req, trustProxy) {
12
+ const peer = req.socket?.remoteAddress;
13
+ if (trustProxy === void 0) {
14
+ return normalizeIp(req.ip) ?? normalizeIp(peer) ?? "127.0.0.1";
14
15
  }
15
- const realIP = req.headers["x-real-ip"];
16
- if (realIP) {
17
- return Array.isArray(realIP) ? realIP[0] : realIP;
18
- }
19
- return req.ip || req.socket.remoteAddress || "127.0.0.1";
16
+ return resolveClientIp({ headers: req.headers, peer, trustProxy }) ?? normalizeIp(peer) ?? "127.0.0.1";
20
17
  }
21
18
  function defaultOnBlocked(req, res, detection, _next) {
22
19
  res.status(403).json({
@@ -41,7 +38,7 @@ function shouldSkipPath(path, skipPaths) {
41
38
  }
42
39
  function webdecoy(config) {
43
40
  const sdk = new WebDecoy(config);
44
- const getIP2 = config.getIP || defaultGetIP;
41
+ const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
45
42
  const onBlocked = config.onBlocked || defaultOnBlocked;
46
43
  const mode = config.mode ?? "monitor";
47
44
  const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
@@ -149,16 +146,17 @@ function webdecoy(config) {
149
146
  }
150
147
 
151
148
  // src/captcha.ts
152
- import { createCaptchaEndpoints } from "@webdecoy/node";
153
- function getIP(req) {
154
- const forwardedFor = req.headers["x-forwarded-for"];
155
- if (forwardedFor) {
156
- const ips = Array.isArray(forwardedFor) ? forwardedFor[0] : forwardedFor;
157
- return ips.split(",")[0].trim();
149
+ import {
150
+ createCaptchaEndpoints,
151
+ resolveClientIp as resolveClientIp2,
152
+ normalizeIp as normalizeIp2
153
+ } from "@webdecoy/node";
154
+ function getIP(req, trustProxy) {
155
+ const peer = req.socket?.remoteAddress;
156
+ if (trustProxy === void 0) {
157
+ return normalizeIp2(req.ip) ?? normalizeIp2(peer) ?? "127.0.0.1";
158
158
  }
159
- const realIP = req.headers["x-real-ip"];
160
- if (realIP) return Array.isArray(realIP) ? realIP[0] : realIP;
161
- return req.ip || req.socket.remoteAddress || "127.0.0.1";
159
+ return resolveClientIp2({ headers: req.headers, peer, trustProxy }) ?? normalizeIp2(peer) ?? "127.0.0.1";
162
160
  }
163
161
  function normalizeQuery(query) {
164
162
  const out = {};
@@ -176,7 +174,7 @@ function webdecoyCaptcha(options) {
176
174
  query: normalizeQuery(req.query),
177
175
  headers: req.headers,
178
176
  body: req.body,
179
- ip: getIP(req)
177
+ ip: getIP(req, options?.trustProxy)
180
178
  });
181
179
  if (!result) {
182
180
  next();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.11.1",
3
+ "version": "0.12.0",
4
4
  "description": "Web Decoy middleware for Express.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 14",
22
22
  "clean": "rm -rf dist"
23
23
  },
24
24
  "keywords": [
@@ -40,7 +40,7 @@
40
40
  "url": "https://github.com/WebDecoy/node/issues"
41
41
  },
42
42
  "dependencies": {
43
- "@webdecoy/node": "^0.11.1"
43
+ "@webdecoy/node": "^0.12.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"
@@ -49,9 +49,6 @@
49
49
  "@types/express": "^4.17.21",
50
50
  "@types/jest": "^29.5.11",
51
51
  "@types/node": "^20.11.0",
52
- "@typescript-eslint/eslint-plugin": "^6.18.1",
53
- "@typescript-eslint/parser": "^6.18.1",
54
- "eslint": "^8.56.0",
55
52
  "express": "^4.18.2",
56
53
  "jest": "^29.7.0",
57
54
  "ts-jest": "^29.1.1",