|
18 | 18 | import static org.junit.Assert.assertNotEquals; |
19 | 19 |
|
20 | 20 | import java.io.IOException; |
| 21 | +import java.io.StringReader; |
21 | 22 | import java.io.UnsupportedEncodingException; |
22 | 23 | import java.net.URI; |
23 | 24 | import java.util.ArrayList; |
24 | 25 | import java.util.Arrays; |
25 | 26 | import java.util.List; |
26 | 27 |
|
| 28 | +import javax.xml.XMLConstants; |
| 29 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 30 | + |
27 | 31 | import org.junit.Ignore; |
28 | 32 | import org.owasp.esapi.ESAPI; |
29 | 33 | import org.owasp.esapi.Encoder; |
|
41 | 45 | import org.owasp.esapi.errors.EncodingException; |
42 | 46 | import org.owasp.esapi.errors.IntrusionException; |
43 | 47 | import org.owasp.esapi.Randomizer; |
| 48 | +import org.w3c.dom.Element; |
| 49 | +import org.xml.sax.InputSource; |
44 | 50 |
|
45 | 51 |
|
46 | 52 | import junit.framework.Test; |
@@ -714,6 +720,63 @@ public void testEncodeForXMLAttributePound() { |
714 | 720 | assertEquals("£", instance.encodeForXMLAttribute("\u00A3")); |
715 | 721 | } |
716 | 722 |
|
| 723 | + /** |
| 724 | + * Mirrors https://github.com/OWASP/owasp-java-encoder/issues/136 (HTML half). |
| 725 | + * An HTML5 parser remaps the numeric reference {@code …} (U+0085, NEL) to |
| 726 | + * U+2026 (HORIZONTAL ELLIPSIS) via the windows-1252 table, so NEL must never be |
| 727 | + * emitted as that reference. ESAPI replaces every C1 control, NEL included, |
| 728 | + * with U+FFFD. |
| 729 | + */ |
| 730 | + public void testEncodeForHTMLNELIsNotEmittedAsWindows1252Reference() { |
| 731 | + System.out.println("encodeForHTMLNELIsNotEmittedAsWindows1252Reference"); |
| 732 | + Encoder instance = ESAPI.encoder(); |
| 733 | + String input = "x\u0085y"; |
| 734 | + |
| 735 | + String attr = instance.encodeForHTMLAttribute(input); |
| 736 | + assertEquals("x�y", attr); |
| 737 | + assertFalse(attr.contains("…")); |
| 738 | + assertFalse(attr.contains("…")); |
| 739 | + |
| 740 | + String content = instance.encodeForHTML(input); |
| 741 | + assertEquals("x�y", content); |
| 742 | + assertFalse(content.contains("…")); |
| 743 | + |
| 744 | + // NEL is treated the same as its C1 neighbours, not singled out. |
| 745 | + assertEquals(instance.encodeForHTMLAttribute("x\u0084y"), attr); |
| 746 | + assertEquals(instance.encodeForHTMLAttribute("x\u0086y"), attr); |
| 747 | + } |
| 748 | + |
| 749 | + /** |
| 750 | + * Mirrors https://github.com/OWASP/owasp-java-encoder/issues/136 (XML 1.1 half). |
| 751 | + * XML 1.1 section 2.11 normalises raw U+0085 (NEL) and U+2028 (LINE SEPARATOR) |
| 752 | + * to LF on input, and attribute-value normalisation then turns that LF into a |
| 753 | + * space. Only the character references survive a parse, so both must be |
| 754 | + * encoded, and the output must round-trip through a real XML 1.1 parser. |
| 755 | + */ |
| 756 | + public void testEncodeForXMLNELAndLineSeparatorRoundTripInXml11() throws Exception { |
| 757 | + System.out.println("encodeForXMLNELAndLineSeparatorRoundTripInXml11"); |
| 758 | + Encoder instance = ESAPI.encoder(); |
| 759 | + |
| 760 | + assertEquals("x…y", instance.encodeForXML("x\u0085y")); |
| 761 | + assertEquals("x
y", instance.encodeForXML("x\u2028y")); |
| 762 | + assertEquals("x…y", instance.encodeForXMLAttribute("x\u0085y")); |
| 763 | + assertEquals("x
y", instance.encodeForXMLAttribute("x\u2028y")); |
| 764 | + |
| 765 | + String input = "a\u0085b\u2028c"; |
| 766 | + String xml = "<?xml version=\"1.1\"?><r a=\"" + instance.encodeForXMLAttribute(input) + "\">" |
| 767 | + + instance.encodeForXML(input) + "</r>"; |
| 768 | + |
| 769 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 770 | + dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); |
| 771 | + dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 772 | + Element root = dbf.newDocumentBuilder() |
| 773 | + .parse(new InputSource(new StringReader(xml))) |
| 774 | + .getDocumentElement(); |
| 775 | + |
| 776 | + assertEquals(input, root.getTextContent()); |
| 777 | + assertEquals(input, root.getAttribute("a")); |
| 778 | + } |
| 779 | + |
717 | 780 | /** |
718 | 781 | * Test of encodeForURL method, of class org.owasp.esapi.Encoder. |
719 | 782 | * |
|
0 commit comments