@webdecoy/nextjs 0.5.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 +71 -3
- package/dist/index.d.ts +71 -3
- package/dist/index.js +39 -6
- package/dist/index.mjs +36 -4
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
-
export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig, EdgeVerdict, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
+
export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Web Decoy Next.js Middleware
|
|
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
|
|
@@ -79,6 +91,62 @@ interface WithBotProtectionOptions extends WebDecoyMiddlewareOptions {
|
|
|
79
91
|
*/
|
|
80
92
|
declare function withBotProtection<T extends (...args: any[]) => any>(handler: T, config: WebDecoyConfig & WithBotProtectionOptions): T;
|
|
81
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Read the edge validator's verdict inside a Next.js app (#481).
|
|
96
|
+
*
|
|
97
|
+
* The Cloudflare clearance worker annotates every forwarded request with
|
|
98
|
+
* `x-wd-clearance` and, when the sensor classified the client, `x-wd-class`.
|
|
99
|
+
* Nothing in this SDK read either header until now, so the only thing an
|
|
100
|
+
* application could do with a scoped route was let the edge 403 it — which on
|
|
101
|
+
* public content means 403ing every client that cannot run JavaScript.
|
|
102
|
+
*
|
|
103
|
+
* This is the read side. It works in middleware, route handlers, server
|
|
104
|
+
* components and server actions, because all it needs is a header bag.
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Anything that can hand us request headers: a `NextRequest`, a `Request`, the
|
|
109
|
+
* `Headers` object, or the result of `await headers()` in a server component.
|
|
110
|
+
*/
|
|
111
|
+
type HeaderSource = {
|
|
112
|
+
headers: Headers;
|
|
113
|
+
} | Headers | {
|
|
114
|
+
get(name: string): string | null;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Parse the edge validator's annotations from a request.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // app/api/search/route.ts
|
|
122
|
+
* import { getEdgeVerdict } from '@webdecoy/nextjs';
|
|
123
|
+
*
|
|
124
|
+
* export async function GET(req: Request) {
|
|
125
|
+
* const edge = getEdgeVerdict(req);
|
|
126
|
+
*
|
|
127
|
+
* // Skip the expensive path for scripted traffic — without 403ing it, and
|
|
128
|
+
* // without touching what a verified crawler sees.
|
|
129
|
+
* if (edge.isScript) {
|
|
130
|
+
* return Response.json(await cheapResults(), {
|
|
131
|
+
* headers: { 'cache-control': 'private, no-store' },
|
|
132
|
+
* });
|
|
133
|
+
* }
|
|
134
|
+
* return Response.json(await fullResults());
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* `present: false` means the edge did not front this request. That is no
|
|
139
|
+
* information — not "human". Do not treat it as a pass.
|
|
140
|
+
*
|
|
141
|
+
* IMPORTANT, and the reason this returns data rather than a response: do NOT vary
|
|
142
|
+
* a **cacheable response body** on this. Cloudflare's default cache key excludes
|
|
143
|
+
* arbitrary request headers and header-based cache keys are Enterprise-only, so
|
|
144
|
+
* the first variant cached for a URL is served to everyone, Googlebot included —
|
|
145
|
+
* and on a cache hit your origin never runs at all. Vary behaviour, or mark the
|
|
146
|
+
* response private, as above.
|
|
147
|
+
*/
|
|
148
|
+
declare function getEdgeVerdict(source: HeaderSource): EdgeVerdict;
|
|
149
|
+
|
|
82
150
|
/**
|
|
83
151
|
* Next.js App Router captcha route handlers.
|
|
84
152
|
*
|
|
@@ -99,4 +167,4 @@ interface NextCaptchaHandlers {
|
|
|
99
167
|
}
|
|
100
168
|
declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
|
|
101
169
|
|
|
102
|
-
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, withBotProtection, withWebDecoy };
|
|
170
|
+
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, withBotProtection, withWebDecoy };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
-
export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig, EdgeVerdict, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
+
export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Web Decoy Next.js Middleware
|
|
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
|
|
@@ -79,6 +91,62 @@ interface WithBotProtectionOptions extends WebDecoyMiddlewareOptions {
|
|
|
79
91
|
*/
|
|
80
92
|
declare function withBotProtection<T extends (...args: any[]) => any>(handler: T, config: WebDecoyConfig & WithBotProtectionOptions): T;
|
|
81
93
|
|
|
94
|
+
/**
|
|
95
|
+
* Read the edge validator's verdict inside a Next.js app (#481).
|
|
96
|
+
*
|
|
97
|
+
* The Cloudflare clearance worker annotates every forwarded request with
|
|
98
|
+
* `x-wd-clearance` and, when the sensor classified the client, `x-wd-class`.
|
|
99
|
+
* Nothing in this SDK read either header until now, so the only thing an
|
|
100
|
+
* application could do with a scoped route was let the edge 403 it — which on
|
|
101
|
+
* public content means 403ing every client that cannot run JavaScript.
|
|
102
|
+
*
|
|
103
|
+
* This is the read side. It works in middleware, route handlers, server
|
|
104
|
+
* components and server actions, because all it needs is a header bag.
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Anything that can hand us request headers: a `NextRequest`, a `Request`, the
|
|
109
|
+
* `Headers` object, or the result of `await headers()` in a server component.
|
|
110
|
+
*/
|
|
111
|
+
type HeaderSource = {
|
|
112
|
+
headers: Headers;
|
|
113
|
+
} | Headers | {
|
|
114
|
+
get(name: string): string | null;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Parse the edge validator's annotations from a request.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // app/api/search/route.ts
|
|
122
|
+
* import { getEdgeVerdict } from '@webdecoy/nextjs';
|
|
123
|
+
*
|
|
124
|
+
* export async function GET(req: Request) {
|
|
125
|
+
* const edge = getEdgeVerdict(req);
|
|
126
|
+
*
|
|
127
|
+
* // Skip the expensive path for scripted traffic — without 403ing it, and
|
|
128
|
+
* // without touching what a verified crawler sees.
|
|
129
|
+
* if (edge.isScript) {
|
|
130
|
+
* return Response.json(await cheapResults(), {
|
|
131
|
+
* headers: { 'cache-control': 'private, no-store' },
|
|
132
|
+
* });
|
|
133
|
+
* }
|
|
134
|
+
* return Response.json(await fullResults());
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*
|
|
138
|
+
* `present: false` means the edge did not front this request. That is no
|
|
139
|
+
* information — not "human". Do not treat it as a pass.
|
|
140
|
+
*
|
|
141
|
+
* IMPORTANT, and the reason this returns data rather than a response: do NOT vary
|
|
142
|
+
* a **cacheable response body** on this. Cloudflare's default cache key excludes
|
|
143
|
+
* arbitrary request headers and header-based cache keys are Enterprise-only, so
|
|
144
|
+
* the first variant cached for a URL is served to everyone, Googlebot included —
|
|
145
|
+
* and on a cache hit your origin never runs at all. Vary behaviour, or mark the
|
|
146
|
+
* response private, as above.
|
|
147
|
+
*/
|
|
148
|
+
declare function getEdgeVerdict(source: HeaderSource): EdgeVerdict;
|
|
149
|
+
|
|
82
150
|
/**
|
|
83
151
|
* Next.js App Router captcha route handlers.
|
|
84
152
|
*
|
|
@@ -99,4 +167,4 @@ interface NextCaptchaHandlers {
|
|
|
99
167
|
}
|
|
100
168
|
declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
|
|
101
169
|
|
|
102
|
-
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, withBotProtection, withWebDecoy };
|
|
170
|
+
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, withBotProtection, withWebDecoy };
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
createCaptchaHandler: () => createCaptchaHandler,
|
|
24
|
+
getEdgeVerdict: () => getEdgeVerdict,
|
|
24
25
|
withBotProtection: () => withBotProtection,
|
|
25
26
|
withWebDecoy: () => withWebDecoy
|
|
26
27
|
});
|
|
@@ -73,6 +74,7 @@ function withWebDecoy(config) {
|
|
|
73
74
|
const sdk = new import_node.WebDecoy(config);
|
|
74
75
|
const getIP2 = config.getIP || defaultGetIP;
|
|
75
76
|
const onBlocked = config.onBlocked || defaultOnBlocked;
|
|
77
|
+
const mode = config.mode ?? "monitor";
|
|
76
78
|
const onError = config.onError || defaultOnError;
|
|
77
79
|
const skipPaths = config.skipPaths;
|
|
78
80
|
return async (req) => {
|
|
@@ -98,6 +100,18 @@ function withWebDecoy(config) {
|
|
|
98
100
|
skipLocalAnalysis: config.skipLocalAnalysis,
|
|
99
101
|
metadata: config.metadata
|
|
100
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
|
+
}
|
|
101
115
|
if (!result.allowed && result.ruleResult) {
|
|
102
116
|
const rr = result.ruleResult;
|
|
103
117
|
if (rr.action === "THROTTLE") {
|
|
@@ -126,12 +140,14 @@ function withWebDecoy(config) {
|
|
|
126
140
|
}
|
|
127
141
|
}
|
|
128
142
|
if (result.allowed) {
|
|
129
|
-
const
|
|
143
|
+
const requestHeaders = new Headers(req.headers);
|
|
144
|
+
requestHeaders.delete("x-webdecoy-decision");
|
|
145
|
+
requestHeaders.delete("x-webdecoy-detection-id");
|
|
130
146
|
if (result.detection) {
|
|
131
|
-
|
|
132
|
-
|
|
147
|
+
requestHeaders.set("x-webdecoy-decision", result.detection.decision || "");
|
|
148
|
+
requestHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
|
|
133
149
|
}
|
|
134
|
-
return
|
|
150
|
+
return import_server.NextResponse.next({ request: { headers: requestHeaders } });
|
|
135
151
|
} else {
|
|
136
152
|
return onBlocked(req, result.detection);
|
|
137
153
|
}
|
|
@@ -189,8 +205,24 @@ function withBotProtection(handler, config) {
|
|
|
189
205
|
});
|
|
190
206
|
}
|
|
191
207
|
|
|
192
|
-
// src/
|
|
208
|
+
// src/edge-verdict.ts
|
|
193
209
|
var import_node2 = require("@webdecoy/node");
|
|
210
|
+
var EDGE_HEADERS = ["x-wd-clearance", "x-wd-class"];
|
|
211
|
+
function bagFrom(source) {
|
|
212
|
+
const getter = "headers" in source && !("get" in source) ? source.headers : source;
|
|
213
|
+
const bag = {};
|
|
214
|
+
for (const name of EDGE_HEADERS) {
|
|
215
|
+
const value = getter.get(name);
|
|
216
|
+
if (value) bag[name] = value;
|
|
217
|
+
}
|
|
218
|
+
return bag;
|
|
219
|
+
}
|
|
220
|
+
function getEdgeVerdict(source) {
|
|
221
|
+
return (0, import_node2.readEdgeVerdict)(bagFrom(source));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// src/captcha.ts
|
|
225
|
+
var import_node3 = require("@webdecoy/node");
|
|
194
226
|
function getIP(headers) {
|
|
195
227
|
const forwarded = headers.get("x-forwarded-for");
|
|
196
228
|
if (forwarded) return forwarded.split(",")[0].trim();
|
|
@@ -204,7 +236,7 @@ function headersToRecord(headers) {
|
|
|
204
236
|
return out;
|
|
205
237
|
}
|
|
206
238
|
function createCaptchaHandler(options) {
|
|
207
|
-
const endpoints = (0,
|
|
239
|
+
const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
|
|
208
240
|
async function handler(req) {
|
|
209
241
|
const url = new URL(req.url);
|
|
210
242
|
const query = {};
|
|
@@ -244,6 +276,7 @@ function createCaptchaHandler(options) {
|
|
|
244
276
|
// Annotate the CommonJS export names for ESM import in node:
|
|
245
277
|
0 && (module.exports = {
|
|
246
278
|
createCaptchaHandler,
|
|
279
|
+
getEdgeVerdict,
|
|
247
280
|
withBotProtection,
|
|
248
281
|
withWebDecoy
|
|
249
282
|
});
|
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") {
|
|
@@ -98,12 +111,14 @@ function withWebDecoy(config) {
|
|
|
98
111
|
}
|
|
99
112
|
}
|
|
100
113
|
if (result.allowed) {
|
|
101
|
-
const
|
|
114
|
+
const requestHeaders = new Headers(req.headers);
|
|
115
|
+
requestHeaders.delete("x-webdecoy-decision");
|
|
116
|
+
requestHeaders.delete("x-webdecoy-detection-id");
|
|
102
117
|
if (result.detection) {
|
|
103
|
-
|
|
104
|
-
|
|
118
|
+
requestHeaders.set("x-webdecoy-decision", result.detection.decision || "");
|
|
119
|
+
requestHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
|
|
105
120
|
}
|
|
106
|
-
return
|
|
121
|
+
return NextResponse.next({ request: { headers: requestHeaders } });
|
|
107
122
|
} else {
|
|
108
123
|
return onBlocked(req, result.detection);
|
|
109
124
|
}
|
|
@@ -161,6 +176,22 @@ function withBotProtection(handler, config) {
|
|
|
161
176
|
});
|
|
162
177
|
}
|
|
163
178
|
|
|
179
|
+
// src/edge-verdict.ts
|
|
180
|
+
import { readEdgeVerdict } from "@webdecoy/node";
|
|
181
|
+
var EDGE_HEADERS = ["x-wd-clearance", "x-wd-class"];
|
|
182
|
+
function bagFrom(source) {
|
|
183
|
+
const getter = "headers" in source && !("get" in source) ? source.headers : source;
|
|
184
|
+
const bag = {};
|
|
185
|
+
for (const name of EDGE_HEADERS) {
|
|
186
|
+
const value = getter.get(name);
|
|
187
|
+
if (value) bag[name] = value;
|
|
188
|
+
}
|
|
189
|
+
return bag;
|
|
190
|
+
}
|
|
191
|
+
function getEdgeVerdict(source) {
|
|
192
|
+
return readEdgeVerdict(bagFrom(source));
|
|
193
|
+
}
|
|
194
|
+
|
|
164
195
|
// src/captcha.ts
|
|
165
196
|
import { createCaptchaEndpoints } from "@webdecoy/node";
|
|
166
197
|
function getIP(headers) {
|
|
@@ -215,6 +246,7 @@ function createCaptchaHandler(options) {
|
|
|
215
246
|
}
|
|
216
247
|
export {
|
|
217
248
|
createCaptchaHandler,
|
|
249
|
+
getEdgeVerdict,
|
|
218
250
|
withBotProtection,
|
|
219
251
|
withWebDecoy
|
|
220
252
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/nextjs",
|
|
3
|
-
"version": "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.
|
|
45
|
+
"@webdecoy/node": "^0.7.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"next": ">=13.0.0"
|