@webdecoy/node 0.21.2 → 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 +203 -155
- package/dist/{chunk-HSG6RWOQ.mjs → chunk-FL7FXNYV.mjs} +483 -483
- package/dist/{chunk-NM4QG3XD.mjs → chunk-P7Z7XFK6.mjs} +221 -1
- package/dist/{index-GXXXr_mb.d.ts → detect-and-gate-BES1jZ8P.d.mts} +146 -1
- package/dist/{index-GXXXr_mb.d.mts → detect-and-gate-C7Uw14OR.d.ts} +146 -1
- package/dist/index.d.mts +4 -91
- package/dist/index.d.ts +4 -91
- package/dist/index.js +684 -613
- package/dist/index.mjs +16 -146
- package/dist/{sdk-FCU-Ce-1.d.mts → sdk-B8M-WYTc.d.mts} +2 -2
- package/dist/{sdk-FCU-Ce-1.d.ts → sdk-B8M-WYTc.d.ts} +2 -2
- package/dist/testing.d.mts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/testing.js +246 -246
- package/dist/testing.mjs +1 -1
- package/dist/vercel.d.mts +42 -9
- package/dist/vercel.d.ts +42 -9
- package/dist/vercel.js +2784 -1
- package/dist/vercel.mjs +16 -3
- package/package.json +11 -1
package/README.md
CHANGED
|
@@ -1,173 +1,247 @@
|
|
|
1
1
|
# @webdecoy/node
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The core WebDecoy SDK for Node.js and edge runtimes: tripwires, rate limits and other local rules, plus optional server-side bot detection reported to your WebDecoy dashboard.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@webdecoy/node)
|
|
6
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
7
|
|
|
8
|
+
Full documentation: [docs.webdecoy.com/sdk-plugins/node-sdk](https://docs.webdecoy.com/sdk-plugins/node-sdk/). Copy-paste install snippets for every stack: [docs.webdecoy.com/installation/install-with-ai](https://docs.webdecoy.com/installation/install-with-ai/).
|
|
9
|
+
|
|
10
|
+
## Using a framework? Install its adapter
|
|
11
|
+
|
|
12
|
+
Most apps should install a framework adapter alongside this package. The adapters handle request mapping, client IP, monitor mode and honeytoken injection for you. Install `@webdecoy/node` too: it is where `tripwire()`, `rateLimit()` and the other rules are imported from.
|
|
13
|
+
|
|
14
|
+
| Framework | Install |
|
|
15
|
+
|---|---|
|
|
16
|
+
| Express | `npm install @webdecoy/express @webdecoy/node` ([README](https://www.npmjs.com/package/@webdecoy/express)) |
|
|
17
|
+
| Fastify | `npm install @webdecoy/fastify @webdecoy/node` ([README](https://www.npmjs.com/package/@webdecoy/fastify)) |
|
|
18
|
+
| Next.js | `npm install @webdecoy/nextjs @webdecoy/node` ([README](https://www.npmjs.com/package/@webdecoy/nextjs)) |
|
|
19
|
+
| Hono (Workers, Bun, Deno, Node) | `npm install @webdecoy/hono @webdecoy/node` ([README](https://www.npmjs.com/package/@webdecoy/hono)) |
|
|
20
|
+
| Any other server or fetch runtime | `npm install @webdecoy/node` (this page) |
|
|
21
|
+
|
|
8
22
|
## Installation
|
|
9
23
|
|
|
10
24
|
```bash
|
|
11
25
|
npm install @webdecoy/node
|
|
12
|
-
# or
|
|
13
|
-
yarn add @webdecoy/node
|
|
14
26
|
```
|
|
15
27
|
|
|
16
|
-
## Quick
|
|
28
|
+
## Quick start
|
|
17
29
|
|
|
18
|
-
```
|
|
30
|
+
```js
|
|
31
|
+
import http from 'node:http';
|
|
19
32
|
import { WebDecoy } from '@webdecoy/node';
|
|
20
33
|
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
34
|
+
const wd = new WebDecoy({ apiKey: process.env.WEBDECOY_API_KEY });
|
|
35
|
+
|
|
36
|
+
http.createServer(async (req, res) => {
|
|
37
|
+
const decision = await wd.protect({
|
|
38
|
+
method: req.method,
|
|
39
|
+
path: new URL(req.url, 'http://localhost').pathname,
|
|
40
|
+
ip: req.socket.remoteAddress ?? '',
|
|
41
|
+
user_agent: req.headers['user-agent'],
|
|
42
|
+
headers: Object.fromEntries(
|
|
43
|
+
Object.entries(req.headers).map(([name, value]) => [name, String(value)])
|
|
44
|
+
),
|
|
45
|
+
timestamp: Date.now(),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// protect() returns a decision and blocks nothing itself.
|
|
49
|
+
// Log decision.conclusion ('ALLOW', 'DENY', 'CHALLENGE' or 'ERROR') first;
|
|
50
|
+
// refuse with a 403 when decision.allowed is false only once you are ready.
|
|
51
|
+
res.end('ok');
|
|
52
|
+
}).listen(3000);
|
|
53
|
+
```
|
|
24
54
|
|
|
25
|
-
|
|
26
|
-
method: 'GET',
|
|
27
|
-
path: '/api/data',
|
|
28
|
-
ip: '203.0.113.42',
|
|
29
|
-
user_agent: req.headers['user-agent'],
|
|
30
|
-
headers: req.headers,
|
|
31
|
-
timestamp: Date.now(),
|
|
32
|
-
});
|
|
55
|
+
Behind a proxy, `req.socket.remoteAddress` is the proxy. Use `resolveClientIp()` from this package to read the real client address (see [Client IP behind a proxy](#client-ip-behind-a-proxy)).
|
|
33
56
|
|
|
34
|
-
|
|
35
|
-
// Block the request
|
|
36
|
-
return res.status(403).json({ error: 'Access denied' });
|
|
37
|
-
}
|
|
38
|
-
```
|
|
57
|
+
### The API key is optional
|
|
39
58
|
|
|
40
|
-
|
|
59
|
+
`apiKey` is optional. Without it the SDK runs **local rules only** (tripwires by default, plus any rules you add), makes no network calls, and reports nothing to the dashboard. With it, the SDK also sends suspicious requests for server-side detection and reports rule hits to your dashboard.
|
|
41
60
|
|
|
42
|
-
|
|
43
|
-
const webdecoy = new WebDecoy({
|
|
44
|
-
// Required: Your API key from the dashboard
|
|
45
|
-
apiKey: 'sk_live_xxxxx',
|
|
61
|
+
`new WebDecoy()` does **not** read `WEBDECOY_API_KEY` from the environment by itself (only the Vercel middleware does). Pass it as `apiKey`, as above.
|
|
46
62
|
|
|
47
|
-
|
|
48
|
-
apiUrl: 'https://in.webdecoy.com',
|
|
63
|
+
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_` (production) or `sk_test_` (testing); any other prefix throws when the SDK is constructed. Keep the key on the server and out of committed files.
|
|
49
64
|
|
|
50
|
-
|
|
51
|
-
enableTLSFingerprinting: true,
|
|
65
|
+
### Monitor first
|
|
52
66
|
|
|
53
|
-
|
|
54
|
-
threatScoreThreshold: 70,
|
|
67
|
+
`protect()` only returns a decision. It never writes a response, so nothing is blocked until your code acts on `decision.allowed`. The framework adapters and `createFetchGuard()` follow the same rule: they start in **monitor mode** (`mode: 'monitor'`), record the decision and serve every request. Set `mode: 'enforce'` only after you have reviewed what would have been blocked.
|
|
55
68
|
|
|
56
|
-
|
|
57
|
-
timeout: 5000,
|
|
69
|
+
## The decision
|
|
58
70
|
|
|
59
|
-
|
|
60
|
-
debug: false,
|
|
71
|
+
`protect(metadata, options?)` returns a `Decision`:
|
|
61
72
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
73
|
+
```ts
|
|
74
|
+
const decision = await wd.protect(metadata);
|
|
75
|
+
|
|
76
|
+
decision.conclusion // 'ALLOW' | 'DENY' | 'CHALLENGE' | 'ERROR'
|
|
77
|
+
decision.allowed // true for ALLOW and ERROR (fails open), false for DENY and CHALLENGE
|
|
78
|
+
decision.id // 'dec_...'
|
|
79
|
+
decision.isDenied() // also isAllowed(), isChallenged(), isErrored()
|
|
80
|
+
decision.deniedBy('tripwire') // whether a named rule denied the request
|
|
81
|
+
decision.results // every configured rule, its state and its conclusion
|
|
82
|
+
decision.reason // the deciding reason, when there is one
|
|
83
|
+
decision.detection // { decision, confidence, threat_level, bot_detected, bot_type?, detection_id, rule_enforced }
|
|
84
|
+
decision.error // set when conclusion is 'ERROR'
|
|
85
|
+
decision.edge // what a WebDecoy edge validator said about this request, if one fronted it
|
|
65
86
|
```
|
|
66
87
|
|
|
67
|
-
|
|
88
|
+
Each entry in `results` has a `state`:
|
|
68
89
|
|
|
69
|
-
|
|
90
|
+
| `state` | Meaning |
|
|
91
|
+
|---|---|
|
|
92
|
+
| `RUN` | Evaluated, and its conclusion counts. |
|
|
93
|
+
| `DRY_RUN` | Evaluated; the conclusion is recorded but not enforced. |
|
|
94
|
+
| `NOT_RUN` | Could not evaluate because a signal it needs was absent (for example a `filter()` rule with no IP enrichment). |
|
|
95
|
+
| `CACHED` | Not evaluated; a prior decision for this key was reused. |
|
|
70
96
|
|
|
71
|
-
|
|
97
|
+
`ERROR` is not a denial. It means no verdict was reached (for example WebDecoy could not be reached, or the request had no IP), and `allowed` is `true`.
|
|
72
98
|
|
|
73
|
-
|
|
99
|
+
`ProtectResult` is the exported interface type for this decision.
|
|
74
100
|
|
|
75
|
-
|
|
101
|
+
## Local rules
|
|
76
102
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
103
|
+
Rules run in-process, in order, before any server call. The first `DENY` or `THROTTLE` decides. When you pass no `rules`, the SDK arms `tripwire()` with its built-in scanner-bait paths; pass `rules: []` to opt out.
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { WebDecoy, tripwire, rateLimit, filter } from '@webdecoy/node';
|
|
107
|
+
|
|
108
|
+
const wd = new WebDecoy({
|
|
109
|
+
rules: [
|
|
110
|
+
tripwire({ paths: ['/admin-backup.zip'] }), // plus built-in paths such as /.env
|
|
111
|
+
rateLimit({ max: 100, window: 60 }), // 100 requests per 60 s per IP
|
|
112
|
+
filter({ expression: 'ip.tor or ip.vpn' }), // needs an apiKey (IP enrichment)
|
|
113
|
+
],
|
|
85
114
|
});
|
|
86
115
|
```
|
|
87
116
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
bot_type?: string;
|
|
98
|
-
detection_id: string;
|
|
99
|
-
rule_enforced: boolean;
|
|
100
|
-
};
|
|
101
|
-
error?: string;
|
|
102
|
-
}
|
|
103
|
-
```
|
|
117
|
+
| Rule | What it does | Needs an API key? |
|
|
118
|
+
|---|---|---|
|
|
119
|
+
| `tripwire({ paths?, prefixes?, patterns?, includeDefaults?, action?, dryRun? })` | Matches requests for decoy and scanner-bait paths. | No |
|
|
120
|
+
| `honeytoken()` | Returns `{ path, linkHtml }`: a hidden decoy link and the path to arm with `tripwire({ paths: [path] })`. | No |
|
|
121
|
+
| `rateLimit({ max, window, algorithm?, keyBy?, store?, action?, dryRun? })` | Fixed or sliding window, keyed by IP by default. In-memory unless you pass a shared `store` (for example `upstashRateLimitStore()`). | No |
|
|
122
|
+
| `attackSignatures({ inspect?, exclude?, action?, dryRun? })` | Denies requests carrying unambiguous injection payloads. | No |
|
|
123
|
+
| `webBotAuth()` | Verifies Web Bot Auth (RFC 9421) agent signatures and denies impersonators of known agents. | No |
|
|
124
|
+
| `botPolicy({ deny, allow })` | One crawler policy that produces both your `robots.txt` and the rule that enforces it. | No |
|
|
125
|
+
| `filter({ expression, action?, dryRun? })` | Expressions over IP reputation and geo, such as `ip.tor` or `ip.country in ["CN", "RU"]`. | Yes |
|
|
104
126
|
|
|
105
|
-
|
|
127
|
+
A tripwire match is evidence, not proof of intent: link prefetchers, preview bots and your own security scanners can request a hidden or scanner-bait path. Use `dryRun: true` on a rule to observe it before enforcing.
|
|
106
128
|
|
|
107
|
-
|
|
129
|
+
## Fetch runtimes (Bun, Deno, Workers, Astro, SvelteKit, Remix)
|
|
108
130
|
|
|
109
|
-
|
|
131
|
+
`createFetchGuard()` wraps the SDK for any runtime that hands you a WHATWG `Request`. It is the same implementation `@webdecoy/hono` uses.
|
|
110
132
|
|
|
111
|
-
|
|
133
|
+
```ts
|
|
134
|
+
import { createFetchGuard } from '@webdecoy/node';
|
|
112
135
|
|
|
113
|
-
|
|
136
|
+
const guard = createFetchGuard({ apiKey: process.env.WEBDECOY_API_KEY });
|
|
114
137
|
|
|
115
|
-
|
|
138
|
+
export default {
|
|
139
|
+
async fetch(request: Request): Promise<Response> {
|
|
140
|
+
const { response } = await guard.check(request);
|
|
141
|
+
if (response) return response; // only ever set in enforce mode
|
|
142
|
+
return guard.decorate(await handle(request)); // handle() is your app
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Options: everything in [Configuration](#configuration), plus `mode` (default `'monitor'`), `skipPaths`, `trustProxy` (default `1`), `getIP`, `honeytoken` (default on when an `apiKey` is set; injects a hidden trap link into HTML through `decorate()`) and `onBlocked`.
|
|
148
|
+
|
|
149
|
+
## Vercel middleware: detection and clearance
|
|
150
|
+
|
|
151
|
+
One Routing Middleware file for Astro, SvelteKit, Nuxt, Remix or a static site on Vercel. It reports bots to WebDecoy and runs the clearance gate on the paths you protect, both configured from the project's environment.
|
|
116
152
|
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
LocalAnalysis,
|
|
126
|
-
SDKDetectionRequest,
|
|
127
|
-
} from '@webdecoy/node';
|
|
153
|
+
```ts
|
|
154
|
+
// middleware.ts (project root)
|
|
155
|
+
export { default } from '@webdecoy/node/vercel';
|
|
156
|
+
|
|
157
|
+
export const config = {
|
|
158
|
+
// Everything but static assets. Keep /.well-known: the challenge uses it.
|
|
159
|
+
matcher: ['/((?!_astro/|_next/static/|favicon.ico|.*\\.(?:css|js|png|jpg|svg|webp|woff2?)$).*)'],
|
|
160
|
+
};
|
|
128
161
|
```
|
|
129
162
|
|
|
130
|
-
|
|
163
|
+
| Variable | Turns on | Secret? |
|
|
164
|
+
|---|---|---|
|
|
165
|
+
| `WEBDECOY_API_KEY` | Detection: bots, tripwire hits and agents the bot registry names are reported to your dashboard. From the **API Keys** page. | Yes |
|
|
166
|
+
| `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` | The clearance gate, with the sensor key. From **Enforcement, Setup**. | No |
|
|
167
|
+
| `WEBDECOY_SENSOR_KEY` | The clearance gate. From **Enforcement, Setup**. | Yes |
|
|
168
|
+
|
|
169
|
+
Either half works without the other; with neither, every request passes. Detection starts in monitor mode (the request is served and the verdict rides on the request as `x-webdecoy-decision`, `x-webdecoy-detection-id` and `x-webdecoy-would-block`), and the gate follows the site's mode in WebDecoy, monitor until you switch it. Every failure, WebDecoy unreachable included, lets the request through. Without `WEBDECOY_API_KEY` the middleware is the clearance gate alone, exactly as before.
|
|
170
|
+
|
|
171
|
+
To customize it, use `createVercelMiddleware(options)` from `@webdecoy/node/vercel`: it takes the core [Configuration](#configuration) options (`rules`, `threatScoreThreshold`, ...), `mode`, `skipPaths`, `trustProxy` and the gate's `siteKey`, `scannerId`, `sensorKey` and `ingest`, each defaulting to the environment. `clearanceMiddleware()` is the gate alone. Next.js sites use `@webdecoy/nextjs/vercel`, the same middleware in Next's types. Guide: [Vercel middleware](https://docs.webdecoy.com/installation/vercel-middleware/).
|
|
131
172
|
|
|
132
|
-
|
|
133
|
-
**in-process** bot-detection engine and captcha — no remote call required. It
|
|
134
|
-
scores ~40 behavioral, environmental, and fingerprint signals collected by the
|
|
135
|
-
[`@webdecoy/client`](https://www.npmjs.com/package/@webdecoy/client) browser
|
|
136
|
-
widget, verifies a SHA-256 proof-of-work, and issues a signed session token.
|
|
173
|
+
Verify it with the reserved test User-Agent; a detection labeled **Test** appears within a few seconds:
|
|
137
174
|
|
|
175
|
+
```bash
|
|
176
|
+
curl -A "WebDecoy-Test/1.0" https://your-site.vercel.app/
|
|
138
177
|
```
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
178
|
+
|
|
179
|
+
## Environment variables
|
|
180
|
+
|
|
181
|
+
The SDK reads only these, and only in the features named:
|
|
182
|
+
|
|
183
|
+
| Variable | Read by |
|
|
184
|
+
|---|---|
|
|
185
|
+
| `WEBDECOY_API_KEY` | The Vercel middleware (`@webdecoy/node/vercel`, `createDetectAndGate()`), when `apiKey` is not passed. |
|
|
186
|
+
| `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID`, `WEBDECOY_SENSOR_KEY` | The clearance gate (`@webdecoy/node/vercel`, `createClearanceGate()`), when the matching option is not passed. |
|
|
187
|
+
| `WEBDECOY_INGEST` | The clearance gate, to override its ingest endpoint. Optional. |
|
|
188
|
+
| `NODE_ENV` | The self-hosted captcha: with `production`, a missing or default `secret` throws. |
|
|
189
|
+
|
|
190
|
+
Everywhere else `WEBDECOY_API_KEY` is a naming convention used in these docs: pass its value as `apiKey`.
|
|
191
|
+
|
|
192
|
+
## Configuration
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
const wd = new WebDecoy({
|
|
196
|
+
apiKey: process.env.WEBDECOY_API_KEY, // optional; without it, local rules only
|
|
197
|
+
rules: [tripwire()], // optional; default [tripwire()], [] to opt out
|
|
198
|
+
apiUrl: 'https://in.webdecoy.com', // optional (default shown)
|
|
199
|
+
threatScoreThreshold: 80, // optional, 0-100 (default 80)
|
|
200
|
+
timeout: 5000, // optional, ms (default 5000)
|
|
201
|
+
enableTLSFingerprinting: true, // optional (default true)
|
|
202
|
+
tlsRejectUnauthorized: true, // optional (default true)
|
|
203
|
+
countAIReferrals: true, // optional (default true; aggregate counts, only with an apiKey)
|
|
204
|
+
debug: false, // optional (default false)
|
|
205
|
+
});
|
|
144
206
|
```
|
|
145
207
|
|
|
146
|
-
|
|
208
|
+
Also accepted: `characteristics` (what identifies the same caller for rate limits and caching; default the client IP), `decisionCache`, `webBotAuth`, `logger` (any object with `debug`/`info`/`warn`/`error`; `fromPino()` adapts pino) and `tracer` (an OpenTelemetry tracer).
|
|
147
209
|
|
|
148
|
-
|
|
149
|
-
serves `GET /__webdecoy/challenge`, `POST /__webdecoy/verify`, `POST
|
|
150
|
-
/__webdecoy/score`, and `POST /__webdecoy/token/verify`.
|
|
210
|
+
`protect()` also takes per-request options: `threshold`, `skipLocalAnalysis` and `metadata`.
|
|
151
211
|
|
|
152
|
-
|
|
153
|
-
import express from 'express';
|
|
154
|
-
import { webdecoyCaptcha } from '@webdecoy/express';
|
|
212
|
+
Without an API key no request leaves your server. With one, a request goes to WebDecoy only when local analysis flags it as needing verification (or carries TLS details); that call is awaited, bounded by `timeout`, and fails open on error.
|
|
155
213
|
|
|
156
|
-
|
|
157
|
-
|
|
214
|
+
## Client IP behind a proxy
|
|
215
|
+
|
|
216
|
+
Rate limits and detections are keyed on the client address. `X-Forwarded-For` is partly written by the client, so read it only as far as your own infrastructure wrote it:
|
|
158
217
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
218
|
+
```ts
|
|
219
|
+
import { resolveClientIp } from '@webdecoy/node';
|
|
220
|
+
|
|
221
|
+
const ip = resolveClientIp({
|
|
222
|
+
headers: req.headers,
|
|
223
|
+
peer: req.socket.remoteAddress,
|
|
224
|
+
trustProxy: 1, // proxies in front of this app; or 'cloudflare', 'railway', or your proxies' CIDRs
|
|
225
|
+
});
|
|
164
226
|
```
|
|
165
227
|
|
|
166
|
-
|
|
228
|
+
`trustProxy` defaults to `false` here (believe no forwarding header). The adapters accept the same option.
|
|
229
|
+
|
|
230
|
+
## Self-hosted captcha and detection engine
|
|
167
231
|
|
|
168
|
-
|
|
232
|
+
The SDK also ships an in-process captcha: it scores behavioral, environmental and fingerprint signals collected by [`@webdecoy/client`](https://www.npmjs.com/package/@webdecoy/client), verifies a SHA-256 proof of work, and issues a signed session token. No remote call is needed.
|
|
233
|
+
|
|
234
|
+
Mount the endpoints with your adapter (`webdecoyCaptcha` in Express, `webdecoyCaptchaPlugin` in Fastify, `createCaptchaHandler` in Next.js) or with `createCaptchaEndpoints()`. They serve `GET /__webdecoy/challenge`, `POST /__webdecoy/verify`, `POST /__webdecoy/score` and `POST /__webdecoy/token/verify`.
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import express from 'express';
|
|
238
|
+
import { webdecoyCaptcha } from '@webdecoy/express';
|
|
169
239
|
import { Captcha } from '@webdecoy/node';
|
|
170
240
|
|
|
241
|
+
const app = express();
|
|
242
|
+
app.use(express.json());
|
|
243
|
+
app.use(webdecoyCaptcha({ secret: process.env.WEBDECOY_SECRET })); // required in production
|
|
244
|
+
|
|
171
245
|
const captcha = new Captcha({ secret: process.env.WEBDECOY_SECRET });
|
|
172
246
|
|
|
173
247
|
app.post('/login', async (req, res) => {
|
|
@@ -177,61 +251,35 @@ app.post('/login', async (req, res) => {
|
|
|
177
251
|
});
|
|
178
252
|
```
|
|
179
253
|
|
|
180
|
-
|
|
254
|
+
`WEBDECOY_SECRET` is just the variable name used here; pass the value as `secret`. Generate one with `openssl rand -hex 32`. Challenge and token stores are in memory by default; for serverless or multi-instance deployments, pass shared `challengeStore` and `tokenStore` implementations.
|
|
181
255
|
|
|
182
|
-
|
|
256
|
+
To score signals yourself, use `new DetectionEngine(options).score(signals, context)`.
|
|
183
257
|
|
|
184
|
-
|
|
185
|
-
import { DetectionEngine } from '@webdecoy/node';
|
|
258
|
+
## Testing your rules
|
|
186
259
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
siteKey: 'site',
|
|
191
|
-
userAgent,
|
|
192
|
-
headers,
|
|
193
|
-
});
|
|
194
|
-
// verdict: { success, score, recommendation: 'allow'|'challenge'|'block', categoryScores, detections }
|
|
195
|
-
```
|
|
260
|
+
```ts
|
|
261
|
+
import { createTestHarness, get, expectDenied, expectAllowed } from '@webdecoy/node/testing';
|
|
262
|
+
import { tripwire } from '@webdecoy/node';
|
|
196
263
|
|
|
197
|
-
|
|
198
|
-
> across categories, so no single signal can cross the block threshold alone —
|
|
199
|
-
> a missing proof-of-work on an otherwise-clean request yields `challenge`, not
|
|
200
|
-
> `block`. Tune category weights via the `weights` option.
|
|
264
|
+
const wd = createTestHarness({ rules: [tripwire()] });
|
|
201
265
|
|
|
202
|
-
|
|
266
|
+
test('a scanner is denied and a visitor is not', async () => {
|
|
267
|
+
expectDenied(await wd.protect(get('/.env')), { rule: 'tripwire' });
|
|
268
|
+
expectAllowed(await wd.protect(get('/')));
|
|
269
|
+
});
|
|
270
|
+
```
|
|
203
271
|
|
|
204
|
-
|
|
205
|
-
(`NODE_ENV=production`); a missing or default secret throws. Generate one with
|
|
206
|
-
`openssl rand -hex 32`.
|
|
207
|
-
- Challenge/token/fingerprint stores are **in-memory** by default. For
|
|
208
|
-
serverless or multi-instance deployments, supply a shared store via the
|
|
209
|
-
`challengeStore` / `tokenStore` options (the `ChallengeStore` / `TokenStore`
|
|
210
|
-
interfaces are the seam for Redis).
|
|
211
|
-
- IP reputation (VPN/proxy/Tor, abuse score, geo) is still served by
|
|
212
|
-
`api.webdecoy.com` via the SDK's IP-enrichment client.
|
|
272
|
+
The harness is offline by default: it drops any `apiKey` unless you pass `allowNetwork: true`.
|
|
213
273
|
|
|
214
|
-
##
|
|
274
|
+
## Verify the install
|
|
215
275
|
|
|
216
|
-
|
|
276
|
+
With an `apiKey` configured, send one request with the reserved test User-Agent:
|
|
217
277
|
|
|
218
278
|
```bash
|
|
219
|
-
|
|
279
|
+
curl -A "WebDecoy-Test/1.0" http://localhost:3000/
|
|
220
280
|
```
|
|
221
281
|
|
|
222
|
-
See [
|
|
223
|
-
|
|
224
|
-
## Getting an API Key
|
|
225
|
-
|
|
226
|
-
1. Sign up at [app.webdecoy.com](https://app.webdecoy.com)
|
|
227
|
-
2. Create a new organization and property
|
|
228
|
-
3. Generate an API key in Settings
|
|
229
|
-
|
|
230
|
-
API keys start with `sk_live_` for production or `sk_test_` for testing.
|
|
231
|
-
|
|
232
|
-
## Documentation
|
|
233
|
-
|
|
234
|
-
For full documentation, visit the [GitHub repository](https://github.com/WebDecoy/node).
|
|
282
|
+
A detection labeled **Test** appears on the **Detections** page within a few seconds. Without an `apiKey` nothing is reported; the decision's `error` says so. See [Verify your install](https://docs.webdecoy.com/getting-started/verify-installation/).
|
|
235
283
|
|
|
236
284
|
## License
|
|
237
285
|
|