aiwaf 1.1.4 → 1.1.6

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/bin/aiwaf.js CHANGED
@@ -9,9 +9,9 @@ function print(obj) {
9
9
  process.stdout.write(`${JSON.stringify(obj, null, 2)}\n`);
10
10
  }
11
11
 
12
- async function clearBlacklist() {
13
- const blacklistManager = require('../lib/blacklistManager');
14
- const deleted = await blacklistManager.clear();
12
+ async function clearBlocklist() {
13
+ const blocklistManager = require('../lib/blocklistManager');
14
+ const deleted = await blocklistManager.clear();
15
15
  print({ ok: true, cleared: deleted });
16
16
  }
17
17
 
@@ -51,13 +51,13 @@ function parseDuration(value) {
51
51
  }
52
52
 
53
53
  async function collectOperationalState() {
54
- const blacklistManager = require('../lib/blacklistManager');
54
+ const blocklistManager = require('../lib/blocklistManager');
55
55
  const exemptionStore = require('../lib/exemptionStore');
56
56
  const geoStore = require('../lib/geoStore');
57
57
  const dynamicKeywordStore = require('../lib/dynamicKeywordStore');
58
58
  const modelStore = require('../lib/modelStore');
59
- const [blacklist, ipExemptions, pathExemptions, geo, dynamicKeywords, model] = await Promise.all([
60
- blacklistManager.exportRecords(),
59
+ const [blocklist, ipExemptions, pathExemptions, geo, dynamicKeywords, model] = await Promise.all([
60
+ blocklistManager.exportRecords(),
61
61
  exemptionStore.listIps(),
62
62
  exemptionStore.listPaths(),
63
63
  geoStore.listBlockedCountries(),
@@ -67,7 +67,7 @@ async function collectOperationalState() {
67
67
  return {
68
68
  schema_version: '1.0',
69
69
  exported_at: new Date().toISOString(),
70
- blacklist,
70
+ blocklist,
71
71
  ip_exemptions: ipExemptions,
72
72
  path_exemptions: pathExemptions,
73
73
  geo_blocked_countries: geo,
@@ -79,7 +79,7 @@ async function collectOperationalState() {
79
79
  async function exportOperationalState(filePath = 'aiwaf-export.json') {
80
80
  const state = await collectOperationalState();
81
81
  return print({ ok: true, output: writeJsonFile(filePath, state), counts: {
82
- blacklist: state.blacklist.length,
82
+ blocklist: state.blocklist.length,
83
83
  ip_exemptions: state.ip_exemptions.length,
84
84
  path_exemptions: state.path_exemptions.length,
85
85
  geo: state.geo_blocked_countries.length,
@@ -90,12 +90,12 @@ async function exportOperationalState(filePath = 'aiwaf-export.json') {
90
90
  async function importOperationalState(filePath) {
91
91
  if (!filePath) throw new Error('Import file is required');
92
92
  const state = readJsonFile(filePath);
93
- const blacklistManager = require('../lib/blacklistManager');
93
+ const blocklistManager = require('../lib/blocklistManager');
94
94
  const exemptionStore = require('../lib/exemptionStore');
95
95
  const geoStore = require('../lib/geoStore');
96
96
  const dynamicKeywordStore = require('../lib/dynamicKeywordStore');
97
97
  const modelStore = require('../lib/modelStore');
98
- const imported = { blacklist: await blacklistManager.importRecords(state.blacklist || []) };
98
+ const imported = { blocklist: await blocklistManager.importRecords(state.blocklist ?? state.blacklist ?? []) };
99
99
  for (const row of state.ip_exemptions || []) await exemptionStore.addIp(row.ip_address || row.ip, row.reason);
100
100
  for (const row of state.path_exemptions || []) await exemptionStore.addPath(row.path_prefix || row.path, row.reason);
101
101
  for (const row of state.geo_blocked_countries || []) await geoStore.addBlockedCountry(row.country_code || row.country, row.reason);
@@ -109,20 +109,20 @@ async function importOperationalState(filePath) {
109
109
  }
110
110
 
111
111
  async function operationalStatus() {
112
- const blacklistManager = require('../lib/blacklistManager');
112
+ const blocklistManager = require('../lib/blocklistManager');
113
113
  const exemptionStore = require('../lib/exemptionStore');
114
114
  const geoStore = require('../lib/geoStore');
115
115
  const dynamicKeywordStore = require('../lib/dynamicKeywordStore');
116
116
  const requestLogStore = require('../lib/requestLogStore');
117
117
  const modelStore = require('../lib/modelStore');
118
- const [blacklist, ips, paths, geo, keywords, logs, model] = await Promise.all([
119
- blacklistManager.getStatistics(), exemptionStore.listIps(), exemptionStore.listPaths(),
118
+ const [blocklist, ips, paths, geo, keywords, logs, model] = await Promise.all([
119
+ blocklistManager.getStatistics(), exemptionStore.listIps(), exemptionStore.listPaths(),
120
120
  geoStore.listBlockedCountries(), dynamicKeywordStore.list(100000), requestLogStore.recent(1),
121
121
  modelStore.load(process.env)
122
122
  ]);
123
123
  return print({
124
124
  status: 'enabled',
125
- blacklist,
125
+ blocklist,
126
126
  exemptions: { ips: ips.length, paths: paths.length },
127
127
  geo_blocked_countries: geo.length,
128
128
  dynamic_keywords: keywords.length,
@@ -133,9 +133,9 @@ async function operationalStatus() {
133
133
  }
134
134
 
135
135
  async function operationalStats(limit = 5000) {
136
- const blacklistManager = require('../lib/blacklistManager');
136
+ const blocklistManager = require('../lib/blocklistManager');
137
137
  const requestLogStore = require('../lib/requestLogStore');
138
- const [blacklist, logs] = await Promise.all([blacklistManager.getStatistics(), requestLogStore.recent(limit)]);
138
+ const [blocklist, logs] = await Promise.all([blocklistManager.getStatistics(), requestLogStore.recent(limit)]);
139
139
  const statuses = {};
140
140
  const paths = {};
141
141
  let blocked = 0;
@@ -145,7 +145,7 @@ async function operationalStats(limit = 5000) {
145
145
  if (row.blocked === true || row.blocked === 1 || String(row.blocked).toLowerCase() === 'true') blocked += 1;
146
146
  });
147
147
  return print({
148
- blacklist,
148
+ blocklist,
149
149
  requests: { total: logs.length, blocked, statuses },
150
150
  top_paths: Object.entries(paths).sort((a, b) => b[1] - a[1]).slice(0, 10).map(([requestPath, count]) => ({ path: requestPath, count }))
151
151
  });
@@ -155,7 +155,9 @@ function parseManifestArgs(args = []) {
155
155
  const options = {
156
156
  framework: 'express',
157
157
  output: process.env.AIWAF_PATH_MANIFEST || '.aiwaf/paths.json',
158
- routes: []
158
+ routes: [],
159
+ source: '.',
160
+ audit: true
159
161
  };
160
162
  for (let index = 0; index < args.length; index += 1) {
161
163
  const arg = args[index];
@@ -163,6 +165,10 @@ function parseManifestArgs(args = []) {
163
165
  options.framework = args[++index] || options.framework;
164
166
  } else if (arg === '--output' || arg === '-o') {
165
167
  options.output = args[++index] || options.output;
168
+ } else if (arg === '--source') {
169
+ options.source = args[++index] || options.source;
170
+ } else if (arg === '--no-audit') {
171
+ options.audit = false;
166
172
  } else if (arg === '--routes' || arg === '-r') {
167
173
  const routeFile = args[++index];
168
174
  options.routes = routeFile ? readJsonFile(routeFile) : [];
@@ -172,10 +178,10 @@ function parseManifestArgs(args = []) {
172
178
  }
173
179
 
174
180
  async function diagnoseIp(ip) {
175
- const blacklistManager = require('../lib/blacklistManager');
181
+ const blocklistManager = require('../lib/blocklistManager');
176
182
  const exemptionStore = require('../lib/exemptionStore');
177
- const blocked = await blacklistManager.isBlocked(ip);
178
- const blockInfo = await blacklistManager.getBlockInfo(ip);
183
+ const blocked = await blocklistManager.isBlocked(ip);
184
+ const blockInfo = await blocklistManager.getBlockInfo(ip);
179
185
  const exempt = await exemptionStore.isIpExempt(ip);
180
186
  print({ ip, blocked, exempt, block_info: blockInfo });
181
187
  }
@@ -231,25 +237,25 @@ function runPathShell() {
231
237
  }
232
238
 
233
239
  async function runReset(args) {
234
- const flags = { blacklist: false, keywords: false, exemptions: false, confirm: false };
240
+ const flags = { blocklist: false, keywords: false, exemptions: false, confirm: false };
235
241
  for (const arg of args) {
236
- if (arg === '--blacklist') flags.blacklist = true;
242
+ if (arg === '--blocklist') flags.blocklist = true;
237
243
  if (arg === '--keywords') flags.keywords = true;
238
244
  if (arg === '--exemptions') flags.exemptions = true;
239
- if (arg === '--all') { flags.blacklist = true; flags.keywords = true; flags.exemptions = true; }
245
+ if (arg === '--all') { flags.blocklist = true; flags.keywords = true; flags.exemptions = true; }
240
246
  if (arg === '--confirm') flags.confirm = true;
241
247
  }
242
- if (!flags.blacklist && !flags.keywords && !flags.exemptions) {
243
- return print({ error: 'Specify what to reset: --blacklist, --keywords, --exemptions, or --all' });
248
+ if (!flags.blocklist && !flags.keywords && !flags.exemptions) {
249
+ return print({ error: 'Specify what to reset: --blocklist, --keywords, --exemptions, or --all' });
244
250
  }
245
251
  if (!flags.confirm) {
246
252
  return print({ error: 'Must pass --confirm to execute reset' });
247
253
  }
248
254
 
249
255
  const results = {};
250
- if (flags.blacklist) {
251
- const blacklistManager = require('../lib/blacklistManager');
252
- results.blacklist = await blacklistManager.clear();
256
+ if (flags.blocklist) {
257
+ const blocklistManager = require('../lib/blocklistManager');
258
+ results.blocklist = await blocklistManager.clear();
253
259
  }
254
260
  if (flags.keywords) {
255
261
  const dynamicKeywordStore = require('../lib/dynamicKeywordStore');
@@ -263,35 +269,39 @@ async function runReset(args) {
263
269
  }
264
270
 
265
271
  async function main() {
266
- const [, , cmd, subcmd, ...args] = process.argv;
272
+ const argv = [...process.argv];
273
+ for (const index of [2, 3]) {
274
+ argv[index] = { blacklist: 'blocklist', whitelist: 'allowlist' }[argv[index]] || argv[index];
275
+ }
276
+ const [, , cmd, subcmd, ...args] = argv;
267
277
 
268
278
  if (!cmd || ['help', '--help', '-h'].includes(cmd)) {
269
279
  print({
270
280
  usage: 'aiwaf <command> [subcommand] [args]',
271
281
  commands: [
272
- 'list blacklist|exemptions|geo|request-logs',
282
+ 'list blocklist|exemptions|geo|request-logs',
273
283
  'list dynamic-keywords',
274
284
  'list model-info',
275
285
  'list recent-blocks [hours]',
276
286
  'list top-reasons [limit]',
277
- 'add blacklist <ip> [reason] [--duration 24h|--permanent]',
287
+ 'add blocklist <ip> [reason] [--duration 24h|--permanent]',
278
288
  'add ip-exemption <ip> [reason]',
279
289
  'add path-exemption <pathPrefix> [reason]',
280
290
  'add dynamic-keyword <keyword> [count]',
281
- 'remove blacklist <ip>',
291
+ 'remove blocklist <ip>',
282
292
  'remove ip-exemption <ip>',
283
293
  'remove path-exemption <pathPrefix>',
284
294
  'remove dynamic-keyword <keyword>',
285
295
  'geo block <CC> [reason]',
286
296
  'geo unblock <CC>',
287
297
  'geo summary',
288
- 'clear blacklist|request-logs',
298
+ 'clear blocklist|request-logs',
289
299
  'clear dynamic-keywords',
290
300
  'status',
291
301
  'stats [request-limit]',
292
302
  'logs analyze [limit]',
293
- 'blacklist migrate [--duration 24h]',
294
- 'blacklist cleanup',
303
+ 'blocklist migrate [--duration 24h]',
304
+ 'blocklist cleanup',
295
305
  'export [output.json]',
296
306
  'import <input.json>',
297
307
  'model info|export|import|clear [file]',
@@ -299,13 +309,18 @@ async function main() {
299
309
  'manifest --framework <name> --routes <routes.json> [--output .aiwaf/paths.json]',
300
310
  'whois <domain|ip>',
301
311
  'diagnose <ip>',
302
- 'reset [--blacklist] [--keywords] [--exemptions] [--all] --confirm',
303
- 'pathshell'
312
+ 'reset [--blocklist] [--keywords] [--exemptions] [--all] --confirm',
313
+ 'pathshell',
314
+ 'audit [path] [--format text|json] [--output file] [--include-tests] [--fail-on-findings]'
304
315
  ]
305
316
  });
306
317
  return;
307
318
  }
308
319
 
320
+ if (cmd === 'audit') {
321
+ process.exitCode = require('../lib/injectionAnalysis').runAuditCommand([subcmd, ...args].filter(item => item !== undefined));
322
+ return;
323
+ }
309
324
  if (cmd === 'train') return runTrain();
310
325
  if (cmd === 'pathshell') return runPathShell();
311
326
  if (cmd === 'reset') return runReset([subcmd, ...args].filter(Boolean));
@@ -318,16 +333,16 @@ async function main() {
318
333
  return operationalStats(Number(args[0] || 5000));
319
334
  }
320
335
 
321
- if (cmd === 'blacklist' && subcmd === 'cleanup') {
322
- const blacklistManager = require('../lib/blacklistManager');
323
- return print({ ok: true, cleaned: await blacklistManager.cleanupExpired() });
336
+ if (cmd === 'blocklist' && subcmd === 'cleanup') {
337
+ const blocklistManager = require('../lib/blocklistManager');
338
+ return print({ ok: true, cleaned: await blocklistManager.cleanupExpired() });
324
339
  }
325
340
 
326
- if (cmd === 'blacklist' && subcmd === 'migrate') {
341
+ if (cmd === 'blocklist' && subcmd === 'migrate') {
327
342
  const durationIndex = args.indexOf('--duration');
328
343
  const duration = durationIndex >= 0 ? parseDuration(args[durationIndex + 1]) : null;
329
- const blacklistManager = require('../lib/blacklistManager');
330
- return print({ ok: true, ...(await blacklistManager.migrateLegacy({ duration })) });
344
+ const blocklistManager = require('../lib/blocklistManager');
345
+ return print({ ok: true, ...(await blocklistManager.migrateLegacy({ duration })) });
331
346
  }
332
347
 
333
348
  if (cmd === 'model') {
@@ -357,14 +372,22 @@ async function main() {
357
372
  options.framework,
358
373
  null,
359
374
  options.output,
360
- { routes: options.routes }
375
+ { routes: options.routes, auditSource: options.audit ? options.source : null }
361
376
  );
362
- return print({ ok: true, output: options.output, framework: options.framework, routes: Object.keys(manifest.routes || {}).length });
377
+ const audit = manifest.injection_audit;
378
+ return print({
379
+ ok: true,
380
+ output: options.output,
381
+ framework: options.framework,
382
+ routes: Object.keys(manifest.routes || {}).length,
383
+ injection_check_routes: Object.values(manifest.routes || {}).filter(entry => entry && entry.injection_risks).length,
384
+ unlinked_findings: audit ? audit.unlinked_findings : null
385
+ });
363
386
  }
364
387
 
365
- if (cmd === 'list' && subcmd === 'blacklist') {
366
- const blacklistManager = require('../lib/blacklistManager');
367
- const rows = await blacklistManager.getBlockedIPs();
388
+ if (cmd === 'list' && subcmd === 'blocklist') {
389
+ const blocklistManager = require('../lib/blocklistManager');
390
+ const rows = await blocklistManager.getBlockedIPs();
368
391
  return print(rows);
369
392
  }
370
393
 
@@ -403,13 +426,13 @@ async function main() {
403
426
  }
404
427
 
405
428
  if (cmd === 'list' && subcmd === 'recent-blocks') {
406
- const blacklistManager = require('../lib/blacklistManager');
407
- return print(await blacklistManager.getRecentBlocks(Number(args[0] || 24)));
429
+ const blocklistManager = require('../lib/blocklistManager');
430
+ return print(await blocklistManager.getRecentBlocks(Number(args[0] || 24)));
408
431
  }
409
432
 
410
433
  if (cmd === 'list' && subcmd === 'top-reasons') {
411
- const blacklistManager = require('../lib/blacklistManager');
412
- return print(await blacklistManager.getTopBlockedReasons(Number(args[0] || 10)));
434
+ const blocklistManager = require('../lib/blocklistManager');
435
+ return print(await blocklistManager.getTopBlockedReasons(Number(args[0] || 10)));
413
436
  }
414
437
 
415
438
  if (cmd === 'add' && subcmd === 'ip-exemption') {
@@ -419,15 +442,15 @@ async function main() {
419
442
  return print({ ok: true, ip });
420
443
  }
421
444
 
422
- if (cmd === 'add' && subcmd === 'blacklist') {
423
- const blacklistManager = require('../lib/blacklistManager');
445
+ if (cmd === 'add' && subcmd === 'blocklist') {
446
+ const blocklistManager = require('../lib/blocklistManager');
424
447
  const [ip, ...values] = args;
425
448
  const durationIndex = values.indexOf('--duration');
426
449
  const permanent = values.includes('--permanent');
427
450
  const duration = durationIndex >= 0 ? parseDuration(values[durationIndex + 1]) : undefined;
428
451
  const reasonParts = values.filter((value, index) => value !== '--permanent'
429
452
  && value !== '--duration' && index !== durationIndex + 1);
430
- await blacklistManager.block(ip, reasonParts.join(' ') || 'manual', { duration, permanent });
453
+ await blocklistManager.block(ip, reasonParts.join(' ') || 'manual', { duration, permanent });
431
454
  return print({ ok: true, ip });
432
455
  }
433
456
 
@@ -445,10 +468,10 @@ async function main() {
445
468
  return print({ ok: true, keyword });
446
469
  }
447
470
 
448
- if (cmd === 'remove' && subcmd === 'blacklist') {
449
- const blacklistManager = require('../lib/blacklistManager');
471
+ if (cmd === 'remove' && subcmd === 'blocklist') {
472
+ const blocklistManager = require('../lib/blocklistManager');
450
473
  const [ip] = args;
451
- const deleted = await blacklistManager.unblock(ip);
474
+ const deleted = await blocklistManager.unblock(ip);
452
475
  return print({ ok: true, deleted });
453
476
  }
454
477
 
@@ -493,8 +516,8 @@ async function main() {
493
516
  return print(rows);
494
517
  }
495
518
 
496
- if (cmd === 'clear' && subcmd === 'blacklist') {
497
- return clearBlacklist();
519
+ if (cmd === 'clear' && subcmd === 'blocklist') {
520
+ return clearBlocklist();
498
521
  }
499
522
 
500
523
  if (cmd === 'clear' && subcmd === 'request-logs') {