@webdecoy/fastify 0.21.1 → 0.21.3
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 +153 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# @webdecoy/fastify
|
|
2
|
+
|
|
3
|
+
WebDecoy plugin for Fastify 4 and 5: tripwires, rate limits and other local rules, plus optional server-side bot detection reported to your WebDecoy dashboard. Starts in monitor mode.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@webdecoy/fastify)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @webdecoy/fastify @webdecoy/node
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`@webdecoy/node` is the core SDK. Install it directly: rules such as `tripwire()` and `rateLimit()` are imported from it.
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import Fastify from 'fastify';
|
|
20
|
+
import webdecoy from '@webdecoy/fastify';
|
|
21
|
+
|
|
22
|
+
const app = Fastify();
|
|
23
|
+
|
|
24
|
+
await app.register(webdecoy, {
|
|
25
|
+
apiKey: process.env.WEBDECOY_API_KEY,
|
|
26
|
+
skipPaths: ['/health'],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
app.get('/', async (request) => {
|
|
30
|
+
// request.webdecoy carries the detection (decision, confidence, threat_level, bot_detected)
|
|
31
|
+
// request.webdecoyDecision carries the full typed decision
|
|
32
|
+
return { ok: true };
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await app.listen({ port: 3000 });
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The plugin hooks every route (it is registered with `fastify-plugin`, so it is not scoped to an encapsulation context). The default export and the named export `webdecoyPlugin` are the same plugin.
|
|
39
|
+
|
|
40
|
+
### API key
|
|
41
|
+
|
|
42
|
+
`apiKey` is optional. Without it the plugin runs local rules only (the built-in tripwire paths by default) and nothing reports to your dashboard. The plugin does **not** read `WEBDECOY_API_KEY` from the environment by itself: pass it as `apiKey`, as above.
|
|
43
|
+
|
|
44
|
+
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_`. Keep it in your server environment, never in committed code.
|
|
45
|
+
|
|
46
|
+
### Monitor mode
|
|
47
|
+
|
|
48
|
+
The plugin starts in **monitor mode**: it records the decision on the request and serves every request, even one it would have blocked. Review what it would have blocked, then opt in:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
await app.register(webdecoy, {
|
|
52
|
+
apiKey: process.env.WEBDECOY_API_KEY,
|
|
53
|
+
mode: 'enforce',
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
In enforce mode a rule refusal answers `403` (naming the rule), a rate-limit throttle answers `429` with `Retry-After`, and a request blocked by the server-side score goes to `onBlocked` (default `403`).
|
|
58
|
+
|
|
59
|
+
## Local rules
|
|
60
|
+
|
|
61
|
+
With no `rules` option the plugin arms `tripwire()` with its built-in scanner-bait paths (`/.env`, `/wp-config.php` and similar). Pass `rules: []` to opt out, or add your own:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
import { tripwire, rateLimit } from '@webdecoy/node';
|
|
65
|
+
|
|
66
|
+
await app.register(webdecoy, {
|
|
67
|
+
rules: [
|
|
68
|
+
tripwire({ paths: ['/admin-backup.zip'] }),
|
|
69
|
+
rateLimit({ max: 100, window: 60 }),
|
|
70
|
+
],
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
app.addHook('onResponse', async (request) => {
|
|
74
|
+
if (request.webdecoyDecision?.deniedBy('tripwire')) {
|
|
75
|
+
request.log.warn({ decision: request.webdecoyDecision.id }, 'tripwire hit');
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
See the [@webdecoy/node README](https://www.npmjs.com/package/@webdecoy/node) for every rule and the decision shape (`conclusion`, `allowed`, `results`, `deniedBy()`).
|
|
81
|
+
|
|
82
|
+
## Options
|
|
83
|
+
|
|
84
|
+
The plugin accepts every core SDK option (`apiKey`, `rules`, `apiUrl`, `threatScoreThreshold`, `timeout`, `debug`, ...) and the per-request options `threshold`, `skipLocalAnalysis` and `metadata`, plus:
|
|
85
|
+
|
|
86
|
+
| Option | Default | Description |
|
|
87
|
+
|---|---|---|
|
|
88
|
+
| `mode` | `'monitor'` | `'monitor'` records and serves every request; `'enforce'` blocks. |
|
|
89
|
+
| `skipPaths` | none | Paths to skip, as strings or regular expressions. |
|
|
90
|
+
| `trustProxy` | unset: uses `request.ip` | How much of `X-Forwarded-For` to believe: a hop count, `'cloudflare'`, `'railway'`, or your proxies' CIDRs. |
|
|
91
|
+
| `getIP` | derived from `trustProxy` | `(request) => string`. Overrides `trustProxy`. |
|
|
92
|
+
| `honeytoken` | on when `apiKey` is set | Inject a hidden trap link into HTML replies and arm the tripwire it points at. |
|
|
93
|
+
| `onBlocked` | `403` JSON | `(request, reply, detection, decision) => void`. Enforce mode, server-score blocks. |
|
|
94
|
+
| `onError` | log and allow | `(request, reply, error) => void`. The plugin fails open. |
|
|
95
|
+
|
|
96
|
+
### Request properties
|
|
97
|
+
|
|
98
|
+
| Property | What it is |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `request.webdecoy` | The detection: `decision`, `confidence`, `threat_level`, `bot_detected`, `bot_type`, `detection_id`, `rule_enforced`. |
|
|
101
|
+
| `request.webdecoyDecision` | The full decision: `conclusion`, `allowed`, `results`, `deniedBy(rule)`, `id`. In monitor mode this is the only place a verdict surfaces. |
|
|
102
|
+
| `request.webdecoyEdge` | What a WebDecoy edge validator said about the request, when one fronts your app. |
|
|
103
|
+
|
|
104
|
+
### Client IP behind a proxy
|
|
105
|
+
|
|
106
|
+
Unset, the plugin uses `request.ip`, which follows Fastify's own `trustProxy` server option and otherwise is the socket address. Behind a load balancer or platform proxy, set `trustProxy` on the WebDecoy plugin rather than relying on Fastify's numeric server option:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
await app.register(webdecoy, {
|
|
110
|
+
apiKey: process.env.WEBDECOY_API_KEY,
|
|
111
|
+
trustProxy: 1, // or 'cloudflare', 'railway', or ['10.0.0.0/8']
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Honeytoken injection
|
|
116
|
+
|
|
117
|
+
With an `apiKey`, the plugin injects a hidden link into buffered HTML replies (`reply.send(html)`, `@fastify/view`) and arms a tripwire on its path. Streamed replies are left untouched and a warning is logged once.
|
|
118
|
+
|
|
119
|
+
## Self-hosted captcha
|
|
120
|
+
|
|
121
|
+
`webdecoyCaptchaPlugin` mounts the captcha endpoints (`/__webdecoy/challenge`, `/verify`, `/score`, `/token/verify`) used by [`@webdecoy/client`](https://www.npmjs.com/package/@webdecoy/client):
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
import { webdecoyCaptchaPlugin } from '@webdecoy/fastify';
|
|
125
|
+
|
|
126
|
+
await app.register(webdecoyCaptchaPlugin, { secret: process.env.WEBDECOY_SECRET });
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`secret` is required when `NODE_ENV` is `production`. Generate one with `openssl rand -hex 32`.
|
|
130
|
+
|
|
131
|
+
## Environment variables
|
|
132
|
+
|
|
133
|
+
None are read automatically. `WEBDECOY_API_KEY` and `WEBDECOY_SECRET` above are naming conventions: pass their values as `apiKey` and `secret`.
|
|
134
|
+
|
|
135
|
+
## Verify the install
|
|
136
|
+
|
|
137
|
+
With an `apiKey` configured, send one request with the reserved test User-Agent:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
curl -A "WebDecoy-Test/1.0" http://localhost:3000/
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
A detection labeled **Test** appears on the **Detections** page within a few seconds. If nothing appears, check that the plugin is registered before your routes and that `apiKey` is passed.
|
|
144
|
+
|
|
145
|
+
## Documentation
|
|
146
|
+
|
|
147
|
+
- [Fastify plugin](https://docs.webdecoy.com/sdk-plugins/fastify/)
|
|
148
|
+
- [Install with an AI assistant](https://docs.webdecoy.com/installation/install-with-ai/): copy-paste snippets for every stack
|
|
149
|
+
- [Verify your install](https://docs.webdecoy.com/getting-started/verify-installation/)
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/fastify",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.3",
|
|
4
4
|
"description": "Web Decoy plugin for Fastify",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"url": "https://github.com/WebDecoy/node/issues"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@webdecoy/node": "^0.21.
|
|
43
|
+
"@webdecoy/node": "^0.21.3",
|
|
44
44
|
"fastify-plugin": "^4.5.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|