Skip to content

Commit 923ced1

Browse files
fix: use classpath constant for commercial average template resource lookup (#268)
BenchmarkScore.java:965 passed scoreCardDir (a java.io.File representing the filesystem output directory) to getResourceAsStream(), which expects a classpath resource path. With the default config (resultsfileordir: "results"), File.toString() accidentally produces "scorecard" — matching the classpath prefix. But any nested resultsfileordir path (e.g. "some/dir/results") causes getParent() to return a non-null prefix, producing an invalid classpath like "some/dir/scorecard/commercialAveTemplate.html", which resolves to null and throws NullPointerException. Fix: use the SCORECARDDIRNAME constant ("scorecard"), consistent with ToolReport.java:64 and the vulntemplate loading at line 910.
1 parent ee31e2d commit 923ced1

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

plugin/src/main/java/org/owasp/benchmarkutils/score/BenchmarkScore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ private static void generateVulnerabilityScorecards(
962962
+ commercialAveragesTable.filename());
963963
// Resources in a jar file have to be loaded as streams. Not directly as Files.
964964
InputStream vulnTemplateStream =
965-
CL.getResourceAsStream(scoreCardDir + "/commercialAveTemplate.html");
965+
CL.getResourceAsStream(SCORECARDDIRNAME + "/commercialAveTemplate.html");
966966
String html = IOUtils.toString(vulnTemplateStream, StandardCharsets.UTF_8);
967967
html = html.replace("${testsuite}", BenchmarkScore.TESTSUITENAME.fullName());
968968
html = html.replace("${version}", TESTSUITEVERSION);

plugin/src/test/java/org/owasp/benchmarkutils/score/BenchmarkScoreTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
package org.owasp.benchmarkutils.score;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2122
import static org.junit.jupiter.api.Assertions.assertThrows;
2223

2324
import java.io.ByteArrayOutputStream;
25+
import java.io.InputStream;
2426
import java.io.PrintStream;
2527
import org.junit.jupiter.api.AfterEach;
2628
import org.junit.jupiter.api.BeforeEach;
@@ -109,4 +111,16 @@ void throwsExceptionAndInformsAboutUsageOnTwoElementsArraySecondNull() {
109111

110112
expectUsageMessage();
111113
}
114+
115+
@Test
116+
void commercialAveTemplateResolvesViaClasspathConstant() {
117+
String resourcePath =
118+
BenchmarkScore.SCORECARDDIRNAME + "/commercialAveTemplate.html";
119+
InputStream stream =
120+
BenchmarkScore.class.getClassLoader().getResourceAsStream(resourcePath);
121+
122+
assertNotNull(
123+
stream,
124+
"commercialAveTemplate.html must be loadable via SCORECARDDIRNAME classpath lookup");
125+
}
112126
}

0 commit comments

Comments
 (0)