<commit_before>/* eslint no-console: ["error", {"allow": ["log", "warn", "error"]}] */

/*
ISC License

Copyright (c) 2017, <NAME>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/

const argv = require('minimist')(process.argv.slice(2));

const {executeTests, formatResult} = require('./src/web_exploit_detector');

function printUsage() {
  logger(3, `Usage: node ${process.argv[1]} --webroot=<web_root>`);
  logger(3, '  --debug: sets the debug flag: enables the "debug" rules.');
  logger(3, '  --exclude-tags=<list>: excludes all rules that include one or more of the comma-separated tags in <list>.');
  logger(3, '  --help: shows this message');
  logger(3, '  --include-deprecated: includes rulesmarked as deprecated.');
  logger(3, '  --parallel: runs rules in parallel if set. This is technically faster, but has a higher resource usage when running.');
  logger(3, '  --tags=<list>: only runs rules matching the comma-separated <list> of tags.');
  logger(3, '  --verbose: enables verbose log output.');
  logger(3, '  --webroot=<web_root>: sets the base directory that will be searched recursively to <web_root>.');
  logger(3, '\n\nExamples: ');
  logger(3, '  - Search the /var/www/html directory structure: -');
  logger(3, `    node ${process.argv[1]} --webroot=/var/www/html`);
  logger(3, '  - Search using only WordPress-related rules: -');
  logger(3, `    node ${process.argv[1]} --webroot=/var/www/html --tags=wordpress`);
  logger(3, '  - Search using all rules except those tagged with "jquery" or "js": -');
  logger(3, `    node ${process.argv[1]} --webroot=/var/www/html --exclude-tags=jquery,js`);
}

function logger(level, msg) {
  switch (level) {
    case 1:
      console.error(msg);
      break;
    case 2:
      console.warn(msg);
      break;
    case 3:
      console.log(msg);
      break;
    default:
      if (argv.verbose)
        console.log(msg);
  }
}


logger(3, 'Web Exploit Detector by <NAME> ((c) 2017)\n');

if (argv.help)
{
  printUsage();
  process.exit(0);
}

if (!argv.webroot)
{
  logger(1, 'ERROR: --webroot command line argument must be specified\n');
  printUsage();
  process.exit(1);
}

executeTests({
  debug:              argv.debug,
  exclude_tags:       argv['exclude-tags'],
  include_deprecated: argv['include-deprecated'],
  logger,
  parallel:           argv.parallel,
  tags:               argv.tags,
  verbose:            argv.verbose,
  webroot:            argv.webroot,
})
  .then(res => 
    logger(3, 
      'Processing complete, results: -\n\n' +
      res
        .map(formatResult)
        .filter(x => x !== null)
        .join('\n')
    )
  );<commit_msg>Compatibility with older versions of Node.js<commit_after>/* eslint no-console: ["error", {"allow": ["log", "warn", "error"]}] */

/*
ISC License

Copyright (c) 2017, <NAME>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/

const argv = require('minimist')(process.argv.slice(2));

const wed = require('./src/web_exploit_detector');

function printUsage() {
  logger(3, `Usage: node ${process.argv[1]} --webroot=<web_root>`);
  logger(3, '  --debug: sets the debug flag: enables the "debug" rules.');
  logger(3, '  --exclude-tags=<list>: excludes all rules that include one or more of the comma-separated tags in <list>.');
  logger(3, '  --help: shows this message');
  logger(3, '  --include-deprecated: includes rulesmarked as deprecated.');
  logger(3, '  --parallel: runs rules in parallel if set. This is technically faster, but has a higher resource usage when running.');
  logger(3, '  --tags=<list>: only runs rules matching the comma-separated <list> of tags.');
  logger(3, '  --verbose: enables verbose log output.');
  logger(3, '  --webroot=<web_root>: sets the base directory that will be searched recursively to <web_root>.');
  logger(3, '\n\nExamples: ');
  logger(3, '  - Search the /var/www/html directory structure: -');
  logger(3, `    node ${process.argv[1]} --webroot=/var/www/html`);
  logger(3, '  - Search using only WordPress-related rules: -');
  logger(3, `    node ${process.argv[1]} --webroot=/var/www/html --tags=wordpress`);
  logger(3, '  - Search using all rules except those tagged with "jquery" or "js": -');
  logger(3, `    node ${process.argv[1]} --webroot=/var/www/html --exclude-tags=jquery,js`);
}

function logger(level, msg) {
  switch (level) {
    case 1:
      console.error(msg);
      break;
    case 2:
      console.warn(msg);
      break;
    case 3:
      console.log(msg);
      break;
    default:
      if (argv.verbose)
        console.log(msg);
  }
}


logger(3, 'Web Exploit Detector by <NAME> ((c) 2017)\n');

if (argv.help)
{
  printUsage();
  process.exit(0);
}

if (!argv.webroot)
{
  logger(1, 'ERROR: --webroot command line argument must be specified\n');
  printUsage();
  process.exit(1);
}

wed.executeTests({
  debug:              argv.debug,
  exclude_tags:       argv['exclude-tags'],
  include_deprecated: argv['include-deprecated'],
  logger,
  parallel:           argv.parallel,
  tags:               argv.tags,
  verbose:            argv.verbose,
  webroot:            argv.webroot,
})
  .then(res => 
    logger(3, 
      'Processing complete, results: -\n\n' +
      res
        .map(wed.formatResult)
        .filter(x => x !== null)
        .join('\n')
    )
  );
{'fraction_non_alphanumeric': 0.14003645200486026, 'fraction_numerical': 0.011239368165249088, 'mean_word_length': 3.2759740259740258, 'pattern_counts': {'":': 4, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2570664', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2018, 'n_words': 684}