@webdecoy/express 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,10 +1,7 @@
1
- import { Request, Response, NextFunction, RequestHandler } from 'express';
2
- import { EdgeVerdict, ProtectOptions, TrustedProxies, 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
  /**
@@ -73,8 +70,12 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
73
70
  * `next` is passed so a handler can record the verdict and continue — the
74
71
  * omission that made monitoring impossible. Call exactly one of `next()` or a
75
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.
76
77
  */
77
- onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
78
+ onBlocked?: (req: Request, res: Response, detection: SDKDetectionResponse, next: NextFunction, decision: ProtectResult) => void;
78
79
  /**
79
80
  * Custom function to handle errors
80
81
  * By default, logs error and allows request (fail open)
@@ -131,6 +132,13 @@ declare global {
131
132
  detection_id: string;
132
133
  rule_enforced: boolean;
133
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;
134
142
  /** What the edge validator said about this request. */
135
143
  webdecoyEdge?: EdgeVerdict;
136
144
  }
package/dist/index.d.ts CHANGED
@@ -1,10 +1,7 @@
1
- import { Request, Response, NextFunction, RequestHandler } from 'express';
2
- import { EdgeVerdict, ProtectOptions, TrustedProxies, 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
  /**
@@ -73,8 +70,12 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
73
70
  * `next` is passed so a handler can record the verdict and continue — the
74
71
  * omission that made monitoring impossible. Call exactly one of `next()` or a
75
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.
76
77
  */
77
- onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
78
+ onBlocked?: (req: Request, res: Response, detection: SDKDetectionResponse, next: NextFunction, decision: ProtectResult) => void;
78
79
  /**
79
80
  * Custom function to handle errors
80
81
  * By default, logs error and allows request (fail open)
@@ -131,6 +132,13 @@ declare global {
131
132
  detection_id: string;
132
133
  rule_enforced: boolean;
133
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;
134
142
  /** What the edge validator said about this request. */
135
143
  webdecoyEdge?: EdgeVerdict;
136
144
  }
