Skip to content

Commit

Permalink
fix(ImportCDX): VCS sanitization failing on characters like colon
Browse files Browse the repository at this point in the history
Signed-off-by: afsahsyeda <afsah.syeda@siemens-healthineers.com>
  • Loading branch information
afsahsyeda authored and heliocastro committed Oct 23, 2024
1 parent dc18109 commit 48e0f6c
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private Map<String, List<org.cyclonedx.model.Component>> getVcsToComponentMap(Li
.filter(Objects::nonNull)
.filter(ref -> ExternalReference.Type.VCS.equals(ref.getType()))
.map(ExternalReference::getUrl)
.map(url -> sanitizeVCS(url))
.map(url -> sanitizeVCS(url.toLowerCase()))
.filter(url -> CommonUtils.isValidUrl(url))
.map(url -> new AbstractMap.SimpleEntry<>(url, comp)))
.collect(Collectors.groupingBy(e -> e.getKey(),
Expand Down Expand Up @@ -986,20 +986,24 @@ private String getComponentNameFromVCS(String vcsUrl, boolean isGetVendorandName
* Sanitize different repository URLS based on their defined schema
*/
public String sanitizeVCS(String vcs) {
// GitHub repository URL Format: https://github.com/supplier/name
if (vcs.toLowerCase().contains("github.com")) {
URI uri = URI.create(vcs);
String[] urlParts = uri.getPath().split("/");
if (urlParts.length >= 3) {
String firstSegment = urlParts[1];
String secondSegment = urlParts[2].replaceAll("\\.git.*", "").replaceAll("#.*", "");
vcs = "https://github.com/" + firstSegment + "/" + secondSegment;
return vcs;
} else {
log.error("Invalid GitHub repository URL: " + vcs);
if (vcs.contains("github.com")) {
vcs = "https://" + vcs.substring(vcs.indexOf("github.com")).trim();
try {
URI uri = URI.create(vcs);
String[] urlParts = uri.getPath().split("/");

if (urlParts.length >= 3) {
String firstSegment = urlParts[1];
String secondSegment = urlParts[2].replaceAll("\\.git.*", "").replaceAll("#.*", "");
vcs = "https://github.com/" + firstSegment + "/" + secondSegment;
return vcs;
} else {
log.error("Invalid GitHub repository URL: {}", vcs);
}
} catch (IllegalArgumentException e) {
log.error("Invalid URL format: {}", vcs, e);
}
}
// Other formats yet to be defined
return vcs;
}

Expand Down

0 comments on commit 48e0f6c

Please sign in to comment.