@webdecoy/express 0.11.1 → 0.13.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,10 +1,7 @@
1
- import { Request, Response, NextFunction, RequestHandler } from 'express';
2
- import { EdgeVerdict, ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
1
+ import * as _webdecoy_node from '@webdecoy/node';
2
+ import { EdgeVerdict, ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
3
  export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
-
5
- /**
6
- * Web Decoy Express Middleware
7
- */
4
+ import { Request, Response, NextFunction, RequestHandler } from 'express';
8
5
 
9
6
  interface WebDecoyMiddlewareOptions extends ProtectOptions {
10
7
  /**
@@ -38,8 +35,29 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
38
35
  */
39
36
  honeytoken?: boolean;
40
37
  /**
41
- * Custom function to extract IP address from request
42
- * By default, uses req.ip or x-forwarded-for header
38
+ * How much of the `X-Forwarded-For` chain to believe.
39
+ *
40
+ * Leave this unset and Express decides: `req.ip` already honours the app's own
41
+ * `trust proxy` setting, which defaults to the socket address. Set it to
42
+ * override that for WebDecoy alone — a number of trusted hops, `'cloudflare'`,
43
+ * or CIDRs of your proxies.
44
+ *
45
+ * THIS CHANGED IN 0.12.0, and it is a behaviour change worth reading.
46
+ *
47
+ * Before, the middleware read the leftmost `X-Forwarded-For` value and called
48
+ * it the client — the one value in that header the client writes itself. A
49
+ * single `-H 'X-Forwarded-For: 1.2.3.4'` bought a fresh rate-limit bucket per
50
+ * forged address and put an address of the caller's choosing on every
51
+ * detection we reported.
52
+ *
53
+ * If your app is behind a proxy and does not already set `trust proxy`, set
54
+ * this, or every request will be attributed to the proxy.
55
+ */
56
+ trustProxy?: TrustedProxies;
57
+ /**
58
+ * Custom function to extract IP address from request.
59
+ *
60
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
43
61
  */
44
62
  getIP?: (req: Request) => string;
45
63
  /**
@@ -52,8 +70,12 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
52
70
  * `next` is passed so a handler can record the verdict and continue — the
53
71
  * omission that made monitoring impossible. Call exactly one of `next()` or a
54
72
  * response method.
73
+ *
74
+ * `decision` is the full typed verdict — `conclusion`, every rule's outcome
75
+ * including the ones that dry-ran or never ran, and `deniedBy('tripwire')` —
76
+ * for handlers that need to know WHY rather than just THAT.
55
77
  */
56
- onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
78
+ onBlocked?: (req: Request, res: Response, detection: SDKDetectionResponse, next: NextFunction, decision: ProtectResult) => void;
57
79
  /**
58
80
  * Custom function to handle errors
59
81
  * By default, logs error and allows request (fail open)
@@ -110,6 +132,13 @@ declare global {
110
132
  detection_id: string;
111
133
  rule_enforced: boolean;
112
134
  };
135
+ /**
136
+ * The full typed verdict: `conclusion`, every rule's outcome including the
137
+ * ones that dry-ran or never ran, and `deniedBy()`. Populated in monitor
138
+ * mode too, which is where it matters — that is the only place a verdict
139
+ * surfaces when nothing is blocked.
140
+ */
141
+ webdecoyDecision?: _webdecoy_node.ProtectResult;
113
142
  /** What the edge validator said about this request. */
114
143
  webdecoyEdge?: EdgeVerdict;
115
144
  }
@@ -134,6 +163,14 @@ declare global {
134
163
  * ```
135
164
  */
136
165
 
137
- declare function webdecoyCaptcha(options?: CaptchaEndpointsOptions): RequestHandler;
166
+ interface ExpressCaptchaOptions extends CaptchaEndpointsOptions {
167
+ /**
168
+ * How much of the `X-Forwarded-For` chain to believe. Same meaning and same
169
+ * default as the middleware's option: unset defers to Express's own
170
+ * `trust proxy` setting.
171
+ */
172
+ trustProxy?: TrustedProxies;
173
+ }
174
+ declare function webdecoyCaptcha(options?: ExpressCaptchaOptions): RequestHandler;
138
175
 
139
- export { type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
176
+ export { type ExpressCaptchaOptions, type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,7 @@
1
- import { Request, Response, NextFunction, RequestHandler } from 'express';
2
- import { EdgeVerdict, ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
1
+ import * as _webdecoy_node from '@webdecoy/node';
2
+ import { EdgeVerdict, ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
3
  export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
-
5
- /**
6
- * Web Decoy Express Middleware
7
- */
4
+ import { Request, Response, NextFunction, RequestHandler } from 'express';
8
5
 
9
6
  interface WebDecoyMiddlewareOptions extends ProtectOptions {
10
7
  /**
@@ -38,8 +35,29 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
38
35
  */
39
36
  honeytoken?: boolean;
40
37
  /**
41
- * Custom function to extract IP address from request
42
- * By default, uses req.ip or x-forwarded-for header
38
+ * How much of the `X-Forwarded-For` chain to believe.
39
+ *
40
+ * Leave this unset and Express decides: `req.ip` already honours the app's own
41
+ * `trust proxy` setting, which defaults to the socket address. Set it to
42
+ * override that for WebDecoy alone — a number of trusted hops, `'cloudflare'`,
43
+ * or CIDRs of your proxies.
44
+ *
45
+ * THIS CHANGED IN 0.12.0, and it is a behaviour change worth reading.
46
+ *
47
+ * Before, the middleware read the leftmost `X-Forwarded-For` value and called
48
+ * it the client — the one value in that header the client writes itself. A
49
+ * single `-H 'X-Forwarded-For: 1.2.3.4'` bought a fresh rate-limit bucket per
50
+ * forged address and put an address of the caller's choosing on every
51
+ * detection we reported.
52
+ *
53
+ * If your app is behind a proxy and does not already set `trust proxy`, set
54
+ * this, or every request will be attributed to the proxy.
55
+ */
56
+ trustProxy?: TrustedProxies;
57
+ /**
58
+ * Custom function to extract IP address from request.
59
+ *
60
+ * Overrides `trustProxy` entirely — you are choosing the address yourself.
43
61
  */
44
62
  getIP?: (req: Request) => string;
45
63
  /**
@@ -52,8 +70,12 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
52
70
  * `next` is passed so a handler can record the verdict and continue — the
53
71
  * omission that made monitoring impossible. Call exactly one of `next()` or a
54
72
  * response method.
73
+ *
74
+ * `decision` is the full typed verdict — `conclusion`, every rule's outcome
75
+ * including the ones that dry-ran or never ran, and `deniedBy('tripwire')` —
76
+ * for handlers that need to know WHY rather than just THAT.
55
77
  */
56
- onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
78
+ onBlocked?: (req: Request, res: Response, detection: SDKDetectionResponse, next: NextFunction, decision: ProtectResult) => void;
57
79
  /**
58
80
  * Custom function to handle errors
59
81
  * By default, logs error and allows request (fail open)
@@ -110,6 +132,13 @@ declare global {
110
132
  detection_id: string;
111
133
  rule_enforced: boolean;
112
134
  };
135
+ /**
136
+ * The full typed verdict: `conclusion`, every rule's outcome including the
137
+ * ones that dry-ran or never ran, and `deniedBy()`. Populated in monitor
138
+ * mode too, which is where it matters — that is the only place a verdict
139
+ * surfaces when nothing is blocked.
140
+ */
141
+ webdecoyDecision?: _webdecoy_node.ProtectResult;
113
142
  /** What the edge validator said about this request. */
114
143
  webdecoyEdge?: EdgeVerdict;
115
144
  }
@@ -134,6 +163,14 @@ declare global {
134
163
  * ```
135
164
  */
136
165
 
137
- declare function webdecoyCaptcha(options?: CaptchaEndpointsOptions): RequestHandler;
166
+ interface ExpressCaptchaOptions extends CaptchaEndpointsOptions {
167
+ /**
168
+ * How much of the `X-Forwarded-For` chain to believe. Same meaning and same
169
+ * default as the middleware's option: unset defers to Express's own
170
+ * `trust proxy` setting.
171
+ */
172
+ trustProxy?: TrustedProxies;
173
+ }
174
+ declare function webdecoyCaptcha(options?: ExpressCaptchaOptions): RequestHandler;
138
175
 
139
- export { type WebDecoyMiddlewareOptions, webdecoy, webdecoyCaptcha };
176
+ 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);
@@ -88,6 +83,9 @@ function webdecoy(config) {
88
83
  ip: getIP2(req),
89
84
  user_agent: req.headers["user-agent"],
90
85
  headers: req.headers,
86
+ // `req.path` excludes the query, which is where injection payloads
87
+ // live, so attackSignatures() cannot see them without this.
88
+ query: req.originalUrl.includes("?") ? req.originalUrl.slice(req.originalUrl.indexOf("?") + 1) : void 0,
91
89
  timestamp: Date.now()
92
90
  };
93
91
  const result = await sdk.protect(metadata, {
@@ -131,6 +129,7 @@ function webdecoy(config) {
131
129
  }
132
130
  if (mode === "monitor") {
133
131
  req.webdecoy = result.detection;
132
+ req.webdecoyDecision = result;
134
133
  req.webdecoyEdge = result.edge;
135
134
  req.webdecoyWouldBlock = !result.allowed;
136
135
  return next();
@@ -158,10 +157,11 @@ function webdecoy(config) {
158
157
  }
159
158
  if (result.allowed) {
160
159
  req.webdecoy = result.detection;
160
+ req.webdecoyDecision = result;
161
161
  req.webdecoyEdge = result.edge;
162
162
  return next();
163
163
  } else {
164
- return onBlocked(req, res, result.detection, next);
164
+ return onBlocked(req, res, result.detection, next, result);
165
165
  }
166
166
  } catch (error) {
167
167
  onError(req, res, error);
@@ -172,15 +172,12 @@ function webdecoy(config) {
172
172
 
173
173
  // src/captcha.ts
174
174
  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();
175
+ function getIP(req, trustProxy) {
176
+ const peer = req.socket?.remoteAddress;
177
+ if (trustProxy === void 0) {
178
+ return (0, import_node3.normalizeIp)(req.ip) ?? (0, import_node3.normalizeIp)(peer) ?? "127.0.0.1";
180
179
  }
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";
180
+ return (0, import_node3.resolveClientIp)({ headers: req.headers, peer, trustProxy }) ?? (0, import_node3.normalizeIp)(peer) ?? "127.0.0.1";
184
181
  }
185
182
  function normalizeQuery(query) {
186
183
  const out = {};
@@ -191,14 +188,17 @@ function normalizeQuery(query) {
191
188
  }
192
189
  function webdecoyCaptcha(options) {
193
190
  const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
194
- return async (req, res, next) => {
191
+ return (req, res, next) => {
192
+ void handle(req, res, next).catch(next);
193
+ };
194
+ async function handle(req, res, next) {
195
195
  const result = await endpoints.handle({
196
196
  method: req.method,
197
197
  pathname: req.path,
198
198
  query: normalizeQuery(req.query),
199
199
  headers: req.headers,
200
200
  body: req.body,
201
- ip: getIP(req)
201
+ ip: getIP(req, options?.trustProxy)
202
202
  });
203
203
  if (!result) {
204
204
  next();
@@ -207,7 +207,7 @@ function webdecoyCaptcha(options) {
207
207
  res.status(result.status);
208
208
  for (const [k, v] of Object.entries(result.headers)) res.setHeader(k, v);
209
209
  res.json(result.body);
210
- };
210
+ }
211
211
  }
212
212
  // Annotate the CommonJS export names for ESM import in node:
213
213
  0 && (module.exports = {
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);
@@ -66,6 +63,9 @@ function webdecoy(config) {
66
63
  ip: getIP2(req),
67
64
  user_agent: req.headers["user-agent"],
68
65
  headers: req.headers,
66
+ // `req.path` excludes the query, which is where injection payloads
67
+ // live, so attackSignatures() cannot see them without this.
68
+ query: req.originalUrl.includes("?") ? req.originalUrl.slice(req.originalUrl.indexOf("?") + 1) : void 0,
69
69
  timestamp: Date.now()
70
70
  };
71
71
  const result = await sdk.protect(metadata, {
@@ -109,6 +109,7 @@ function webdecoy(config) {
109
109
  }
110
110
  if (mode === "monitor") {
111
111
  req.webdecoy = result.detection;
112
+ req.webdecoyDecision = result;
112
113
  req.webdecoyEdge = result.edge;
113
114
  req.webdecoyWouldBlock = !result.allowed;
114
115
  return next();
@@ -136,10 +137,11 @@ function webdecoy(config) {
136
137
  }
137
138
  if (result.allowed) {
138
139
  req.webdecoy = result.detection;
140
+ req.webdecoyDecision = result;
139
141
  req.webdecoyEdge = result.edge;
140
142
  return next();
141
143
  } else {
142
- return onBlocked(req, res, result.detection, next);
144
+ return onBlocked(req, res, result.detection, next, result);
143
145
  }
144
146
  } catch (error) {
145
147
  onError(req, res, error);
@@ -149,16 +151,17 @@ function webdecoy(config) {
149
151
  }
150
152
 
151
153
  // 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();
154
+ import {
155
+ createCaptchaEndpoints,
156
+ resolveClientIp as resolveClientIp2,
157
+ normalizeIp as normalizeIp2
158
+ } from "@webdecoy/node";
159
+ function getIP(req, trustProxy) {
160
+ const peer = req.socket?.remoteAddress;
161
+ if (trustProxy === void 0) {
162
+ return normalizeIp2(req.ip) ?? normalizeIp2(peer) ?? "127.0.0.1";
158
163
  }
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";
164
+ return resolveClientIp2({ headers: req.headers, peer, trustProxy }) ?? normalizeIp2(peer) ?? "127.0.0.1";
162
165
  }
163
166
  function normalizeQuery(query) {
164
167
  const out = {};
@@ -169,14 +172,17 @@ function normalizeQuery(query) {
169
172
  }
170
173
  function webdecoyCaptcha(options) {
171
174
  const endpoints = createCaptchaEndpoints(options);
172
- return async (req, res, next) => {
175
+ return (req, res, next) => {
176
+ void handle(req, res, next).catch(next);
177
+ };
178
+ async function handle(req, res, next) {
173
179
  const result = await endpoints.handle({
174
180
  method: req.method,
175
181
  pathname: req.path,
176
182
  query: normalizeQuery(req.query),
177
183
  headers: req.headers,
178
184
  body: req.body,
179
- ip: getIP(req)
185
+ ip: getIP(req, options?.trustProxy)
180
186
  });
181
187
  if (!result) {
182
188
  next();
@@ -185,7 +191,7 @@ function webdecoyCaptcha(options) {
185
191
  res.status(result.status);
186
192
  for (const [k, v] of Object.entries(result.headers)) res.setHeader(k, v);
187
193
  res.json(result.body);
188
- };
194
+ }
189
195
  }
190
196
  export {
191
197
  webdecoy,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.11.1",
3
+ "version": "0.13.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 10",
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.13.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",