@webdecoy/express 0.6.0 → 0.7.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
@@ -7,6 +7,25 @@ export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionRe
7
7
  */
8
8
 
9
9
  interface WebDecoyMiddlewareOptions extends ProtectOptions {
10
+ /**
11
+ * Whether a blocking verdict actually blocks. Defaults to `'monitor'`.
12
+ *
13
+ * MONITOR IS THE DEFAULT ON PURPOSE, and this changed in 0.7.0.
14
+ *
15
+ * Before, installing this middleware with an API key began returning 403 to
16
+ * any request whose server-side score cleared `threatScoreThreshold` — which
17
+ * defaults to 80 — and there was no supported way to watch first. `dryRun` on
18
+ * a rule governs that rule, not the score, and `onBlocked` did not receive
19
+ * `next`, so "record it and serve the request anyway" could not be expressed.
20
+ *
21
+ * Found by installing it on a live site that takes payments: the homepage
22
+ * returned `{"error":"Forbidden"}` on the first request, as did an ordinary
23
+ * `python-requests` user agent.
24
+ *
25
+ * Nobody adopts a defence by having it break their site on the first install.
26
+ * Watch what it would have done, then set `mode: 'enforce'`.
27
+ */
28
+ mode?: 'monitor' | 'enforce';
10
29
  /**
11
30
  * Custom function to extract IP address from request
12
31
  * By default, uses req.ip or x-forwarded-for header
@@ -16,7 +35,14 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
16
35
  * Custom function to handle blocked requests
17
36
  * By default, returns 403 Forbidden
18
37
  */
19
- onBlocked?: (req: Request, res: Response, detection: any) => void;
38
+ /**
39
+ * Called when a request would be blocked.
40
+ *
41
+ * `next` is passed so a handler can record the verdict and continue — the
42
+ * omission that made monitoring impossible. Call exactly one of `next()` or a
43
+ * response method.
44
+ */
45
+ onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
20
46
  /**
21
47
  * Custom function to handle errors
22
48
  * By default, logs error and allows request (fail open)
package/dist/index.d.ts CHANGED
@@ -7,6 +7,25 @@ export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionRe
7
7
  */
8
8
 
9
9
  interface WebDecoyMiddlewareOptions extends ProtectOptions {
10
+ /**
11
+ * Whether a blocking verdict actually blocks. Defaults to `'monitor'`.
12
+ *
13
+ * MONITOR IS THE DEFAULT ON PURPOSE, and this changed in 0.7.0.
14
+ *
15
+ * Before, installing this middleware with an API key began returning 403 to
16
+ * any request whose server-side score cleared `threatScoreThreshold` — which
17
+ * defaults to 80 — and there was no supported way to watch first. `dryRun` on
18
+ * a rule governs that rule, not the score, and `onBlocked` did not receive
19
+ * `next`, so "record it and serve the request anyway" could not be expressed.
20
+ *
21
+ * Found by installing it on a live site that takes payments: the homepage
22
+ * returned `{"error":"Forbidden"}` on the first request, as did an ordinary
23
+ * `python-requests` user agent.
24
+ *
25
+ * Nobody adopts a defence by having it break their site on the first install.
26
+ * Watch what it would have done, then set `mode: 'enforce'`.
27
+ */
28
+ mode?: 'monitor' | 'enforce';
10
29
  /**
11
30
  * Custom function to extract IP address from request
12
31
  * By default, uses req.ip or x-forwarded-for header
@@ -16,7 +35,14 @@ interface WebDecoyMiddlewareOptions extends ProtectOptions {
16
35
  * Custom function to handle blocked requests
17
36
  * By default, returns 403 Forbidden
18
37
  */
19
- onBlocked?: (req: Request, res: Response, detection: any) => void;
38
+ /**
39
+ * Called when a request would be blocked.
40
+ *
41
+ * `next` is passed so a handler can record the verdict and continue — the
42
+ * omission that made monitoring impossible. Call exactly one of `next()` or a
43
+ * response method.
44
+ */
45
+ onBlocked?: (req: Request, res: Response, detection: any, next: NextFunction) => void;
20
46
  /**
21
47
  * Custom function to handle errors
22
48
  * By default, logs error and allows request (fail open)
package/dist/index.js CHANGED
@@ -39,7 +39,7 @@ function defaultGetIP(req) {
39
39
  }
40
40
  return req.ip || req.socket.remoteAddress || "127.0.0.1";
41
41
  }
42
- function defaultOnBlocked(req, res, detection) {
42
+ function defaultOnBlocked(req, res, detection, _next) {
43
43
  res.status(403).json({
44
44
  error: "Forbidden",
45
45
  message: "Access denied by Web Decoy protection",
@@ -64,6 +64,7 @@ function webdecoy(config) {
64
64
  const sdk = new import_node.WebDecoy(config);
65
65
  const getIP2 = config.getIP || defaultGetIP;
66
66
  const onBlocked = config.onBlocked || defaultOnBlocked;
67
+ const mode = config.mode ?? "monitor";
67
68
  const onError = config.onError || defaultOnError;
68
69
  const skipPaths = config.skipPaths;
69
70
  return async (req, res, next) => {
@@ -84,6 +85,12 @@ function webdecoy(config) {
84
85
  skipLocalAnalysis: config.skipLocalAnalysis,
85
86
  metadata: config.metadata
86
87
  });
88
+ if (mode === "monitor") {
89
+ req.webdecoy = result.detection;
90
+ req.webdecoyEdge = result.edge;
91
+ req.webdecoyWouldBlock = !result.allowed;
92
+ return next();
93
+ }
87
94
  if (!result.allowed && result.ruleResult) {
88
95
  const rr = result.ruleResult;
89
96
  if (rr.action === "THROTTLE") {
@@ -110,7 +117,7 @@ function webdecoy(config) {
110
117
  req.webdecoyEdge = result.edge;
111
118
  return next();
112
119
  } else {
113
- return onBlocked(req, res, result.detection);
120
+ return onBlocked(req, res, result.detection, next);
114
121
  }
115
122
  } catch (error) {
116
123
  onError(req, res, error);
package/dist/index.mjs CHANGED
@@ -12,7 +12,7 @@ function defaultGetIP(req) {
12
12
  }
13
13
  return req.ip || req.socket.remoteAddress || "127.0.0.1";
14
14
  }
15
- function defaultOnBlocked(req, res, detection) {
15
+ function defaultOnBlocked(req, res, detection, _next) {
16
16
  res.status(403).json({
17
17
  error: "Forbidden",
18
18
  message: "Access denied by Web Decoy protection",
@@ -37,6 +37,7 @@ function webdecoy(config) {
37
37
  const sdk = new WebDecoy(config);
38
38
  const getIP2 = config.getIP || defaultGetIP;
39
39
  const onBlocked = config.onBlocked || defaultOnBlocked;
40
+ const mode = config.mode ?? "monitor";
40
41
  const onError = config.onError || defaultOnError;
41
42
  const skipPaths = config.skipPaths;
42
43
  return async (req, res, next) => {
@@ -57,6 +58,12 @@ function webdecoy(config) {
57
58
  skipLocalAnalysis: config.skipLocalAnalysis,
58
59
  metadata: config.metadata
59
60
  });
61
+ if (mode === "monitor") {
62
+ req.webdecoy = result.detection;
63
+ req.webdecoyEdge = result.edge;
64
+ req.webdecoyWouldBlock = !result.allowed;
65
+ return next();
66
+ }
60
67
  if (!result.allowed && result.ruleResult) {
61
68
  const rr = result.ruleResult;
62
69
  if (rr.action === "THROTTLE") {
@@ -83,7 +90,7 @@ function webdecoy(config) {
83
90
  req.webdecoyEdge = result.edge;
84
91
  return next();
85
92
  } else {
86
- return onBlocked(req, res, result.detection);
93
+ return onBlocked(req, res, result.detection, next);
87
94
  }
88
95
  } catch (error) {
89
96
  onError(req, res, error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/express",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Web Decoy middleware for Express.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -40,7 +40,7 @@
40
40
  "url": "https://github.com/WebDecoy/node/issues"
41
41
  },
42
42
  "dependencies": {
43
- "@webdecoy/node": "^0.6.0"
43
+ "@webdecoy/node": "^0.7.0"
44
44
  },
45
45
  "peerDependencies": {
46
46
  "express": "^4.18.0 || ^5.0.0"