@webdecoy/nextjs 0.6.0 → 0.8.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 +79 -3
- package/dist/index.d.ts +79 -3
- package/dist/index.js +30 -2
- package/dist/index.mjs +27 -0
- 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, EdgeVerdict, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
-
export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
+
export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, 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
|
|
@@ -135,6 +147,70 @@ type HeaderSource = {
|
|
|
135
147
|
*/
|
|
136
148
|
declare function getEdgeVerdict(source: HeaderSource): EdgeVerdict;
|
|
137
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Honeytoken for Next.js (#482).
|
|
152
|
+
*
|
|
153
|
+
* WHY THIS IS A HELPER AND NOT AUTOMATIC
|
|
154
|
+
*
|
|
155
|
+
* The Express adapter injects the hidden link by wrapping `res.write`/`res.end`
|
|
156
|
+
* and rewriting the finished HTML. Next middleware cannot do that. It runs
|
|
157
|
+
* BEFORE the route handler and returns a `NextResponse`; the App Router then
|
|
158
|
+
* streams the RSC payload directly to the client. There is no complete document
|
|
159
|
+
* for middleware to rewrite, and buffering a stream to synthesise one would
|
|
160
|
+
* defeat streaming — trading the framework's headline feature for a hidden link.
|
|
161
|
+
*
|
|
162
|
+
* Partial support would be worse than none. Injecting only into the
|
|
163
|
+
* non-streamed cases would give a trap that works in development and silently
|
|
164
|
+
* stops working under the rendering mode most production apps use, which is the
|
|
165
|
+
* failure mode this whole issue is about: a feature that looks installed and
|
|
166
|
+
* detects nothing.
|
|
167
|
+
*
|
|
168
|
+
* So Next gets an honest one-liner instead. It is one line in the root layout,
|
|
169
|
+
* and it is the same derived path the tripwire is armed on.
|
|
170
|
+
*
|
|
171
|
+
* Rendered from `linkProps` as ordinary JSX rather than through
|
|
172
|
+
* `dangerouslySetInnerHTML`: the markup is a single anchor, so there is no
|
|
173
|
+
* reason to hand React a raw HTML string and every reason not to teach that
|
|
174
|
+
* habit in a snippet people will paste.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```tsx
|
|
178
|
+
* // app/layout.tsx
|
|
179
|
+
* import { honeytokenLink } from '@webdecoy/nextjs';
|
|
180
|
+
*
|
|
181
|
+
* export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
182
|
+
* const { linkProps } = await honeytokenLink(process.env.WEBDECOY_API_KEY!);
|
|
183
|
+
* const { text, ...anchor } = linkProps;
|
|
184
|
+
* return (
|
|
185
|
+
* <html>
|
|
186
|
+
* <body>
|
|
187
|
+
* {children}
|
|
188
|
+
* <a {...anchor}>{text}</a>
|
|
189
|
+
* </body>
|
|
190
|
+
* </html>
|
|
191
|
+
* );
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* Then arm it in middleware:
|
|
196
|
+
*
|
|
197
|
+
* ```typescript
|
|
198
|
+
* import { withWebDecoy } from '@webdecoy/nextjs';
|
|
199
|
+
* import { tripwire } from '@webdecoy/node';
|
|
200
|
+
*
|
|
201
|
+
* const bait = await honeytokenLink(process.env.WEBDECOY_API_KEY!);
|
|
202
|
+
* export default withWebDecoy({
|
|
203
|
+
* apiKey: process.env.WEBDECOY_API_KEY!,
|
|
204
|
+
* rules: [tripwire({ paths: bait.activePaths, includeDefaults: false })],
|
|
205
|
+
* });
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
declare function honeytokenLink(apiKey: string, options?: {
|
|
210
|
+
rotate?: boolean;
|
|
211
|
+
text?: string;
|
|
212
|
+
}): Promise<SiteHoneytoken>;
|
|
213
|
+
|
|
138
214
|
/**
|
|
139
215
|
* Next.js App Router captcha route handlers.
|
|
140
216
|
*
|
|
@@ -155,4 +231,4 @@ interface NextCaptchaHandlers {
|
|
|
155
231
|
}
|
|
156
232
|
declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
|
|
157
233
|
|
|
158
|
-
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, withBotProtection, withWebDecoy };
|
|
234
|
+
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ProtectOptions, WebDecoyConfig, EdgeVerdict, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
-
export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
+
export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, 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
|
|
@@ -135,6 +147,70 @@ type HeaderSource = {
|
|
|
135
147
|
*/
|
|
136
148
|
declare function getEdgeVerdict(source: HeaderSource): EdgeVerdict;
|
|
137
149
|
|
|
150
|
+
/**
|
|
151
|
+
* Honeytoken for Next.js (#482).
|
|
152
|
+
*
|
|
153
|
+
* WHY THIS IS A HELPER AND NOT AUTOMATIC
|
|
154
|
+
*
|
|
155
|
+
* The Express adapter injects the hidden link by wrapping `res.write`/`res.end`
|
|
156
|
+
* and rewriting the finished HTML. Next middleware cannot do that. It runs
|
|
157
|
+
* BEFORE the route handler and returns a `NextResponse`; the App Router then
|
|
158
|
+
* streams the RSC payload directly to the client. There is no complete document
|
|
159
|
+
* for middleware to rewrite, and buffering a stream to synthesise one would
|
|
160
|
+
* defeat streaming — trading the framework's headline feature for a hidden link.
|
|
161
|
+
*
|
|
162
|
+
* Partial support would be worse than none. Injecting only into the
|
|
163
|
+
* non-streamed cases would give a trap that works in development and silently
|
|
164
|
+
* stops working under the rendering mode most production apps use, which is the
|
|
165
|
+
* failure mode this whole issue is about: a feature that looks installed and
|
|
166
|
+
* detects nothing.
|
|
167
|
+
*
|
|
168
|
+
* So Next gets an honest one-liner instead. It is one line in the root layout,
|
|
169
|
+
* and it is the same derived path the tripwire is armed on.
|
|
170
|
+
*
|
|
171
|
+
* Rendered from `linkProps` as ordinary JSX rather than through
|
|
172
|
+
* `dangerouslySetInnerHTML`: the markup is a single anchor, so there is no
|
|
173
|
+
* reason to hand React a raw HTML string and every reason not to teach that
|
|
174
|
+
* habit in a snippet people will paste.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```tsx
|
|
178
|
+
* // app/layout.tsx
|
|
179
|
+
* import { honeytokenLink } from '@webdecoy/nextjs';
|
|
180
|
+
*
|
|
181
|
+
* export default async function RootLayout({ children }: { children: React.ReactNode }) {
|
|
182
|
+
* const { linkProps } = await honeytokenLink(process.env.WEBDECOY_API_KEY!);
|
|
183
|
+
* const { text, ...anchor } = linkProps;
|
|
184
|
+
* return (
|
|
185
|
+
* <html>
|
|
186
|
+
* <body>
|
|
187
|
+
* {children}
|
|
188
|
+
* <a {...anchor}>{text}</a>
|
|
189
|
+
* </body>
|
|
190
|
+
* </html>
|
|
191
|
+
* );
|
|
192
|
+
* }
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* Then arm it in middleware:
|
|
196
|
+
*
|
|
197
|
+
* ```typescript
|
|
198
|
+
* import { withWebDecoy } from '@webdecoy/nextjs';
|
|
199
|
+
* import { tripwire } from '@webdecoy/node';
|
|
200
|
+
*
|
|
201
|
+
* const bait = await honeytokenLink(process.env.WEBDECOY_API_KEY!);
|
|
202
|
+
* export default withWebDecoy({
|
|
203
|
+
* apiKey: process.env.WEBDECOY_API_KEY!,
|
|
204
|
+
* rules: [tripwire({ paths: bait.activePaths, includeDefaults: false })],
|
|
205
|
+
* });
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
declare function honeytokenLink(apiKey: string, options?: {
|
|
210
|
+
rotate?: boolean;
|
|
211
|
+
text?: string;
|
|
212
|
+
}): Promise<SiteHoneytoken>;
|
|
213
|
+
|
|
138
214
|
/**
|
|
139
215
|
* Next.js App Router captcha route handlers.
|
|
140
216
|
*
|
|
@@ -155,4 +231,4 @@ interface NextCaptchaHandlers {
|
|
|
155
231
|
}
|
|
156
232
|
declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
|
|
157
233
|
|
|
158
|
-
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, withBotProtection, withWebDecoy };
|
|
234
|
+
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
|
package/dist/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
createCaptchaHandler: () => createCaptchaHandler,
|
|
24
24
|
getEdgeVerdict: () => getEdgeVerdict,
|
|
25
|
+
honeytokenLink: () => honeytokenLink,
|
|
25
26
|
withBotProtection: () => withBotProtection,
|
|
26
27
|
withWebDecoy: () => withWebDecoy
|
|
27
28
|
});
|
|
@@ -74,6 +75,7 @@ function withWebDecoy(config) {
|
|
|
74
75
|
const sdk = new import_node.WebDecoy(config);
|
|
75
76
|
const getIP2 = config.getIP || defaultGetIP;
|
|
76
77
|
const onBlocked = config.onBlocked || defaultOnBlocked;
|
|
78
|
+
const mode = config.mode ?? "monitor";
|
|
77
79
|
const onError = config.onError || defaultOnError;
|
|
78
80
|
const skipPaths = config.skipPaths;
|
|
79
81
|
return async (req) => {
|
|
@@ -99,6 +101,18 @@ function withWebDecoy(config) {
|
|
|
99
101
|
skipLocalAnalysis: config.skipLocalAnalysis,
|
|
100
102
|
metadata: config.metadata
|
|
101
103
|
});
|
|
104
|
+
if (mode === "monitor") {
|
|
105
|
+
const monitorHeaders = new Headers(req.headers);
|
|
106
|
+
monitorHeaders.delete("x-webdecoy-decision");
|
|
107
|
+
monitorHeaders.delete("x-webdecoy-detection-id");
|
|
108
|
+
monitorHeaders.delete("x-webdecoy-would-block");
|
|
109
|
+
if (result.detection) {
|
|
110
|
+
monitorHeaders.set("x-webdecoy-decision", result.detection.decision || "");
|
|
111
|
+
monitorHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
|
|
112
|
+
}
|
|
113
|
+
monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
|
|
114
|
+
return import_server.NextResponse.next({ request: { headers: monitorHeaders } });
|
|
115
|
+
}
|
|
102
116
|
if (!result.allowed && result.ruleResult) {
|
|
103
117
|
const rr = result.ruleResult;
|
|
104
118
|
if (rr.action === "THROTTLE") {
|
|
@@ -208,8 +222,21 @@ function getEdgeVerdict(source) {
|
|
|
208
222
|
return (0, import_node2.readEdgeVerdict)(bagFrom(source));
|
|
209
223
|
}
|
|
210
224
|
|
|
211
|
-
// src/
|
|
225
|
+
// src/honeytoken.ts
|
|
212
226
|
var import_node3 = require("@webdecoy/node");
|
|
227
|
+
var cache = /* @__PURE__ */ new Map();
|
|
228
|
+
function honeytokenLink(apiKey, options = {}) {
|
|
229
|
+
const key = `${apiKey}|${options.rotate ? "rot" : "stable"}|${options.text ?? "."}`;
|
|
230
|
+
let existing = cache.get(key);
|
|
231
|
+
if (!existing) {
|
|
232
|
+
existing = (0, import_node3.siteHoneytoken)({ secret: apiKey, ...options });
|
|
233
|
+
cache.set(key, existing);
|
|
234
|
+
}
|
|
235
|
+
return existing;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/captcha.ts
|
|
239
|
+
var import_node4 = require("@webdecoy/node");
|
|
213
240
|
function getIP(headers) {
|
|
214
241
|
const forwarded = headers.get("x-forwarded-for");
|
|
215
242
|
if (forwarded) return forwarded.split(",")[0].trim();
|
|
@@ -223,7 +250,7 @@ function headersToRecord(headers) {
|
|
|
223
250
|
return out;
|
|
224
251
|
}
|
|
225
252
|
function createCaptchaHandler(options) {
|
|
226
|
-
const endpoints = (0,
|
|
253
|
+
const endpoints = (0, import_node4.createCaptchaEndpoints)(options);
|
|
227
254
|
async function handler(req) {
|
|
228
255
|
const url = new URL(req.url);
|
|
229
256
|
const query = {};
|
|
@@ -264,6 +291,7 @@ function createCaptchaHandler(options) {
|
|
|
264
291
|
0 && (module.exports = {
|
|
265
292
|
createCaptchaHandler,
|
|
266
293
|
getEdgeVerdict,
|
|
294
|
+
honeytokenLink,
|
|
267
295
|
withBotProtection,
|
|
268
296
|
withWebDecoy
|
|
269
297
|
});
|
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") {
|
|
@@ -179,6 +192,19 @@ function getEdgeVerdict(source) {
|
|
|
179
192
|
return readEdgeVerdict(bagFrom(source));
|
|
180
193
|
}
|
|
181
194
|
|
|
195
|
+
// src/honeytoken.ts
|
|
196
|
+
import { siteHoneytoken } from "@webdecoy/node";
|
|
197
|
+
var cache = /* @__PURE__ */ new Map();
|
|
198
|
+
function honeytokenLink(apiKey, options = {}) {
|
|
199
|
+
const key = `${apiKey}|${options.rotate ? "rot" : "stable"}|${options.text ?? "."}`;
|
|
200
|
+
let existing = cache.get(key);
|
|
201
|
+
if (!existing) {
|
|
202
|
+
existing = siteHoneytoken({ secret: apiKey, ...options });
|
|
203
|
+
cache.set(key, existing);
|
|
204
|
+
}
|
|
205
|
+
return existing;
|
|
206
|
+
}
|
|
207
|
+
|
|
182
208
|
// src/captcha.ts
|
|
183
209
|
import { createCaptchaEndpoints } from "@webdecoy/node";
|
|
184
210
|
function getIP(headers) {
|
|
@@ -234,6 +260,7 @@ function createCaptchaHandler(options) {
|
|
|
234
260
|
export {
|
|
235
261
|
createCaptchaHandler,
|
|
236
262
|
getEdgeVerdict,
|
|
263
|
+
honeytokenLink,
|
|
237
264
|
withBotProtection,
|
|
238
265
|
withWebDecoy
|
|
239
266
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/nextjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.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.8.0"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
48
|
"next": ">=13.0.0"
|