77import java .io .IOException ;
88import java .io .PrintWriter ;
99import java .io .Writer ;
10+ import java .net .URLEncoder ;
1011import java .util .Iterator ;
1112import java .util .List ;
1213import java .util .Optional ;
@@ -68,6 +69,10 @@ public String defaultFileExtension() {
6869 return "html" ;
6970 }
7071
72+ private static String escape (String s ) {
73+ return StringEscapeUtils .escapeHtml4 (s );
74+ }
75+
7176 /**
7277 * Write the body of the main body of the HTML content.
7378 */
@@ -135,16 +140,16 @@ private void glomRuleViolations(Writer writer, Iterator<RuleViolation> violation
135140 buf .append ("> " ).append (System .lineSeparator ());
136141 buf .append ("<td align=\" center\" >" ).append (violationCount ).append ("</td>" ).append (System .lineSeparator ());
137142 buf .append ("<td width=\" *%\" >" )
138- .append (renderFileName (rv .getFileId (), rv .getBeginLine ()))
143+ .append (renderFileNameEscaped (rv .getFileId (), rv .getBeginLine ()))
139144 .append ("</td>" )
140145 .append (System .lineSeparator ());
141146 buf .append ("<td align=\" center\" width=\" 5%\" >" ).append (rv .getBeginLine ()).append ("</td>" ).append (System .lineSeparator ());
142147
143- String d = StringEscapeUtils . escapeHtml4 (rv .getDescription ());
148+ String d = escape (rv .getDescription ());
144149
145150 String infoUrl = rv .getRule ().getExternalInfoUrl ();
146151 if (StringUtils .isNotBlank (infoUrl )) {
147- d = "<a href=\" " + infoUrl + "\" >" + d + "</a>" ;
152+ d = "<a href=\" " + URLEncoder . encode ( infoUrl , "UTF-8" ) + "\" >" + d + "</a>" ;
148153 }
149154 buf .append ("<td width=\" *\" >" )
150155 .append (d )
@@ -157,13 +162,13 @@ private void glomRuleViolations(Writer writer, Iterator<RuleViolation> violation
157162 }
158163 }
159164
160- private String renderFileName (FileId fileId , int beginLine ) {
161- return maybeWrap (StringEscapeUtils . escapeHtml4 (determineFileName (fileId )),
165+ private String renderFileNameEscaped (FileId fileId , int beginLine ) {
166+ return maybeWrap (escape (determineFileName (fileId )),
162167 linePrefix == null || beginLine < 0 ? "" : linePrefix + beginLine );
163168 }
164169
165- private String renderRuleName (Rule rule ) {
166- String name = rule .getName ();
170+ private String renderRuleNameEscaped (Rule rule ) {
171+ String name = escape ( rule .getName () );
167172 String infoUrl = rule .getExternalInfoUrl ();
168173 if (StringUtils .isNotBlank (infoUrl )) {
169174 return "<a href=\" " + infoUrl + "\" >" + name + "</a>" ;
@@ -192,8 +197,8 @@ private void glomProcessingErrors(PrintWriter writer, List<Report.ProcessingErro
192197 }
193198 colorize = !colorize ;
194199 buf .append ("> " ).append (System .lineSeparator ());
195- buf .append ("<td>" ).append (renderFileName (pe .getFileId (), -1 )).append ("</td>" ).append (System .lineSeparator ());
196- buf .append ("<td><pre>" ).append (pe .getDetail ()).append ("</pre></td>" ).append (System .lineSeparator ());
200+ buf .append ("<td>" ).append (renderFileNameEscaped (pe .getFileId (), -1 )).append ("</td>" ).append (System .lineSeparator ());
201+ buf .append ("<td><pre>" ).append (escape ( pe .getDetail () )).append ("</pre></td>" ).append (System .lineSeparator ());
197202 buf .append ("</tr>" ).append (System .lineSeparator ());
198203 writer .write (buf .toString ());
199204 }
@@ -221,11 +226,12 @@ private void glomSuppressions(PrintWriter writer, List<Report.SuppressedViolatio
221226 colorize = !colorize ;
222227 buf .append ("> " ).append (System .lineSeparator ());
223228 RuleViolation rv = sv .getRuleViolation ();
224- buf .append ("<td align=\" left\" >" ).append (renderFileName (rv .getFileId (), rv .getBeginLine ())).append ("</td>" ).append (System .lineSeparator ());
229+ String userMessage = Optional .ofNullable (sv .getUserMessage ()).orElse ("" );
230+ buf .append ("<td align=\" left\" >" ).append (renderFileNameEscaped (rv .getFileId (), rv .getBeginLine ())).append ("</td>" ).append (System .lineSeparator ());
225231 buf .append ("<td align=\" center\" >" ).append (rv .getBeginLine ()).append ("</td>" ).append (System .lineSeparator ());
226- buf .append ("<td align=\" center\" >" ).append (renderRuleName (rv .getRule ())).append ("</td>" ).append (System .lineSeparator ());
227- buf .append ("<td align=\" center\" >" ).append (sv .getSuppressor ().getId ()).append ("</td>" ).append (System .lineSeparator ());
228- buf .append ("<td align=\" center\" >" ).append (sv . getUserMessage () == null ? "" : sv . getUserMessage ( )).append ("</td>" ).append (System .lineSeparator ());
232+ buf .append ("<td align=\" center\" >" ).append (renderRuleNameEscaped (rv .getRule ())).append ("</td>" ).append (System .lineSeparator ());
233+ buf .append ("<td align=\" center\" >" ).append (escape ( sv .getSuppressor ().getId () )).append ("</td>" ).append (System .lineSeparator ());
234+ buf .append ("<td align=\" center\" >" ).append (escape ( userMessage )).append ("</td>" ).append (System .lineSeparator ());
229235 buf .append ("</tr>" ).append (System .lineSeparator ());
230236 writer .write (buf .toString ());
231237 }
@@ -252,24 +258,24 @@ private void glomConfigurationErrors(final PrintWriter writer, final List<Config
252258 }
253259 colorize = !colorize ;
254260 buf .append ("> " ).append (System .lineSeparator ());
255- buf .append ("<td>" ).append (renderRuleName (ce .rule ())).append ("</td>" ).append (System .lineSeparator ());
256- buf .append ("<td>" ).append (ce .issue ()).append ("</td>" ).append (System .lineSeparator ());
261+ buf .append ("<td>" ).append (renderRuleNameEscaped (ce .rule ())).append ("</td>" ).append (System .lineSeparator ());
262+ buf .append ("<td>" ).append (escape ( ce .issue () )).append ("</td>" ).append (System .lineSeparator ());
257263 buf .append ("</tr>" ).append (System .lineSeparator ());
258264 writer .write (buf .toString ());
259265 }
260266 writer .write ("</table>" );
261267 }
262268
263- private String maybeWrap (String filename , String line ) {
269+ private String maybeWrap (String filenameEscaped , String line ) {
264270 if (StringUtils .isBlank (linkPrefix )) {
265- return filename ;
271+ return filenameEscaped ;
266272 }
267- String newFileName = filename .replace ('\\' , '/' );
273+ String newFileName = filenameEscaped .replace ('\\' , '/' );
268274
269275 if (replaceHtmlExtension ) {
270- int index = filename .lastIndexOf ('.' );
276+ int index = filenameEscaped .lastIndexOf ('.' );
271277 if (index >= 0 ) {
272- newFileName = filename .substring (0 , index );
278+ newFileName = filenameEscaped .substring (0 , index );
273279 }
274280 }
275281
0 commit comments