-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathindex.js
More file actions
212 lines (186 loc) · 5.51 KB
/
Copy pathindex.js
File metadata and controls
212 lines (186 loc) · 5.51 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'use strict';
var path = require('path');
var yamlParser = require('js-yaml');
function isDirValid(dir) {
return path.normalize(dir).indexOf('..') !== 0;
}
var paramValidators = {
add: function(params) {
if (params.indexOf('http') === 0) {
// No need to normalize a url
return true;
}
return isDirValid(params.split(' ')[0]);
}
}
function finish(result) {
return result;
}
function getRules(rulefile) {
//TODO throw exceptions if invalid file!
try {
var doc = yamlParser.safeLoad(rulefile);
//console.log(doc);
return doc;
} catch (e) {
console.log(e);
return null;
}
}
function createReqInstructionHash(ruleObj) {
var hash = {};
var arr = ruleObj.required_instructions;
for (var i = 0, len = arr.length; i < len; i++) {
hash[arr[i].instruction] = arr[i];
arr[i].exists = false;
}
return hash;
}
function initLineRulesRegexes(ruleObj) {
var lineRules = ruleObj.line_rules;
if (!lineRules) {
return;
}
for (var rule in lineRules) {
if (lineRules.hasOwnProperty(rule)) {
lineRules[rule].paramSyntaxRegex = eval(lineRules[rule].paramSyntaxRegex);
for (var semanticRule in lineRules[rule].rules) {
lineRules[rule].rules[semanticRule].regex = eval(lineRules[rule].rules[semanticRule].regex);
}
}
}
}
function checkRequiredInstructions(instructions, result) {
for (var instruction in instructions) {
if (instructions.hasOwnProperty(instruction)) {
if (!instructions[instruction].exists) {
result[instructions[instruction].level].count++;
result[instructions[instruction].level].data.push(instructions[instruction]);
}
}
}
}
function checkLineRules(ruleObject, instruction, line, lineNumber, result) {
if (!ruleObject.line_rules[instruction]) {
console.log("No Line Rules for instruction :" + instruction);
return;
}
var rules = ruleObject.line_rules[instruction].rules;
for (var index in rules) {
if (rules.hasOwnProperty(index)) {
var rule = rules[index];
if (rule.regex && rule.regex.test(line) && !rule.inverse_rule) {
result[rule.level].count++;
var ruleCopy = JSON.parse(JSON.stringify(rule));
ruleCopy.line = lineNumber;
result[rule.level].data.push(ruleCopy);
} else if (rule.regex && !rule.regex.test(line) && rule.inverse_rule) {
result[rule.level].count++;
var ruleCopy = JSON.parse(JSON.stringify(rule));
ruleCopy.line = lineNumber;
result[rule.level].data.push(ruleCopy);
}
}
}
}
function validator(rulefile) {
/**
* Static rules /Regex can be reused
*/
var ruleObject = getRules(rulefile);
var validInstructionsRegex = eval(ruleObject.general.valid_instruction_regex);
var continuationRegex = eval(ruleObject.general.multiline_regex);
var ignoreRegex = eval(ruleObject.general.ignore_regex);
initLineRulesRegexes(ruleObject);
function getProfile() {
return ruleObject.profile;
};
function validate(dockerfile) {
if (typeof dockerfile !== 'string') {
//TODO clean this up
return finish([{
message: 'Invalid type'
}]);
}
dockerfile = dockerfile.trim();
var requiredInstructions = createReqInstructionHash(ruleObject);
var fromCheck = false;
var hasCmd = false;
var currentLine = 0;
var result = {
error: {
count: 0,
data: []
},
warn: {
count: 0,
data: []
},
info: {
count: 0,
data: []
},
summary: []
}
var linesArr = dockerfile.split(/\r?\n/);
function addError(lineNumber, msg) {
result.error.data.push({
message: msg,
line: lineNumber,
level: 'error'
});
result.error.count++;
};
function isPartialLine(line) {
return (continuationRegex.test(line));
};
function validateLine(line) {
currentLine++;
var lineOffSet = 0;
if (!line || line[0] === '#') {
return;
}
while (isPartialLine(line)) {
line = line.replace(continuationRegex, " ");
line = line + linesArr[currentLine + lineOffSet];
linesArr[currentLine + lineOffSet] = undefined;
lineOffSet++;
}
// First instruction must be FROM
if (!fromCheck) {
fromCheck = true;
if (line.toUpperCase().indexOf('FROM') !== 0) {
addError(currentLine, 'Missing or misplaced FROM');
}
}
var instruction = validInstructionsRegex.exec(line);
if (!instruction) {
addError(currentLine, 'Invalid instruction');
return false;
}
instruction = instruction[0].trim().toUpperCase();
if (instruction in requiredInstructions) {
requiredInstructions[instruction].exists = true;
}
checkLineRules(ruleObject, instruction, line, currentLine, result);
var params = line.replace(validInstructionsRegex, '');
if (ruleObject.line_rules[instruction] && ruleObject.line_rules[instruction].paramSyntaxRegex) {
var validParams = ruleObject.line_rules[instruction].paramSyntaxRegex.test(params);
//&& (paramValidators[instruction] ? paramValidators[instruction](params) : true);
if (!validParams) {
addError(currentLine, 'Bad Parameters');
return false;
}
}
return true;
}
linesArr.forEach(validateLine);
checkRequiredInstructions(requiredInstructions, result);
return finish(result);
};
return {
getProfile: getProfile,
validate: validate
}
}
module.exports = validator;