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

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

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 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
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportSender.java

    r10067 r10404  
    1818import javax.swing.JPanel;
    1919import javax.swing.SwingUtilities;
    20 import javax.xml.parsers.DocumentBuilder;
    21 import javax.xml.parsers.DocumentBuilderFactory;
    2220import javax.xml.parsers.ParserConfigurationException;
    2321import javax.xml.xpath.XPath;
     
    10098
    10199            try (InputStream in = connection.getContent()) {
    102                 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    103                 Document document = builder.parse(in);
    104                 return retrieveDebugToken(document);
     100                return retrieveDebugToken(Utils.parseSafeDOM(in));
    105101            }
    106102        } catch (IOException | SAXException | ParserConfigurationException | XPathExpressionException t) {
Note: See TracChangeset for help on using the changeset viewer.