Skip to content

Commit 4c68dbd

Browse files
committed
Fix the scatterchart statistics in the right column on the Vulnerabilities and Category Groups pages,
fix the menu name for the ToolsByGrp commercial average link, fix some of the test cases now that stats are calculated 0-100, rather than 0-1. Add a CWE column to the Tools CommercialAverages page table and fix the associated test cases.
1 parent c3805eb commit 4c68dbd

File tree

9 files changed

+141
-84
lines changed

9 files changed

+141
-84
lines changed

library/src/main/java/org/owasp/benchmarkutils/helpers/Categories.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,27 @@ private void load(InputStream xmlFileStream)
313313
this.allCategories = allCategories;
314314
}
315315

316+
/**
317+
* Look up the CWE associated with the supplied vulnerability category long name.
318+
*
319+
* @param name The category name to look up the CWE for. E.g., Command Injection.
320+
* @return the associated CWE.
321+
*/
322+
public static int getCWEByName(String name) {
323+
String lowerName = name.toLowerCase(); // The Map uses lowercase names
324+
if (_instance == null) {
325+
throw new NullPointerException("ERROR: Categories singleton not initialized");
326+
}
327+
if (_instance.nameToCategoryMap.get(lowerName) == null) {
328+
System.err.println(
329+
"ERROR: No matching Category found for name: '"
330+
+ name
331+
+ "' provided to method: getCWEByName()");
332+
return -1;
333+
}
334+
return _instance.nameToCategoryMap.get(lowerName).getCWE();
335+
}
336+
316337
// NOTE: All these methods return the actual internal objects so COULD be modified by the caller
317338
// causing unexpected side effects.
318339

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ private static void generateVulnerabilityScorecards(
10991099
Paths.get(
11001100
scoreCardDir.getAbsolutePath()
11011101
+ File.separator
1102-
+ commercialAveragesTable.filename());
1102+
+ commercialAveragesTable.filename(useCategoryGroups));
11031103
// Resources in a jar file have to be loaded as streams, not directly as Files.
11041104
InputStream vulnTemplateStream =
11051105
CL.getResourceAsStream(scoreCardDir + "/commercialAveTemplate.html");

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,10 @@ public CategoryGroup getCategoryGroup() {
271271
*
272272
* @param cwe The CWE # reported by this tool.
273273
* @param filename The filename that might be a test case.
274+
* <p>public void setCWEAndTestCaseID(int cwe, String filename) { if
275+
* (ExpectedResultsProvider.getExpectedResults().isTestCaseFile(filename)) { // DRW FIXME:TODO -
276+
* Not implemented yet. Maybe just delete? } }
274277
*/
275-
public void setCWEAndTestCaseID(int cwe, String filename) {
276-
if (ExpectedResultsProvider.getExpectedResults().isTestCaseFile(filename)) {
277-
// DRW TODO
278-
}
279-
}
280278

281279
/**
282280
* The CWE category name, e.g., pathtraver, hash, cmdi, etc.

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,9 @@ private void makeLegend(
445445

446446
this.commercialToolCount++;
447447
this.overallToolCount++;
448-
double score = categoryMetrics.score * 100;
449-
double tpr = categoryMetrics.truePositiveRate * 100;
450-
double fpr = categoryMetrics.falsePositiveRate * 100;
448+
double score = categoryMetrics.score;
449+
double tpr = categoryMetrics.truePositiveRate;
450+
double fpr = categoryMetrics.falsePositiveRate;
451451
// don't show the commercial tool results if in 'show ave only mode'
452452
if (!BenchmarkScore.config.showAveOnlyMode) {
453453
// Special hack to make it line up better if the letter is an 'I' or 'i'
@@ -463,16 +463,16 @@ private void makeLegend(
463463
i,
464464
label,
465465
tool.getToolNameAndVersion(),
466-
toolMetrics.getTruePositiveRate(),
467-
toolMetrics.getFalsePositiveRate());
466+
tpr,
467+
fpr);
468468

469469
i++; // increment the location of the label
470470
// Weak hack if more than 26 tools scored. This will only get us to 52
471471
if (ch == 'Z') ch = 'a';
472472
else ch++;
473473
}
474474
commercialTotalScore += score;
475-
commercialTotalPrecision += categoryMetrics.precision * 100;
475+
commercialTotalPrecision += categoryMetrics.precision;
476476
commercialTotalTPR += tpr;
477477
commercialTotalFPR += fpr;
478478

@@ -536,8 +536,8 @@ private void makeLegend(
536536
i,
537537
ch + ": ",
538538
"Commercial Average",
539-
commercialAveTPR / 100,
540-
commercialAveFPR / 100);
539+
commercialAveTPR,
540+
commercialAveFPR);
541541

542542
Point2D averagePoint =
543543
new Point2D.Double(aveFalsePosRates * 100, aveTruePosRates * 100);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public String generateHtml(
122122
return html;
123123
}
124124

125-
/** Generate a Detailed results table for whatever tool's results are passed in. */
125+
// Generate a Detailed results table for whatever tool's results are passed in.
126126
private static String generateDetailedResultsTableForTool(
127127
Tool tool,
128128
Map<String, CategoryMetrics> overallAveToolMetrics,
@@ -183,7 +183,8 @@ private static String generateDetailedResultsTableForTool(
183183
else if (categoryMetrics.truePositiveRate > .7
184184
&& categoryMetrics.falsePositiveRate < .3) style = "class=\"success\"";
185185

186-
// We use a lineBuff so we can sort the lines in different ways before output the table
186+
// We use a lineBuff so we can sort the lines in different ways before outputting the
187+
// table
187188
StringBuffer lineBuff = new StringBuffer();
188189
lineBuff.append("<tr " + style + ">");
189190
if (CategoryGroups.isCategoryGroupsEnabled()) {
@@ -224,7 +225,7 @@ else if (categoryMetrics.truePositiveRate > .7
224225
// default value hard spaces equal to triangle width
225226
String precisionBonus = "&nbsp;&nbsp;&nbsp;&nbsp;";
226227
// r.precision has range 0-1, but currentCategoryMetrics.precision is 1 to 100.
227-
// FIXME: Fix precision calculations so they are the same units
228+
// FIXME: Fix r.precision calculations so they are the same units
228229
double precisionDiff =
229230
100 * categoryMetrics.precision - currentCategoryMetrics.precision;
230231
if (precisionDiff >= 5)
@@ -257,6 +258,7 @@ else if (fscoreDiff <= -5) {
257258
// default value hard spaces equal to triangle width
258259
recallBonus = "&nbsp;&nbsp;&nbsp;&nbsp;";
259260
// FIXME: Fix truePositiveRate calculations so they are the same units
261+
// Note: This might be done already.
260262
double recallDiff =
261263
100 * categoryMetrics.truePositiveRate
262264
- currentCategoryMetrics.truePositiveRate;

plugin/src/main/java/org/owasp/benchmarkutils/score/report/html/CommercialAveragesTable.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
25+
import org.owasp.benchmarkutils.helpers.Categories;
2526
import org.owasp.benchmarkutils.score.domain.TestSuiteName;
2627
import org.owasp.benchmarkutils.score.report.ScatterVulns;
2728

@@ -70,7 +71,10 @@ private int commercialToolTotal() {
7071
private void addHeaderTo(HtmlStringBuilder htmlBuilder) {
7172
htmlBuilder.beginTr();
7273
if (this.useCategoryGroups) htmlBuilder.th("Category Group");
73-
else htmlBuilder.th("Vulnerability Category");
74+
else {
75+
htmlBuilder.th(50, "CWE");
76+
htmlBuilder.th("Vulnerability Category");
77+
}
7478
htmlBuilder.th("Low Tool Type");
7579
htmlBuilder.th("Low Score");
7680
htmlBuilder.th("Ave Score");
@@ -81,14 +85,21 @@ private void addHeaderTo(HtmlStringBuilder htmlBuilder) {
8185

8286
private void appendRowTo(HtmlStringBuilder htmlBuilder, ScatterVulns scatter) {
8387
htmlBuilder.beginTr();
88+
if (!this.useCategoryGroups) htmlBuilder.td(Categories.getCWEByName(scatter.CATEGORY));
8489
htmlBuilder.td(scatter.CATEGORY);
8590
htmlBuilder.td(scatter.getCommercialLowToolType() + "");
8691

8792
htmlBuilder.td(scatter.getCommercialLow(), cssClassFor(scatter.getCommercialLow()));
8893
htmlBuilder.td(scatter.getCommercialAve());
8994

9095
htmlBuilder.td(scatter.getCommercialHigh(), cssClassFor(scatter.getCommercialHigh()));
91-
htmlBuilder.td(scatter.getCommercialHighToolType() + "");
96+
// If no tools score above 0 in a category, the High type is null, so set it equal to the
97+
// low type since they are all zero
98+
if (scatter.getCommercialHighToolType() != null) {
99+
htmlBuilder.td(scatter.getCommercialHighToolType() + "");
100+
} else {
101+
htmlBuilder.td(scatter.getCommercialLowToolType() + "");
102+
}
92103
htmlBuilder.endTr();
93104
}
94105

@@ -106,6 +117,7 @@ private static String cssClassFor(int commercialLow) {
106117

107118
private void addFooterTo(HtmlStringBuilder htmlBuilder) {
108119
htmlBuilder.beginTr();
120+
if (!this.useCategoryGroups) htmlBuilder.td("");
109121
htmlBuilder.td("Average across all categories for " + commercialToolTotal() + " tools");
110122
htmlBuilder.td("");
111123
htmlBuilder.td(
@@ -139,10 +151,10 @@ public boolean hasEntries() {
139151
return !entries.isEmpty();
140152
}
141153

142-
public String filename() {
154+
public String filename(boolean forCategoryGroups) {
143155
return format(
144156
"{0}_v{1}_Scorecard_for_Commercial_Tools"
145-
+ (this.useCategoryGroups ? "_CategoryGroups" : "")
157+
+ (forCategoryGroups ? "_CategoryGroups" : "")
146158
+ ".html",
147159
testSuiteName.simpleName(),
148160
testSuiteVersion);

plugin/src/main/java/org/owasp/benchmarkutils/score/report/html/HtmlStringBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ public HtmlStringBuilder th(String content) {
5959
return this;
6060
}
6161

62+
/**
63+
* Define header with fixed width in pixels
64+
*
65+
* @param width
66+
* @param content
67+
* @return The specified HTML column header
68+
*/
69+
public HtmlStringBuilder th(int width, String content) {
70+
sb.append("<th style=\"width: " + width + "px;\">").append(content).append("</th>");
71+
72+
return this;
73+
}
74+
6275
public HtmlStringBuilder th(long content) {
6376
sb.append("<th>").append(content).append("</th>");
6477

plugin/src/main/java/org/owasp/benchmarkutils/score/report/html/MenuUpdater.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private String toolMenu() {
9797
.forEach(tool -> sb.append(toolMenuEntry(tool, false)));
9898

9999
if (commercialAveragesTable.hasEntries()) {
100-
sb.append(commercialAveragesMenuEntry());
100+
sb.append(commercialAveragesMenuEntry(false));
101101
}
102102

103103
return sb.toString();
@@ -111,9 +111,8 @@ private String toolCatalogGroupsMenu() {
111111
.filter(tool -> !(config.showAveOnlyMode && tool.isCommercial()))
112112
.forEach(tool -> sb.append(toolMenuEntry(tool, true)));
113113

114-
// DRW TODO2: Need to test this for CatagoryGroups
115114
if (commercialAveragesTable.hasEntries()) {
116-
sb.append(commercialAveragesMenuEntry());
115+
sb.append(commercialAveragesMenuEntry(true));
117116
}
118117
}
119118
return sb.toString();
@@ -127,10 +126,10 @@ private String toolMenuEntry(Tool tool, boolean forCategoryGroups) {
127126
System.lineSeparator());
128127
}
129128

130-
private String commercialAveragesMenuEntry() {
129+
private String commercialAveragesMenuEntry(boolean forCategoryGroups) {
131130
return format(
132131
"<li><a href=\"{0}\">Commercial Average</a></li>{1}",
133-
commercialAveragesTable.filename(), System.lineSeparator());
132+
commercialAveragesTable.filename(forCategoryGroups), System.lineSeparator());
134133
}
135134

136135
private String vulnerabilityMenu() {

0 commit comments

Comments
 (0)