aiwaf 1.2.3 → 1.2.5

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.
@@ -0,0 +1,53 @@
1
+ 'use strict';
2
+
3
+ // AIWAF_PROFILE: one setting for sensible defaults.
4
+ // - balanced (the default) keeps every default as it is.
5
+ // - strict halves the rate limits and turns the method policy on.
6
+ // - monitor is a dry run: every check runs, and what it would block is logged instead, as
7
+ // "AIWAF monitor: would block ...". Nothing is blocked and no IP is added to the blocklist.
8
+ // A setting given explicitly always wins over the profile.
9
+
10
+ const PROFILES = ['strict', 'balanced', 'monitor'];
11
+ let active = null;
12
+
13
+ function resolve(value) {
14
+ const raw = value === undefined || value === null || value === '' ? process.env.AIWAF_PROFILE : value;
15
+ const name = String(raw || 'balanced').trim().toLowerCase();
16
+ if (!PROFILES.includes(name)) throw new Error(`AIWAF_PROFILE must be one of ${PROFILES.join(', ')}, not ${JSON.stringify(raw)}`);
17
+ return name;
18
+ }
19
+
20
+ function activate(value) {
21
+ active = resolve(value);
22
+ return active;
23
+ }
24
+
25
+ function monitoring() {
26
+ try {
27
+ return (active || resolve()) === 'monitor';
28
+ } catch (_error) {
29
+ return false;
30
+ }
31
+ }
32
+
33
+ // The defaults a profile sets, given Node's own defaults.
34
+ function defaultsFor(profile) {
35
+ if (profile === 'strict') {
36
+ return { MAX_REQ: 50, FLOOD_REQ: 100, AIWAF_METHOD_POLICY_ENABLED: true, AIWAF_SQL_INJECTION_MODE: 'block', AIWAF_INJECTION_RISK_MODE: 'block' };
37
+ }
38
+ if (profile === 'monitor') return { AIWAF_SQL_INJECTION_MODE: 'monitor', AIWAF_INJECTION_RISK_MODE: 'log' };
39
+ return {};
40
+ }
41
+
42
+ function wouldBlock(method, path, ip, status, reason) {
43
+ console.warn(`AIWAF monitor: would block ${method || '-'} ${path || '-'} from ${ip || '-'} with ${status} (${reason || 'blocked'})`);
44
+ }
45
+
46
+ // True in monitor mode, after logging the IP that would have been added to the blocklist.
47
+ function skipBlocklist(ip, reason) {
48
+ if (!monitoring()) return false;
49
+ console.warn(`AIWAF monitor: would add ${ip} to the blocklist (${reason})`);
50
+ return true;
51
+ }
52
+
53
+ module.exports = { PROFILES, activate, defaultsFor, monitoring, resolve, skipBlocklist, wouldBlock };
package/lib/routeRisks.js CHANGED
@@ -8,7 +8,10 @@ const { bodyBytes } = require('./sqlInjection');
8
8
  const wasmChecks = require('./wasmChecks');
9
9
 
10
10
  // The operations a saved finding can name. The rules for each run in aiwaf-wasm.
11
- const SINKS = ['sql', 'nosql', 'command', 'path', 'ssrf', 'code', 'template', 'deserialization'];
11
+ const SINKS = [
12
+ 'sql', 'nosql', 'command', 'path', 'ssrf', 'code', 'template', 'deserialization', 'xss', 'redirect', 'ldap', 'xpath', 'xxe',
13
+ 'redos', 'prototype'
14
+ ];
12
15
 
13
16
  /** Returns the first rule for this operation that matches the value. Needs aiwaf-wasm loaded. */
