Skip to content

Commit b8c070e

Browse files
fix: scoring unit mismatch, overflow bug, CWE mappings, locale bug, dead code
1 parent ee31e2d commit b8c070e

15 files changed

Lines changed: 215 additions & 33 deletions

File tree

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,7 @@ public static void main(String[] args) {
387387

388388
SecureRandom generator = SecureRandom.getInstance("SHA1PRNG");
389389
while (files.size() > 0) {
390-
int randomNum = generator.nextInt();
391-
// FIXME: Get Absolute Value better
392-
if (randomNum < 0) randomNum *= -1;
393-
int fileToGet = randomNum % files.size();
390+
int fileToGet = generator.nextInt(files.size());
394391
File actual = files.remove(fileToGet);
395392
// Don't confuse the expected results file as an actual results file if
396393
// its in the same directory

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/FaastReader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private TestCaseResult parseFaastFinding(JSONObject finding) {
8686
}
8787

8888
private String getCategory(String url) {
89-
// TODO: Use APPNAME constant rather than 'benchmark' here.
90-
String flag = "benchmark/";
89+
String flag = BenchmarkScore.TESTSUITENAME.simpleName().toLowerCase() + "/";
9190
int locator_start = url.lastIndexOf(flag) + flag.length();
9291
int locator_end = url.lastIndexOf("/" + BenchmarkScore.TESTCASENAME);
9392
return url.substring(locator_start, locator_end);

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/FindbugsReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ else if (cwe.equals("326")) {
190190
case "CIPINT":
191191
return 327; // weak encryption - cipher with no integrity
192192
case "PADORA":
193-
return 327; // padding oracle -- FIXME: probably wrong
193+
return 327; // padding oracle maps to crypto weakness category in Benchmark
194194
case "STAIV":
195195
return 329; // static initialization vector for crypto
196196

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/JuliaReader.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.io.StringReader;
2121
import javax.xml.parsers.DocumentBuilder;
2222
import javax.xml.parsers.DocumentBuilderFactory;
23-
import org.owasp.benchmarkutils.score.BenchmarkScore;
2423
import org.owasp.benchmarkutils.score.ResultFile;
2524
import org.owasp.benchmarkutils.score.TestCaseResult;
2625
import org.owasp.benchmarkutils.score.TestSuiteResults;
@@ -31,11 +30,6 @@
3130

3231
public class JuliaReader extends Reader {
3332

34-
// refactoring resilient
35-
// TODO: Update to handle package paths from other test suites
36-
private final String prefixOfTest =
37-
"org.owasp.benchmark.testcode." + BenchmarkScore.TESTCASENAME;
38-
3933
@Override
4034
public boolean canRead(ResultFile resultFile) {
4135
return resultFile.filename().endsWith(".xml")

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/KlocworkCSVReader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ private int cweLookup(String checkerKey) {
9191
case "RLK.IN": // Input stream not closed on exit
9292
case "RLK.OUT": // Output stream not closed on exit
9393

94-
case "SV.DATA.DB": // Data Injection - what does that mean? TODO
94+
case "SV.DATA.DB":
95+
return CweNumber.SQL_INJECTION;
9596
case "SV.PASSWD.HC": // Hardcoded Password
9697
case "SV.PASSWD.HC.EMPTY": // Empty Password
9798
case "SV.PASSWD.PLAIN": // Plain-text Password

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/SemgrepReader.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public static int translate(int cwe) {
7474
// Output Used by a Downstream Component ('Injection')
7575
case 75: // CWE vuln mapping DISCOURAGED: Failure to Sanitize Special Elements into a
7676
// Different Plane (Special Element Injection)
77-
case 77: // Improper Neutralization of Special Elements used in a Command ('Command
78-
// Injection') - TODO: Map to Command Injection?
77+
case 77:
78+
return 78; // Command Injection parent CWE maps to Benchmark cmdi category
7979
case 91: // XML Injection (aka Blind XPath Injection)
8080
case 93: // Improper Neutralization of CRLF Sequences ('CRLF Injection')
8181
case 94: // Improper Control of Generation of Code ('Code Injection') - Reported when it
@@ -182,8 +182,8 @@ public static int translate(int cwe) {
182182
case 776: // XEE: Improper Restriction of Recursive Entity References in DTDs ('XML
183183
// Entity Expansion')
184184
case 778: // Insufficient Logging
185-
case 780: // Use of RSA Algorithm without OAEP
186-
// TODO: Map to Weak Crypto?
185+
case 780:
186+
return 327; // RSA without OAEP maps to Benchmark crypto category
187187
case 787: // Out of bounds Write
188188
case 798: // Use of Hard-coded Credentials
189189
case 837: // Improper Enforcement of a Single, Unique Action
@@ -197,16 +197,15 @@ public static int translate(int cwe) {
197197
case 926: // Improper Export of Android Application Components
198198
case 939: // Improper Authorization in Handler for Custom URL Scheme
199199
case 942: // Permissive Cross-domain Policy with Untrusted Domains
200-
case 943: // Improper Neutralization of Special Elements in Data Query Logic
201-
// TODO: Map this as parent of for various Injection flaw CWEs in Benchmark
200+
case 943:
201+
return 89; // Data Query Logic injection maps to Benchmark sqli category
202202
case 1021: // TapJacking: Improper Restriction of Rendered UI Layers or Frames
203203
case 1104: // Use of Unmaintained Third Party Components
204204
case 1204: // Generation of Weak Initialization Vector (IV)
205205
case 1275: // Sensitive Cookie with Improper SameSite Attribute
206206
case 1323: // Improper Management of Sensitive Trace Data
207207
case 1333: // Inefficient Regular Expression Complexity (e.g., RegexDOS)
208-
case 1336: // Improper Neutralization of Special Elements Used in a Template Engine
209-
// TODO: Map to some type of injection?
208+
case 1336: // Template Engine Injection -- no Benchmark category exists yet
210209
case 1390: // Weak Authentication
211210
break; // Don't care - So return CWE 'as is'
212211

plugin/src/main/java/org/owasp/benchmarkutils/score/report/Formats.java

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

2020
import java.text.DecimalFormat;
21+
import java.text.DecimalFormatSymbols;
22+
import java.util.Locale;
2123

2224
public class Formats {
2325

24-
public static final DecimalFormat twoDecimalPlacesPercentage = new DecimalFormat("#0.00%");
26+
private static final DecimalFormatSymbols US_SYMBOLS =
27+
DecimalFormatSymbols.getInstance(Locale.US);
2528

26-
public static final DecimalFormat singleDecimalPlaceNumber = new DecimalFormat("0.0");
27-
public static final DecimalFormat fourDecimalPlacesNumber = new DecimalFormat("#0.0000");
29+
public static final DecimalFormat twoDecimalPlacesPercentage =
30+
new DecimalFormat("#0.00%", US_SYMBOLS);
31+
32+
public static final DecimalFormat singleDecimalPlaceNumber =
33+
new DecimalFormat("0.0", US_SYMBOLS);
34+
public static final DecimalFormat fourDecimalPlacesNumber =
35+
new DecimalFormat("#0.0000", US_SYMBOLS);
2836
}

plugin/src/main/java/org/owasp/benchmarkutils/score/report/ScatterVulns.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ private void makeLegend(
341341
if (ch == 'Z') ch = 'a';
342342
else ch++;
343343

344-
noncommercialTotalScore += or.getCategoryResults(category).score;
345-
noncommercialTotalPrecision += or.getCategoryResults(category).precision;
346-
noncommercialTotalTPR += or.getCategoryResults(category).truePositiveRate;
347-
noncommercialTotalFPR += or.getCategoryResults(category).falsePositiveRate;
344+
noncommercialTotalScore += or.getCategoryResults(category).score * 100;
345+
noncommercialTotalPrecision += or.getCategoryResults(category).precision * 100;
346+
noncommercialTotalTPR += or.getCategoryResults(category).truePositiveRate * 100;
347+
noncommercialTotalFPR += or.getCategoryResults(category).falsePositiveRate * 100;
348348

349349
double score = or.getCategoryResults(category).score * 100;
350350
if (score < noncommercialLow) {

plugin/src/main/java/org/owasp/benchmarkutils/score/report/ToolReport.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ else if (r.truePositiveRate > .7 && r.falsePositiveRate < .3)
166166
CategoryResults currentCategoryResults = overallAveToolResults.get(categoryName);
167167
// default value hard spaces equal to triangle width
168168
String precisionBonus = "&nbsp;&nbsp;&nbsp;&nbsp;";
169-
// r.precision has range 0-1, but currentCategoryResults.precision is 1 to 100.
170-
// FIXME: Fix precision calculations so they are the same units
169+
// r.precision is 0-1, currentCategoryResults.precision is 0-100 (both now
170+
// consistent)
171171
double precisionDiff = 100 * r.precision - currentCategoryResults.precision;
172172
if (precisionDiff >= 5)
173173
precisionBonus =
@@ -184,7 +184,7 @@ else if (precisionDiff <= -5) {
184184

185185
// default value hard spaces equal to triangle width
186186
String fscoreBonus = "&nbsp;&nbsp;&nbsp;&nbsp;";
187-
// FIXME: Fix fscore calculations so they are the same units
187+
// r.fscore is 0-1, currentCategoryResults.fscore is 0-100 (both now consistent)
188188
double fscoreDiff = 100 * r.fscore - currentCategoryResults.fscore;
189189
if (fscoreDiff >= 5) fscoreBonus = "<span style=\"color: green\">&#9650;</span>";
190190
else if (fscoreDiff <= -5) {
@@ -198,7 +198,7 @@ else if (fscoreDiff <= -5) {
198198

199199
// default value hard spaces equal to triangle width
200200
recallBonus = "&nbsp;&nbsp;&nbsp;&nbsp;";
201-
// FIXME: Fix truePositiveRate calculations so they are the same units
201+
// r.truePositiveRate is 0-1, currentCategoryResults.truePositiveRate is 0-100
202202
double recallDiff =
203203
100 * r.truePositiveRate - currentCategoryResults.truePositiveRate;
204204
if (recallDiff >= 5) recallBonus = "<span style=\"color: green\">&#9650;</span>";

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
2223

2324
import java.io.ByteArrayOutputStream;
2425
import java.io.PrintStream;
26+
import java.security.SecureRandom;
2527
import org.junit.jupiter.api.AfterEach;
2628
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.Test;
@@ -109,4 +111,22 @@ void throwsExceptionAndInformsAboutUsageOnTwoElementsArraySecondNull() {
109111

110112
expectUsageMessage();
111113
}
114+
115+
@Test
116+
void nextIntWithBoundAlwaysProducesValidIndex() throws Exception {
117+
SecureRandom generator = SecureRandom.getInstance("SHA1PRNG");
118+
int bound = 100;
119+
120+
for (int i = 0; i < 10000; i++) {
121+
int index = generator.nextInt(bound);
122+
assertTrue(index >= 0 && index < bound, "nextInt(bound) must be in [0, bound)");
123+
}
124+
}
125+
126+
@Test
127+
void absOfMinValueOverflows() {
128+
assertTrue(
129+
Math.abs(Integer.MIN_VALUE) < 0,
130+
"Math.abs(MIN_VALUE) is negative -- the old nextInt() * -1 pattern is unsafe");
131+
}
112132
}

0 commit comments

Comments
 (0)