Skip to content

Commit b1ad39b

Browse files
agustingrohisasmendiagus
authored andcommitted
CLI-82 Adds deobfuscation on results append response
1 parent fcbebf5 commit b1ad39b

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

Diff for: package-lock.json

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "scanoss",
3-
"version": "0.9.1",
3+
"version": "0.9.2",
44
"description": "The SCANOSS JS package provides a simple, easy to consume module for interacting with SCANOSS APIs/Engine.",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",

Diff for: src/sdk/scanner/Scanner.ts

+17-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class Scanner extends EventEmitter {
3434

3535
private obfuscateMapFilePath: string;
3636

37-
private obfuscateMap: Array<Record<string, string>>;
37+
private obfuscateMap: Record<string, string>;
3838

3939
private scanRoot: string;
4040

@@ -76,7 +76,7 @@ export class Scanner extends EventEmitter {
7676
this.responseBuffer = [];
7777
this.filesToScan = {};
7878
this.filesNotScanned = {};
79-
this.obfuscateMap = [];
79+
this.obfuscateMap = {};
8080

8181
this.wfpProvider = new WfpCalculator(this.scannerCfg);
8282
this.dispatcher = new Dispatcher(this.scannerCfg);
@@ -97,7 +97,7 @@ export class Scanner extends EventEmitter {
9797
this.emit(ScannerEvents.WINNOWING_NEW_CONTENT, fingerprintPackage);
9898
this.reportLog(`[ SCANNER ]: New WFP content`);
9999

100-
if (fingerprintPackage.isObfuscated()) this.obfuscateMap.push(fingerprintPackage.getObfuscationMap());
100+
if (fingerprintPackage.isObfuscated()) this.obfuscateMap = { ...this.obfuscateMap, ...fingerprintPackage.getObfuscationMap()};
101101
const item = new DispatchableItem();
102102
item.setFingerprintPackage(fingerprintPackage);
103103
item.uuid = uuidv4();
@@ -201,6 +201,14 @@ export class Scanner extends EventEmitter {
201201
return false;
202202
}
203203

204+
private deobfuscationResponses(responses: Record<string, any>, obfuscateMap: Record<string, string>): Record<string, any>{
205+
const deObfuscation = {};
206+
Object.entries(responses).forEach(([key, value]: [string, any]) => {
207+
deObfuscation[obfuscateMap[key]] = value;
208+
});
209+
return deObfuscation;
210+
}
211+
204212
private bufferToFiles() {
205213
let wfpContent = '';
206214
const serverResponse = {};
@@ -211,15 +219,11 @@ export class Scanner extends EventEmitter {
211219
Object.assign(serverResponse, serverResponseToAppend);
212220
}
213221

214-
const obfuscateMap = {}
215-
for (const obfuscateMapChunk of this.obfuscateMap) {
216-
Object.assign(obfuscateMap, obfuscateMapChunk)
217-
}
218-
219-
this.appendOutputFiles(wfpContent, serverResponse, obfuscateMap);
222+
this.appendOutputFiles(wfpContent, serverResponse);
220223
this.responseBuffer = [];
221-
this.obfuscateMap = [];
222-
const responses = new DispatcherResponse(serverResponse, wfpContent);
224+
const r = (this.scannerCfg.WFP_OBFUSCATION &&
225+
this.scannerCfg.RESULTS_DEOBFUSCATION) ? this.deobfuscationResponses(serverResponse, this.obfuscateMap) : serverResponse;
226+
const responses = new DispatcherResponse(r, wfpContent);
223227
this.reportLog(`[ SCANNER ]: Persisted results of ${responses.getNumberOfFilesScanned()} files...`);
224228
this.emit(ScannerEvents.RESULTS_APPENDED, responses, this.filesNotScanned);
225229
return responses;
@@ -269,7 +273,6 @@ export class Scanner extends EventEmitter {
269273
const results = JSON.parse(await fs.promises.readFile(this.resultFilePath, 'utf8'));
270274

271275
if (this.scannerCfg.WFP_OBFUSCATION && this.scannerCfg.RESULTS_DEOBFUSCATION) {
272-
this.obfuscateMap = JSON.parse(await fs.promises.readFile(this.obfuscateMapFilePath, 'utf8'));
273276
for (const key of Object.keys(this.obfuscateMap)) {
274277
const component = results[key];
275278
const originalPath = this.obfuscateMap[key];
@@ -283,6 +286,7 @@ export class Scanner extends EventEmitter {
283286
// eslint-disable-next-line no-restricted-syntax
284287
for (const key of sortedPaths) resultSorted[key] = results[key];
285288
await fs.promises.writeFile(this.resultFilePath, JSON.stringify(resultSorted, null, 2));
289+
await fs.promises.writeFile(this.obfuscateMapFilePath, JSON.stringify(this.obfuscateMap, null, 2));
286290
this.reportLog(
287291
`[ SCANNER ]: Scan finished (Scanned: ${this.processedFiles}, Not Scanned: ${
288292
Object.keys(this.filesNotScanned).length
@@ -322,7 +326,7 @@ export class Scanner extends EventEmitter {
322326

323327
}
324328

325-
private appendOutputFiles(wfpContent: string, serverResponse: ScannerResults, obfuscationMap: Record<string, string>) {
329+
private appendOutputFiles(wfpContent: string, serverResponse: ScannerResults) {
326330
fs.appendFileSync(this.wfpFilePath, wfpContent);
327331

328332
const storedResultStr = fs.readFileSync(this.resultFilePath, 'utf-8');
@@ -331,14 +335,6 @@ export class Scanner extends EventEmitter {
331335
const newResultStr = JSON.stringify(storedResultObj);
332336
fs.writeFileSync(this.resultFilePath, newResultStr);
333337

334-
335-
if (this.scannerCfg.WFP_OBFUSCATION) {
336-
const storedObfuscationMapStr = fs.readFileSync(this.obfuscateMapFilePath, 'utf-8');
337-
const storedObfuscationMap = JSON.parse(storedObfuscationMapStr);
338-
Object.assign(storedObfuscationMap, obfuscationMap);
339-
const newObfuscationMap = JSON.stringify(storedObfuscationMap);
340-
fs.writeFileSync(this.obfuscateMapFilePath, newObfuscationMap);
341-
}
342338
}
343339

344340

0 commit comments

Comments
 (0)