1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import java.io.InputStream;
21 import javax.xml.XMLConstants;
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.parsers.SAXParser;
26 import javax.xml.parsers.SAXParserFactory;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.SAXNotRecognizedException;
29 import org.xml.sax.SAXNotSupportedException;
30 import org.xml.sax.SAXParseException;
31
32
33
34
35
36
37 public final class XmlUtils {
38
39
40
41
42
43 public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
44
45
46
47
48 public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
49
50
51
52
53 public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
54
55
56
57
58 private XmlUtils() {
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 public static SAXParser buildSecureSaxParser(InputStream schemaStream) throws ParserConfigurationException,
75 SAXNotRecognizedException, SAXNotSupportedException, SAXException {
76 final SAXParserFactory factory = SAXParserFactory.newInstance();
77 factory.setNamespaceAware(true);
78 factory.setValidating(true);
79 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
80 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
81
82
83
84 final SAXParser saxParser = factory.newSAXParser();
85 saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
86 saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaStream);
87 return saxParser;
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public static SAXParser buildSecureSaxParser() throws ParserConfigurationException,
103 SAXNotRecognizedException, SAXNotSupportedException, SAXException {
104 final SAXParserFactory factory = SAXParserFactory.newInstance();
105 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
106 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
107 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
108 return factory.newSAXParser();
109 }
110
111
112
113
114
115
116
117
118 public static DocumentBuilder buildSecureDocumentBuilder() throws ParserConfigurationException {
119 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
120 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
121 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
122 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
123 final DocumentBuilder db = factory.newDocumentBuilder();
124 return db;
125 }
126
127
128
129
130
131
132
133 public static String getPrettyParseExceptionInfo(SAXParseException ex) {
134
135 final StringBuilder sb = new StringBuilder();
136
137 if (ex.getSystemId() != null) {
138 sb.append("systemId=").append(ex.getSystemId()).append(", ");
139 }
140 if (ex.getPublicId() != null) {
141 sb.append("publicId=").append(ex.getPublicId()).append(", ");
142 }
143 if (ex.getLineNumber() > 0) {
144 sb.append("Line=").append(ex.getLineNumber());
145 }
146 if (ex.getColumnNumber() > 0) {
147 sb.append(", Column=").append(ex.getColumnNumber());
148 }
149 sb.append(": ").append(ex.getMessage());
150
151 return sb.toString();
152 }
153 }