This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
index.js
79 lines (72 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
const path = require('path');
const async = require('async');
const util = require('../../util');
module.exports = function FileContent(options) {
options = util.defaultValue(options, {});
options = util.permittedArgs(options, ['patterns']);
options.patterns = util.defaultValue(options.patterns, () => { return path.join(__dirname, './data'); });
const self = {};
self.key = 'contents';
self.name = 'File Contents';
self.description = 'Scans files for dangerous content';
self.enabled = true;
let fileManager;
const makeContentMatcher = pattern => {
return item => {
const rx = pattern.exec(item);
const result = (rx !== null);
let line = 0;
if(result === true) {
line = item.split(rx[0])[0].split('\n').length;
}
return {
isMatch: result,
line: line
};
};
};
self.handles = function(manager) {
util.enforceType(manager, Object);
fileManager = manager;
return true;
};
self.run = function(results, done) {
const patterns = require(options.patterns);
(function buildPatternChecks() {
patterns.forEach(pattern => {
pattern.contentMatcher = makeContentMatcher(pattern.content);
pattern.check = (file, content) => {
const result = pattern.contentMatcher(content);
if(result.isMatch === true) {
const message = pattern.caption;
const mitigation = 'Check line number: ' + result.line;
const item = {
code: pattern.code,
offender: file,
description: message,
mitigation: mitigation,
data: pattern
};
results[pattern.level](item);
}
};
});
})();
(function executeChecksAgainstFiles() {
const checkPatternAgainstFiles = (pattern, nextPattern) => {
const checkPatternAgainstLanguageFile = (file, nextFile) => {
const validateFileContents = (err, contents) => {
if(err) { return nextFile(); }
pattern.check(file, contents);
async.setImmediate(nextFile);
};
fileManager.readFile(file, validateFileContents);
};
async.eachSeries(fileManager.languageFiles, checkPatternAgainstLanguageFile, nextPattern);
};
async.eachSeries(patterns, checkPatternAgainstFiles, done);
})();
};
return Object.freeze(self);
};