scanoss 0.2.8 → 0.2.12

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.
Files changed (170) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/LICENSE +20 -18
  3. package/README.md +6 -5
  4. package/build/main/bin/cli-bin.js +10 -3
  5. package/build/main/commands/dep.d.ts +1 -0
  6. package/build/main/commands/dep.js +26 -0
  7. package/build/main/commands/scan.js +20 -38
  8. package/build/main/index.d.ts +3 -1
  9. package/build/main/index.js +4 -2
  10. package/build/main/lib/dependencies/Dependency.d.ts +9 -0
  11. package/build/main/lib/dependencies/Dependency.js +50 -0
  12. package/build/main/lib/dependencies/DependencyTypes.d.ts +19 -0
  13. package/build/main/lib/dependencies/DependencyTypes.js +3 -0
  14. package/build/main/lib/dependencies/PurlGenerator.d.ts +2 -0
  15. package/build/main/lib/dependencies/PurlGenerator.js +39 -0
  16. package/build/main/lib/dependencies/parsers/golangParser.d.ts +0 -0
  17. package/build/main/lib/dependencies/parsers/golangParser.js +3 -0
  18. package/build/main/lib/dependencies/parsers/mavenParser.d.ts +2 -0
  19. package/build/main/lib/dependencies/parsers/mavenParser.js +44 -0
  20. package/build/main/lib/dependencies/parsers/npmParser.d.ts +3 -0
  21. package/build/main/lib/dependencies/parsers/npmParser.js +45 -0
  22. package/build/main/lib/dependencies/parsers/pyParser.d.ts +2 -0
  23. package/build/main/lib/dependencies/parsers/pyParser.js +51 -0
  24. package/build/main/lib/dependencies/parsers/rubyParser.d.ts +3 -0
  25. package/build/main/lib/dependencies/parsers/rubyParser.js +135 -0
  26. package/build/main/lib/dependencies/parsers/types.d.ts +15 -0
  27. package/build/main/lib/dependencies/parsers/types.js +33 -0
  28. package/build/main/lib/dependencies/parsers/utils.d.ts +2 -0
  29. package/build/main/lib/dependencies/parsers/utils.js +20 -0
  30. package/build/main/lib/dependencies/src/PurlGenerator.d.ts +2 -0
  31. package/build/main/lib/dependencies/src/PurlGenerator.js +62 -0
  32. package/build/main/lib/dependencies/src/parsers/golangParser.d.ts +0 -0
  33. package/build/main/lib/dependencies/src/parsers/golangParser.js +3 -0
  34. package/build/main/lib/dependencies/src/parsers/mavenParser.d.ts +2 -0
  35. package/build/main/lib/dependencies/src/parsers/mavenParser.js +44 -0
  36. package/build/main/lib/dependencies/src/parsers/npmParser.d.ts +3 -0
  37. package/build/main/lib/dependencies/src/parsers/npmParser.js +45 -0
  38. package/build/main/lib/dependencies/src/parsers/pyParser.d.ts +2 -0
  39. package/build/main/lib/dependencies/src/parsers/pyParser.js +51 -0
  40. package/build/main/lib/dependencies/src/parsers/rubyParser.d.ts +3 -0
  41. package/build/main/lib/dependencies/src/parsers/rubyParser.js +135 -0
  42. package/build/main/lib/dependencies/src/parsers/types.d.ts +15 -0
  43. package/build/main/lib/dependencies/src/parsers/types.js +33 -0
  44. package/build/main/lib/dependencies/src/parsers/utils.d.ts +2 -0
  45. package/build/main/lib/dependencies/src/parsers/utils.js +19 -0
  46. package/build/main/lib/dependencyScan/dependencyScan.d.ts +2 -0
  47. package/build/main/lib/dependencyScan/dependencyScan.js +37 -0
  48. package/build/main/lib/dependencyScan/parser.d.ts +7 -0
  49. package/build/main/lib/dependencyScan/parser.js +41 -0
  50. package/build/main/lib/dependencyScan/utils.d.ts +1 -0
  51. package/build/main/lib/dependencyScan/utils.js +16 -0
  52. package/build/main/lib/scanner/Dependency/dependency.d.ts +9 -0
  53. package/build/main/lib/scanner/Dependency/dependency.js +46 -0
  54. package/build/main/lib/scanner/Dependency/dependencyScan.d.ts +9 -0
  55. package/build/main/lib/scanner/Dependency/dependencyScan.js +48 -0
  56. package/build/main/lib/scanner/Dependency/parser.d.ts +7 -0
  57. package/build/main/lib/scanner/Dependency/parser.js +41 -0
  58. package/build/main/lib/scanner/Dependency/utils.d.ts +1 -0
  59. package/build/main/lib/scanner/Dependency/utils.js +16 -0
  60. package/build/main/lib/scanner/Dispatcher/DispatchableItem.js +1 -1
  61. package/build/main/lib/scanner/Dispatcher/Dispatcher.d.ts +3 -2
  62. package/build/main/lib/scanner/Dispatcher/Dispatcher.js +15 -13
  63. package/build/main/lib/scanner/Scannable/ScannableItem.d.ts +4 -3
  64. package/build/main/lib/scanner/Scannable/ScannableItem.js +6 -6
  65. package/build/main/lib/scanner/Scanner.d.ts +7 -6
  66. package/build/main/lib/scanner/Scanner.js +45 -42
  67. package/build/main/lib/scanner/ScannerTypes.d.ts +32 -0
  68. package/build/main/lib/scanner/ScannerTypes.js +35 -0
  69. package/build/main/lib/scanner/Winnower/Winnower.d.ts +5 -2
  70. package/build/main/lib/scanner/Winnower/Winnower.js +29 -24
  71. package/build/main/lib/scanner/Winnower/WinnowerResponse.d.ts +3 -0
  72. package/build/main/lib/scanner/Winnower/WinnowerResponse.js +7 -1
  73. package/build/main/lib/tree/Tree.js +4 -4
  74. package/build/module/bin/cli-bin.js +10 -3
  75. package/build/module/commands/dep.d.ts +1 -0
  76. package/build/module/commands/dep.js +19 -0
  77. package/build/module/commands/scan.js +16 -34
  78. package/build/module/index.d.ts +3 -1
  79. package/build/module/index.js +4 -2
  80. package/build/module/lib/dependencies/Dependency.d.ts +9 -0
  81. package/build/module/lib/dependencies/Dependency.js +45 -0
  82. package/build/module/lib/dependencies/DependencyTypes.d.ts +19 -0
  83. package/build/module/lib/dependencies/DependencyTypes.js +2 -0
  84. package/build/module/lib/dependencies/PurlGenerator.d.ts +2 -0
  85. package/build/module/lib/dependencies/PurlGenerator.js +32 -0
  86. package/build/module/lib/dependencies/parsers/golangParser.d.ts +0 -0
  87. package/build/module/lib/dependencies/parsers/golangParser.js +3 -0
  88. package/build/module/lib/dependencies/parsers/mavenParser.d.ts +2 -0
  89. package/build/module/lib/dependencies/parsers/mavenParser.js +37 -0
  90. package/build/module/lib/dependencies/parsers/npmParser.d.ts +3 -0
  91. package/build/module/lib/dependencies/parsers/npmParser.js +37 -0
  92. package/build/module/lib/dependencies/parsers/pyParser.d.ts +2 -0
  93. package/build/module/lib/dependencies/parsers/pyParser.js +44 -0
  94. package/build/module/lib/dependencies/parsers/rubyParser.d.ts +3 -0
  95. package/build/module/lib/dependencies/parsers/rubyParser.js +132 -0
  96. package/build/module/lib/dependencies/parsers/types.d.ts +15 -0
  97. package/build/module/lib/dependencies/parsers/types.js +32 -0
  98. package/build/module/lib/dependencies/parsers/utils.d.ts +2 -0
  99. package/build/module/lib/dependencies/parsers/utils.js +15 -0
  100. package/build/module/lib/dependencies/src/PurlGenerator.d.ts +2 -0
  101. package/build/module/lib/dependencies/src/PurlGenerator.js +55 -0
  102. package/build/module/lib/dependencies/src/parsers/golangParser.d.ts +0 -0
  103. package/build/module/lib/dependencies/src/parsers/golangParser.js +3 -0
  104. package/build/module/lib/dependencies/src/parsers/mavenParser.d.ts +2 -0
  105. package/build/module/lib/dependencies/src/parsers/mavenParser.js +37 -0
  106. package/build/module/lib/dependencies/src/parsers/npmParser.d.ts +3 -0
  107. package/build/module/lib/dependencies/src/parsers/npmParser.js +37 -0
  108. package/build/module/lib/dependencies/src/parsers/pyParser.d.ts +2 -0
  109. package/build/module/lib/dependencies/src/parsers/pyParser.js +44 -0
  110. package/build/module/lib/dependencies/src/parsers/rubyParser.d.ts +3 -0
  111. package/build/module/lib/dependencies/src/parsers/rubyParser.js +132 -0
  112. package/build/module/lib/dependencies/src/parsers/types.d.ts +15 -0
  113. package/build/module/lib/dependencies/src/parsers/types.js +32 -0
  114. package/build/module/lib/dependencies/src/parsers/utils.d.ts +2 -0
  115. package/build/module/lib/dependencies/src/parsers/utils.js +14 -0
  116. package/build/module/lib/dependencyScan/dependencyScan.d.ts +2 -0
  117. package/build/module/lib/dependencyScan/dependencyScan.js +29 -0
  118. package/build/module/lib/dependencyScan/parser.d.ts +7 -0
  119. package/build/module/lib/dependencyScan/parser.js +38 -0
  120. package/build/module/lib/dependencyScan/utils.d.ts +1 -0
  121. package/build/module/lib/dependencyScan/utils.js +12 -0
  122. package/build/module/lib/scanner/Dependency/dependency.d.ts +9 -0
  123. package/build/module/lib/scanner/Dependency/dependency.js +38 -0
  124. package/build/module/lib/scanner/Dependency/dependencyScan.d.ts +9 -0
  125. package/build/module/lib/scanner/Dependency/dependencyScan.js +40 -0
  126. package/build/module/lib/scanner/Dependency/parser.d.ts +7 -0
  127. package/build/module/lib/scanner/Dependency/parser.js +38 -0
  128. package/build/module/lib/scanner/Dependency/utils.d.ts +1 -0
  129. package/build/module/lib/scanner/Dependency/utils.js +12 -0
  130. package/build/module/lib/scanner/Dispatcher/DispatchableItem.js +1 -1
  131. package/build/module/lib/scanner/Dispatcher/Dispatcher.d.ts +3 -2
  132. package/build/module/lib/scanner/Dispatcher/Dispatcher.js +6 -4
  133. package/build/module/lib/scanner/Scannable/ScannableItem.d.ts +4 -3
  134. package/build/module/lib/scanner/Scannable/ScannableItem.js +7 -7
  135. package/build/module/lib/scanner/Scanner.d.ts +7 -6
  136. package/build/module/lib/scanner/Scanner.js +26 -22
  137. package/build/module/lib/scanner/ScannerTypes.d.ts +32 -0
  138. package/build/module/lib/scanner/ScannerTypes.js +32 -0
  139. package/build/module/lib/scanner/Winnower/Winnower.d.ts +5 -2
  140. package/build/module/lib/scanner/Winnower/Winnower.js +24 -18
  141. package/build/module/lib/scanner/Winnower/WinnowerResponse.d.ts +3 -0
  142. package/build/module/lib/scanner/Winnower/WinnowerResponse.js +8 -2
  143. package/build/module/lib/tree/Tree.js +4 -4
  144. package/build/tsconfig.module.tsbuildinfo +1 -1
  145. package/build/tsconfig.tsbuildinfo +1 -1
  146. package/package-lock.json +18277 -0
  147. package/package.json +2 -1
  148. package/src/bin/cli-bin.ts +12 -5
  149. package/src/commands/dep.ts +23 -0
  150. package/src/commands/scan.ts +15 -47
  151. package/src/index.ts +3 -2
  152. package/src/lib/dependencies/Dependency.ts +58 -0
  153. package/src/lib/dependencies/DependencyTypes.ts +22 -0
  154. package/src/lib/dependencies/PurlGenerator.ts +41 -0
  155. package/src/lib/dependencies/parsers/golangParser.ts +4 -0
  156. package/src/lib/dependencies/parsers/mavenParser.ts +48 -0
  157. package/src/lib/dependencies/parsers/npmParser.ts +44 -0
  158. package/src/lib/dependencies/parsers/pyParser.ts +46 -0
  159. package/src/lib/dependencies/parsers/rubyParser.ts +181 -0
  160. package/src/lib/dependencies/parsers/types.ts +50 -0
  161. package/src/lib/dependencies/parsers/utils.ts +16 -0
  162. package/src/lib/scanner/Dispatcher/DispatchableItem.ts +1 -1
  163. package/src/lib/scanner/Dispatcher/Dispatcher.ts +10 -6
  164. package/src/lib/scanner/Scannable/ScannableItem.ts +10 -7
  165. package/src/lib/scanner/Scanner.ts +31 -28
  166. package/src/lib/scanner/{ScannerEvents.ts → ScannerTypes.ts} +12 -0
  167. package/src/lib/scanner/Winnower/Winnower.ts +25 -17
  168. package/src/lib/scanner/Winnower/WinnowerResponse.ts +9 -1
  169. package/src/lib/tree/Tree.ts +3 -3
  170. package/yarn.lock +5214 -5127
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scanoss",
3
- "version": "0.2.8",
3
+ "version": "0.2.12",
4
4
  "description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
