@webdecoy/nextjs 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,18 @@ export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, Request
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. Before,
14
+ * installing this with an API key began returning 403 to any request whose
15
+ * server-side score cleared `threatScoreThreshold` (default 80), and there was
16
+ * no supported way to watch first. Found by installing it on a live site that
17
+ * takes payments, where the homepage returned Forbidden on the first request.
18
+ *
19
+ * Watch what it would have done, then set `mode: 'enforce'`.
20
+ */
21
+ mode?: 'monitor' | 'enforce';
10
22
  /**
11
23
  * Custom function to extract IP address from request
12
24
  * By default, uses x-forwarded-for or x-real-ip headers
package/dist/index.d.ts CHANGED
@@ -7,6 +7,18 @@ export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, Request
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. Before,
14
+ * installing this with an API key began returning 403 to any request whose
15
+ * server-side score cleared `threatScoreThreshold` (default 80), and there was
16
+ * no supported way to watch first. Found by installing it on a live site that
17
+ * takes payments, where the homepage returned Forbidden on the first request.
18
+ *
19
+ * Watch what it would have done, then set `mode: 'enforce'`.
20
+ */
21
+ mode?: 'monitor' | 'enforce';
10
22
  /**
11
23
  * Custom function to extract IP address from request
12
24
  * By default, uses x-forwarded-for or x-real-ip headers
package/dist/index.js CHANGED
@@ -74,6 +74,7 @@ function withWebDecoy(config) {
74
74
  const sdk = new import_node.WebDecoy(config);
75
75
  const getIP2 = config.getIP || defaultGetIP;
76
76
  const onBlocked = config.onBlocked || defaultOnBlocked;
77
+ const mode = config.mode ?? "monitor";
77
78
  const onError = config.onError || defaultOnError;
78
79
  const skipPaths = config.skipPaths;
79
80
  return async (req) => {
@@ -99,6 +100,18 @@ function withWebDecoy(config) {
99
100
  skipLocalAnalysis: config.skipLocalAnalysis,
100
101
  metadata: config.metadata
101
102
  });
103
+ if (mode === "monitor") {
104
+ const monitorHeaders = new Headers(req.headers);
105
+ monitorHeaders.delete("x-webdecoy-decision");
106
+ monitorHeaders.delete("x-webdecoy-detection-id");
107
+ monitorHeaders.delete("x-webdecoy-would-block");
108
+ if (result.detection) {
109
+ monitorHeaders.set("x-webdecoy-decision", result.detection.decision || "");
110
+ monitorHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
111
+ }
112
+ monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
113
+ return import_server.NextResponse.next({ request: { headers: monitorHeaders } });
114
+ }
102
115
  if (!result.allowed && result.ruleResult) {
103
116
  const rr = result.ruleResult;
104
117
  if (rr.action === "THROTTLE") {
package/dist/index.mjs CHANGED
@@ -45,6 +45,7 @@ function withWebDecoy(config) {
45
45
  const sdk = new WebDecoy(config);
46
46
  const getIP2 = config.getIP || defaultGetIP;
47
47
  const onBlocked = config.onBlocked || defaultOnBlocked;
48
+ const mode = config.mode ?? "monitor";
48
49
  const onError = config.onError || defaultOnError;
49
50
  const skipPaths = config.skipPaths;
50
51
  return async (req) => {
@@ -70,6 +71,18 @@ function withWebDecoy(config) {
70
71
  skipLocalAnalysis: config.skipLocalAnalysis,
71
72
  metadata: config.metadata
72
73
  });
74
+ if (mode === "monitor") {
75
+ const monitorHeaders = new Headers(req.headers);
76
+ monitorHeaders.delete("x-webdecoy-decision");
77
+ monitorHeaders.delete("x-webdecoy-detection-id");
78
+ monitorHeaders.delete("x-webdecoy-would-block");
79
+ if (result.detection) {
80
+ monitorHeaders.set("x-webdecoy-decision", result.detection.decision || "");
81
+ monitorHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
82
+ }
83
+ monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
84
+ return NextResponse.next({ request: { headers: monitorHeaders } });
85
+ }
73
86
  if (!result.allowed && result.ruleResult) {
74
87
  const rr = result.ruleResult;
75
88
  if (rr.action === "THROTTLE") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Web Decoy middleware for Next.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -42,7 +42,7 @@
42
42
  "url": "https://github.com/WebDecoy/node/issues"
43
43
  },
44
44
  "dependencies": {
45
- "@webdecoy/node": "^0.6.0"
45
+ "@webdecoy/node": "^0.7.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "next": ">=13.0.0"