@webdecoy/hono 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 +131 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @webdecoy/hono
|
|
2
|
+
|
|
3
|
+
WebDecoy middleware for Hono on Cloudflare Workers, Bun, Deno and Node: 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/hono)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @webdecoy/hono @webdecoy/node
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`@webdecoy/node` is the core SDK. Install it directly: rules such as `tripwire()` and `rateLimit()` are imported from it. Requires Hono 4.
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Hono } from 'hono';
|
|
20
|
+
import { webdecoy } from '@webdecoy/hono';
|
|
21
|
+
|
|
22
|
+
const app = new Hono();
|
|
23
|
+
|
|
24
|
+
app.use('*', webdecoy({
|
|
25
|
+
apiKey: process.env.WEBDECOY_API_KEY,
|
|
26
|
+
skipPaths: ['/health'],
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
app.get('/', (c) => {
|
|
30
|
+
// c.get('webdecoy') carries the decision; in monitor mode this is where it surfaces
|
|
31
|
+
return c.text('ok');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export default app;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
On Cloudflare Workers, `process.env` needs the `nodejs_compat` compatibility flag and a recent compatibility date. Behind Cloudflare, `trustProxy: 'cloudflare'` is stronger than the default of one trusted hop.
|
|
38
|
+
|
|
39
|
+
### API key
|
|
40
|
+
|
|
41
|
+
`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.
|
|
42
|
+
|
|
43
|
+
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_`. Store it as a secret in your runtime's environment (for example `wrangler secret put WEBDECOY_API_KEY`), never in committed code.
|
|
44
|
+
|
|
45
|
+
### Monitor mode
|
|
46
|
+
|
|
47
|
+
The middleware starts in **monitor mode**: it stores the decision on the context and serves every request, even one it would have blocked. Review what it would have blocked, then opt in with `mode: 'enforce'`. In enforce mode a rule refusal answers `403`, a rate-limit throttle answers `429` with `Retry-After`, and a server-score block answers `403`, unless you pass `onBlocked`.
|
|
48
|
+
|
|
49
|
+
## Reading the decision
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
app.get('/', (c) => {
|
|
53
|
+
const decision = c.get('webdecoy'); // also available as c.get('webdecoyDecision')
|
|
54
|
+
if (decision?.deniedBy('tripwire')) console.log('tripwire hit', decision.id);
|
|
55
|
+
return c.text('ok');
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The decision has `conclusion` (`'ALLOW'`, `'DENY'`, `'CHALLENGE'` or `'ERROR'`), `allowed`, `results` (every rule and what it concluded), `deniedBy(rule)`, `id` and `detection`. See the [@webdecoy/node README](https://www.npmjs.com/package/@webdecoy/node) for the full shape.
|
|
60
|
+
|
|
61
|
+
## Local rules
|
|
62
|
+
|
|
63
|
+
With no `rules` option the middleware arms `tripwire()` with its built-in scanner-bait paths (`/.env`, `/wp-config.php` and similar). Pass `rules: []` to opt out, or add your own:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { tripwire, rateLimit } from '@webdecoy/node';
|
|
67
|
+
|
|
68
|
+
app.use('*', webdecoy({
|
|
69
|
+
rules: [
|
|
70
|
+
tripwire({ paths: ['/admin-backup.zip'] }),
|
|
71
|
+
rateLimit({ max: 100, window: 60 }),
|
|
72
|
+
],
|
|
73
|
+
}));
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`rateLimit()` counts in memory per isolate or process. On Workers or any multi-instance deployment, pass a shared store such as `upstashRateLimitStore()` from `@webdecoy/node`.
|
|
77
|
+
|
|
78
|
+
## Options
|
|
79
|
+
|
|
80
|
+
`webdecoy()` accepts every core SDK option (`apiKey`, `rules`, `apiUrl`, `threatScoreThreshold`, `timeout`, `debug`, ...) and the per-request options `threshold`, `skipLocalAnalysis` and `metadata`, plus:
|
|
81
|
+
|
|
82
|
+
| Option | Default | Description |
|
|
83
|
+
|---|---|---|
|
|
84
|
+
| `mode` | `'monitor'` | `'monitor'` records and serves every request; `'enforce'` blocks. |
|
|
85
|
+
| `skipPaths` | none | Paths to skip, as strings or regular expressions. |
|
|
86
|
+
| `trustProxy` | `1` | How much of `X-Forwarded-For` to believe: a hop count, `'cloudflare'`, `'railway'`, or your proxies' CIDRs. |
|
|
87
|
+
| `getIP` | derived from `trustProxy` | `(request: Request) => string`. Overrides `trustProxy`. |
|
|
88
|
+
| `honeytoken` | on when `apiKey` is set | Inject a hidden trap link into full HTML responses and arm the tripwire it points at. |
|
|
89
|
+
| `onBlocked` | `403`, or `429` for a throttle | `(c, decision) => Response`. Enforce mode only; replaces the default response for every block. |
|
|
90
|
+
|
|
91
|
+
## Without Hono
|
|
92
|
+
|
|
93
|
+
For Bun, Deno, Astro, SvelteKit, Remix or a bare Workers `fetch` handler, `createFetchGuard()` from `@webdecoy/node` is the same implementation this middleware wraps:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { createFetchGuard } from '@webdecoy/node';
|
|
97
|
+
|
|
98
|
+
const guard = createFetchGuard({ apiKey: process.env.WEBDECOY_API_KEY });
|
|
99
|
+
|
|
100
|
+
export default {
|
|
101
|
+
async fetch(request: Request): Promise<Response> {
|
|
102
|
+
const { response } = await guard.check(request);
|
|
103
|
+
if (response) return response; // only ever set in enforce mode
|
|
104
|
+
return guard.decorate(await handle(request)); // handle() is your app
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Environment variables
|
|
110
|
+
|
|
111
|
+
None are read automatically. `WEBDECOY_API_KEY` above is a naming convention: pass its value as `apiKey`.
|
|
112
|
+
|
|
113
|
+
## Verify the install
|
|
114
|
+
|
|
115
|
+
With an `apiKey` configured, send one request with the reserved test User-Agent:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
curl -A "WebDecoy-Test/1.0" http://localhost:8787/
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Use your dev server's port. A detection labeled **Test** appears on the **Detections** page within a few seconds.
|
|
122
|
+
|
|
123
|
+
## Documentation
|
|
124
|
+
|
|
125
|
+
- [Install with an AI assistant](https://docs.webdecoy.com/installation/install-with-ai/#hono): copy-paste snippets for every stack
|
|
126
|
+
- [Node.js SDK](https://docs.webdecoy.com/sdk-plugins/node-sdk/)
|
|
127
|
+
- [Verify your install](https://docs.webdecoy.com/getting-started/verify-installation/)
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/hono",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Web Decoy middleware for Hono \u2014 Cloudflare Workers, Bun, Deno, Node",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"url": "https://github.com/WebDecoy/node/issues"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@webdecoy/node": "^0.
|
|
47
|
+
"@webdecoy/node": "^0.22.0"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"hono": "^4.0.0"
|