|
29 | 29 | package org.owasp.html; |
30 | 30 |
|
31 | 31 | import com.google.common.annotations.VisibleForTesting; |
| 32 | +import com.google.common.collect.ImmutableSet; |
| 33 | + |
32 | 34 | import java.io.Closeable; |
33 | 35 | import java.io.Flushable; |
34 | 36 | import java.io.IOException; |
| 37 | +import java.util.Collections; |
35 | 38 | import java.util.Iterator; |
36 | 39 | import java.util.List; |
| 40 | +import java.util.Set; |
37 | 41 | import javax.annotation.WillCloseWhenClosed; |
38 | 42 | import javax.annotation.concurrent.NotThreadSafe; |
39 | 43 |
|
@@ -250,7 +254,26 @@ private final void writeCloseTag(String uncanonElementName) |
250 | 254 | Encoding.stripBannedCodeunits(cdataContent); |
251 | 255 | int problemIndex = checkHtmlCdataCloseable(lastTagOpened, cdataContent); |
252 | 256 | if (problemIndex == -1) { |
253 | | - output.append(cdataContent); |
| 257 | + String prefix = ""; |
| 258 | + String suffix = ""; |
| 259 | + Set<String> bannedSubstrings = Collections.emptySet(); |
| 260 | + if ("style".equals(elementName)) { |
| 261 | + prefix = "/*<![CDATA[<!--*/\n"; |
| 262 | + suffix = "\n/*-->]]>*/"; |
| 263 | + bannedSubstrings = BANNED_IN_STYLE_ELEMENTS; |
| 264 | + } |
| 265 | + |
| 266 | + for (String bannedSubstring : bannedSubstrings) { |
| 267 | + if (cdataContent.indexOf(bannedSubstring) >= 0) { |
| 268 | + cdataContent.setLength(0); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + if (cdataContent.length() != 0) { |
| 273 | + output.append(prefix); |
| 274 | + output.append(cdataContent); |
| 275 | + output.append(suffix); |
| 276 | + } |
254 | 277 | } else { |
255 | 278 | error( |
256 | 279 | "Invalid CDATA text content", |
@@ -434,4 +457,8 @@ public void close() throws IOException { |
434 | 457 | private static boolean isTagEnd(char ch) { |
435 | 458 | return ch < 63 && 0 != (TAG_ENDS & (1L << ch)); |
436 | 459 | } |
| 460 | + |
| 461 | + private static Set<String> BANNED_IN_STYLE_ELEMENTS = ImmutableSet.of( |
| 462 | + "<![CDATA[", "]]>", "<!--", "-->" |
| 463 | + ); |
437 | 464 | } |
0 commit comments