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
38 public final class XmlUtils {
39
40
41
42
43
44 public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
45
46
47
48
49 public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
50
51
52
53
54 public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
55
56
57
58
59 private XmlUtils() {
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public static SAXParser buildSecureSaxParser(InputStream... schemaStream) throws ParserConfigurationException,
79 SAXNotRecognizedException, SAXNotSupportedException, SAXException {
80 final SAXParserFactory factory = SAXParserFactory.newInstance();
81 factory.setNamespaceAware(true);
82 factory.setValidating(true);
83 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
84 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
85 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
86 factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
87 factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
88 factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
89
90 String accessExternalSchema = System.getProperty("javax.xml.accessExternalSchema");
91 if (accessExternalSchema == null) {
92 accessExternalSchema = "file, https";
93 } else if (!"ALL".equalsIgnoreCase(accessExternalSchema)) {
94 if (!accessExternalSchema.contains("file")) {
95 accessExternalSchema += ", file";
96 }
97 if (!accessExternalSchema.contains("https")) {
98 accessExternalSchema += ", https";
99 }
100 }
101 System.setProperty("javax.xml.accessExternalSchema", accessExternalSchema);
102
103 final SAXParser saxParser = factory.newSAXParser();
104 saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
105 saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaStream);
106 return saxParser;
107 }
108
109
110
111
112
113
114
115
116
117
118 public static boolean parseBoolean(String lexicalXSDBoolean) {
119 final boolean result;
120 switch (lexicalXSDBoolean) {
121 case "true":
122 case "1":
123 result = true;
124 break;
125 case "false":
126 case "0":
127 result = false;
128 break;
129 default:
130 throw new IllegalArgumentException("'" + lexicalXSDBoolean + "' is not a valid xs:boolean value");
131 }
132 return result;
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148 public static SAXParser buildSecureSaxParser() throws ParserConfigurationException,
149 SAXNotRecognizedException, SAXNotSupportedException, SAXException {
150 final SAXParserFactory factory = SAXParserFactory.newInstance();
151 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
152 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
153 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
154 return factory.newSAXParser();
155 }
156
157
158
159
160
161
162
163
164 public static DocumentBuilder buildSecureDocumentBuilder() throws ParserConfigurationException {
165 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
166 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
167 factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
168 factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
169 return factory.newDocumentBuilder();
170 }
171
172
173
174
175
176
177
178 public static String getPrettyParseExceptionInfo(SAXParseException ex) {
179
180 final StringBuilder sb = new StringBuilder();
181
182 if (ex.getSystemId() != null) {
183 sb.append("systemId=").append(ex.getSystemId()).append(", ");
184 }
185 if (ex.getPublicId() != null) {
186 sb.append("publicId=").append(ex.getPublicId()).append(", ");
187 }
188 if (ex.getLineNumber() > 0) {
189 sb.append("Line=").append(ex.getLineNumber());
190 }
191 if (ex.getColumnNumber() > 0) {
192 sb.append(", Column=").append(ex.getColumnNumber());
193 }
194 sb.append(": ").append(ex.getMessage());
195
196 return sb.toString();
197 }
198 }