Skip to content

Commit

Permalink
chore: intellij's var2const
Browse files Browse the repository at this point in the history
  • Loading branch information
FauxFaux committed Jan 21, 2020
1 parent b143a76 commit 997351c
Show file tree
Hide file tree
Showing 36 changed files with 320 additions and 317 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -103,9 +103,9 @@ The first example rule (above) is how the policy is stored by default. However,
Installed via npm: `npm install -S snyk-policy`. Typically loaded and applied to vulnerabilities:

```js
var policy = require('snyk-policy');
const policy = require('snyk-policy');

var vulns = snyk.test('snyk-demo-app@1.0.0'); // assumes snyk is loaded
const vulns = snyk.test('snyk-demo-app@1.0.0'); // assumes snyk is loaded
policy.load(process.cwd()).then(rules => {
console.log(rules.filter(vulns));
});
Expand Down
14 changes: 7 additions & 7 deletions lib/add.js
@@ -1,9 +1,9 @@
module.exports = add;

var debug = require('debug')('snyk:policy');
var emailValidator = require('email-validator');
const debug = require('debug')('snyk:policy');
const emailValidator = require('email-validator');

var validReasonTypes = ['not-vulnerable', 'wont-fix', 'temporary-ignore'];
const validReasonTypes = ['not-vulnerable', 'wont-fix', 'temporary-ignore'];

function add(policy, type, options) {
if (type !== 'ignore' && type !== 'patch') {
Expand All @@ -14,9 +14,9 @@ function add(policy, type, options) {
throw new Error('policy.add: required option props { id, path }');
}

var id = options.id;
var path = options.path;
var data = Object.keys(options).reduce(function (acc, curr) {
const id = options.id;
const path = options.path;
const data = Object.keys(options).reduce(function (acc, curr) {
if (curr === 'id' || curr === 'path') {
return acc;
}
Expand Down Expand Up @@ -49,7 +49,7 @@ function add(policy, type, options) {
debug('policy.add: path already exists', policy[type][id][path]);
}

var rule = {};
const rule = {};
rule[path] = data;

policy[type][id].push(rule);
Expand Down
16 changes: 8 additions & 8 deletions lib/filter/get-vuln-source.js
Expand Up @@ -2,24 +2,24 @@

module.exports = getVulnSource;

var debug = require('debug')('snyk:policy');
var resolve = require('snyk-resolve');
var path = require('path');
var statSync = require('fs').statSync;
var { parsePackageString: moduleToObject } = require('snyk-module');
const debug = require('debug')('snyk:policy');
const resolve = require('snyk-resolve');
const path = require('path');
const statSync = require('fs').statSync;
let {parsePackageString: moduleToObject} = require('snyk-module');

function getVulnSource(vuln, cwd, live) {
var from = vuln.from.slice(1).map(function (pkg) {
const from = vuln.from.slice(1).map(function (pkg) {
return moduleToObject(pkg).name;
});

var viaPath = path.resolve(
const viaPath = path.resolve(
cwd || process.cwd(),
'node_modules',
from.join('/node_modules/')
);

var source = vuln.__filename ?
let source = vuln.__filename ?
path.dirname(vuln.__filename) :
viaPath;

Expand Down
20 changes: 10 additions & 10 deletions lib/filter/ignore.js
@@ -1,8 +1,8 @@
module.exports = filterIgnored;

var cloneDeep = require('lodash.clonedeep');
var debug = require('debug')('snyk:policy');
var matchToRule = require('../match').matchToRule;
const cloneDeep = require('lodash.clonedeep');
const debug = require('debug')('snyk:policy');
const matchToRule = require('../match').matchToRule;

// given an ignore ruleset (parsed from the .snyk yaml file) and a array of
// vulnerabilities, return the vulnerabilities that *are not* ignored
Expand All @@ -17,7 +17,7 @@ function filterIgnored(ignore, vuln, filtered) {
}

debug('filtering ignored');
var now = (new Date()).toJSON();
const now = (new Date()).toJSON();

return vuln.map(function (vuln) {
if (!ignore[vuln.id]) {
Expand All @@ -32,16 +32,16 @@ function filterIgnored(ignore, vuln, filtered) {
// keep it.

// if rules.find, then ignore vuln
var appliedRules = ignore[vuln.id].filter(function (rule) {
var path = Object.keys(rule)[0]; // this is a string
var expires = rule[path].expires;
const appliedRules = ignore[vuln.id].filter(function (rule) {
const path = Object.keys(rule)[0]; // this is a string
let expires = rule[path].expires;

if (expires && expires.toJSON) {
expires = expires.toJSON();
}

// first check if the path is a match on the rule
var pathMatch = matchToRule(vuln, rule);
const pathMatch = matchToRule(vuln, rule);

if (pathMatch && expires && expires < now) {
debug('%s vuln rule has expired (%s)', vuln.id, expires);
Expand Down Expand Up @@ -69,8 +69,8 @@ function filterIgnored(ignore, vuln, filtered) {
if (appliedRules.length) {
vuln.filtered = {
ignored: appliedRules.map(function (rule) {
var path = Object.keys(rule)[0];
var ruleData = cloneDeep(rule[path]);
const path = Object.keys(rule)[0];
const ruleData = cloneDeep(rule[path]);
ruleData.path = path.split(' > ');
return ruleData;
}),
Expand Down
14 changes: 7 additions & 7 deletions lib/filter/index.js
@@ -1,9 +1,9 @@
module.exports = filter;

var debug = require('debug')('snyk:policy');
var ignore = require('./ignore');
var patch = require('./patch');
var notes = require('./notes');
const debug = require('debug')('snyk:policy');
const ignore = require('./ignore');
const patch = require('./patch');
const notes = require('./notes');

// warning: mutates vulns
function filter(vulns, policy, root) {
Expand All @@ -15,7 +15,7 @@ function filter(vulns, policy, root) {
return vulns;
}

var filtered = {
const filtered = {
ignore: [],
patch: [],
};
Expand Down Expand Up @@ -57,12 +57,12 @@ function filter(vulns, policy, root) {
if (policy.failThreshold && vulns.ok === false) {
// check what's left and switch the failure flag if there's anything
// under our threshold
var levels = {
const levels = {
high: 3,
medium: 2,
low: 1,
};
var level = levels[policy.failThreshold];
const level = levels[policy.failThreshold];
vulns.ok = true;
vulns.vulnerabilities.some(function (vuln) {
if (levels[vuln.severity] >= level) {
Expand Down
14 changes: 7 additions & 7 deletions lib/filter/notes.js
@@ -1,14 +1,14 @@
module.exports = attachNotes;

var debug = require('debug')('snyk:policy');
var matchToRule = require('../match').matchToRule;
const debug = require('debug')('snyk:policy');
const matchToRule = require('../match').matchToRule;

function attachNotes(notes, vuln) {
if (!notes) {
return vuln;
}
debug('attaching notes');
var now = (new Date()).toJSON();
const now = (new Date()).toJSON();

return vuln.map(function (vuln) {
if (!notes[vuln.id]) {
Expand All @@ -19,11 +19,11 @@ function attachNotes(notes, vuln) {

// if rules.some, then add note to the vuln
notes[vuln.id].forEach(function (rule) {
var path = Object.keys(rule)[0]; // this is a string
var expires = rule[path].expires;
const path = Object.keys(rule)[0]; // this is a string
let expires = rule[path].expires;

// first check if the path is a match on the rule
var pathMatch = matchToRule(vuln, rule);
const pathMatch = matchToRule(vuln, rule);

if (expires && expires.toJSON) {
expires = expires.toJSON();
Expand All @@ -43,7 +43,7 @@ function attachNotes(notes, vuln) {

if (pathMatch) {
// strip any control characters in the 3rd party reason file
var reason = rule[path].reason.replace('/[\x00-\x1F\x7F-\x9F]/u', '');
const reason = rule[path].reason.replace('/[\x00-\x1F\x7F-\x9F]/u', '');
if (debug.enabled) {
debug('adding note based on path match: %s ~= %s', path,
vuln.from.slice(1).join(' > '));
Expand Down
37 changes: 19 additions & 18 deletions lib/filter/patch.js
@@ -1,11 +1,11 @@
module.exports = filterPatched;

var cloneDeep = require('lodash.clonedeep');
var debug = require('debug')('snyk:policy');
var matchToRule = require('../match').matchToRule;
var path = require('path');
var statSync = require('fs').statSync;
var getVulnSource = require('./get-vuln-source');
const cloneDeep = require('lodash.clonedeep');
const debug = require('debug')('snyk:policy');
const matchToRule = require('../match').matchToRule;
const path = require('path');
const statSync = require('fs').statSync;
const getVulnSource = require('./get-vuln-source');

// cwd is used for testing
function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {
Expand All @@ -32,13 +32,13 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {
// keep it.

// if rules.find, then ignore vuln
var vulnRules = patched[vuln.id].map(function (rule) {
const vulnRules = patched[vuln.id].map(function (rule) {

// first check if the path is a match on the rule
var pathMatch = matchToRule(vuln, rule);
const pathMatch = matchToRule(vuln, rule);

if (pathMatch) {
var path = Object.keys(rule)[0]; // this is a string
const path = Object.keys(rule)[0]; // this is a string
debug('(patch) ignoring based on path match: %s ~= %s', path,
vuln.from.slice(1).join(' > '));
return rule;
Expand All @@ -48,24 +48,25 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {
}).filter(Boolean);

// run through the potential rules to check if there's a patch flag in place
var appliedRules = vulnRules.filter(function () {
const appliedRules = vulnRules.filter(function () {
// the target directory where our module name will live
if (skipVerifyPatch) {
return true;
}

var source = getVulnSource(vuln, cwd, true);
const source = getVulnSource(vuln, cwd, true);

var id = vuln.id.replace(/:/g, '-');
var flag = path.resolve(source, '.snyk-' + id + '.flag');
var oldFlag = path.resolve(source, '.snyk-' + vuln.id + '.flag');
var res = false;
const id = vuln.id.replace(/:/g, '-');
const flag = path.resolve(source, '.snyk-' + id + '.flag');
const oldFlag = path.resolve(source, '.snyk-' + vuln.id + '.flag');
let res = false;
try {
res = statSync(flag);
} catch (e) {
try {
res = statSync(oldFlag);
} catch (e) {}
} catch (e) {
}
}

debug('flag found for %s? %s', vuln.id);
Expand All @@ -76,8 +77,8 @@ function filterPatched(patched, vulns, cwd, skipVerifyPatch, filteredPatches) {
if (appliedRules.length) {
vuln.filtered = {
patches: appliedRules.map(function (rule) {
var path = Object.keys(rule)[0];
var ruleData = cloneDeep(rule[path]) || {};
const path = Object.keys(rule)[0];
const ruleData = cloneDeep(rule[path]) || {};
ruleData.path = path.split(' > ');
return ruleData;
}),
Expand Down

0 comments on commit 997351c

Please sign in to comment.