@@ -54,11 +54,7 @@ public TestResults parse(File f) throws Exception {
54
54
55
55
for ( Node flaw : issueList ) {
56
56
try {
57
- TestCaseResult tcr = parseZapIssue (flaw );
58
- if (tcr != null ) {
59
- // System.out.println( tcr.getNumber() + " " + tcr.getName() + " -> " + tcr.getCWE() + "\t" + tcr.getEvidence() );
60
- tr .put (tcr );
61
- }
57
+ parseAndAddZapIssues (flaw , tr );
62
58
} catch ( Exception e ) {
63
59
// print and continue
64
60
e .printStackTrace ();
@@ -80,9 +76,20 @@ public TestResults parse(File f) throws Exception {
80
76
// <riskdesc>Low (Medium)</riskdesc>
81
77
// <desc>Web Browser XSS Protection is not enabled, or is disabled by the configuration of the 'X-XSS-Protection' HTTP response header on the web server
82
78
// </desc>
79
+
83
80
// <uri>http://localhost:8080/benchmark/BenchmarkTest00028.html</uri>
84
81
// <param/>
85
82
// <attack/>
83
+ // OR, for merged reports:
84
+ // <instances>
85
+ // <instance>
86
+ // <uri>http://localhost:8080/benchmark/BenchmarkTest00028.html</uri>
87
+ // <param/>
88
+ // <attack/>
89
+ // </instance>
90
+ // <!-- more "instance" elements per merged alert -->
91
+ // </instances>
92
+
86
93
// <otherinfo>The X-XSS-Protection HTTP response header allows the web server to enable or disable the web browser's XSS protection mechanism. The following values would attempt to enable it:
87
94
// <solution>Ensure that the web browser's XSS filter is enabled, by setting the X-XSS-Protection HTTP response header to '1'.
88
95
// </solution>
@@ -98,23 +105,43 @@ public TestResults parse(File f) throws Exception {
98
105
99
106
100
107
101
- private TestCaseResult parseZapIssue (Node flaw ) throws URISyntaxException {
102
- TestCaseResult tcr = new TestCaseResult () ;
108
+ private void parseAndAddZapIssues (Node flaw , TestResults tr ) throws URISyntaxException {
109
+ int cwe = - 1 ;
103
110
Node rule = getNamedChild ("cweid" , flaw );
104
111
if ( rule != null ) {
105
- tcr . setCWE ( cweLookup ( rule .getTextContent () ) );
112
+ cwe = cweLookup ( rule .getTextContent () );
106
113
}
107
114
108
115
String cat = getNamedChild ("alert" , flaw ).getTextContent ();
109
- tcr .setCategory ( cat );
110
116
111
- String conf = getNamedChild ( "confidence" , flaw ).getTextContent ();
112
- tcr .setConfidence ( Integer .parseInt ( conf ) );
117
+ int conf = Integer .parseInt (getNamedChild ( "confidence" , flaw ).getTextContent ());
118
+
119
+ Node instances = getNamedChild ("instances" , flaw );
120
+ if (instances == null ) {
121
+ addIssue (flaw , tr , cwe , cat , conf );
122
+ return ;
123
+ }
124
+
125
+ List <Node > instanceList = getNamedChildren ("instance" , instances );
126
+ for (Node instance : instanceList ) {
127
+ addIssue (instance , tr , cwe , cat , conf );
128
+ }
129
+ }
130
+
131
+ private void addIssue (Node alertData , TestResults tr , int cwe , String category , int confidence ) throws URISyntaxException {
132
+ int testNumber = extractTestNumber (getNamedChild ("uri" , alertData ).getTextContent ());
133
+ if (testNumber != -1 ) {
134
+ TestCaseResult tcr = createTestCaseResult (cwe , category , confidence , testNumber );
135
+ // System.out.println( tcr.getNumber() + " " + tcr.getName() + " -> " + tcr.getCWE() + "\t" + tcr.getEvidence() );
136
+ tr .put (tcr );
137
+ }
138
+ }
113
139
114
- tcr .setEvidence ( cat );
140
+ private static int extractTestNumber (String uri ) throws URISyntaxException {
141
+ // Remove the query and fragment from the URI because some of alert URIs (e.g. generated by DOM XSS) might be malformed
142
+ // (characters that should be escaped are not) which leads to exceptions when parsed by java.net.URI.
143
+ URI url = new URI (removeQueryAndFragment (uri ));
115
144
116
- String uri = getNamedChild ( "uri" , flaw ).getTextContent ();
117
- URI url = new URI ( uri );
118
145
String testfile = url .getPath ();
119
146
testfile = testfile .substring ( testfile .lastIndexOf ('/' ) +1 );
120
147
@@ -124,15 +151,38 @@ private TestCaseResult parseZapIssue(Node flaw) throws URISyntaxException {
124
151
testno = testno .substring (0 , testno .length () -5 );
125
152
}
126
153
try {
127
- tcr .setNumber ( Integer .parseInt ( testno ) );
128
- return tcr ;
154
+ return Integer .parseInt ( testno );
129
155
} catch ( NumberFormatException e ) {
130
156
System .out .println ( "> Parse error " + testfile + ":: " + testno );
131
157
}
132
158
}
133
- return null ;
159
+ return - 1 ;
134
160
}
135
161
162
+ private static String removeQueryAndFragment (String uri ) {
163
+ String strippedUri = uri ;
164
+ int idx = strippedUri .indexOf ('?' );
165
+ if (idx != -1 ) {
166
+ strippedUri = strippedUri .substring (0 , idx );
167
+ }
168
+ idx = strippedUri .indexOf ('#' );
169
+ if (idx != -1 ) {
170
+ strippedUri = strippedUri .substring (0 , idx );
171
+ }
172
+ return strippedUri ;
173
+ }
174
+
175
+ private static TestCaseResult createTestCaseResult (int cwe , String category , int confidence , int testNumber ) {
176
+ TestCaseResult tcr = new TestCaseResult ();
177
+ if (cwe != -1 ) {
178
+ tcr .setCWE (cwe );
179
+ }
180
+ tcr .setCategory (category );
181
+ tcr .setEvidence (category );
182
+ tcr .setConfidence (confidence );
183
+ tcr .setNumber (testNumber );
184
+ return tcr ;
185
+ }
136
186
137
187
private int cweLookup (String orig ) {
138
188
0 commit comments