@contrast/contrast 1.0.4 → 1.0.5
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/.prettierignore +2 -0
- package/dist/audit/autodetection/autoDetectLanguage.js +32 -0
- package/dist/audit/catalogueApplication/catalogueApplication.js +2 -11
- package/dist/audit/languageAnalysisEngine/languageAnalysisFactory.js +4 -2
- package/dist/audit/languageAnalysisEngine/reduceIdentifiedLanguages.js +25 -0
- package/dist/audit/languageAnalysisEngine/report/commonReportingFunctions.js +3 -17
- package/dist/audit/languageAnalysisEngine/report/reportingFeature.js +1 -1
- package/dist/audit/languageAnalysisEngine/sendSnapshot.js +2 -16
- package/dist/commands/audit/auditConfig.js +8 -2
- package/dist/commands/audit/auditController.js +8 -2
- package/dist/commands/scan/processScan.js +6 -3
- package/dist/commands/scan/sca/scaAnalysis.js +44 -0
- package/dist/common/HTTPClient.js +0 -1
- package/dist/common/errorHandling.js +7 -17
- package/dist/constants/constants.js +14 -2
- package/dist/constants/locales.js +28 -35
- package/dist/constants.js +20 -0
- package/dist/scaAnalysis/common/formatMessage.js +11 -0
- package/dist/scaAnalysis/common/treeUpload.js +30 -0
- package/dist/scaAnalysis/java/analysis.js +116 -0
- package/dist/scaAnalysis/java/index.js +18 -0
- package/dist/scaAnalysis/java/javaBuildDepsParser.js +326 -0
- package/dist/scan/autoDetection.js +46 -1
- package/dist/scan/fileUtils.js +73 -1
- package/dist/scan/formatScanOutput.js +212 -0
- package/dist/scan/help.js +3 -1
- package/dist/scan/models/groupedResultsModel.js +2 -1
- package/dist/scan/scan.js +1 -96
- package/dist/scan/scanController.js +1 -2
- package/dist/scan/scanResults.js +3 -17
- package/package.json +2 -1
- package/src/audit/autodetection/autoDetectLanguage.ts +40 -0
- package/src/audit/catalogueApplication/catalogueApplication.js +4 -16
- package/src/audit/languageAnalysisEngine/languageAnalysisFactory.js +9 -5
- package/src/audit/languageAnalysisEngine/reduceIdentifiedLanguages.js +71 -0
- package/src/audit/languageAnalysisEngine/report/commonReportingFunctions.ts +3 -25
- package/src/audit/languageAnalysisEngine/report/reportingFeature.ts +1 -1
- package/src/audit/languageAnalysisEngine/sendSnapshot.js +2 -24
- package/src/commands/audit/auditConfig.ts +12 -3
- package/src/commands/audit/auditController.ts +9 -2
- package/src/commands/audit/processAudit.ts +3 -0
- package/src/commands/scan/processScan.js +8 -3
- package/src/commands/scan/sca/scaAnalysis.js +73 -0
- package/src/common/HTTPClient.js +1 -1
- package/src/common/errorHandling.ts +7 -24
- package/src/constants/constants.js +14 -2
- package/src/constants/locales.js +30 -49
- package/src/constants.js +22 -0
- package/src/scaAnalysis/common/formatMessage.js +10 -0
- package/src/scaAnalysis/common/treeUpload.js +34 -0
- package/src/scaAnalysis/java/analysis.js +159 -0
- package/src/scaAnalysis/java/index.js +21 -0
- package/src/scaAnalysis/java/javaBuildDepsParser.js +391 -0
- package/src/scan/autoDetection.js +54 -1
- package/src/scan/fileUtils.js +91 -1
- package/src/scan/formatScanOutput.ts +241 -0
- package/src/scan/help.js +3 -1
- package/src/scan/models/groupedResultsModel.ts +7 -5
- package/src/scan/models/resultContentModel.ts +2 -2
- package/src/scan/scan.ts +0 -130
- package/src/scan/scanController.js +1 -2
- package/src/scan/scanResults.js +9 -17
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const { getHttpClient } = require('../../utils/commonApi');
|
|
3
|
+
const { handleResponseErrors } = require('../../common/errorHandling');
|
|
4
|
+
const { APP_VERSION } = require('../../constants/constants');
|
|
5
|
+
const commonSendSnapShot = async (analysis, config) => {
|
|
6
|
+
const requestBody = {
|
|
7
|
+
appID: config.applicationId,
|
|
8
|
+
cliVersion: APP_VERSION,
|
|
9
|
+
snapshot: analysis
|
|
10
|
+
};
|
|
11
|
+
const client = getHttpClient(config);
|
|
12
|
+
return client
|
|
13
|
+
.sendSnapshot(requestBody, config)
|
|
14
|
+
.then(res => {
|
|
15
|
+
if (res.statusCode === 201) {
|
|
16
|
+
console.log('snapshot processed successfully');
|
|
17
|
+
return res.body;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
console.log(res.statusCode);
|
|
21
|
+
handleResponseErrors(res, 'snapshot');
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
.catch(err => {
|
|
25
|
+
console.log(err);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
module.exports = {
|
|
29
|
+
commonSendSnapShot
|
|
30
|
+
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const child_process = require('child_process');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const i18n = require('i18n');
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const MAVEN = 'maven';
|
|
7
|
+
const GRADLE = 'gradle';
|
|
8
|
+
const determineProjectTypeAndCwd = (files, projectPath) => {
|
|
9
|
+
const projectData = {};
|
|
10
|
+
if (files[0].includes('pom.xml')) {
|
|
11
|
+
projectData.projectType = MAVEN;
|
|
12
|
+
projectData.cwd = projectPath
|
|
13
|
+
? projectPath
|
|
14
|
+
: files[0].replace('pom.xml', '');
|
|
15
|
+
}
|
|
16
|
+
else if (files[0].includes('build.gradle')) {
|
|
17
|
+
projectData.projectType = GRADLE;
|
|
18
|
+
projectData.cwd = projectPath
|
|
19
|
+
? projectPath
|
|
20
|
+
: files[0].replace('pom.xml', '');
|
|
21
|
+
}
|
|
22
|
+
return projectData;
|
|
23
|
+
};
|
|
24
|
+
const buildMaven = async (config, projectData, timeout) => {
|
|
25
|
+
let cmdStdout;
|
|
26
|
+
let mvn_settings = '';
|
|
27
|
+
try {
|
|
28
|
+
if (config.mavenSettingsPath) {
|
|
29
|
+
mvn_settings = ' -s ' + config.mavenSettingsPath;
|
|
30
|
+
}
|
|
31
|
+
cmdStdout = child_process.execSync('mvn dependency:tree -B' + mvn_settings, {
|
|
32
|
+
cwd: projectData.cwd,
|
|
33
|
+
timeout
|
|
34
|
+
});
|
|
35
|
+
return cmdStdout.toString();
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
try {
|
|
39
|
+
child_process.execSync('mvn --version', {
|
|
40
|
+
cwd: projectData.cwd,
|
|
41
|
+
timeout
|
|
42
|
+
});
|
|
43
|
+
throw new Error(i18n.__('mavenDependencyTreeNonZero', projectData.cwd, `${err.message}`));
|
|
44
|
+
}
|
|
45
|
+
catch (mvnErr) {
|
|
46
|
+
throw new Error(i18n.__('mavenNotInstalledError', projectData.cwd, `${mvnErr.message}`));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const buildGradle = (config, projectData, timeout) => {
|
|
51
|
+
let cmdStdout;
|
|
52
|
+
let output = {};
|
|
53
|
+
try {
|
|
54
|
+
if (config.subProject) {
|
|
55
|
+
cmdStdout = child_process.execSync('.' +
|
|
56
|
+
path.sep +
|
|
57
|
+
'gradlew :' +
|
|
58
|
+
config.subProject +
|
|
59
|
+
':dependencies --configuration runtimeClasspath', {
|
|
60
|
+
cwd: projectData.cwd,
|
|
61
|
+
timeout
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
cmdStdout = child_process.execSync('.' +
|
|
66
|
+
path.sep +
|
|
67
|
+
'gradlew dependencies --configuration runtimeClasspath', {
|
|
68
|
+
cwd: projectData.cwd,
|
|
69
|
+
timeout
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (cmdStdout
|
|
73
|
+
.toString()
|
|
74
|
+
.includes("runtimeClasspath - Runtime classpath of source set 'main'.\n" +
|
|
75
|
+
'No dependencies')) {
|
|
76
|
+
cmdStdout = child_process.execSync('.' + path.sep + 'gradlew dependencies', {
|
|
77
|
+
cwd: projectData.cwd,
|
|
78
|
+
timeout
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
output.mvnDependancyTreeOutput = cmdStdout.toString();
|
|
82
|
+
return output;
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
if (fs.existsSync(projectData.cwd + 'gradlew') ||
|
|
86
|
+
fs.existsSync(projectData.cwd + 'gradlew.bat')) {
|
|
87
|
+
throw new Error(i18n.__('gradleDependencyTreeNonZero', projectData.cwd, `${err.message}`));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error(i18n.__('gradleWrapperUnavailable', projectData.cwd, `${err.message}`));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const getJavaBuildDeps = async (config, files) => {
|
|
95
|
+
const timeout = 960000;
|
|
96
|
+
let output = {
|
|
97
|
+
mvnDependancyTreeOutput: undefined,
|
|
98
|
+
projectType: undefined
|
|
99
|
+
};
|
|
100
|
+
try {
|
|
101
|
+
const projectData = determineProjectTypeAndCwd(files, config.projectPath);
|
|
102
|
+
if (projectData.projectType === MAVEN) {
|
|
103
|
+
output.mvnDependancyTreeOutput = await buildMaven(config, projectData, timeout);
|
|
104
|
+
}
|
|
105
|
+
else if (projectData.projectType === GRADLE) {
|
|
106
|
+
output.mvnDependancyTreeOutput = buildGradle(config, projectData, timeout);
|
|
107
|
+
}
|
|
108
|
+
output.projectType = projectData.projectType;
|
|
109
|
+
return output;
|
|
110
|
+
}
|
|
111
|
+
catch (err) {
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
module.exports = {
|
|
115
|
+
getJavaBuildDeps
|
|
116
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const { getJavaBuildDeps } = require('./analysis');
|
|
3
|
+
const { parseBuildDeps } = require('./javaBuildDepsParser');
|
|
4
|
+
const { createJavaTSMessage } = require('../common/formatMessage');
|
|
5
|
+
const javaAnalysis = async (config, languageFiles) => {
|
|
6
|
+
languageFiles.java.forEach(file => {
|
|
7
|
+
file.replace('build.gradle.kts', 'build.gradle');
|
|
8
|
+
});
|
|
9
|
+
const javaDeps = await buildJavaTree(config, languageFiles.java);
|
|
10
|
+
return createJavaTSMessage(javaDeps);
|
|
11
|
+
};
|
|
12
|
+
const buildJavaTree = async (config, files) => {
|
|
13
|
+
const javaBuildDeps = await getJavaBuildDeps(config, files);
|
|
14
|
+
return parseBuildDeps(config, javaBuildDeps);
|
|
15
|
+
};
|
|
16
|
+
module.exports = {
|
|
17
|
+
javaAnalysis
|
|
18
|
+
};
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const i18n = require('i18n');
|
|
3
|
+
const StringBuilder = require('string-builder');
|
|
4
|
+
let sb = new StringBuilder();
|
|
5
|
+
const parseBuildDeps = (config, input) => {
|
|
6
|
+
const { mvnDependancyTreeOutput, projectType } = input;
|
|
7
|
+
try {
|
|
8
|
+
return parseGradle(mvnDependancyTreeOutput, config, projectType);
|
|
9
|
+
}
|
|
10
|
+
catch (err) {
|
|
11
|
+
throw new Error(i18n.__('javaParseProjectFile') + `${err.message}`);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const preParser = shavedOutput => {
|
|
15
|
+
let obj = [];
|
|
16
|
+
for (let dep in shavedOutput) {
|
|
17
|
+
obj.push(shavedOutput[dep]
|
|
18
|
+
.replace('+-', '+---')
|
|
19
|
+
.replace('[INFO]', '')
|
|
20
|
+
.replace('\\-', '\\---')
|
|
21
|
+
.replace(':jar:', ':')
|
|
22
|
+
.replace(':test', '')
|
|
23
|
+
.replace(':compile', '')
|
|
24
|
+
.replace(' +', '+')
|
|
25
|
+
.replace(' |', '|')
|
|
26
|
+
.replace(' \\', '\\')
|
|
27
|
+
.replace(':runtime', ''));
|
|
28
|
+
}
|
|
29
|
+
let depTree = [];
|
|
30
|
+
for (let x in obj) {
|
|
31
|
+
let nodeLevel = computeRelationToLastElement(obj[x]);
|
|
32
|
+
let notLastLevel = obj[x].startsWith('|') ||
|
|
33
|
+
obj[x].startsWith('+') ||
|
|
34
|
+
obj[x].startsWith('\\');
|
|
35
|
+
if (notLastLevel) {
|
|
36
|
+
if (nodeLevel === 0) {
|
|
37
|
+
depTree.push(obj[x]);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
let level = computeLevel(nodeLevel);
|
|
41
|
+
let validatedLevel = addIndentation(nodeLevel === 2 ? 5 : level, obj[x]);
|
|
42
|
+
depTree.push(validatedLevel);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
let level = computeLevel(nodeLevel);
|
|
47
|
+
let validatedLevel = addIndentation(nodeLevel === 3 ? 5 : level, obj[x]);
|
|
48
|
+
depTree.push(validatedLevel);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return depTree;
|
|
52
|
+
};
|
|
53
|
+
const shaveOutput = (gradleDependencyTreeOutput, projectType) => {
|
|
54
|
+
let shavedOutput = gradleDependencyTreeOutput.split('\n');
|
|
55
|
+
if (projectType === 'maven') {
|
|
56
|
+
shavedOutput = preParser(shavedOutput);
|
|
57
|
+
}
|
|
58
|
+
let obj = [];
|
|
59
|
+
for (let key in shavedOutput) {
|
|
60
|
+
if (shavedOutput[key].includes('project :')) {
|
|
61
|
+
}
|
|
62
|
+
else if (shavedOutput[key].includes('+---') ||
|
|
63
|
+
shavedOutput[key].includes('\\---')) {
|
|
64
|
+
obj.push(shavedOutput[key]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return obj;
|
|
68
|
+
};
|
|
69
|
+
const computeIndentation = element => {
|
|
70
|
+
let hasPlus = element.includes('+');
|
|
71
|
+
let hasSlash = element.includes('\\');
|
|
72
|
+
if (hasPlus) {
|
|
73
|
+
return element.substring(element.indexOf('+'));
|
|
74
|
+
}
|
|
75
|
+
if (hasSlash) {
|
|
76
|
+
return element.substring(element.indexOf('\\'));
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
const computeLevel = nodeLevel => {
|
|
80
|
+
let num = [5, 8, 11, 14, 17, 20];
|
|
81
|
+
for (let z in num) {
|
|
82
|
+
if (num[z] === nodeLevel) {
|
|
83
|
+
let n = parseInt(z);
|
|
84
|
+
return 5 * (n + 2);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const addIndentation = (number, str) => {
|
|
89
|
+
str = computeIndentation(str);
|
|
90
|
+
sb.clear();
|
|
91
|
+
for (let j = 0; j < number; j++) {
|
|
92
|
+
sb.append(' ');
|
|
93
|
+
}
|
|
94
|
+
sb.append(str);
|
|
95
|
+
return sb.toString();
|
|
96
|
+
};
|
|
97
|
+
const computeRelationToLastElement = element => {
|
|
98
|
+
let hasPlus = element.includes('+---');
|
|
99
|
+
let hasSlash = element.includes('\\---');
|
|
100
|
+
if (hasPlus) {
|
|
101
|
+
return element.split('+---')[0].length;
|
|
102
|
+
}
|
|
103
|
+
if (hasSlash) {
|
|
104
|
+
return element.split('\\---')[0].length;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const stripElement = element => {
|
|
108
|
+
return element
|
|
109
|
+
.replace(/[|]/g, '')
|
|
110
|
+
.replace('+---', '')
|
|
111
|
+
.replace('\\---', '')
|
|
112
|
+
.replace(/[' ']/g, '')
|
|
113
|
+
.replace('(c)', '')
|
|
114
|
+
.replace('->', '@')
|
|
115
|
+
.replace('(*)', '');
|
|
116
|
+
};
|
|
117
|
+
const checkVersion = element => {
|
|
118
|
+
let version = element.split(':');
|
|
119
|
+
return version[version.length - 1];
|
|
120
|
+
};
|
|
121
|
+
const createElement = (element, isRoot) => {
|
|
122
|
+
let tree;
|
|
123
|
+
let cleanElement = stripElement(element);
|
|
124
|
+
let splitGroupName = cleanElement.split(':');
|
|
125
|
+
let validateVersion = false;
|
|
126
|
+
if (!element.includes('->')) {
|
|
127
|
+
validateVersion = true;
|
|
128
|
+
}
|
|
129
|
+
tree = {
|
|
130
|
+
artifactID: splitGroupName[1],
|
|
131
|
+
group: splitGroupName[0],
|
|
132
|
+
version: validateVersion
|
|
133
|
+
? checkVersion(cleanElement)
|
|
134
|
+
: splitGroupName[splitGroupName.length - 1],
|
|
135
|
+
scope: 'compile',
|
|
136
|
+
type: isRoot ? 'direct' : 'transitive',
|
|
137
|
+
edges: {}
|
|
138
|
+
};
|
|
139
|
+
return tree;
|
|
140
|
+
};
|
|
141
|
+
const getElementHeader = element => {
|
|
142
|
+
let elementHeader = stripElement(element);
|
|
143
|
+
elementHeader = elementHeader.replace(':', '/');
|
|
144
|
+
elementHeader = elementHeader.replace(':', '@');
|
|
145
|
+
return elementHeader;
|
|
146
|
+
};
|
|
147
|
+
const buildElement = (element, rootElement, parentOfCurrent, tree, isRoot) => {
|
|
148
|
+
let childElement = createElement(element, isRoot);
|
|
149
|
+
let elementHeader = getElementHeader(element);
|
|
150
|
+
let levelsArray = [rootElement, parentOfCurrent];
|
|
151
|
+
const treeNode = getNestedObject(tree, levelsArray);
|
|
152
|
+
const rootNode = getNestedObject(tree, [rootElement]);
|
|
153
|
+
if (!rootNode.hasOwnProperty(elementHeader)) {
|
|
154
|
+
tree[rootElement][elementHeader] = childElement;
|
|
155
|
+
}
|
|
156
|
+
treeNode.edges[elementHeader] = elementHeader;
|
|
157
|
+
};
|
|
158
|
+
const hasChildren = (nextNodeLevel, nodeLevel) => {
|
|
159
|
+
if (nextNodeLevel > nodeLevel) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
const lastChild = (nextNodeLevel, nodeLevel) => {
|
|
164
|
+
if (nextNodeLevel < nodeLevel) {
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const calculateLevels = (nextNodeLevel, nodeLevel) => {
|
|
169
|
+
return (nodeLevel - nextNodeLevel) / 5;
|
|
170
|
+
};
|
|
171
|
+
const buildTree = shavedOutput => {
|
|
172
|
+
let tree = {};
|
|
173
|
+
let rootElement;
|
|
174
|
+
let levelNodes = [];
|
|
175
|
+
shavedOutput.forEach((element, index) => {
|
|
176
|
+
if (index === 0) {
|
|
177
|
+
let cleanElement = stripElement(element);
|
|
178
|
+
let elementHeader = getElementHeader(cleanElement);
|
|
179
|
+
let splitElement = element.split(' ');
|
|
180
|
+
let splitGroupName = splitElement[1].split(':');
|
|
181
|
+
let validateVersion = false;
|
|
182
|
+
if (!element.includes('->')) {
|
|
183
|
+
validateVersion = true;
|
|
184
|
+
}
|
|
185
|
+
tree[splitGroupName[0]] = {};
|
|
186
|
+
tree[splitGroupName[0]][elementHeader] = {
|
|
187
|
+
artifactID: splitGroupName[1],
|
|
188
|
+
group: splitGroupName[0],
|
|
189
|
+
version: validateVersion
|
|
190
|
+
? checkVersion(cleanElement)
|
|
191
|
+
: splitElement[splitElement.length - 1],
|
|
192
|
+
scope: 'compile',
|
|
193
|
+
type: 'direct',
|
|
194
|
+
edges: {}
|
|
195
|
+
};
|
|
196
|
+
rootElement = splitGroupName[0];
|
|
197
|
+
levelNodes.push(elementHeader);
|
|
198
|
+
}
|
|
199
|
+
if (shavedOutput.length - 1 === index) {
|
|
200
|
+
const parentOfCurrent = levelNodes[levelNodes.length - 1];
|
|
201
|
+
let nodeLevel = computeRelationToLastElement(element);
|
|
202
|
+
let validateVersion = false;
|
|
203
|
+
if (!element.includes('->')) {
|
|
204
|
+
validateVersion = true;
|
|
205
|
+
}
|
|
206
|
+
if (nodeLevel === 0) {
|
|
207
|
+
let cleanElement = stripElement(element);
|
|
208
|
+
let elementHeader = getElementHeader(cleanElement);
|
|
209
|
+
let splitElement = element.split(' ');
|
|
210
|
+
let splitGroupName = splitElement[1].split(':');
|
|
211
|
+
tree[rootElement][elementHeader] = {
|
|
212
|
+
artifactID: splitGroupName[1],
|
|
213
|
+
group: splitGroupName[0],
|
|
214
|
+
version: validateVersion
|
|
215
|
+
? checkVersion(cleanElement)
|
|
216
|
+
: splitElement[splitElement.length - 1],
|
|
217
|
+
scope: 'compile',
|
|
218
|
+
type: 'direct',
|
|
219
|
+
edges: {}
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
buildElement(element, rootElement, parentOfCurrent, tree);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (index >= 1 && index < shavedOutput.length - 1) {
|
|
227
|
+
let nodeLevel = computeRelationToLastElement(element);
|
|
228
|
+
let nextNodeLevel = computeRelationToLastElement(shavedOutput[index + 1]);
|
|
229
|
+
const parentOfCurrent = levelNodes[levelNodes.length - 1];
|
|
230
|
+
let isRoot = false;
|
|
231
|
+
if (nodeLevel === 0) {
|
|
232
|
+
isRoot = true;
|
|
233
|
+
}
|
|
234
|
+
if (isRoot) {
|
|
235
|
+
let cleanElement = stripElement(element);
|
|
236
|
+
let elementHeader = getElementHeader(cleanElement);
|
|
237
|
+
let splitElement = element.split(' ');
|
|
238
|
+
let splitGroupName = splitElement[1].split(':');
|
|
239
|
+
let validateVersion = false;
|
|
240
|
+
if (!element.includes('->')) {
|
|
241
|
+
validateVersion = true;
|
|
242
|
+
}
|
|
243
|
+
tree[rootElement][elementHeader] = {
|
|
244
|
+
artifactID: splitGroupName[1],
|
|
245
|
+
group: splitGroupName[0],
|
|
246
|
+
version: validateVersion
|
|
247
|
+
? checkVersion(cleanElement)
|
|
248
|
+
: splitElement[splitElement.length - 1],
|
|
249
|
+
scope: 'compile',
|
|
250
|
+
type: 'direct',
|
|
251
|
+
edges: {}
|
|
252
|
+
};
|
|
253
|
+
levelNodes.push(elementHeader);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
let elementHeader = getElementHeader(element);
|
|
257
|
+
buildElement(element, rootElement, parentOfCurrent, tree, isRoot);
|
|
258
|
+
if (hasChildren(nextNodeLevel, nodeLevel)) {
|
|
259
|
+
buildElement(element, rootElement, parentOfCurrent, tree, isRoot);
|
|
260
|
+
levelNodes.push(elementHeader);
|
|
261
|
+
}
|
|
262
|
+
if (lastChild(nextNodeLevel, nodeLevel)) {
|
|
263
|
+
let levelDifference = calculateLevels(nextNodeLevel, nodeLevel);
|
|
264
|
+
if (levelDifference === 0) {
|
|
265
|
+
levelNodes.pop();
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
let i;
|
|
269
|
+
for (i = 0; i < levelDifference; i++) {
|
|
270
|
+
levelNodes.pop();
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
return tree;
|
|
277
|
+
};
|
|
278
|
+
const getNestedObject = (nestedObj, pathArr) => {
|
|
279
|
+
return pathArr.reduce((obj, key) => (obj && obj[key] !== 'undefined' ? obj[key] : undefined), nestedObj);
|
|
280
|
+
};
|
|
281
|
+
const parseSubProject = shavedOutput => {
|
|
282
|
+
let obj = [];
|
|
283
|
+
for (let key in shavedOutput) {
|
|
284
|
+
if (!shavedOutput[key].includes('project')) {
|
|
285
|
+
obj.push(shavedOutput[key]);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return obj;
|
|
289
|
+
};
|
|
290
|
+
const validateIndentation = shavedOutput => {
|
|
291
|
+
let validatedTree = [];
|
|
292
|
+
shavedOutput.forEach((element, index) => {
|
|
293
|
+
let nextNodeLevel;
|
|
294
|
+
let nodeLevel = computeRelationToLastElement(element);
|
|
295
|
+
if (shavedOutput[index + 1] !== undefined) {
|
|
296
|
+
nextNodeLevel = computeRelationToLastElement(shavedOutput[index + 1]);
|
|
297
|
+
}
|
|
298
|
+
if (index === 0) {
|
|
299
|
+
validatedTree.push(shavedOutput[index]);
|
|
300
|
+
validatedTree.push(shavedOutput[index + 1]);
|
|
301
|
+
}
|
|
302
|
+
else if (nextNodeLevel > nodeLevel + 5) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
validatedTree.push(shavedOutput[index + 1]);
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
validatedTree.pop();
|
|
310
|
+
return validatedTree;
|
|
311
|
+
};
|
|
312
|
+
const parseGradle = (gradleDependencyTreeOutput, config, projectType) => {
|
|
313
|
+
let shavedOutput = shaveOutput(gradleDependencyTreeOutput, projectType);
|
|
314
|
+
if (config.subProject) {
|
|
315
|
+
let subProject = parseSubProject(shavedOutput);
|
|
316
|
+
let validatedOutput = validateIndentation(subProject);
|
|
317
|
+
return buildTree(validatedOutput);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
let validatedOutput = validateIndentation(shavedOutput);
|
|
321
|
+
return buildTree(validatedOutput);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
module.exports = {
|
|
325
|
+
parseBuildDeps
|
|
326
|
+
};
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const i18n = require('i18n');
|
|
3
3
|
const fileFinder = require('./fileUtils');
|
|
4
|
+
const languageResolver = require('../audit/languageAnalysisEngine/reduceIdentifiedLanguages');
|
|
5
|
+
const rootFile = require('../audit/languageAnalysisEngine/getProjectRootFilenames');
|
|
4
6
|
const autoDetectFileAndLanguage = async (configToUse) => {
|
|
5
7
|
const entries = await fileFinder.findFile();
|
|
6
8
|
if (entries.length === 1) {
|
|
@@ -18,6 +20,31 @@ const autoDetectFileAndLanguage = async (configToUse) => {
|
|
|
18
20
|
errorOnFileDetection(entries);
|
|
19
21
|
}
|
|
20
22
|
};
|
|
23
|
+
const autoDetectAuditFilesAndLanguages = async () => {
|
|
24
|
+
let languagesFound = [];
|
|
25
|
+
console.log(i18n.__('searchingAuditFileDirectory', process.cwd()));
|
|
26
|
+
await fileFinder.findFilesJava(languagesFound);
|
|
27
|
+
await fileFinder.findFilesJavascript(languagesFound);
|
|
28
|
+
await fileFinder.findFilesPython(languagesFound);
|
|
29
|
+
await fileFinder.findFilesGo(languagesFound);
|
|
30
|
+
await fileFinder.findFilesPhp(languagesFound);
|
|
31
|
+
await fileFinder.findFilesRuby(languagesFound);
|
|
32
|
+
if (languagesFound.length === 1) {
|
|
33
|
+
return languagesFound;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
console.log('found multiple languages, please specify one using --file to run SCA analysis');
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const manualDetectAuditFilesAndLanguages = async (projectPath) => {
|
|
40
|
+
let projectRootFilenames = await rootFile.getProjectRootFilenames(projectPath);
|
|
41
|
+
let identifiedLanguages = languageResolver.deduceLanguageScaAnalysis(projectRootFilenames);
|
|
42
|
+
if (Object.keys(identifiedLanguages).length === 0) {
|
|
43
|
+
console.log(i18n.__('languageAnalysisNoLanguage', projectPath));
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
return [identifiedLanguages];
|
|
47
|
+
};
|
|
21
48
|
const hasWhiteSpace = s => {
|
|
22
49
|
const filename = s.split('/').pop();
|
|
23
50
|
return filename.indexOf(' ') >= 0;
|
|
@@ -38,7 +65,25 @@ const errorOnFileDetection = entries => {
|
|
|
38
65
|
}
|
|
39
66
|
process.exit(1);
|
|
40
67
|
};
|
|
68
|
+
const errorOnAuditFileDetection = entries => {
|
|
69
|
+
if (entries.length > 1) {
|
|
70
|
+
console.log(i18n.__('searchingDirectoryScan'));
|
|
71
|
+
for (let file in entries) {
|
|
72
|
+
console.log('-', entries[file]);
|
|
73
|
+
}
|
|
74
|
+
console.log('');
|
|
75
|
+
console.log(i18n.__('specifyFileAuditNotFound'));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.log(i18n.__('noFileFoundScan'));
|
|
79
|
+
console.log('');
|
|
80
|
+
console.log(i18n.__('specifyFileAuditNotFound'));
|
|
81
|
+
}
|
|
82
|
+
};
|
|
41
83
|
module.exports = {
|
|
42
84
|
autoDetectFileAndLanguage,
|
|
43
|
-
errorOnFileDetection
|
|
85
|
+
errorOnFileDetection,
|
|
86
|
+
autoDetectAuditFilesAndLanguages,
|
|
87
|
+
errorOnAuditFileDetection,
|
|
88
|
+
manualDetectAuditFilesAndLanguages
|
|
44
89
|
};
|
package/dist/scan/fileUtils.js
CHANGED
|
@@ -10,6 +10,72 @@ const findFile = async () => {
|
|
|
10
10
|
onlyFiles: true
|
|
11
11
|
});
|
|
12
12
|
};
|
|
13
|
+
const findFilesJava = async (languagesFound) => {
|
|
14
|
+
const result = await fg(['**/pom.xml', '**/build.gradle', '**/build.gradle.kts'], {
|
|
15
|
+
dot: false,
|
|
16
|
+
deep: 1,
|
|
17
|
+
onlyFiles: true
|
|
18
|
+
});
|
|
19
|
+
if (result.length > 0) {
|
|
20
|
+
return languagesFound.push({ java: result });
|
|
21
|
+
}
|
|
22
|
+
return languagesFound;
|
|
23
|
+
};
|
|
24
|
+
const findFilesJavascript = async (languagesFound) => {
|
|
25
|
+
const result = await fg(['**/package.json', '**/yarn.lock', '**/package.lock.json'], {
|
|
26
|
+
dot: false,
|
|
27
|
+
deep: 1,
|
|
28
|
+
onlyFiles: true
|
|
29
|
+
});
|
|
30
|
+
if (result.length > 0) {
|
|
31
|
+
return languagesFound.push({ javascript: result });
|
|
32
|
+
}
|
|
33
|
+
return languagesFound;
|
|
34
|
+
};
|
|
35
|
+
const findFilesPython = async (languagesFound) => {
|
|
36
|
+
const result = await fg(['**/Pipfile.lock', '**/Pipfile'], {
|
|
37
|
+
dot: false,
|
|
38
|
+
deep: 3,
|
|
39
|
+
onlyFiles: true
|
|
40
|
+
});
|
|
41
|
+
if (result.length > 0) {
|
|
42
|
+
return languagesFound.push({ python: result });
|
|
43
|
+
}
|
|
44
|
+
return languagesFound;
|
|
45
|
+
};
|
|
46
|
+
const findFilesGo = async (languagesFound) => {
|
|
47
|
+
const result = await fg(['**/go.mod'], {
|
|
48
|
+
dot: false,
|
|
49
|
+
deep: 3,
|
|
50
|
+
onlyFiles: true
|
|
51
|
+
});
|
|
52
|
+
if (result.length > 0) {
|
|
53
|
+
return languagesFound.push({ go: result });
|
|
54
|
+
}
|
|
55
|
+
return languagesFound;
|
|
56
|
+
};
|
|
57
|
+
const findFilesRuby = async (languagesFound) => {
|
|
58
|
+
const result = await fg(['**/Gemfile', '**/Gemfile.lock'], {
|
|
59
|
+
dot: false,
|
|
60
|
+
deep: 3,
|
|
61
|
+
onlyFiles: true
|
|
62
|
+
});
|
|
63
|
+
if (result.length > 0) {
|
|
64
|
+
return languagesFound.push({ ruby: result });
|
|
65
|
+
}
|
|
66
|
+
return languagesFound;
|
|
67
|
+
};
|
|
68
|
+
const findFilesPhp = async (languagesFound) => {
|
|
69
|
+
const result = await fg(['**/composer.json', '**/composer.lock'], {
|
|
70
|
+
dot: false,
|
|
71
|
+
deep: 3,
|
|
72
|
+
onlyFiles: true
|
|
73
|
+
});
|
|
74
|
+
if (result.length > 0) {
|
|
75
|
+
return languagesFound.push({ php: result });
|
|
76
|
+
}
|
|
77
|
+
return languagesFound;
|
|
78
|
+
};
|
|
13
79
|
const checkFilePermissions = file => {
|
|
14
80
|
let readableFile = false;
|
|
15
81
|
try {
|
|
@@ -27,5 +93,11 @@ const fileExists = path => {
|
|
|
27
93
|
module.exports = {
|
|
28
94
|
findFile,
|
|
29
95
|
fileExists,
|
|
30
|
-
checkFilePermissions
|
|
96
|
+
checkFilePermissions,
|
|
97
|
+
findFilesJava,
|
|
98
|
+
findFilesJavascript,
|
|
99
|
+
findFilesPython,
|
|
100
|
+
findFilesGo,
|
|
101
|
+
findFilesPhp,
|
|
102
|
+
findFilesRuby
|
|
31
103
|
};
|