14
17
  function ruleFor(sink, value) {
@@ -104,7 +107,7 @@ function fileRoute(filename) {
104
107
  function routesFor(finding, routes) {
105
108
  const entry = String(finding.function || '').split(' -> ')[0];
106
109
  const paths = Object.keys(routes);
107
- const fromSource = (/^\S+ (\/\S*)$/.exec(entry) || [])[1] || fileRoute(finding.file);
110
+ const fromSource = (/^\S+ (\/\S*)$/.exec(entry) || [])[1] || fileRoute(finding.entry_file || finding.file);
108
111
  if (fromSource) {
109
112
  const wanted = trimRoute(fromSource);
110
113
  const exact = paths.filter(path => trimRoute(path) === wanted);
@@ -65,6 +65,9 @@ function toPathRules(value, defaultValue = []) {
65
65
 
66
66
  function normalizeSettings(rawOpts = {}) {
67
67
  const legacy = rawOpts.AIWAF_SETTINGS || {};
68
+ // AIWAF_PROFILE supplies the defaults below; a setting given explicitly still wins.
69
+ const profile = require('./profiles').resolve(firstDefined(rawOpts.AIWAF_PROFILE, rawOpts.profile));
70
+ const preset = require('./profiles').defaultsFor(profile);
68
71
 
69
72
  const normalized = {
70
73
  ...rawOpts,
@@ -72,8 +75,8 @@ function normalizeSettings(rawOpts = {}) {
72
75
  dynamicTopN: toNumber(firstDefined(rawOpts.dynamicTopN, rawOpts.DYNAMIC_TOP_N, legacy.keywords?.dynamicTopN), 10),
73
76
 
74
77
  WINDOW_SEC: toNumber(firstDefined(rawOpts.WINDOW_SEC, rawOpts.windowSec, rawOpts.AIWAF_WINDOW_SECONDS, legacy.rate?.window, process.env.AIWAF_RATE_WINDOW), 60),
75
- MAX_REQ: toNumber(firstDefined(rawOpts.MAX_REQ, rawOpts.maxReq, legacy.rate?.max, process.env.AIWAF_RATE_MAX), 100),
76
- FLOOD_REQ: toNumber(firstDefined(rawOpts.FLOOD_REQ, rawOpts.floodReq, legacy.rate?.flood, process.env.AIWAF_RATE_FLOOD), 200),
78
+ MAX_REQ: toNumber(firstDefined(rawOpts.MAX_REQ, rawOpts.maxReq, legacy.rate?.max, process.env.AIWAF_RATE_MAX), preset.MAX_REQ ?? 100),
79
+ FLOOD_REQ: toNumber(firstDefined(rawOpts.FLOOD_REQ, rawOpts.floodReq, legacy.rate?.flood, process.env.AIWAF_RATE_FLOOD), preset.FLOOD_REQ ?? 200),
77
80
 
78
81
  HONEYPOT_FIELD: firstDefined(rawOpts.HONEYPOT_FIELD, rawOpts.honeypotField, legacy.honeypot?.field),
79
82
  AIWAF_MIN_FORM_TIME: toNumber(firstDefined(rawOpts.AIWAF_MIN_FORM_TIME, rawOpts.minFormTime, legacy.honeypot?.minFormTime, process.env.AIWAF_MIN_FORM_TIME), 0),
@@ -81,7 +84,7 @@ function normalizeSettings(rawOpts = {}) {
81
84
  AIWAF_LOGIN_PATH_PREFIXES: toArray(firstDefined(rawOpts.AIWAF_LOGIN_PATH_PREFIXES, rawOpts.loginPathPrefixes, legacy.honeypot?.loginPathPrefixes, process.env.AIWAF_LOGIN_PATH_PREFIXES), [...loginPathPrefixes]),
82
85
  AIWAF_POST_ONLY_SUFFIXES: toArray(firstDefined(rawOpts.AIWAF_POST_ONLY_SUFFIXES, rawOpts.postOnlySuffixes, legacy.honeypot?.postOnlySuffixes, process.env.AIWAF_POST_ONLY_SUFFIXES), undefined),
83
86
  AIWAF_ALLOWED_METHODS: toArray(firstDefined(rawOpts.AIWAF_ALLOWED_METHODS, rawOpts.allowedMethods, legacy.honeypot?.allowedMethods, process.env.AIWAF_ALLOWED_METHODS), undefined),
84
- AIWAF_METHOD_POLICY_ENABLED: toBool(firstDefined(rawOpts.AIWAF_METHOD_POLICY_ENABLED, rawOpts.methodPolicyEnabled, legacy.honeypot?.methodPolicyEnabled, process.env.AIWAF_METHOD_POLICY_ENABLED), false),
87
+ AIWAF_METHOD_POLICY_ENABLED: toBool(firstDefined(rawOpts.AIWAF_METHOD_POLICY_ENABLED, rawOpts.methodPolicyEnabled, legacy.honeypot?.methodPolicyEnabled, process.env.AIWAF_METHOD_POLICY_ENABLED), preset.AIWAF_METHOD_POLICY_ENABLED ?? false),
85
88
 
86
89
  AIWAF_EXEMPT_PATHS: toArray(firstDefined(rawOpts.AIWAF_EXEMPT_PATHS, rawOpts.exemptPaths, legacy.exemptions?.paths, process.env.AIWAF_EXEMPT_PATHS), []),
87
90
  AIWAF_EXEMPT_IPS: toArray(firstDefined(rawOpts.AIWAF_EXEMPT_IPS, rawOpts.exemptIps, legacy.exemptions?.ips, process.env.AIWAF_EXEMPT_IPS), []),
@@ -134,7 +137,9 @@ function normalizeSettings(rawOpts = {}) {
134
137
  AIWAF_DISABLE_MIDDLEWARES: toArray(firstDefined(rawOpts.AIWAF_DISABLE_MIDDLEWARES, rawOpts.disableMiddlewares, rawOpts.disabledMiddlewares, legacy.middlewares?.disabled, process.env.AIWAF_DISABLE_MIDDLEWARES), []),
135
138
  AIWAF_PATH_RULES: toPathRules(firstDefined(rawOpts.AIWAF_PATH_RULES, rawOpts.pathRules, legacy.pathRules, process.env.AIWAF_PATH_RULES), []),
136
139
  AIWAF_PATH_MANIFEST: firstDefined(rawOpts.AIWAF_PATH_MANIFEST, rawOpts.pathManifest, legacy.pathManifest, process.env.AIWAF_PATH_MANIFEST, '.aiwaf/paths.json'),
137
- AIWAF_INJECTION_RISK_MODE: firstDefined(rawOpts.AIWAF_INJECTION_RISK_MODE, rawOpts.injectionRiskMode, process.env.AIWAF_INJECTION_RISK_MODE, 'block'),
140
+ AIWAF_INJECTION_RISK_MODE: firstDefined(rawOpts.AIWAF_INJECTION_RISK_MODE, rawOpts.injectionRiskMode, process.env.AIWAF_INJECTION_RISK_MODE, preset.AIWAF_INJECTION_RISK_MODE, 'block'),
141
+ AIWAF_SQL_INJECTION_MODE: firstDefined(rawOpts.AIWAF_SQL_INJECTION_MODE, rawOpts.sqlInjectionMode, process.env.AIWAF_SQL_INJECTION_MODE, preset.AIWAF_SQL_INJECTION_MODE),
142
+ AIWAF_PROFILE: profile,
138
143
  AIWAF_ROUTE_PLAN_VERSION: firstDefined(rawOpts.AIWAF_ROUTE_PLAN_VERSION, rawOpts.routePlanVersion, legacy.routePlanVersion, process.env.AIWAF_ROUTE_PLAN_VERSION, 0),
139
144
  AIWAF_ACCESS_LOG: firstDefined(rawOpts.AIWAF_ACCESS_LOG, rawOpts.accessLog, process.env.AIWAF_ACCESS_LOG, process.env.NODE_LOG_PATH),
140
145
 
@@ -158,7 +158,7 @@ function applyAutoMiddlewareDefaults(opts, enabled, rawOpts = {}) {
158
158
 
159
159
  // The first aiwaf-wasm with every engine feature from_settings relies on: stores, shared rate
160
160
  // counts, path-rule limits, monitor mode and UUID resolvers.
161
- const NATIVE_DEFAULT_WASM = '0.2.8';
161
+ const NATIVE_DEFAULT_WASM = '0.2.9';
162
162
 
163
163
  function installedWasmVersion() {
164
164
  const fs = require('fs');
@@ -225,6 +225,18 @@ const recentRequests = limit => Promise.resolve().then(() => requestLogStore.rec
225
225
  .catch(error => { storeFailed('request log read', error); return []; });
226
226
 
227
227
  module.exports = function aiwaf(rawOpts = {}) {
228
+ const profiles = require('./profiles');
229
+ if (profiles.activate(rawOpts.AIWAF_PROFILE ?? rawOpts.profile) === 'monitor') {
230
+ // Monitor runs the regular middleware, whose every block can be logged instead.
231
+ if (rawOpts.AIWAF_NATIVE_PIPELINE !== undefined && rawOpts.AIWAF_NATIVE_PIPELINE !== false) {
232
+ throw new Error('AIWAF_PROFILE=monitor runs the regular middleware: leave AIWAF_NATIVE_PIPELINE unset.');
233
+ }
234
+ if (rawOpts.AIWAF_NATIVE_PIPELINE === undefined) {
235
+ staysRegular('AIWAF_PROFILE is monitor, which logs what it would block instead of blocking.');
236
+ console.warn('AIWAF monitor: nothing is blocked; what would be blocked is logged.');
237
+ }
238
+ rawOpts = { ...rawOpts, AIWAF_NATIVE_PIPELINE: false };
239
+ }
228
240
  if (rawOpts.AIWAF_NATIVE_PIPELINE === false) {
229
241
  const { AIWAF_NATIVE_PIPELINE: _off, ...regularOpts } = rawOpts;
230
242
  rawOpts = regularOpts;
@@ -388,6 +400,11 @@ module.exports = function aiwaf(rawOpts = {}) {
388
400
  markRequestStart(req);
389
401
 
390
402
  const deny = (status, code, reason, country = '') => {
403
+ if (opts.AIWAF_PROFILE === 'monitor') {
404
+ // A dry run: say what would be blocked, and let the request through.
405
+ require('./profiles').wouldBlock(method, path, ip, status, reason || code);
406
+ return next();
407
+ }
391
408
  middlewareLogger.markBlocked(res, reason || code, country);
392
409
  if (process.env.AIWAF_DEBUG_MIDDLEWARE) {
393
410
  console.error(`[AIWAF-BLOCK] status=${status} code=${code} reason=${reason} ip=${ip}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiwaf",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "AIWAF Web Application Firewall for JavaScript applications and major Node.js frameworks",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@babel/parser": "^7.29.2",
37
- "aiwaf-wasm": "^0.2.8",
37
+ "aiwaf-wasm": "^0.2.9",
38
38
  "express": "^4.21.2",
39
39
  "glob": "^14.0.6",
40
40
  "knex": "^2.4.2",