aiwaf 1.0.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.
Files changed (50) hide show
  1. package/INSTALLATION.md +144 -0
  2. package/LICENSE +21 -0
  3. package/README.md +621 -0
  4. package/bin/aiwaf.js +349 -0
  5. package/index.js +62 -0
  6. package/lib/adonisMiddleware.js +82 -0
  7. package/lib/anomalyDetector.js +221 -0
  8. package/lib/blacklistManager.js +134 -0
  9. package/lib/blockResponses.js +25 -0
  10. package/lib/csvStore.js +84 -0
  11. package/lib/dynamicKeyword.js +42 -0
  12. package/lib/dynamicKeywordStore.js +169 -0
  13. package/lib/exemptionStore.js +204 -0
  14. package/lib/exemptions.js +56 -0
  15. package/lib/fastifyPlugin.js +49 -0
  16. package/lib/featureUtils.js +183 -0
  17. package/lib/geoBlocker.js +159 -0
  18. package/lib/geoPolicy.js +42 -0
  19. package/lib/geoStore.js +111 -0
  20. package/lib/hapiPlugin.js +92 -0
  21. package/lib/headerValidation.js +275 -0
  22. package/lib/honeypotDetector.js +91 -0
  23. package/lib/isolationForest.js +117 -0
  24. package/lib/keywordDetector.js +5 -0
  25. package/lib/keywordFallbackStore.js +39 -0
  26. package/lib/koaMiddleware.js +68 -0
  27. package/lib/middlewareLogger.js +123 -0
  28. package/lib/middlewarePlan.js +72 -0
  29. package/lib/modelSecurity.js +17 -0
  30. package/lib/modelStore.js +129 -0
  31. package/lib/nestMiddleware.js +12 -0
  32. package/lib/nextMiddleware.js +78 -0
  33. package/lib/pathManifest.js +533 -0
  34. package/lib/pathRules.js +184 -0
  35. package/lib/rateLimiter.js +153 -0
  36. package/lib/redisClient.js +35 -0
  37. package/lib/requestLogStore.js +167 -0
  38. package/lib/runtimeUtils.js +166 -0
  39. package/lib/settingsCompat.js +140 -0
  40. package/lib/sourceAst.js +294 -0
  41. package/lib/trainingLogic.js +87 -0
  42. package/lib/uuidDetector.js +46 -0
  43. package/lib/wafMiddleware.js +459 -0
  44. package/lib/wasmAdapter.js +480 -0
  45. package/lib/whois.js +38 -0
  46. package/package.json +85 -0
  47. package/resources/model.json +9536 -0
  48. package/train.js +595 -0
  49. package/utils/cache.js +3 -0
  50. package/utils/db.js +11 -0
@@ -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
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');
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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Aayush Gauba
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.