Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.fortify.cli.aviator.audit.model;

import com.fortify.cli.aviator._common.exception.AviatorSimpleException;

public enum Priority {
Critical,
Expand All @@ -21,13 +22,13 @@ public enum Priority {

public static Priority fromString(String value) {
if (value == null) {
throw new IllegalArgumentException("Priority value cannot be null.");
throw new AviatorSimpleException("Priority value cannot be null.");
}
for (Priority p : values()) {
if (p.name().equalsIgnoreCase(value)) {
return p;
}
}
throw new IllegalArgumentException("No Priority constant with name '" + value + "' found.");
throw new AviatorSimpleException("No Priority constant with name '" + value + "' found.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fortify.cli.aviator._common.exception.AviatorSimpleException;

public class NumberRangeComparer implements SearchComparer {
private static final Logger logger = LoggerFactory.getLogger(NumberRangeComparer.class);

Expand All @@ -35,7 +37,7 @@ public NumberRangeComparer(String rangeTerm) {

String trimmed = rangeTerm.trim();
if (trimmed.length() < 3) { // Must be at least 3 chars like (1,2)
throw new IllegalArgumentException("Invalid range format: " + rangeTerm);
throw new AviatorSimpleException("Invalid range format: " + rangeTerm);
}

// Determine inclusiveness from the start and end characters
Expand All @@ -52,15 +54,15 @@ public NumberRangeComparer(String rangeTerm) {
}

if (sep == -1) {
throw new IllegalArgumentException("Invalid range format (missing separator ',' or '-'): " + rangeTerm);
throw new AviatorSimpleException("Invalid range format (missing separator ',' or '-'): " + rangeTerm);
}

try {
this.lowerBound = new BigDecimal(inner.substring(0, sep).trim());
this.upperBound = new BigDecimal(inner.substring(sep + 1).trim());
} catch (NumberFormatException e) {
logger.error("Error parsing number in range term '{}'", rangeTerm, e);
throw new IllegalArgumentException("Invalid number in range format: " + rangeTerm);
throw new AviatorSimpleException("Invalid number in range format: " + rangeTerm, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@
*/
package com.fortify.cli.aviator.fpr.model;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.fortify.cli.aviator._common.exception.AviatorTechnicalException;
import com.fortify.cli.aviator.fpr.filter.FilterSet;
import com.fortify.cli.aviator.fpr.filter.FilterTemplate;
import com.fortify.cli.aviator.util.FprHandle;
Expand Down Expand Up @@ -53,12 +59,7 @@ public class FPRInfo {
public FPRInfo(FprHandle fprHandle) {
FPRName = String.valueOf(fprHandle.getFprPath().getFileName());
buildId = "";
try {
extractInfoFromAuditFvdlStreaming(fprHandle);
} catch (Exception e) {
// It's better to wrap this in a specific runtime exception
throw new RuntimeException("Failed to extract info from audit.fvdl", e);
}
extractInfoFromAuditFvdlStreaming(fprHandle);
}

/**
Expand All @@ -70,30 +71,30 @@ public FPRInfo(FprHandle fprHandle) {
* - Build information (BuildID, SourceBasePath, NumberFiles, ScanTime)
*
* @param fprHandle for getting path
* @throws Exception if parsing fails
* @throws AviatorTechnicalException if parsing fails
*/
private void extractInfoFromAuditFvdlStreaming(FprHandle fprHandle) throws Exception {
private void extractInfoFromAuditFvdlStreaming(FprHandle fprHandle) {
Path auditPath = fprHandle.getPath("/audit.fvdl");

if (!Files.exists(auditPath)) {
throw new IllegalStateException("audit.fvdl not found in FPR: " + fprHandle.getFprPath());
throw new AviatorTechnicalException("audit.fvdl not found in FPR: " + fprHandle.getFprPath());
}

javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance();
XMLInputFactory factory = XMLInputFactory.newInstance();
// Security: Disable external entity processing
factory.setProperty(javax.xml.stream.XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
factory.setProperty(javax.xml.stream.XMLInputFactory.SUPPORT_DTD, false);
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);

try (java.io.InputStream inputStream = Files.newInputStream(auditPath)) {
javax.xml.stream.XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
try (InputStream inputStream = Files.newInputStream(auditPath)) {
XMLStreamReader reader = factory.createXMLStreamReader(inputStream);

boolean inBuild = false;
String currentElement = null;

while (reader.hasNext()) {
int event = reader.next();

if (event == javax.xml.stream.XMLStreamConstants.START_ELEMENT) {
if (event == XMLStreamConstants.START_ELEMENT) {
String localName = reader.getLocalName();

if ("UUID".equals(localName)) {
Expand All @@ -109,7 +110,7 @@ private void extractInfoFromAuditFvdlStreaming(FprHandle fprHandle) throws Excep
currentElement = localName;
}

} else if (event == javax.xml.stream.XMLStreamConstants.CHARACTERS && inBuild && currentElement != null) {
} else if (event == XMLStreamConstants.CHARACTERS && inBuild && currentElement != null) {
// Read text content for Build child elements
String text = reader.getText().trim();
if (!text.isEmpty()) {
Expand All @@ -129,7 +130,7 @@ private void extractInfoFromAuditFvdlStreaming(FprHandle fprHandle) throws Excep
}
}

} else if (event == javax.xml.stream.XMLStreamConstants.END_ELEMENT) {
} else if (event == XMLStreamConstants.END_ELEMENT) {
String localName = reader.getLocalName();

if ("Build".equals(localName)) {
Expand All @@ -149,8 +150,8 @@ private void extractInfoFromAuditFvdlStreaming(FprHandle fprHandle) throws Excep

reader.close();

} catch (javax.xml.stream.XMLStreamException e) {
throw new Exception("Failed to parse audit.fvdl using streaming parser", e);
} catch (IOException | XMLStreamException e) {
throw new AviatorTechnicalException("Failed to parse audit.fvdl using streaming parser", e);
}

if (buildId == null) {
Expand All @@ -164,15 +165,15 @@ private void extractInfoFromAuditFvdlStreaming(FprHandle fprHandle) throws Excep
*
* @param reader XMLStreamReader positioned at START_ELEMENT
* @return Text content of the element, or empty string if no text
* @throws javax.xml.stream.XMLStreamException if reading fails
* @throws XMLStreamException if reading fails
*/
private String readElementText(javax.xml.stream.XMLStreamReader reader) throws javax.xml.stream.XMLStreamException {
private String readElementText(XMLStreamReader reader) throws XMLStreamException {
StringBuilder text = new StringBuilder();
while (reader.hasNext()) {
int event = reader.next();
if (event == javax.xml.stream.XMLStreamConstants.CHARACTERS) {
if (event == XMLStreamConstants.CHARACTERS) {
text.append(reader.getText());
} else if (event == javax.xml.stream.XMLStreamConstants.END_ELEMENT) {
} else if (event == XMLStreamConstants.END_ELEMENT) {
break;
}
}
Expand All @@ -183,7 +184,7 @@ private void extractInfoFromAuditFvdl(FprHandle fprHandle) throws Exception {
Path auditPath = fprHandle.getPath("/audit.fvdl");

if (!Files.exists(auditPath)) {
throw new IllegalStateException("audit.fvdl not found in FPR: " + fprHandle.getFprPath());
throw new AviatorTechnicalException("audit.fvdl not found in FPR: " + fprHandle.getFprPath());
}

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ private String calculateHashBase64(String content, String algorithm) {
byte[] digest = md.digest(content.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(digest);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(algorithm + " algorithm not found", e);
throw new AviatorTechnicalException("Hashing algorithm not available: " + algorithm, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.fortify.cli.aviator._common.exception.AviatorTechnicalException;
import com.fortify.cli.aviator.fpr.filter.Filter;
import com.fortify.cli.aviator.fpr.filter.FilterSet;
import com.fortify.cli.aviator.fpr.filter.FilterTemplate;
Expand Down Expand Up @@ -114,9 +115,11 @@ public Optional<FilterTemplate> parseFilterTemplate() {
auditProcessor.setFilterTemplateDoc(doc);

return Optional.of(filterTemplate);
} catch (AviatorTechnicalException e) {
throw e;
} catch (Exception e) {
logger.error("Error parsing filtertemplate.xml", e);
throw new RuntimeException("Failed to parse filtertemplate.xml", e);
throw new AviatorTechnicalException("Failed to parse filtertemplate.xml", e);
}
}

Expand Down Expand Up @@ -230,7 +233,7 @@ private void saveDocument(Document doc, File outputFile) {
logger.info("Successfully saved updated filtertemplate.xml");
} catch (Exception e) {
logger.error("Error saving XML document: {}", e.getMessage(), e);
throw new RuntimeException("Failed to save XML document", e);
throw new AviatorTechnicalException("Failed to save XML document", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ private String calculateHashBase64(String content, String algorithm) {
hash = Base64.getEncoder().encodeToString(digest);
return hash;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(algorithm + " algorithm not found", e);
throw new AviatorTechnicalException("Hashing algorithm not available: " + algorithm, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.junit.jupiter.api.Test;

import com.fortify.cli.aviator._common.exception.AviatorSimpleException;
import com.fortify.cli.aviator.fpr.filter.comparer.NumberRangeComparer;
import com.fortify.cli.aviator.fpr.filter.comparer.SearchComparer;

Expand Down Expand Up @@ -86,17 +87,17 @@ void testSingleValueExactMatch() {

@Test
void testThrowsExceptionOnMalformedRangeMissingEnd() {
assertThrows(IllegalArgumentException.class, () -> new NumberRangeComparer("[1,5"));
assertThrows(AviatorSimpleException.class, () -> new NumberRangeComparer("[1,5"));
}

@Test
void testThrowsExceptionOnMalformedRangeMissingStart() {
assertThrows(IllegalArgumentException.class, () -> new NumberRangeComparer("1,5]"));
assertThrows(AviatorSimpleException.class, () -> new NumberRangeComparer("1,5]"));
}

@Test
void testThrowsExceptionOnMalformedRangeMissingSeparator() {
assertThrows(IllegalArgumentException.class, () -> new NumberRangeComparer("[1 5]"));
assertThrows(AviatorSimpleException.class, () -> new NumberRangeComparer("[1 5]"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2021-2026 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.aviator.fpr.model;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import com.fortify.cli.aviator._common.exception.AviatorTechnicalException;
import com.fortify.cli.aviator.util.FprHandle;

@DisplayName("FPRInfo")
class FPRInfoTest {
@TempDir
Path tempDir;

@Test
@DisplayName("throws AviatorTechnicalException when audit.fvdl is missing")
void throwsTechnicalExceptionWhenAuditFvdlIsMissing() throws Exception {
Path fprPath = createFpr(null);

try (FprHandle fprHandle = new FprHandle(fprPath)) {
AviatorTechnicalException exception = assertThrows(AviatorTechnicalException.class, () -> new FPRInfo(fprHandle));

assertTrue(exception.getMessage().contains("audit.fvdl"));
}
}

private Path createFpr(String auditFvdlContent) throws IOException {
Path fprPath = tempDir.resolve("test.fpr");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Files.newOutputStream(fprPath))) {
if (auditFvdlContent != null) {
writeEntry(zipOutputStream, "audit.fvdl", auditFvdlContent);
}
writeEntry(zipOutputStream, "src-archive/index.xml", """
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<index>
<entry key=\"Test.java\">src-archive/Test.java</entry>
</index>
""");
writeEntry(zipOutputStream, "src-archive/Test.java", "public class Test {}\n");
}
return fprPath;
}

private void writeEntry(ZipOutputStream zipOutputStream, String entryName, String content) throws IOException {
zipOutputStream.putNextEntry(new ZipEntry(entryName));
zipOutputStream.write(content.getBytes(StandardCharsets.UTF_8));
zipOutputStream.closeEntry();
}
}
Loading
Loading