package/dist/index.js CHANGED
@@ -45,36 +45,20 @@ function defaultOnBlocked(req, res, detection, _next) {
45
45
  function defaultOnError(req, res, error) {
46
46
  console.error("[WebDecoy] Middleware error:", error);
47
47
  }
48
- function shouldSkipPath(path, skipPaths) {
49
- if (!skipPaths || skipPaths.length === 0) {
50
- return false;
51
- }
52
- return skipPaths.some((pattern) => {
53
- if (typeof pattern === "string") {
54
- return path === pattern || path.startsWith(pattern);
55
- }
56
- return pattern.test(path);
57
- });
58
- }
59
48
  function webdecoy(config) {
60
49
  const sdk = new import_node.WebDecoy(config);
61
50
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
62
51
  const onBlocked = config.onBlocked || defaultOnBlocked;
63
52
  const mode = config.mode ?? "monitor";
64
- const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
65
- let token = null;
66
- if (honeytokenEnabled) {
67
- void (0, import_node2.siteHoneytoken)({ secret: config.apiKey }).then((t) => {
68
- token = t;
69
- sdk.addRule((0, import_node2.tripwire)({ paths: t.activePaths, includeDefaults: false }));
70
- }).catch(() => {
71
- });
72
- }
53
+ const getToken = (0, import_node2.armSiteHoneytoken)(sdk, {
54
+ apiKey: config.apiKey,
55
+ enabled: config.honeytoken
56
+ });
73
57
  const onError = config.onError || defaultOnError;
74
58
  const skipPaths = config.skipPaths;
75
59
  return async (req, res, next) => {
76
60
  try {
77
- if (shouldSkipPath(req.path, skipPaths)) {
61
+ if ((0, import_node2.shouldSkipPath)(req.path, skipPaths)) {
78
62
  return next();
79
63
  }
80
64
  const metadata = {
@@ -83,6 +67,9 @@ function webdecoy(config) {
83
67
  ip: getIP2(req),
84
68
  user_agent: req.headers["user-agent"],
85
69
  headers: req.headers,
70
+ // `req.path` excludes the query, which is where injection payloads
71
+ // live, so attackSignatures() cannot see them without this.
72
+ query: req.originalUrl.includes("?") ? req.originalUrl.slice(req.originalUrl.indexOf("?") + 1) : void 0,
86
73
  timestamp: Date.now()
87
74
  };
88
75
  const result = await sdk.protect(metadata, {
@@ -90,8 +77,8 @@ function webdecoy(config) {
90
77
  skipLocalAnalysis: config.skipLocalAnalysis,
91
78
  metadata: config.metadata
92
79
  });
93
- if (token) {
94
- const ht = token;
80
+ const ht = getToken();
81
+ if (ht) {
95
82
  const originalWrite = res.write.bind(res);
96
83
  const originalEnd = res.end.bind(res);
97
84
  const chunks = [];
@@ -126,37 +113,26 @@ function webdecoy(config) {
126
113
  }
127
114
  if (mode === "monitor") {
128
115
  req.webdecoy = result.detection;
116
+ req.webdecoyDecision = result;
129
117
  req.webdecoyEdge = result.edge;
130
118
  req.webdecoyWouldBlock = !result.allowed;
131
119
  return next();
132
120
  }
133
- if (!result.allowed && result.ruleResult) {
134
- const rr = result.ruleResult;
135
- if (rr.action === "THROTTLE") {
136
- const retryAfter = rr.metadata?.retryAfter ?? 60;
137
- res.setHeader("Retry-After", String(retryAfter));
138
- res.status(429).json({
139
- error: "Too Many Requests",
140
- message: rr.reason || "Rate limit exceeded",
141
- retry_after: retryAfter
142
- });
143
- return;
144
- }
145
- if (rr.action === "DENY") {
146
- res.status(403).json({
147
- error: "Forbidden",
148
- message: rr.reason || "Access denied by rule",
149
- rule: rr.rule
150
- });
151
- return;
121
+ const block = (0, import_node2.ruleBlockResponse)(result);
122
+ if (block) {
123
+ for (const [name, value] of Object.entries(block.headers)) {
124
+ res.setHeader(name, value);
152
125
  }
126
+ res.status(block.status).json(block.body);
127
+ return;
153
128
  }
154
129
  if (result.allowed) {
155
130
  req.webdecoy = result.detection;
131
+ req.webdecoyDecision = result;
156
132
  req.webdecoyEdge = result.edge;
157
133
  return next();
158
134
  } else {
159
- return onBlocked(req, res, result.detection, next);
135
+ return onBlocked(req, res, result.detection, next, result);
160
136
  }
161
137
  } catch (error) {
162
138
  onError(req, res, error);
@@ -183,7 +159,10 @@ function normalizeQuery(query) {
183
159
  }
184
160
  function webdecoyCaptcha(options) {
185
161
  const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
186
- return async (req, res, next) => {
162
+ return (req, res, next) => {
163
+ void handle(req, res, next).catch(next);
164
+ };
165
+ async function handle(req, res, next) {
187
166
  const result = await endpoints.handle({
188
167
  method: req.method,
189
168
  pathname: req.path,
@@ -199,7 +178,7 @@ function webdecoyCaptcha(options) {
199
178
  res.status(result.status);
200
179
  for (const [k, v] of Object.entries(result.headers)) res.setHeader(k, v);
201
180
  res.json(result.body);
202
- };
181
+ }
203
182
  }
204
183
  // Annotate the CommonJS export names for ESM import in node:
205
184
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,12 +1,13 @@
1
1
  // src/middleware.ts
2
2
  import { WebDecoy } from "@webdecoy/node";
3
3
  import {
4
- siteHoneytoken,
5
4
  injectHoneytokenLink,
6
5
  isInjectableHtml,
7
- tripwire,
8
6
  resolveClientIp,
9
- normalizeIp
7
+ normalizeIp,
8
+ shouldSkipPath,
9
+ ruleBlockResponse,
10
+ armSiteHoneytoken
10
11
  } from "@webdecoy/node";
11
12
  function resolveIP(req, trustProxy) {
12
13
  const peer = req.socket?.remoteAddress;
@@ -25,31 +26,15 @@ function defaultOnBlocked(req, res, detection, _next) {
25
26
  function defaultOnError(req, res, error) {
26
27
  console.error("[WebDecoy] Middleware error:", error);
27
28
  }
28
- function shouldSkipPath(path, skipPaths) {
29
- if (!skipPaths || skipPaths.length === 0) {
30
- return false;
31
- }
32
- return skipPaths.some((pattern) => {
33
- if (typeof pattern === "string") {
34
- return path === pattern || path.startsWith(pattern);
35
- }
36
- return pattern.test(path);
37
- });
38
- }
39
29
  function webdecoy(config) {
40
30
  const sdk = new WebDecoy(config);
41
31
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
42
32
  const onBlocked = config.onBlocked || defaultOnBlocked;
43
33
  const mode = config.mode ?? "monitor";
44
- const honeytokenEnabled = (config.honeytoken ?? true) && Boolean(config.apiKey);
45
- let token = null;
46
- if (honeytokenEnabled) {
47
- void siteHoneytoken({ secret: config.apiKey }).then((t) => {
48
- token = t;
49
- sdk.addRule(tripwire({ paths: t.activePaths, includeDefaults: false }));
50
- }).catch(() => {
51
- });
52
- }
34
+ const getToken = armSiteHoneytoken(sdk, {
35
+ apiKey: config.apiKey,
36
+ enabled: config.honeytoken
37
+ });
53
38
  const onError = config.onError || defaultOnError;
54
39
  const skipPaths = config.skipPaths;
55
40
  return async (req, res, next) => {
@@ -63,6 +48,9 @@ function webdecoy(config) {
63
48
  ip: getIP2(req),
64
49
  user_agent: req.headers["user-agent"],
65
50
  headers: req.headers,
51
+ // `req.path` excludes the query, which is where injection payloads
52
+ // live, so attackSignatures() cannot see them without this.
53
+ query: req.originalUrl.includes("?") ? req.originalUrl.slice(req.originalUrl.indexOf("?") + 1) : void 0,
66
54
  timestamp: Date.now()
67
55
  };
68
56
  const result = await sdk.protect(metadata, {
@@ -70,8 +58,8 @@ function webdecoy(config) {
70
58
  skipLocalAnalysis: config.skipLocalAnalysis,
71
59
  metadata: config.metadata
72
60
  });
73
- if (token) {
74
- const ht = token;
61
+ const ht = getToken();
62
+ if (ht) {
75
63
  const originalWrite = res.write.bind(res);
76
64
  const originalEnd = res.end.bind(res);
77
65
  const chunks = [];
@@ -106,37 +94,26 @@ function webdecoy(config) {
106
94
  }
107
95
  if (mode === "monitor") {
108
96
  req.webdecoy = result.detection;
97
+ req.webdecoyDecision = result;
109
98
  req.webdecoyEdge = result.edge;
110
99
  req.webdecoyWouldBlock = !result.allowed;
111
100
  return next();
112
101
  }
113
- if (!result.allowed && result.ruleResult) {
114
- const rr = result.ruleResult;
115
- if (rr.action === "THROTTLE") {
116
- const retryAfter = rr.metadata?.retryAfter ?? 60;
117
- res.setHeader("Retry-After", String(retryAfter));
118
- res.status(429).json({
119
- error: "Too Many Requests",
120
- message: rr.reason || "Rate limit exceeded",
121
- retry_after: retryAfter
122
- });
123
- return;
124
- }
125
- if (rr.action === "DENY") {
126
- res.status(403).json({
127
- error: "Forbidden",
128
- message: rr.reason || "Access denied by rule",
129
- rule: rr.rule
130
- });
131
- return;
102
+ const block = ruleBlockResponse(result);
103
+ if (block) {
104
+ for (const [name, value] of Object.entries(block.headers)) {
105
+ res.setHeader(name, value);
132
106
  }
107
+ res.status(block.status).json(block.body);
108
+ return;
133
109
  }
134
110
  if (result.allowed) {
135
111
  req.webdecoy = result.detection;
112
+ req.webdecoyDecision = result;
136
113
  req.webdecoyEdge = result.edge;
137
114
  return next();
138
115
  } else {
139
- return onBlocked(req, res, result.detection, next);
116
+ return onBlocked(req, res, result.detection, next, result);
140
117
  }
141
118
  } catch (error) {
142
119
  onError(req, res, error);
@@ -167,7 +144,10 @@ function normalizeQuery(query) {
167
144
  }
168
145
  function webdecoyCaptcha(options) {
169
146
  const endpoints = createCaptchaEndpoints(options);
170
- return async (req, res, next) => {
147
+ return (req, res, next) => {
148
+ void handle(req, res, next).catch(next);
149
+ };
150
+ async function handle(req, res, next) {
171
151
  const result = await endpoints.handle({
172
152
  method: req.method,
173
153
  pathname: req.path,
@@ -183,7 +163,7 @@ function webdecoyCaptcha(options) {
183
163
  res.status(result.status);
184
164
  for (const [k, v] of Object.entries(result.headers)) res.setHeader(k, v);
185
165
  res.json(result.body);
186
- };
166
+ }
187
167
  }
188
168
  export {
189
169
  webdecoy,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.12.0",
3
+ "version": "0.14.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 --max-warnings 14",
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.12.0"
43
+ "@webdecoy/node": "^0.14.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"