5
5
  "main": "build/main/index.js",
6
6
  "typings": "build/main/index.d.ts",
@@ -53,6 +53,7 @@
53
53
  "isbinaryfile": "^4.0.8",
54
54
  "node-fetch": "2.6.1",
55
55
  "p-queue": "6.6.2",
56
+ "packageurl-js": "^0.0.5",
56
57
  "sort-paths": "^1.1.1"
57
58
  },
58
59
  "devDependencies": {
@@ -1,18 +1,21 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { program } from 'commander';
4
+ import { depHandler } from '../commands/dep';
4
5
  import { scanHandler } from '../commands/scan';
5
6
 
6
-
7
7
  function CLIErrorHandler(e: Error) {
8
8
  console.error(' ');
9
9
  console.error(e);
10
10
  process.exit(1);
11
11
  }
12
12
 
13
+
14
+
15
+
13
16
  async function main() {
14
17
  program
15
- .version('0.2.0')
18
+ .version("0.2.12")
16
19
  .description('The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.')
17
20
 
18
21
  program
@@ -20,8 +23,9 @@ async function main() {
20
23
  .description('Scan a folder/file')
21
24
  //.option('-w, --wfp', 'Scan a .wfp file instead of a folder') // In progress
22
25
  .option('-c, --concurrency <number>', 'Number of concurrent connections to use while scanning (optional -default 10)')
23
- .option('-f, --filter <path>', 'Loads an user defined filter (optional)')
26
+ .option('--filter <path>', 'Loads an user defined filter (optional)')
24
27
  .option('-o, --output <filename>', 'Output result file name (optional - default stdout)')
28
+ .option('-F, --flags <flags>', 'Scanning engine flags (1: disable snippet matching, 2 enable snippet ids, 4: disable dependencies, 8: disable licenses, 16: disable copyrights,32: disable vulnerabilities, 64: disable quality, 128: disable cryptography,256: disable best match, 512: Report identified files)')
25
29
  .option('-P, --post-size <postsize>', 'Number of kilobytes to limit the post to while scanning (optional - default 64)')
26
30
  .option('-R, --max-retry <retry>', 'Max number of retries for each POST (optional -default 5)')
27
31
  .option('-M, --timeout <timeout>', 'Timeout (in seconds) for API communication (optional -default 120)')
@@ -34,10 +38,13 @@ async function main() {
34
38
  $ scanoss-js scan -o scan-output.json <source-folder>`
35
39
  );
36
40
 
41
+ program
42
+ .command('dep <source>')
43
+ .description('Scan for dependencies [ BETA ]')
44
+ .option('-o, --output <filename>', 'Output result file name (optional - default stdout)')
45
+ .action((source, options) => {depHandler(source, options).catch((e) => {CLIErrorHandler(e)})})
37
46
 
38
47
  await program.parseAsync(process.argv);
39
-
40
-
41
48
  }
42
49
 
43
50
 
@@ -0,0 +1,23 @@
1
+ import fs from "fs";
2
+ import { Dependency } from "..";
3
+ import { Tree } from "../lib/tree/Tree";
4
+
5
+ export async function depHandler(rootPath: string, options: any): Promise<void> {
6
+ rootPath = rootPath.replace(/\/$/, ''); // Remove trailing slash if exists
7
+ rootPath = rootPath.replace(/^\./, process.env.PWD); // Convert relative path to absolute path.
8
+
9
+ const tree = new Tree(rootPath);
10
+ tree.buildTree();
11
+
12
+ const fileList = tree.getRootFolder().getFiles().map((path) => {return rootPath+path});
13
+
14
+ const dependency = new Dependency();
15
+ const results = await dependency.scan(fileList);
16
+
17
+ if(options.output) {
18
+ fs.promises.writeFile(options.output, JSON.stringify(results, null, 2));
19
+ } else {
20
+ console.log(JSON.stringify(results, null, 2));
21
+ }
22
+
23
+ }
@@ -1,32 +1,15 @@
1
1
  import { Scanner } from '../lib/scanner/Scanner';
2
- import { ScannerEvents } from '../lib/scanner/ScannerEvents';
2
+ import { ScannerEvents, ScannerInput } from '../lib/scanner/ScannerTypes';
3
3
  import { ScannerCfg } from '../lib/scanner/ScannerCfg';
4
4
  import { Tree } from '../lib/tree/Tree';
5
- import File from '../lib/tree/File';
6
5
 
7
6
  import cliProgress from 'cli-progress';
8
- import { WinnowerResponse } from '../lib/scanner/Winnower/WinnowerResponse';
9
7
  import { DispatcherResponse } from '../lib/scanner/Dispatcher/DispatcherResponse';
10
8
  import { defaultFilter } from '../lib/filters/defaultFilter';
11
9
  import { FilterList } from '../lib/filters/filtering';
12
10
 
13
11
  import fs from 'fs';
14
12
 
15
- enum FilterTypes {
16
- BANNED = 'BANNED',
17
- WHITELIST = 'WHITELIST',
18
- FULL_SCAN = 'FULL_SCAN',
19
- QUICK_SCAN = 'QUICK_SCAN',
20
- }
21
-
22
-
23
- interface ScannerInput {
24
- EngineFlags: string;
25
- FolderRoot?: string;
26
- FileList: Array<string>;
27
- Snippets: boolean;
28
- };
29
-
30
13
  // Async function that verify if a path is a folder. If the path is not valid the promise will be rejected
31
14
  const isFolder = (path: string): Promise<boolean> => {
32
15
  return new Promise((resolve, reject) => {
@@ -40,35 +23,29 @@ const isFolder = (path: string): Promise<boolean> => {
40
23
  });
41
24
  }
42
25
 
43
- async function printJSON(path: string) {
44
- const jsonTxt = await fs.promises.readFile(path, 'utf8');
45
- console.log(jsonTxt);
46
- }
47
-
48
-
49
-
50
26
  export async function scanHandler(rootPath: string, options: any): Promise<void> {
51
27
 
52
- let fileList = [];
53
- let scannerInput = {};
54
- let filesCounter = 0; // Used by the progress bar
28
+ let scannerInput: ScannerInput = {fileList: []};
29
+
30
+ rootPath = rootPath.replace(/\/$/, ''); // Remove trailing slash if exists
31
+ rootPath = rootPath.replace(/^\./, process.env.PWD); // Convert relative path to absolute path.
55
32
  const pathIsFolder = await isFolder(rootPath);
56
33
 
57
34
  // Create scanner and set connections parameters
58
35
  const scannerCfg = new ScannerCfg();
59
36
  if(options.concurrency) scannerCfg.CONCURRENCY_LIMIT = parseInt(options.concurrency);
60
37
  if(options.postSize) scannerCfg.WFP_FILE_MAX_SIZE = parseInt(options.postSize) * 1024;
61
- if(options.apiUrl) scannerCfg.API_URL = options.apiurl;
38
+ if(options.apiurl) scannerCfg.API_URL = options.apiurl;
62
39
  if(options.key) scannerCfg.API_KEY = options.key;
63
40
  if(options.timeout) scannerCfg.TIMEOUT = options.timeout * 1000;
64
41
  if(options.maxRetry) scannerCfg.MAX_RETRIES_FOR_RECOVERABLES_ERRORS = options.maxRetry;
65
-
66
42
  const scanner = new Scanner(scannerCfg);
67
43
 
68
- if(!options.wfp) { // --wfp change the behaviour of the scanner. Scan a WFP File instead of a folder
69
- if(pathIsFolder) { // If a folder is specified, we scan the folder and all its subfolders
70
- if(rootPath.charAt(rootPath.length -1) !== '/') rootPath += '/';
44
+ scannerInput.folderRoot = rootPath + '/'; // This will remove the project root path from the results.
45
+ if(options.flags) scannerInput.engineFlags = options.flags;
71
46
 
47
+ if(!options.wfp) {
48
+ if(pathIsFolder) {
72
49
  const tree = new Tree(rootPath);
73
50
  const filter = new FilterList('');
74
51
 
@@ -79,31 +56,22 @@ export async function scanHandler(rootPath: string, options: any): Promise<void>
79
56
  console.error('Loading default filters...');
80
57
  filter.load(defaultFilter as FilterList);
81
58
  }
82
-
83
59
  console.error('Reading directory... ');
84
60
  tree.loadFilter(filter);
85
61
  tree.buildTree();
86
-
87
- fileList = tree.getFileList();
88
- filesCounter = fileList.length;
89
-
90
- // Converts to scanner input format
91
- // This is temporary, until the scanner is ready to receive a ScannerInput object
92
- for(let f of fileList) scannerInput = Object.assign(scannerInput, {[f]: 'FULL_SCAN'});
62
+ scannerInput.fileList = tree.getFileList();
93
63
  } else {
94
- scannerInput = {[rootPath]: 'FULL_SCAN'};
95
- filesCounter = 1;
64
+ scannerInput.fileList = [rootPath];
96
65
  }
97
66
  } else {
98
67
  const winnowing = fs.readFileSync(rootPath, {encoding: 'utf-8'});
99
- const reg = /file=/g;
100
- filesCounter = [...winnowing.matchAll(reg)].length;
68
+ //filesCounter = [...winnowing.matchAll(/file=/g)].length;
101
69
  }
102
70
 
103
71
  if (!options.verbose) {
104
72
  const optBar1 = { format: 'Scan Progress: [{bar}] {percentage}% | Scanned {value} files of {total}' };
105
73
  const bar1 = new cliProgress.SingleBar(optBar1, cliProgress.Presets.shades_classic);
106
- bar1.start(filesCounter, 0);
74
+ bar1.start(scannerInput.fileList.length, 0);
107
75
 
108
76
  scanner.on(ScannerEvents.DISPATCHER_NEW_DATA, (dispResp: DispatcherResponse) => {
109
77
  bar1.increment(dispResp.getFilesScanned().length);
@@ -122,7 +90,7 @@ export async function scanHandler(rootPath: string, options: any): Promise<void>
122
90
  });
123
91
 
124
92
  if (options.wfp) await scanner.scanFromWinnowingFile(rootPath);
125
- else await scanner.scanList(scannerInput);
93
+ else await scanner.scan([scannerInput]);
126
94
 
127
95
  }
128
96
 
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
- export * from './lib/scanner/ScannerEvents';
1
+ export * from './lib/scanner/ScannerTypes';
2
2
  export * from './lib/scanner/ScannerCfg'
3
3
  export * from './lib/scanner/Scanner'
4
-
4
+ export * from './lib/dependencies/DependencyTypes';
5
+ export * from './lib/dependencies/Dependency';
@@ -0,0 +1,58 @@
1
+ import os from "os";
2
+ import fs from "fs";
3
+
4
+ import { IDependencyResponse, IFile, IDependency } from "./DependencyTypes";
5
+ import { FileListDependency } from "./parsers/types";
6
+ import { generateDependenciesPurls } from "./PurlGenerator";
7
+
8
+ export class Dependency {
9
+
10
+ private workDirectory: string;
11
+
12
+ private resultFilePath: string;
13
+
14
+ constructor () {
15
+ this.setWorkDirectory(`${os.tmpdir()}/depscanner-${new Date().getTime()}`);
16
+ }
17
+
18
+ public async scan (fileList: Array<string>): Promise<IDependencyResponse> {
19
+ const toGrpc = await generateDependenciesPurls(fileList);
20
+ // Here we should call to the grpc server
21
+ return this.adapterToDependencyResponse(toGrpc);
22
+ }
23
+
24
+ public setWorkDirectory(workDirectory: string) {
25
+ this.workDirectory = workDirectory;
26
+ this.resultFilePath = `${this.workDirectory}/dependencies.json`;
27
+
28
+ if (!fs.existsSync(this.workDirectory)) fs.mkdirSync(this.workDirectory);
29
+ }
30
+
31
+ private adapterToDependencyResponse (dependencies: FileListDependency): IDependencyResponse {
32
+ const results = <IDependencyResponse>{files: []};
33
+
34
+ const depArr: Array<IDependency> = [];
35
+ let fileArr = <IFile>{};
36
+
37
+ for (const dependency of dependencies.files){
38
+ for (const purl of dependency.purls) {
39
+ depArr.push({
40
+ component: null,
41
+ purl: purl.purl,
42
+ version: null,
43
+ licenses: [{name: null}]
44
+ });
45
+ }
46
+
47
+ fileArr = ({
48
+ file: dependency.file,
49
+ id: "dependency",
50
+ status: "pending",
51
+ dependencies: depArr
52
+ });
53
+
54
+ results.files.push(fileArr);
55
+ }
56
+ return results
57
+ }
58
+ }
@@ -0,0 +1,22 @@
1
+
2
+ interface ILicense {
3
+ name: string;
4
+ }
5
+
6
+ export interface IDependency {
7
+ component: string;
8
+ purl: string;
9
+ version: string;
10
+ licenses: Array<ILicense>;
11
+ }
12
+
13
+ export interface IFile {
14
+ file: string;
15
+ id: string;
16
+ status: string;
17
+ dependencies: Array<IDependency>;
18
+ }
19
+
20
+ export interface IDependencyResponse {
21
+ files: Array<IFile>;
22
+ }
@@ -0,0 +1,41 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { FileListDependency, ParserDefinitions } from './parsers/types';
4
+
5
+
6
+ import { pomParser } from './parsers/mavenParser';
7
+ import { packagelockParser, packageParser } from './parsers/npmParser';
8
+ import { requirementsParser } from './parsers/pyParser';
9
+ import { gemfilelockParser, gemfileParser } from './parsers/rubyParser';
10
+
11
+
12
+
13
+ /*
14
+ This is a hash map that connect a filename with it's own parser function
15
+ Any parser function must return a FileDependency object (See type.ts file in src/parser
16
+ */
17
+ const Parser: ParserDefinitions = {
18
+ 'requirements.txt': requirementsParser,
19
+ 'pom.xml': pomParser,
20
+ 'package.json': packageParser,
21
+ 'package-lock.json': packagelockParser,
22
+ 'Gemfile': gemfileParser,
23
+ 'Gemfile.lock': gemfilelockParser
24
+ };
25
+
26
+
27
+ export async function generateDependenciesPurls(files: Array<string>): Promise<FileListDependency> {
28
+ let results: FileListDependency = {files: []};
29
+
30
+ for (const filePath of files) {
31
+ const fileName = path.basename(filePath);
32
+ if(Parser[fileName] != null) {
33
+ const fileContent = await fs.promises.readFile(filePath, 'utf8');
34
+ const dependency = Parser[fileName](fileContent, filePath);
35
+ if(dependency.purls.length != 0)
36
+ results.files.push(dependency);
37
+ }
38
+ }
39
+ return results;
40
+ }
41
+
@@ -0,0 +1,4 @@
1
+
2
+
3
+ // Reference https://go.dev/ref/mod#go-mod-file
4
+ //
@@ -0,0 +1,48 @@
1
+ import path from "path";
2
+ import { PackageURL } from "packageurl-js";
3
+ import { FileDependency } from "./types";
4
+ import { isValidPath, isValidUrl } from './utils';
5
+
6
+ const PURL_TYPE = 'maven';
7
+
8
+
9
+ // Parse a pom.txt file from maven manifest file
10
+ // See reference on: https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
11
+ // and https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
12
+ const MANIFEST_FILE = 'pom.xml';
13
+ export function pomParser(fileContent: string, filePath: string): FileDependency {
14
+
15
+ // If the file is not a python manifest file, return an empty results
16
+ const results: FileDependency = {file: filePath, purls: []};
17
+ if(path.basename(filePath) != MANIFEST_FILE)
18
+ return results;
19
+
20
+ const dependencies = fileContent.match(/<dependency>((?:.|\n)*?)<\/dependency>/gm);
21
+ if(dependencies) {
22
+ // TODO: classifier are not supported yet
23
+ dependencies.forEach(dependency => {
24
+ // Extract groupId. It's the purl namespace
25
+ const groupId = dependency.match(/<groupId>([^<]*)<\/groupId>/);
26
+ const namespace = groupId ? groupId[1] : '';
27
+
28
+ // Extract artifact id. It's the purl name
29
+ const artifactId = dependency.match(/<artifactId>([^<]*)<\/artifactId>/);
30
+ const name = artifactId ? artifactId[1] : '';
31
+
32
+ const versionReg = dependency.match(/<version>([^<]*)<\/version>/);
33
+ const version = versionReg ? versionReg[1] : '';
34
+
35
+
36
+ let purlQualifiers;
37
+ const type = dependency.match(/<type>([^<]*)<\/type>/);
38
+ if(type) {
39
+ purlQualifiers = {};
40
+ purlQualifiers['type'] = type[1]
41
+ }
42
+
43
+ const purlString = new PackageURL(PURL_TYPE, namespace, name, version, purlQualifiers, undefined).toString();
44
+ results.purls.push({purl: purlString});
45
+ });
46
+ }
47
+ return results;
48
+ }
@@ -0,0 +1,44 @@
1
+ import path from "path";
2
+ import { PackageURL } from "packageurl-js";
3
+ import { FileDependency } from "./types";
4
+ import { isValidPath, isValidUrl } from './utils';
5
+
6
+ const PURL_TYPE = 'npm';
7
+
8
+
9
+ // Parse a package.json file from node projects
10
+ // See reference on: https://docs.npmjs.com/cli/v8/configuring-npm/package-json
11
+ const MANIFEST_FILE = 'package.json';
12
+ export function packageParser(fileContent: string, filePath: string): FileDependency {
13
+ // If the file is not manifest file, return an empty results
14
+ const results: FileDependency = {file: filePath, purls: []};
15
+ if(path.basename(filePath) != MANIFEST_FILE)
16
+ return results;
17
+ const o = JSON.parse(fileContent);
18
+ let devDeps = Object.keys(o.devDependencies || {});
19
+ let deps = Object.keys(o.dependencies || {});
20
+ let listDeps = [...deps, ...devDeps];
21
+ for(const name of listDeps){
22
+ const purlString = new PackageURL(PURL_TYPE, undefined, name, undefined, undefined, undefined).toString();
23
+ results.purls.push({purl: purlString});
24
+ }
25
+ return results;
26
+ }
27
+
28
+
29
+ // Parse a package-lock.json file from node projects
30
+ // See reference on: https://docs.npmjs.com/cli/v8/configuring-npm/package-json
31
+ const MANIFEST_FILE_1 = 'package-loc.json';
32
+ export function packagelockParser(fileContent: string, filePath: string): FileDependency {
33
+
34
+ const results: FileDependency = {file: filePath, purls: []};
35
+ if(path.basename(filePath) != MANIFEST_FILE_1)
36
+ return results;
37
+
38
+ const o = JSON.parse(fileContent).dependencies;
39
+ for (const [key, value] of Object.entries(o)) {
40
+ if(!key) continue;
41
+ let purl = new PackageURL(PURL_TYPE, undefined, key,value['version'], undefined, undefined).toString();
42
+ results.purls.push({purl});
43
+ }
44
+ }
@@ -0,0 +1,46 @@
1
+ import path from "path";
2
+ import { PackageURL } from "packageurl-js";
3
+ import { FileDependency } from "./types";
4
+ import { isValidPath, isValidUrl } from './utils';
5
+
6
+ const PURL_TYPE = 'pypi';
7
+
8
+ // Parse a requirements.txt file from python projects
9
+ // See reference on: https://pip.pypa.io/en/stable/reference/requirements-file-format/
10
+ const MANIFEST_FILE = 'requirements.txt';
11
+ export function requirementsParser(fileContent: string, filePath: string): FileDependency {
12
+
13
+ // If the file is not a python manifest file, return an empty results
14
+ const results: FileDependency = {file: filePath, purls: []};
15
+ if(path.basename(filePath) != MANIFEST_FILE)
16
+ return results;
17
+
18
+
19
+ const lines: Array<string> = fileContent.split('\n');
20
+ let compName: string = '';
21
+ let compVer: string = '';
22
+
23
+ for (let line of lines) {
24
+ line = line.trim();
25
+ if (!line.startsWith('#') && line.length>0) { // Avoid comments and new lines
26
+ if(isValidUrl(line)) {
27
+ // For reference about the regex see https://www.rfc-editor.org/rfc/rfc3986#appendix-B
28
+ const res = line.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);
29
+ continue;
30
+ }
31
+ else if(isValidPath(line)) {continue;}
32
+ else if(line.startsWith('-r')) {continue;} // recursive dependencies (NOT SUPPORTED YET)
33
+ else {
34
+ // Line contains a package name and/or version.
35
+ const res = line.match(/^([-\w]+)\s*(?:[>=~!]*)\s*([\d\.]*)/); //Extract name and version
36
+ if (res) {
37
+ compName = res.length > 1 ? res[1] : ' ';
38
+ compVer = undefined;
39
+ }
40
+ }
41
+ const purlString = new PackageURL(PURL_TYPE, undefined, compName, compVer, undefined, undefined).toString();
42
+ results.purls.push({purl: purlString});
43
+ }
44
+ }
45
+ return results;
46
+ }
@@ -0,0 +1,181 @@
1
+ import path from "path";
2
+ import { PackageURL } from "packageurl-js";
3
+ import { FileDependency } from "./types";
4
+ import { isValidPath, isValidUrl } from './utils';
5
+ import { stringify } from "querystring";
6
+
7
+ const PURL_TYPE = 'gem';
8
+
9
+
10
+ // Parse a gemfile file from ruby
11
+ // See reference on: https://bundler.io/gemfile.html
12
+ // and https://bundler.io/man/gemfile.5.html
13
+ const MANIFEST_FILE = 'Gemfile';
14
+ export function gemfileParser(fileContent: string, filePath: string): FileDependency {
15
+
16
+ // If the file is not a manifest file, return an empty results
17
+ const results: FileDependency = {file: filePath, purls: []};
18
+ if(path.basename(filePath) != MANIFEST_FILE)
19
+ return results;
20
+
21
+
22
+ const lines: Array<string> = fileContent.split('\n');
23
+ let compName: string = '';
24
+
25
+ for (let line of lines) {
26
+ line = line.trim();
27
+ if (!line.startsWith('#') && line.length>0) { // Avoid comments and empty lines
28
+ // Line contains a package name and/or version.
29
+ const res = line.match(/^gem\s*(["']\w+["'])/); //Extract name
30
+ if (res) {
31
+ compName = res.length > 1 ? res[1] : ' ';
32
+ compName = compName.replace(/['"]/g, '');
33
+ const purlString = new PackageURL(PURL_TYPE, undefined, compName, undefined, undefined, undefined).toString();
34
+ results.purls.push({purl: purlString});
35
+ }
36
+ }
37
+ }
38
+ return results;
39
+ }
40
+
41
+
42
+ const MANIFEST_FILE_1 = 'Gemfile.lock';
43
+ export function gemfilelockParser(fileContent: string, filePath: string): FileDependency {
44
+
45
+ // If the file is not a manifest file, return an empty results
46
+ const results: FileDependency = {file: filePath, purls: []};
47
+ if(path.basename(filePath) != MANIFEST_FILE_1)
48
+ return results;
49
+
50
+ const gemlockParser = new GemfileLockParser();
51
+ const purls = gemlockParser.getDependencies(fileContent);
52
+ for (const purl of purls) {
53
+ results.purls.push({purl});
54
+ }
55
+ return results;
56
+ }
57
+
58
+
59
+ // Section headings: these are also used as switches to track a parsing state
60
+
61
+ const PATH = 'PATH';
62
+ const GIT = 'GIT';
63
+ const SVN = 'SVN';
64
+ const GEM = 'GEM';
65
+ const PLATFORMS = 'PLATFORMS';
66
+ const DEPENDENCIES = 'DEPENDENCIES';
67
+ const SPECS = ' specs:';
68
+
69
+ // types of Gems, which is really where they are provisioned from
70
+ // RubyGems repo, local path or VCS
71
+ const GEM_TYPES = [GEM, PATH, GIT, SVN];
72
+
73
+
74
+ const specRegex = /(?<name>[^ \)\(,!:]+)?(?: \((?<version>[^-]*?)(?:-(?<platform>[^!]*))?\))?/;
75
+ const firstDepLevelRegex = /^ {4}(?! )/;
76
+ /*
77
+ The parsing use a simple state machine, switching states based on sections
78
+ headings. The result is a list of purls
79
+ */
80
+ class GemfileLockParser {
81
+
82
+ private statesMap;
83
+
84
+ private state;
85
+
86
+ private current_options: Record<string, string>;
87
+
88
+ private current_gem;
89
+
90
+ private purlList;
91
+
92
+ constructor () {
93
+
94
+ this.purlList = [];
95
+
96
+ // map of a line start string to the next parsing state function
97
+ this.statesMap = {};
98
+ this.statesMap[DEPENDENCIES] = this.parseDependency;
99
+ this.statesMap[PLATFORMS] = this.parsePlatform;
100
+ this.statesMap[GIT] = this.parseOptions;
101
+ this.statesMap[PATH] = this.parseOptions;
102
+ this.statesMap[SVN] = this.parseOptions;
103
+ this.statesMap[GEM] = this.parseOptions;
104
+ this.statesMap[SPECS] = this.parseSpec;
105
+ }
106
+
107
+ public getDependencies(filecontent: string) {
108
+ this.resetState();
109
+ let file = filecontent.split('\n');
110
+ for (let line of file) {
111
+ line = line.trimEnd();
112
+
113
+ //reset state on empty lines
114
+ if (!line.length) {
115
+ this.resetState();
116
+ continue;
117
+ }
118
+
119
+ //switch to new state
120
+ if (line in this.statesMap) {
121
+ if (GEM_TYPES.includes(line))
122
+ this.current_gem = line;
123
+ this.state=this.statesMap[line];
124
+ continue;
125
+ }
126
+
127
+ // process the line
128
+ if (this.state) this.state(line);
129
+ }
130
+ this.refine();
131
+ return this.purlList;
132
+ }
133
+
134
+ private resetState() {
135
+ this.current_options = {};
136
+ this.state = null;
137
+ }
138
+
139
+ private parseOptions(line: string) {
140
+ const match = line.match(/\s*(\w+):\s*(.*)/);
141
+ const key = match.length>=1 ? match[1] : null;
142
+ const value = match.length>=2 ? match[2] : null;
143
+ if(key) this.current_options[key] = value;
144
+ }
145
+
146
+ private parseDependency(line: string) {}
147
+ private parsePlatform(line: string){}
148
+
149
+
150
+ private parseSpec(line: string) {
151
+
152
+ if(this.current_gem == GEM) {
153
+ if(firstDepLevelRegex.test(line)) {
154
+ line = line.trimStart();
155
+ const match = line.match(specRegex);
156
+
157
+ const purl = new PackageURL( PURL_TYPE,
158
+ undefined,
159
+ match.groups.name,
160
+ match.groups.version,
161
+ undefined,
162
+ undefined ).toString();
163
+ this.purlList.push(purl);
164
+
165
+ } else { // Second level of dependence
166
+
167
+ }
168
+ }
169
+
170
+ if(this.current_gem == GIT) {}
171
+
172
+ // Purl from local dependencies are not generated
173
+ if(this.current_gem == PATH){}
174
+
175
+ }
176
+
177
+ private refine() {}
178
+
179
+
180
+
181
+ }