@webdecoy/nextjs 0.21.3 → 0.22.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/README.md CHANGED
@@ -13,7 +13,47 @@ npm install @webdecoy/nextjs @webdecoy/node
13
13
 
14
14
  `@webdecoy/node` is the core SDK. Install it directly: rules such as `tripwire()` and `rateLimit()` are imported from it. Requires Next.js 13 or later.
15
15
 
16
- ## Quick start: detection middleware
16
+ ## Quick start on Vercel: one middleware, from the environment
17
+
18
+ Detection and clearance enforcement in one file, configured entirely by environment variables:
19
+
20
+ ```ts
21
+ // middleware.ts (Next.js 13-15), or proxy.ts (Next.js 16)
22
+ import webdecoy from '@webdecoy/nextjs/vercel';
23
+
24
+ export default webdecoy;
25
+
26
+ export const config = {
27
+ // Everything but static assets. Keep /.well-known: the challenge uses it.
28
+ matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
29
+ };
30
+ ```
31
+
32
+ Write it as import then export. Next.js 16 does not follow `export { default } from '@webdecoy/nextjs/vercel'` when it checks the file for a default export, and fails the build. `config` must be declared in the file itself.
33
+
34
+ | Variable | Turns on | Secret? |
35
+ |---|---|---|
36
+ | `WEBDECOY_API_KEY` | Detection: bots, tripwire hits and agents the bot registry names are reported to your dashboard. From the **API Keys** page. | Yes |
37
+ | `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` | The clearance gate, with the sensor key. From **Enforcement, Setup**. | No |
38
+ | `WEBDECOY_SENSOR_KEY` | The clearance gate. From **Enforcement, Setup**. | Yes |
39
+
40
+ Either half works without the other; with neither, every request passes. Detection starts in monitor mode and passes its verdict to your app as request headers (see [Monitor mode](#monitor-mode)); the gate follows the site's mode in WebDecoy, monitor until you switch it, and hands the page its verdict as `x-wd-clearance` (read it with `getEdgeVerdict()`). Every failure, WebDecoy unreachable included, lets the request through.
41
+
42
+ To customize, use `createVercelMiddleware(options)` from the same path. It takes the core SDK options (`apiKey`, `rules`, `threatScoreThreshold`, ...), `mode`, `skipPaths`, `trustProxy`, `onBlocked` as `(request, decision) => Response`, and the gate's `siteKey`, `scannerId`, `sensorKey` and `ingest`; the key and the gate's identity come from the environment when not passed.
43
+
44
+ ```ts
45
+ // middleware.ts
46
+ import { createVercelMiddleware } from '@webdecoy/nextjs/vercel';
47
+ import { tripwire } from '@webdecoy/node';
48
+
49
+ export default createVercelMiddleware({
50
+ rules: [tripwire({ paths: ['/admin-backup.zip'] })],
51
+ });
52
+
53
+ export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };
54
+ ```
55
+
56
+ ## Detection middleware
17
57
 
18
58
  ```ts
19
59
  // middleware.ts (project root, or src/ if you use it)
@@ -31,7 +71,7 @@ export const config = {
31
71
 
32
72
  ### API key
33
73
 
34
- `apiKey` is optional. Without it the middleware runs local rules only (the built-in tripwire paths by default) and nothing reports to your dashboard. The middleware does **not** read `WEBDECOY_API_KEY` from the environment by itself: pass it as `apiKey`, as above.
74
+ `apiKey` is optional. Without it the middleware runs local rules only (the built-in tripwire paths by default) and nothing reports to your dashboard. `withWebDecoy` does **not** read `WEBDECOY_API_KEY` from the environment by itself: pass it as `apiKey`, as above. (`@webdecoy/nextjs/vercel` does read it.)
35
75
 
36
76
  To get a key: sign up at [app.webdecoy.com](https://app.webdecoy.com), add your site, then create a key on the **API Keys** page. Keys start with `sk_live_` or `sk_test_`. Set it in your deployment's environment (for example the Vercel project settings), never in committed code.
37
77
 
@@ -109,7 +149,7 @@ export const config = {
109
149
 
110
150
  If any of the first three is missing, the gate passes every request and logs a warning once. Guide: [Vercel middleware](https://docs.webdecoy.com/installation/vercel-middleware/).
111
151
 
112
- A project has one `middleware.ts`, so choose `withWebDecoy` or `withClearance` for it.
152
+ A project has one `middleware.ts`. `@webdecoy/nextjs/vercel` runs detection and the gate together; `withWebDecoy` and `withClearance` are each one half.
113
153
 
114
154
  ## Pages Router API routes
115
155
 
@@ -140,7 +180,7 @@ It reads the socket address and trusts no forwarding header unless you pass `tru
140
180
 
141
181
  ## Environment variables
142
182
 
143
- Only `withClearance()` reads environment variables (table above). `WEBDECOY_API_KEY` and `WEBDECOY_SECRET` are naming conventions: pass their values as `apiKey` and `secret`.
183
+ `@webdecoy/nextjs/vercel` reads `WEBDECOY_API_KEY` and the clearance variables; `withClearance()` reads the clearance variables (table above). Elsewhere `WEBDECOY_API_KEY` and `WEBDECOY_SECRET` are naming conventions: pass their values as `apiKey` and `secret`.
144
184
 
145
185
  ## Verify the install
146
186
 
@@ -0,0 +1,67 @@
1
+ import { NextRequest, NextFetchEvent } from 'next/server';
2
+ import { DetectAndGateOptions } from '@webdecoy/node';
3
+ export { DetectAndGateOptions } from '@webdecoy/node';
4
+
5
+ /**
6
+ * WebDecoy for Next.js on Vercel: detection and the clearance gate in one
7
+ * middleware, configured from the environment.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // middleware.ts (Next.js 13-15), or proxy.ts (Next.js 16)
12
+ * import webdecoy from '@webdecoy/nextjs/vercel';
13
+ *
14
+ * export default webdecoy;
15
+ *
16
+ * export const config = {
17
+ * // Everything but static assets. Keep /.well-known: the challenge uses it.
18
+ * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
19
+ * };
20
+ * ```
21
+ *
22
+ * Import then export, rather than `export { default } from ...`: Next.js 16
23
+ * looks for the default export without following a re-export and fails the
24
+ * build on that one-line form, in `proxy.ts` and `middleware.ts` alike. And
25
+ * `config` has to be written in the file itself, because Next reads it
26
+ * statically.
27
+ *
28
+ * Environment:
29
+ *
30
+ * - `WEBDECOY_API_KEY` turns on detection: bots are reported to WebDecoy,
31
+ * including the reserved test trigger (`curl -A "WebDecoy-Test/1.0" ...`),
32
+ * and the verdict reaches the app as request headers
33
+ * (`x-webdecoy-decision`, `x-webdecoy-detection-id`, `x-webdecoy-would-block`).
34
+ * - `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` and `WEBDECOY_SENSOR_KEY` turn
35
+ * on the clearance gate for the paths the site protects in WebDecoy; the
36
+ * page receives its verdict as `x-wd-clearance` (see `getEdgeVerdict()`).
37
+ *
38
+ * Either half works without the other; with neither, every request passes.
39
+ * Detection starts in monitor mode and the gate follows the site's mode in
40
+ * WebDecoy (monitor by default), so installing this changes no response until
41
+ * you choose to enforce. Every failure lets the request through.
42
+ *
43
+ * To customize, use {@link createVercelMiddleware}:
44
+ *
45
+ * ```typescript
46
+ * // middleware.ts
47
+ * import { createVercelMiddleware } from '@webdecoy/nextjs/vercel';
48
+ * import { tripwire } from '@webdecoy/node';
49
+ *
50
+ * export default createVercelMiddleware({
51
+ * rules: [tripwire({ paths: ['/admin-backup.zip'] })],
52
+ * });
53
+ *
54
+ * export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };
55
+ * ```
56
+ */
57
+
58
+ /**
59
+ * Detection and the clearance gate as Next.js middleware. Options default to
60
+ * the environment (`WEBDECOY_API_KEY`, `WEBDECOY_SITE_KEY`,
61
+ * `WEBDECOY_SCANNER_ID`, `WEBDECOY_SENSOR_KEY`).
62
+ */
63
+ declare function createVercelMiddleware(options?: DetectAndGateOptions): (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
64
+ /** The middleware configured from WEBDECOY_* environment variables. */
65
+ declare const middleware: (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
66
+
67
+ export { createVercelMiddleware, middleware as default };
@@ -0,0 +1,67 @@
1
+ import { NextRequest, NextFetchEvent } from 'next/server';
2
+ import { DetectAndGateOptions } from '@webdecoy/node';
3
+ export { DetectAndGateOptions } from '@webdecoy/node';
4
+
5
+ /**
6
+ * WebDecoy for Next.js on Vercel: detection and the clearance gate in one
7
+ * middleware, configured from the environment.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * // middleware.ts (Next.js 13-15), or proxy.ts (Next.js 16)
12
+ * import webdecoy from '@webdecoy/nextjs/vercel';
13
+ *
14
+ * export default webdecoy;
15
+ *
16
+ * export const config = {
17
+ * // Everything but static assets. Keep /.well-known: the challenge uses it.
18
+ * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
19
+ * };
20
+ * ```
21
+ *
22
+ * Import then export, rather than `export { default } from ...`: Next.js 16
23
+ * looks for the default export without following a re-export and fails the
24
+ * build on that one-line form, in `proxy.ts` and `middleware.ts` alike. And
25
+ * `config` has to be written in the file itself, because Next reads it
26
+ * statically.
27
+ *
28
+ * Environment:
29
+ *
30
+ * - `WEBDECOY_API_KEY` turns on detection: bots are reported to WebDecoy,
31
+ * including the reserved test trigger (`curl -A "WebDecoy-Test/1.0" ...`),
32
+ * and the verdict reaches the app as request headers
33
+ * (`x-webdecoy-decision`, `x-webdecoy-detection-id`, `x-webdecoy-would-block`).
34
+ * - `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` and `WEBDECOY_SENSOR_KEY` turn
35
+ * on the clearance gate for the paths the site protects in WebDecoy; the
36
+ * page receives its verdict as `x-wd-clearance` (see `getEdgeVerdict()`).
37
+ *
38
+ * Either half works without the other; with neither, every request passes.
39
+ * Detection starts in monitor mode and the gate follows the site's mode in
40
+ * WebDecoy (monitor by default), so installing this changes no response until
41
+ * you choose to enforce. Every failure lets the request through.
42
+ *
43
+ * To customize, use {@link createVercelMiddleware}:
44
+ *
45
+ * ```typescript
46
+ * // middleware.ts
47
+ * import { createVercelMiddleware } from '@webdecoy/nextjs/vercel';
48
+ * import { tripwire } from '@webdecoy/node';
49
+ *
50
+ * export default createVercelMiddleware({
51
+ * rules: [tripwire({ paths: ['/admin-backup.zip'] })],
52
+ * });
53
+ *
54
+ * export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };
55
+ * ```
56
+ */
57
+
58
+ /**
59
+ * Detection and the clearance gate as Next.js middleware. Options default to
60
+ * the environment (`WEBDECOY_API_KEY`, `WEBDECOY_SITE_KEY`,
61
+ * `WEBDECOY_SCANNER_ID`, `WEBDECOY_SENSOR_KEY`).
62
+ */
63
+ declare function createVercelMiddleware(options?: DetectAndGateOptions): (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
64
+ /** The middleware configured from WEBDECOY_* environment variables. */
65
+ declare const middleware: (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
66
+
67
+ export { createVercelMiddleware, middleware as default };
package/dist/vercel.js ADDED
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/vercel.ts
21
+ var vercel_exports = {};
22
+ __export(vercel_exports, {
23
+ createVercelMiddleware: () => createVercelMiddleware,
24
+ default: () => vercel_default
25
+ });
26
+ module.exports = __toCommonJS(vercel_exports);
27
+ var import_server = require("next/server");
28
+ var import_node = require("@webdecoy/node");
29
+ function createVercelMiddleware(options = {}) {
30
+ const decide = (0, import_node.createDetectAndGate)(options);
31
+ return async (req, event) => {
32
+ try {
33
+ const { outcome } = await decide(req, {
34
+ waitUntil: event ? (p) => event.waitUntil(p) : void 0
35
+ });
36
+ if (outcome.action === "respond") return outcome.response;
37
+ if (!outcome.headers) return import_server.NextResponse.next();
38
+ return import_server.NextResponse.next({ request: { headers: outcome.headers } });
39
+ } catch {
40
+ return import_server.NextResponse.next();
41
+ }
42
+ };
43
+ }
44
+ var middleware = createVercelMiddleware();
45
+ var vercel_default = middleware;
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ createVercelMiddleware
49
+ });
@@ -0,0 +1,24 @@
1
+ // src/vercel.ts
2
+ import { NextResponse } from "next/server";
3
+ import { createDetectAndGate } from "@webdecoy/node";
4
+ function createVercelMiddleware(options = {}) {
5
+ const decide = createDetectAndGate(options);
6
+ return async (req, event) => {
7
+ try {
8
+ const { outcome } = await decide(req, {
9
+ waitUntil: event ? (p) => event.waitUntil(p) : void 0
10
+ });
11
+ if (outcome.action === "respond") return outcome.response;
12
+ if (!outcome.headers) return NextResponse.next();
13
+ return NextResponse.next({ request: { headers: outcome.headers } });
14
+ } catch {
15
+ return NextResponse.next();
16
+ }
17
+ };
18
+ }
19
+ var middleware = createVercelMiddleware();
20
+ var vercel_default = middleware;
21
+ export {
22
+ createVercelMiddleware,
23
+ vercel_default as default
24
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.21.3",
3
+ "version": "0.22.0",
4
4
  "description": "Web Decoy middleware for Next.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -9,18 +9,30 @@
9
9
  "require": "./dist/index.js",
10
10
  "import": "./dist/index.mjs",
11
11
  "types": "./dist/index.d.ts"
12
+ },
13
+ "./vercel": {
14
+ "require": "./dist/vercel.js",
15
+ "import": "./dist/vercel.mjs",
16
+ "types": "./dist/vercel.d.ts"
17
+ }
18
+ },
19
+ "typesVersions": {
20
+ "*": {
21
+ "vercel": [
22
+ "./dist/vercel.d.ts"
23
+ ]
12
24
  }
13
25
  },
14
26
  "files": [
15
27
  "dist"
16
28
  ],
17
29
  "scripts": {
18
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
19
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
30
+ "build": "tsup src/index.ts src/vercel.ts --format cjs,esm --dts --clean",
31
+ "dev": "tsup src/index.ts src/vercel.ts --format cjs,esm --dts --watch",
20
32
  "test": "jest --passWithNoTests",
21
33
  "lint": "eslint src --max-warnings 2",
22
34
  "clean": "rm -rf dist",
23
- "check:edge": "node ../../scripts/check-edge.mjs src/index.ts"
35
+ "check:edge": "node ../../scripts/check-edge.mjs src/index.ts src/vercel.ts"
24
36
  },
25
37
  "keywords": [
26
38
  "web-decoy",
@@ -42,7 +54,7 @@
42
54
  "url": "https://github.com/WebDecoy/node/issues"
43
55
  },
44
56
  "dependencies": {
45
- "@webdecoy/node": "^0.21.3"
57
+ "@webdecoy/node": "^0.22.0"
46
58
  },
47
59
  "peerDependencies": {
48
60
  "next": ">=13.0.0"