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.
Files changed (100) hide show
  1. package/.dockerignore +6 -0
  2. package/.github/workflows/node.js.yml +1 -1
  3. package/.github/workflows/npm-publish.yml +2 -1
  4. package/INSTALLATION.md +144 -0
  5. package/README.md +468 -92
  6. package/aiwaf.sqlite +0 -0
  7. package/bin/aiwaf.js +172 -0
  8. package/examples/sandbox/README.md +95 -0
  9. package/examples/sandbox/aiwaf-adonis-proxy/Dockerfile +16 -0
  10. package/examples/sandbox/aiwaf-adonis-proxy/package.json +13 -0
  11. package/examples/sandbox/aiwaf-adonis-proxy/server.js +71 -0
  12. package/examples/sandbox/aiwaf-fastify-proxy/Dockerfile +21 -0
  13. package/examples/sandbox/aiwaf-fastify-proxy/package.json +16 -0
  14. package/examples/sandbox/aiwaf-fastify-proxy/server.js +42 -0
  15. package/examples/sandbox/aiwaf-hapi-proxy/Dockerfile +16 -0
  16. package/examples/sandbox/aiwaf-hapi-proxy/package.json +15 -0
  17. package/examples/sandbox/aiwaf-hapi-proxy/server.js +55 -0
  18. package/examples/sandbox/aiwaf-koa-proxy/Dockerfile +16 -0
  19. package/examples/sandbox/aiwaf-koa-proxy/package.json +16 -0
  20. package/examples/sandbox/aiwaf-koa-proxy/server.js +41 -0
  21. package/examples/sandbox/aiwaf-nest-proxy/Dockerfile +17 -0
  22. package/examples/sandbox/aiwaf-nest-proxy/main.js +55 -0
  23. package/examples/sandbox/aiwaf-nest-proxy/package.json +20 -0
  24. package/examples/sandbox/aiwaf-next-proxy/Dockerfile +16 -0
  25. package/examples/sandbox/aiwaf-next-proxy/package.json +16 -0
  26. package/examples/sandbox/aiwaf-next-proxy/pages/index.js +3 -0
  27. package/examples/sandbox/aiwaf-next-proxy/server.js +65 -0
  28. package/examples/sandbox/aiwaf-proxy/Dockerfile +21 -0
  29. package/examples/sandbox/aiwaf-proxy/package.json +15 -0
  30. package/examples/sandbox/aiwaf-proxy/server.js +44 -0
  31. package/examples/sandbox/attack-suite.js +572 -0
  32. package/examples/sandbox/compare-results-modes.js +141 -0
  33. package/examples/sandbox/compare-results.js +324 -0
  34. package/examples/sandbox/docker-compose.yml +142 -0
  35. package/examples/sandbox/run-and-compare.js +32 -0
  36. package/geolock/ipinfo_lite.mmdb +0 -0
  37. package/index.js +15 -2
  38. package/lib/adonisMiddleware.js +82 -0
  39. package/lib/anomalyDetector.js +221 -16
  40. package/lib/blacklistManager.js +129 -5
  41. package/lib/csvStore.js +84 -0
  42. package/lib/dynamicKeyword.js +20 -4
  43. package/lib/dynamicKeywordStore.js +169 -0
  44. package/lib/exemptionStore.js +186 -0
  45. package/lib/exemptions.js +56 -0
  46. package/lib/fastifyPlugin.js +49 -0
  47. package/lib/featureUtils.js +146 -22
  48. package/lib/geoBlocker.js +157 -0
  49. package/lib/geoStore.js +111 -0
  50. package/lib/hapiPlugin.js +92 -0
  51. package/lib/headerValidation.js +259 -0
  52. package/lib/honeypotDetector.js +90 -4
  53. package/lib/koaMiddleware.js +68 -0
  54. package/lib/middlewareLogger.js +123 -0
  55. package/lib/modelStore.js +129 -0
  56. package/lib/nestMiddleware.js +12 -0
  57. package/lib/nextMiddleware.js +78 -0
  58. package/lib/rateLimiter.js +68 -10
  59. package/lib/redisClient.js +8 -3
  60. package/lib/requestLogStore.js +167 -0
  61. package/lib/settingsCompat.js +107 -0
  62. package/lib/uuidDetector.js +34 -9
  63. package/lib/wafMiddleware.js +282 -42
  64. package/lib/wasmAdapter.js +187 -0
  65. package/package.json +41 -4
  66. package/resources/model.json +9536 -1
  67. package/test/adonis-middleware.test.js +129 -0
  68. package/test/anomaly-detector.test.js +36 -0
  69. package/test/cli.test.js +125 -0
  70. package/test/csv-fallback.test.js +165 -0
  71. package/test/dynamic-keyword-integration.test.js +24 -0
  72. package/test/dynamic-keyword-store.test.js +78 -0
  73. package/test/exemptions-db.test.js +38 -0
  74. package/test/fastify-plugin.test.js +106 -0
  75. package/test/geo-mmdb.test.js +77 -0
  76. package/test/hapi-plugin.test.js +115 -0
  77. package/test/header-validation.test.js +66 -0
  78. package/test/honeypot-detector.test.js +42 -0
  79. package/test/isolation-forest.test.js +38 -0
  80. package/test/jest.teardown.js +33 -0
  81. package/test/koa-middleware.test.js +104 -0
  82. package/test/middleware-behavior.test.js +75 -0
  83. package/test/model-store-db.test.js +22 -0
  84. package/test/model-store.test.js +31 -0
  85. package/test/nest-integration.test.js +92 -0
  86. package/test/nest-middleware.test.js +88 -0
  87. package/test/next-handler.test.js +174 -0
  88. package/test/perf-wasm.js +64 -0
  89. package/test/redis-client.test.js +35 -0
  90. package/test/settingsCompat.test.js +95 -0
  91. package/test/train.test.js +137 -0
  92. package/test/uuid-detector.test.js +20 -0
  93. package/test/waf.test.js +213 -11
  94. package/test/wasm-adapter.test.js +59 -0
  95. package/test/wasm-retrain-behavior.test.js +87 -0
  96. package/test/wasm-waf-integration.test.js +65 -0
  97. package/test-anomaly.js +77 -0
  98. package/test-complete-waf.js +147 -0
  99. package/test-simple.js +79 -0
  100. package/train.js +536 -87
package/.dockerignore ADDED
@@ -0,0 +1,6 @@
1
+ node_modules
2
+ **/node_modules
3
+ dist
4
+ build
5
+ npm-debug.log
6
+ *.log
@@ -26,6 +26,6 @@
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
  cache: 'npm'
29
- - run: npm ci
29
+ - run: npm install
30
30
  - run: npm run build --if-present
31
31
  - run: npm test
@@ -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 ci
23
+ - run: npm install
23
24
  - run: npm test
24
25
  - run: npm publish --access public
25
26
  env:
@@ -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.