Ticket #19286: #19286_--_External_entities_security_vulnerability_(e_g__tagging_presets).patch

File #19286_--_External_entities_security_vulnerability_(e_g__tagging_presets).patch, 7.2 KB (added by hiddewie, 4 years ago)
  • src/org/openstreetmap/josm/tools/XmlUtils.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    3535 */
    3636public final class XmlUtils {
    3737
     38    private static final String FEATURE_DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl";
     39
    3840    private XmlUtils() {
    3941        // Hide default constructor for utils classes
    4042    }
     
    100102    public static SAXParser newSafeSAXParser() throws ParserConfigurationException, SAXException {
    101103        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    102104        parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
     105        parserFactory.setFeature(FEATURE_DISALLOW_DOCTYPE_DECL, true);
    103106        parserFactory.setNamespaceAware(true);
    104107        return parserFactory.newSAXParser();
    105108    }
  • test/data/dom_external_entity.xml

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?xml version="1.0" encoding="UTF-8"?>
     2<!DOCTYPE updateProfile [
     3  <!ENTITY file SYSTEM "file:///etc/passwd">
     4]>
     5<root>
     6    &file;
     7</root>
  • test/data/preset_external_entity.xml

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1<?xml version="1.0" encoding="UTF-8"?>
     2<!DOCTYPE updateProfile [
     3  <!ENTITY file SYSTEM "file:///etc/passwd">
     4]>
     5<presets xmlns="http://josm.openstreetmap.de/tagging-preset-1.0">
     6    &file;
     7</presets>
  • test/unit/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetReaderTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
    55import static org.junit.Assert.assertEquals;
    66import static org.junit.Assert.assertThat;
    77import static org.junit.Assert.assertTrue;
     8import static org.junit.Assert.fail;
    89
    910import java.io.IOException;
    1011import java.util.Collection;
     
    6566        assertEquals("[A1, A2, A3, B1, B2, B3, C1, C2, C3]", keys.toString());
    6667    }
    6768
     69    /**
     70     * Test external entity resolving.
     71     * See #19286
     72     */
     73    @Test
     74    public void testExternalEntityResolving() throws IOException {
     75        try {
     76            TaggingPresetReader.readAll(TestUtils.getTestDataRoot() + "preset_external_entity.xml", true);
     77            fail("Reading a file with external entities should throw an SAXParseException!");
     78        } catch (SAXException e) {
     79            Assert.assertEquals("DOCTYPE is disallowed when the feature \"http://apache.org/xml/features/disallow-doctype-decl\" set to true.", e.getMessage());
     80        }
     81    }
     82
    6883    /**
    6984     * Validate internal presets
    7085     * See #9027
  • test/unit/org/openstreetmap/josm/tools/XmlUtilsTest.java

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.tools;
     3
     4import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     5import org.junit.Rule;
     6import org.junit.Test;
     7import org.openstreetmap.josm.TestUtils;
     8import org.openstreetmap.josm.testutils.JOSMTestRules;
     9import org.xml.sax.InputSource;
     10import org.xml.sax.SAXException;
     11import org.xml.sax.helpers.DefaultHandler;
     12
     13import javax.xml.parsers.ParserConfigurationException;
     14import javax.xml.transform.Transformer;
     15import javax.xml.transform.TransformerException;
     16import javax.xml.transform.stream.StreamResult;
     17import javax.xml.transform.stream.StreamSource;
     18import java.io.FileInputStream;
     19import java.io.IOException;
     20import java.io.StringWriter;
     21
     22import static org.junit.Assert.assertEquals;
     23import static org.junit.Assert.assertNotNull;
     24import static org.junit.Assert.fail;
     25
     26/**
     27 * Unit tests of {@link XmlUtils} class.
     28 */
     29public class XmlUtilsTest {
     30
     31    /**
     32     * Use default, basic test rules.
     33     */
     34    @Rule
     35    @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
     36    public JOSMTestRules rules = new JOSMTestRules();
     37
     38    @Test
     39    public void testExternalEntitiesParsingDom() throws IOException, ParserConfigurationException {
     40        try {
     41            final String source = TestUtils.getTestDataRoot() + "dom_external_entity.xml";
     42            XmlUtils.parseSafeDOM(new FileInputStream(source));
     43            fail("Parsing a document with external entities should not be allowed.");
     44        } catch (SAXException e) {
     45            assertEquals("External Entity: Failed to read external document 'passwd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.", e.getMessage());
     46        }
     47    }
     48
     49    @Test
     50    public void testExternalEntitiesSaxParser() throws IOException, ParserConfigurationException {
     51        try {
     52            final String source = TestUtils.getTestDataRoot() + "dom_external_entity.xml";
     53            final DefaultHandler handler = new DefaultHandler();
     54            XmlUtils.parseSafeSAX(new InputSource(new FileInputStream(source)), handler);
     55            fail("Parsing a document with external entities should not be allowed.");
     56        } catch (SAXException e) {
     57            assertEquals("External Entity: Failed to read external document 'passwd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.", e.getMessage());
     58        }
     59    }
     60
     61    @Test
     62    public void testExternalEntitiesTransformer() throws IOException {
     63        try {
     64            final String source = TestUtils.getTestDataRoot() + "dom_external_entity.xml";
     65            final Transformer transformer = XmlUtils.newSafeTransformerFactory().newTransformer();
     66            transformer.transform(new StreamSource(new FileInputStream(source)), new StreamResult(new StringWriter()));
     67            fail("Parsing a document with external entities should not be allowed.");
     68        } catch (TransformerException e) {
     69            assertNotNull(e.getCause());
     70            assertEquals("External Entity: Failed to read external document 'passwd', because 'file' access is not allowed due to restriction set by the accessExternalDTD property.", e.getCause().getMessage());
     71        }
     72    }
     73}
     74 No newline at end of file