aiwaf-js 0.0.4 → 0.0.8
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/.dockerignore +6 -0
- package/.github/workflows/node.js.yml +1 -1
- package/.github/workflows/npm-publish.yml +2 -1
- package/INSTALLATION.md +144 -0
- package/README.md +468 -92
- package/aiwaf.sqlite +0 -0
- package/bin/aiwaf.js +172 -0
- package/examples/sandbox/README.md +95 -0
- package/examples/sandbox/aiwaf-adonis-proxy/Dockerfile +16 -0
- package/examples/sandbox/aiwaf-adonis-proxy/package.json +13 -0
- package/examples/sandbox/aiwaf-adonis-proxy/server.js +71 -0
- package/examples/sandbox/aiwaf-fastify-proxy/Dockerfile +21 -0
- package/examples/sandbox/aiwaf-fastify-proxy/package.json +16 -0
- package/examples/sandbox/aiwaf-fastify-proxy/server.js +42 -0
- package/examples/sandbox/aiwaf-hapi-proxy/Dockerfile +16 -0
- package/examples/sandbox/aiwaf-hapi-proxy/package.json +15 -0
- package/examples/sandbox/aiwaf-hapi-proxy/server.js +55 -0
- package/examples/sandbox/aiwaf-koa-proxy/Dockerfile +16 -0
- package/examples/sandbox/aiwaf-koa-proxy/package.json +16 -0
- package/examples/sandbox/aiwaf-koa-proxy/server.js +41 -0
- package/examples/sandbox/aiwaf-nest-proxy/Dockerfile +17 -0
- package/examples/sandbox/aiwaf-nest-proxy/main.js +55 -0
- package/examples/sandbox/aiwaf-nest-proxy/package.json +20 -0
- package/examples/sandbox/aiwaf-next-proxy/Dockerfile +16 -0
- package/examples/sandbox/aiwaf-next-proxy/package.json +16 -0
- package/examples/sandbox/aiwaf-next-proxy/pages/index.js +3 -0
- package/examples/sandbox/aiwaf-next-proxy/server.js +65 -0
- package/examples/sandbox/aiwaf-proxy/Dockerfile +21 -0
- package/examples/sandbox/aiwaf-proxy/package.json +15 -0
- package/examples/sandbox/aiwaf-proxy/server.js +44 -0
- package/examples/sandbox/attack-suite.js +572 -0
- package/examples/sandbox/compare-results-modes.js +141 -0
- package/examples/sandbox/compare-results.js +324 -0
- package/examples/sandbox/docker-compose.yml +142 -0
- package/examples/sandbox/run-and-compare.js +32 -0
- package/geolock/ipinfo_lite.mmdb +0 -0
- package/index.js +15 -2
- package/lib/adonisMiddleware.js +82 -0
- package/lib/anomalyDetector.js +221 -16
- package/lib/blacklistManager.js +129 -5
- package/lib/csvStore.js +84 -0
- package/lib/dynamicKeyword.js +20 -4
- package/lib/dynamicKeywordStore.js +169 -0
- package/lib/exemptionStore.js +186 -0
- package/lib/exemptions.js +56 -0
- package/lib/fastifyPlugin.js +49 -0
- package/lib/featureUtils.js +146 -22
- package/lib/geoBlocker.js +157 -0
- package/lib/geoStore.js +111 -0
- package/lib/hapiPlugin.js +92 -0
- package/lib/headerValidation.js +259 -0
- package/lib/honeypotDetector.js +90 -4
- package/lib/koaMiddleware.js +68 -0
- package/lib/middlewareLogger.js +123 -0
- package/lib/modelStore.js +129 -0
- package/lib/nestMiddleware.js +12 -0
- package/lib/nextMiddleware.js +78 -0
- package/lib/rateLimiter.js +68 -10
- package/lib/redisClient.js +8 -3
- package/lib/requestLogStore.js +167 -0
- package/lib/settingsCompat.js +107 -0
- package/lib/uuidDetector.js +34 -9
- package/lib/wafMiddleware.js +282 -42
- package/lib/wasmAdapter.js +187 -0
- package/package.json +41 -4
- package/resources/model.json +9536 -1
- package/test/adonis-middleware.test.js +129 -0
- package/test/anomaly-detector.test.js +36 -0
- package/test/cli.test.js +125 -0
- package/test/csv-fallback.test.js +165 -0
- package/test/dynamic-keyword-integration.test.js +24 -0
- package/test/dynamic-keyword-store.test.js +78 -0
- package/test/exemptions-db.test.js +38 -0
- package/test/fastify-plugin.test.js +106 -0
- package/test/geo-mmdb.test.js +77 -0
- package/test/hapi-plugin.test.js +115 -0
- package/test/header-validation.test.js +66 -0
- package/test/honeypot-detector.test.js +42 -0
- package/test/isolation-forest.test.js +38 -0
- package/test/jest.teardown.js +33 -0
- package/test/koa-middleware.test.js +104 -0
- package/test/middleware-behavior.test.js +75 -0
- package/test/model-store-db.test.js +22 -0
- package/test/model-store.test.js +31 -0
- package/test/nest-integration.test.js +92 -0
- package/test/nest-middleware.test.js +88 -0
- package/test/next-handler.test.js +174 -0
- package/test/perf-wasm.js +64 -0
- package/test/redis-client.test.js +35 -0
- package/test/settingsCompat.test.js +95 -0
- package/test/train.test.js +137 -0
- package/test/uuid-detector.test.js +20 -0
- package/test/waf.test.js +213 -11
- package/test/wasm-adapter.test.js +59 -0
- package/test/wasm-retrain-behavior.test.js +87 -0
- package/test/wasm-waf-integration.test.js +65 -0
- package/test-anomaly.js +77 -0
- package/test-complete-waf.js +147 -0
- package/test-simple.js +79 -0
- package/train.js +536 -87
package/.dockerignore
ADDED
|
@@ -8,6 +8,7 @@ on:
|
|
|
8
8
|
jobs:
|
|
9
9
|
build-and-publish:
|
|
10
10
|
runs-on: ubuntu-latest
|
|
11
|
+
environment: npm-publish
|
|
11
12
|
|
|
12
13
|
steps:
|
|
13
14
|
- uses: actions/checkout@v4
|
|
@@ -19,7 +20,7 @@ jobs:
|
|
|
19
20
|
node-version: 20
|
|
20
21
|
registry-url: https://registry.npmjs.org/
|
|
21
22
|
|
|
22
|
-
- run: npm
|
|
23
|
+
- run: npm install
|
|
23
24
|
- run: npm test
|
|
24
25
|
- run: npm publish --access public
|
|
25
26
|
env:
|
package/INSTALLATION.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# AIWAF-JS Installation Guide
|
|
2
|
+
|
|
3
|
+
This guide covers local setup, Redis setup, training setup, and common failure modes.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- Node.js 18+ recommended
|
|
8
|
+
- npm 9+ recommended
|
|
9
|
+
- Build tooling required by `sqlite3` (platform dependent)
|
|
10
|
+
|
|
11
|
+
## 1. Install Package
|
|
12
|
+
|
|
13
|
+
For application use:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install aiwaf-js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For local development in this repository:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 2. Basic Integration (Express)
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const express = require('express');
|
|
29
|
+
const aiwaf = require('aiwaf-js');
|
|
30
|
+
|
|
31
|
+
const app = express();
|
|
32
|
+
app.use(express.json());
|
|
33
|
+
|
|
34
|
+
app.use(aiwaf({
|
|
35
|
+
staticKeywords: ['.php', '.env', '.git'],
|
|
36
|
+
dynamicTopN: 10,
|
|
37
|
+
WINDOW_SEC: 10,
|
|
38
|
+
MAX_REQ: 20,
|
|
39
|
+
FLOOD_REQ: 40,
|
|
40
|
+
HONEYPOT_FIELD: 'hp_field',
|
|
41
|
+
uuidRoutePrefix: '/user'
|
|
42
|
+
}));
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 3. Optional Redis Setup
|
|
46
|
+
|
|
47
|
+
Set `REDIS_URL` (or `AIWAF_REDIS_URL`) before app startup:
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
export REDIS_URL=redis://localhost:6379
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
PowerShell:
|
|
54
|
+
|
|
55
|
+
```powershell
|
|
56
|
+
$env:REDIS_URL = 'redis://localhost:6379'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
If Redis is not configured or not reachable, AIWAF-JS falls back to in-memory behavior.
|
|
60
|
+
|
|
61
|
+
## 3.1 Optional GeoIP MMDB Setup
|
|
62
|
+
|
|
63
|
+
Install MMDB reader:
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install maxmind
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Place your database at `geolock/ipinfo_lite.mmdb` or set:
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
export AIWAF_GEO_MMDB_PATH=/absolute/path/to/ipinfo_lite.mmdb
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 4. Train a Model from Logs
|
|
76
|
+
|
|
77
|
+
By default, trainer reads `/var/log/nginx/access.log`. Override as needed:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
NODE_LOG_PATH=/path/to/access.log npm run train
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Include rotated logs:
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
NODE_LOG_GLOB='/path/to/access.log.*' npm run train
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Output model artifact:
|
|
90
|
+
|
|
91
|
+
- `resources/model.json`
|
|
92
|
+
|
|
93
|
+
## 5. Verify Installation
|
|
94
|
+
|
|
95
|
+
Run tests:
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
npm test
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Check CLI wiring:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
npm run aiwaf -- help
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Run a minimal app and hit a known benign route (`/`) and a suspicious route (for example path with `.php`) to validate block behavior.
|
|
108
|
+
|
|
109
|
+
## 6. Troubleshooting
|
|
110
|
+
|
|
111
|
+
### `Failed to load pretrained model`
|
|
112
|
+
|
|
113
|
+
- Run `npm run train` to generate `resources/model.json`.
|
|
114
|
+
- Ensure process has read access to the `resources/` directory.
|
|
115
|
+
|
|
116
|
+
### Redis warnings or connection failures
|
|
117
|
+
|
|
118
|
+
- Verify `REDIS_URL` value and Redis server health.
|
|
119
|
+
- Runtime is designed to continue with fallback behavior.
|
|
120
|
+
|
|
121
|
+
### SQLite errors (`blocked_ips` table not found)
|
|
122
|
+
|
|
123
|
+
- Ensure process can create/write `./aiwaf.sqlite`.
|
|
124
|
+
- `blacklistManager` auto-initializes the table, but write permissions are required.
|
|
125
|
+
- If DB logging is unavailable, enable CSV middleware logs:
|
|
126
|
+
- `AIWAF_MIDDLEWARE_LOG_CSV=true`
|
|
127
|
+
- `AIWAF_MIDDLEWARE_LOG_CSV_PATH=logs/aiwaf-requests.csv`
|
|
128
|
+
- Core tables also fall back automatically to CSV files in `logs/storage/` when DB operations fail.
|
|
129
|
+
|
|
130
|
+
### `sqlite3` install/build issues
|
|
131
|
+
|
|
132
|
+
- Install platform-native build dependencies and reinstall packages.
|
|
133
|
+
- On CI/container images, ensure compiler toolchain is present.
|
|
134
|
+
|
|
135
|
+
### Training finds no logs
|
|
136
|
+
|
|
137
|
+
- Confirm `NODE_LOG_PATH` exists and is readable.
|
|
138
|
+
- If using rotation, set `NODE_LOG_GLOB` to a valid glob.
|
|
139
|
+
|
|
140
|
+
## 7. Production Notes
|
|
141
|
+
|
|
142
|
+
- Prefer Redis or a custom shared cache backend for multi-instance deployments.
|
|
143
|
+
- Place middleware after body parsing middleware if honeypot detection is required.
|
|
144
|
+
- Review rate limits and thresholds against real traffic profiles before broad rollout.
|