Skip to content

Commit 62c683e

Browse files
committed
feat(SP-2487): implement path deobfuscation on Scanner class
1 parent 0fd920b commit 62c683e

File tree

3 files changed

+318
-19
lines changed

3 files changed

+318
-19
lines changed

src/main/java/com/scanoss/Scanner.java

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.scanoss.filters.factories.FolderFilterFactory;
3131
import com.scanoss.processor.*;
3232
import com.scanoss.rest.ScanApi;
33+
import com.scanoss.settings.Bom;
3334
import com.scanoss.settings.ScanossSettings;
3435
import com.scanoss.utils.JsonUtils;
3536
import lombok.*;
@@ -49,6 +50,7 @@
4950
import java.util.concurrent.Executors;
5051
import java.util.concurrent.Future;
5152
import java.util.function.Predicate;
53+
import java.util.stream.Collectors;
5254

5355
import static com.scanoss.ScanossConstants.*;
5456

@@ -353,13 +355,12 @@ public List<String> wfpFolder(@NonNull String folder) throws ScannerException, W
353355
*/
354356
public String scanFile(@NonNull String filename) throws ScannerException, WinnowingException {
355357
String wfp = wfpFile(filename);
356-
if (wfp != null && !wfp.isEmpty()) {
357-
String response = this.scanApi.scan(wfp, "", 1);
358-
if (response != null && !response.isEmpty()) {
359-
return response;
360-
}
358+
if (wfp == null || wfp.isEmpty()) {
359+
return "";
361360
}
362-
return "";
361+
362+
String result = scanApi.scan(wfp, "", 1);
363+
return postProcessResult(result);
363364
}
364365

365366
/**
@@ -385,18 +386,52 @@ public List<String> scanFileList(@NonNull String folder, @NonNull List<String> f
385386
return postProcessResults(results);
386387
}
387388

389+
388390
/**
389-
* Post-processes scan results based on BOM (Bill of Materials) settings if available.
390-
* @param results List of raw scan results in JSON string format
391-
* @return Processed results, either modified based on BOM or original results if no BOM exists
391+
* Processes the result string and provides a post-processed output.
392+
*
393+
* @param rawResults the raw result string to be processed.
394+
* @return the post-processed result string.
392395
*/
393-
private List<String> postProcessResults(List<String> results) {
394-
if (settings.getBom() != null) {
395-
List<ScanFileResult> scanFileResults = JsonUtils.toScanFileResults(results);
396-
List <ScanFileResult> newScanFileResults = this.postProcessor.process(scanFileResults, this.settings.getBom());
397-
return JsonUtils.toRawJsonString(newScanFileResults);
396+
private String postProcessResult(String rawResults) {
397+
if (rawResults == null || rawResults.isEmpty()) {
398+
return "";
398399
}
399-
return results;
400+
return postProcessResults(List.of(rawResults)).stream()
401+
.findFirst()
402+
.orElse("");
400403
}
401404

405+
/**
406+
* Processes the given list of raw scan results by applying deobfuscation and post-processing steps based on settings.
407+
*
408+
* @param rawResults a list of raw scan results in string format to be processed
409+
* @return a list of processed scan results in string format
410+
*/
411+
private List<String> postProcessResults(List<String> rawResults) {
412+
List<ScanFileResult> scanFileResults = JsonUtils.toScanFileResults(rawResults);
413+
414+
if (obfuscate) {
415+
scanFileResults = deobfuscateResults(scanFileResults);
416+
}
417+
418+
Bom bom = settings.getBom();
419+
if (bom != null) {
420+
scanFileResults = this.postProcessor.process(scanFileResults, bom);
421+
}
422+
423+
return JsonUtils.toRawJsonString(scanFileResults);
424+
}
425+
426+
/**
427+
* Deobfuscate the file paths in a list of ScanFileResult.
428+
*
429+
* @param scanFileResults List of ScanFileResult to be deobfuscated
430+
* @return List of ScanFileResult with deobfuscated file paths
431+
*/
432+
private List<ScanFileResult> deobfuscateResults(@NonNull List<ScanFileResult> scanFileResults) {
433+
return scanFileResults.stream()
434+
.map(result -> result.withFilePath(winnowing.deobfuscateFilePath(result.getFilePath())))
435+
.collect(Collectors.toList());
436+
}
402437
}

src/main/java/com/scanoss/dto/ScanFileResult.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package com.scanoss.dto;
2424

2525
import lombok.Data;
26+
import lombok.With;
2627

2728
import java.util.List;
2829

@@ -31,6 +32,7 @@
3132
*/
3233
@Data
3334
public class ScanFileResult {
35+
@With
3436
private final String filePath;
3537
private final List<ScanFileDetails> fileDetails;
3638
}

0 commit comments

Comments
 (0)