Ignore:
Timestamp:
2016-06-16T19:10:53+02:00 (8 years ago)
Author:
Don-vip
Message:

findbugs security - XML Parsing Vulnerable to XXE - enable FEATURE_SECURE_PROCESSING for DOM builders

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r10315 r10404  
    6464
    6565import javax.xml.XMLConstants;
     66import javax.xml.parsers.DocumentBuilder;
     67import javax.xml.parsers.DocumentBuilderFactory;
    6668import javax.xml.parsers.ParserConfigurationException;
    6769import javax.xml.parsers.SAXParser;
     
    7072import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
    7173import org.openstreetmap.josm.Main;
     74import org.w3c.dom.Document;
    7275import org.xml.sax.InputSource;
    7376import org.xml.sax.SAXException;
     
    14081411        }
    14091412        return null;
     1413    }
     1414
     1415    /**
     1416     * Returns a new secure DOM builder, supporting XML namespaces.
     1417     * @return a new secure DOM builder, supporting XML namespaces
     1418     * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
     1419     * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
     1420     * @since 10404
     1421     */
     1422    public static DocumentBuilder newSafeDOMBuilder() throws ParserConfigurationException {
     1423        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
     1424        builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
     1425        builderFactory.setNamespaceAware(true);
     1426        builderFactory.setValidating(false);
     1427        return builderFactory.newDocumentBuilder();
     1428    }
     1429
     1430    /**
     1431     * Parse the content given {@link InputStream} as XML.
     1432     * This method uses a secure DOM builder, supporting XML namespaces.
     1433     *
     1434     * @param is The InputStream containing the content to be parsed.
     1435     * @return the result DOM document
     1436     * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration.
     1437     * @throws IOException if any IO errors occur.
     1438     * @throws SAXException for SAX errors.
     1439     * @since 10404
     1440     */
     1441    public static Document parseSafeDOM(InputStream is) throws ParserConfigurationException, IOException, SAXException {
     1442        long start = System.currentTimeMillis();
     1443        if (Main.isDebugEnabled()) {
     1444            Main.debug("Starting DOM parsing of " + is);
     1445        }
     1446        Document result = newSafeDOMBuilder().parse(is);
     1447        if (Main.isDebugEnabled()) {
     1448            Main.debug("DOM parsing done in " + getDurationString(System.currentTimeMillis() - start));
     1449        }
     1450        return result;
    14101451    }
    14111452
Note: See TracChangeset for help on using the changeset viewer.