Skip to content

Commit cd8a328

Browse files
committed
improve CDATA Encoder to not emit intermediate characters between adjacent CDATA sections
1 parent d6d3e11 commit cd8a328

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

core/src/main/java/org/owasp/encoder/CDATAEncoder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@
4040
* CDATAEncoder -- encoder for CDATA sections. CDATA sections are generally good for including large blocks of text that contain
4141
* characters that normally require encoding (ampersand, quotes, less-than, etc...). The CDATA context however still does not
4242
* allow invalid characters, and can be closed by the sequence "]]>". This encoder removes invalid XML characters, and encodes
43-
* "]]>" (to "]]>]]<![CDATA[>"). The result is that the data integrity is maintained, but the code receiving the output will
44-
* have to handle multiple CDATA events with character events between. As an alternate approach, the caller could pre-encode "]]>"
45-
* to something of their choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then use this encoder to remove any invalid XML
46-
* characters.
43+
* "]]>" (to "]]]]><![CDATA[>"). The result is that the data integrity is maintained, but the code receiving the output will
44+
* have to handle multiple CDATA events. As an alternate approach, the caller could pre-encode "]]>" to something of their
45+
* choosing (e.g. data.replaceAll("\\]\\]>", "]] >")), then use this encoder to remove any invalid XML characters.
4746
*
4847
* @author Jeff Ichnowski
4948
*/
@@ -53,10 +52,10 @@ class CDATAEncoder extends Encoder {
5352
* The encoding of @{code "]]>"}.
5453
*/
5554
private static final char[] CDATA_END_ENCODED
56-
= "]]>]]<![CDATA[>".toCharArray();
55+
= "]]]]><![CDATA[>".toCharArray();
5756

5857
/**
59-
* Length of {@code "]]>]]<![CDATA[>"}.
58+
* Length of {@code "]]]]><![CDATA[>"}.
6059
*/
6160
private static final int CDATA_END_ENCODED_LENGTH = 15;
6261

@@ -69,8 +68,8 @@ class CDATAEncoder extends Encoder {
6968
protected int maxEncodedLength(int n) {
7069
// "]" becomes "]" (1 -> 1)
7170
// "]]" becomes "]]" (2 -> 2)
72-
// "]]>" becomes "]]>]]<![CDATA[>" (3 -> 15)
73-
// "]]>]" becomes "]]>]]<![CDATA[>]" (3 -> 15 + 1 -> 1)
71+
// "]]>" becomes "]]]]><![CDATA[>" (3 -> 15)
72+
// "]]>]" becomes "]]]]><![CDATA[>]" (3 -> 15 + 1 -> 1)
7473
// ...
7574

7675
int worstCase = n / CDATA_END_LENGTH;

core/src/test/java/org/owasp/encoder/CDATAEncoderTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
public class CDATAEncoderTest extends TestCase {
4646
public static Test suite() {
4747
return new EncoderTestSuiteBuilder(CDATAEncoderTest.class, new CDATAEncoder(), "-safe-", "-]]>-")
48-
.encode("]]>]]<![CDATA[>", "]]>")
48+
.encode("]]]]><![CDATA[>", "]]>")
4949
.encode("]", "]")
5050
.encode("]]", "]]")
51-
.encode("]]>]]<![CDATA[>]", "]]>]")
52-
.encode("]]>]]<![CDATA[>]>", "]]>]>")
53-
.encode("]]>]]<![CDATA[>>", "]]>>")
51+
.encode("]]]]><![CDATA[>]", "]]>]")
52+
.encode("]]]]><![CDATA[>]>", "]]>]>")
53+
.encode("]]]]><![CDATA[>>", "]]>>")
5454
.encode("]]]]]", "]]]]]")
5555
.encode("<\"&\'>", "<\"&\'>") // valid in CDATA, not in XML
5656

0 commit comments

Comments
 (0)