author | thartmann |
Fri, 24 Aug 2018 08:17:23 +0200 | |
changeset 51514 | 1e332d63bd96 |
parent 48947 | b4dd09b5d6d8 |
permissions | -rw-r--r-- |
33542 | 1 |
/* |
48947 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
33542 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
package jdk.xml.internal; |
|
26 |
||
27 |
import java.io.File; |
|
28 |
import java.io.FileInputStream; |
|
29 |
import java.io.FileNotFoundException; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
47312 | 32 |
import java.net.URL; |
33542 | 33 |
import java.security.AccessController; |
47312 | 34 |
import java.security.CodeSource; |
33542 | 35 |
import java.security.PrivilegedAction; |
36 |
import java.security.PrivilegedActionException; |
|
37 |
import java.security.PrivilegedExceptionAction; |
|
38 |
import java.text.MessageFormat; |
|
39 |
import java.util.Locale; |
|
46174 | 40 |
import java.util.MissingResourceException; |
33542 | 41 |
import java.util.Properties; |
42 |
import java.util.ResourceBundle; |
|
43 |
||
44 |
/** |
|
45 |
* This class contains utility methods for reading resources in the JAXP packages |
|
46 |
*/ |
|
47 |
public class SecuritySupport { |
|
48944
25aa8b9f1dae
8198645: Use System.lineSeparator() instead of getProperty("line.separator")
rriggs
parents:
47312
diff
changeset
|
48 |
public final static String NEWLINE = System.lineSeparator(); |
46174 | 49 |
|
33542 | 50 |
/** |
51 |
* Cache for properties in java.home/conf/jaxp.properties |
|
52 |
*/ |
|
53 |
static final Properties cacheProps = new Properties(); |
|
54 |
||
55 |
/** |
|
56 |
* Flag indicating whether java.home/conf/jaxp.properties has been read |
|
57 |
*/ |
|
58 |
static volatile boolean firstTime = true; |
|
59 |
||
60 |
private SecuritySupport() {} |
|
61 |
||
62 |
public static String getErrorMessage(Locale locale, String bundle, String key, |
|
63 |
Object[] arguments) { |
|
64 |
ResourceBundle rb; |
|
65 |
if (locale != null) { |
|
66 |
rb = ResourceBundle.getBundle(bundle,locale); |
|
67 |
} else { |
|
68 |
rb = ResourceBundle.getBundle(bundle); |
|
69 |
} |
|
70 |
||
71 |
String msg = rb.getString(key); |
|
72 |
if (arguments != null) { |
|
73 |
msg = MessageFormat.format(msg, arguments); |
|
74 |
} |
|
75 |
return msg; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
46174 | 79 |
* Reads a system property with privilege |
33542 | 80 |
* |
81 |
* @param propName the name of the property |
|
82 |
* @return the value of the property |
|
83 |
*/ |
|
84 |
public static String getSystemProperty(final String propName) { |
|
85 |
return |
|
86 |
AccessController.doPrivileged( |
|
47312 | 87 |
(PrivilegedAction<String>) () -> System.getProperty(propName)); |
33542 | 88 |
} |
89 |
||
90 |
/** |
|
46174 | 91 |
* Reads a system property with privilege |
92 |
* |
|
93 |
* @param propName the name of the property |
|
94 |
* @return the value of the property |
|
95 |
*/ |
|
96 |
public static String getSystemProperty(final String propName, String defValue) { |
|
97 |
String value = getSystemProperty(propName); |
|
98 |
if (value == null) { |
|
99 |
return defValue; |
|
100 |
} |
|
101 |
return value; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Reads a system property with specified type. |
|
39907 | 106 |
* |
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
107 |
* @param <T> the type of the property value |
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
108 |
* @param type the type of the property value |
39907 | 109 |
* @param propName the name of the property |
110 |
* @param defValue the default value |
|
46174 | 111 |
* @return the value of the property, or the default value if no system |
112 |
* property is found |
|
113 |
*/ |
|
114 |
public static <T> T getSystemProperty(Class<T> type, String propName, String defValue) { |
|
115 |
String value = getSystemProperty(propName); |
|
116 |
if (value == null) { |
|
117 |
value = defValue; |
|
118 |
} |
|
119 |
if (Integer.class.isAssignableFrom(type)) { |
|
120 |
return type.cast(Integer.parseInt(value)); |
|
121 |
} else if (Boolean.class.isAssignableFrom(type)) { |
|
122 |
return type.cast(Boolean.parseBoolean(value)); |
|
123 |
} |
|
124 |
return type.cast(value); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Reads JAXP system property in this order: system property, |
|
129 |
* $java.home/conf/jaxp.properties if the system property is not specified |
|
130 |
* |
|
131 |
* @param <T> the type of the property value |
|
132 |
* @param type the type of the property value |
|
133 |
* @param propName the name of the property |
|
134 |
* @param defValue the default value |
|
135 |
* @return the value of the property, or the default value if no system |
|
39907 | 136 |
* property is found |
137 |
*/ |
|
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
138 |
public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) { |
39907 | 139 |
String value = getJAXPSystemProperty(propName); |
140 |
if (value == null) { |
|
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
141 |
value = defValue; |
39907 | 142 |
} |
42247
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
143 |
if (Integer.class.isAssignableFrom(type)) { |
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
144 |
return type.cast(Integer.parseInt(value)); |
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
145 |
} else if (Boolean.class.isAssignableFrom(type)) { |
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
146 |
return type.cast(Boolean.parseBoolean(value)); |
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
147 |
} |
52fafb950d5a
8158619: Very large CDATA section in XML document causes OOME
joehw
parents:
39907
diff
changeset
|
148 |
return type.cast(value); |
39907 | 149 |
} |
150 |
||
151 |
/** |
|
33542 | 152 |
* Reads JAXP system property in this order: system property, |
153 |
* $java.home/conf/jaxp.properties if the system property is not specified |
|
154 |
* |
|
155 |
* @param propName the name of the property |
|
156 |
* @return the value of the property |
|
157 |
*/ |
|
158 |
public static String getJAXPSystemProperty(String propName) { |
|
159 |
String value = getSystemProperty(propName); |
|
160 |
if (value == null) { |
|
161 |
value = readJAXPProperty(propName); |
|
162 |
} |
|
163 |
return value; |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Reads the specified property from $java.home/conf/jaxp.properties |
|
168 |
* |
|
169 |
* @param propName the name of the property |
|
170 |
* @return the value of the property |
|
171 |
*/ |
|
172 |
public static String readJAXPProperty(String propName) { |
|
173 |
String value = null; |
|
174 |
InputStream is = null; |
|
175 |
try { |
|
176 |
if (firstTime) { |
|
177 |
synchronized (cacheProps) { |
|
178 |
if (firstTime) { |
|
179 |
String configFile = getSystemProperty("java.home") + File.separator |
|
180 |
+ "conf" + File.separator + "jaxp.properties"; |
|
181 |
File f = new File(configFile); |
|
46174 | 182 |
if (isFileExists(f)) { |
33542 | 183 |
is = getFileInputStream(f); |
184 |
cacheProps.load(is); |
|
185 |
} |
|
186 |
firstTime = false; |
|
187 |
} |
|
188 |
} |
|
189 |
} |
|
190 |
value = cacheProps.getProperty(propName); |
|
191 |
||
192 |
} catch (IOException ex) { |
|
193 |
} finally { |
|
194 |
if (is != null) { |
|
195 |
try { |
|
196 |
is.close(); |
|
197 |
} catch (IOException ex) {} |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
return value; |
|
202 |
} |
|
203 |
||
46174 | 204 |
/** |
205 |
* Tests whether the file denoted by this abstract pathname is a directory. |
|
206 |
* @param f the file to be tested |
|
207 |
* @return true if it is a directory, false otherwise |
|
208 |
*/ |
|
209 |
public static boolean isDirectory(final File f) { |
|
33542 | 210 |
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) () |
46174 | 211 |
-> f.isDirectory())); |
33542 | 212 |
} |
213 |
||
46174 | 214 |
/** |
215 |
* Tests whether the file exists. |
|
216 |
* |
|
217 |
* @param f the file to be tested |
|
218 |
* @return true if the file exists, false otherwise |
|
219 |
*/ |
|
220 |
public static boolean isFileExists(final File f) { |
|
221 |
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) () |
|
222 |
-> f.exists())); |
|
223 |
} |
|
224 |
||
47312 | 225 |
/** |
226 |
* Creates and returns a new FileInputStream from a file. |
|
227 |
* @param file the specified file |
|
228 |
* @return the FileInputStream |
|
229 |
* @throws FileNotFoundException if the file is not found |
|
230 |
*/ |
|
46174 | 231 |
public static FileInputStream getFileInputStream(final File file) |
33542 | 232 |
throws FileNotFoundException { |
233 |
try { |
|
234 |
return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) () |
|
235 |
-> new FileInputStream(file)); |
|
236 |
} catch (PrivilegedActionException e) { |
|
237 |
throw (FileNotFoundException) e.getException(); |
|
238 |
} |
|
239 |
} |
|
46174 | 240 |
|
241 |
/** |
|
47312 | 242 |
* Returns the resource as a stream. |
243 |
* @param name the resource name |
|
244 |
* @return the resource stream |
|
245 |
*/ |
|
246 |
public static InputStream getResourceAsStream(final String name) { |
|
247 |
return AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> |
|
248 |
SecuritySupport.class.getResourceAsStream("/"+name)); |
|
249 |
} |
|
250 |
||
251 |
/** |
|
46174 | 252 |
* Gets a resource bundle using the specified base name, the default locale, and the caller's class loader. |
253 |
* @param bundle the base name of the resource bundle, a fully qualified class name |
|
254 |
* @return a resource bundle for the given base name and the default locale |
|
255 |
*/ |
|
256 |
public static ResourceBundle getResourceBundle(String bundle) { |
|
257 |
return getResourceBundle(bundle, Locale.getDefault()); |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* Gets a resource bundle using the specified base name and locale, and the caller's class loader. |
|
262 |
* @param bundle the base name of the resource bundle, a fully qualified class name |
|
263 |
* @param locale the locale for which a resource bundle is desired |
|
264 |
* @return a resource bundle for the given base name and locale |
|
265 |
*/ |
|
266 |
public static ResourceBundle getResourceBundle(final String bundle, final Locale locale) { |
|
267 |
return AccessController.doPrivileged((PrivilegedAction<ResourceBundle>) () -> { |
|
268 |
try { |
|
269 |
return ResourceBundle.getBundle(bundle, locale); |
|
270 |
} catch (MissingResourceException e) { |
|
271 |
try { |
|
272 |
return ResourceBundle.getBundle(bundle, new Locale("en", "US")); |
|
273 |
} catch (MissingResourceException e2) { |
|
274 |
throw new MissingResourceException( |
|
275 |
"Could not load any resource bundle by " + bundle, bundle, ""); |
|
276 |
} |
|
277 |
} |
|
278 |
}); |
|
279 |
} |
|
47312 | 280 |
|
281 |
/** |
|
282 |
* Checks whether the file exists. |
|
283 |
* @param f the specified file |
|
284 |
* @return true if the file exists, false otherwise |
|
285 |
*/ |
|
286 |
public static boolean doesFileExist(final File f) { |
|
287 |
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> f.exists())); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* Checks the LastModified attribute of a file. |
|
292 |
* @param f the specified file |
|
293 |
* @return the LastModified attribute |
|
294 |
*/ |
|
295 |
static long getLastModified(final File f) { |
|
296 |
return (AccessController.doPrivileged((PrivilegedAction<Long>) () -> f.lastModified())); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Strip off path from an URI |
|
301 |
* |
|
302 |
* @param uri an URI with full path |
|
303 |
* @return the file name only |
|
304 |
*/ |
|
305 |
public static String sanitizePath(String uri) { |
|
306 |
if (uri == null) { |
|
307 |
return ""; |
|
308 |
} |
|
309 |
int i = uri.lastIndexOf("/"); |
|
310 |
if (i > 0) { |
|
311 |
return uri.substring(i+1, uri.length()); |
|
312 |
} |
|
313 |
return ""; |
|
314 |
} |
|
315 |
||
316 |
/** |
|
317 |
* Check the protocol used in the systemId against allowed protocols |
|
318 |
* |
|
319 |
* @param systemId the Id of the URI |
|
320 |
* @param allowedProtocols a list of allowed protocols separated by comma |
|
321 |
* @param accessAny keyword to indicate allowing any protocol |
|
322 |
* @return the name of the protocol if rejected, null otherwise |
|
323 |
*/ |
|
324 |
public static String checkAccess(String systemId, String allowedProtocols, |
|
325 |
String accessAny) throws IOException { |
|
326 |
if (systemId == null || (allowedProtocols != null && |
|
327 |
allowedProtocols.equalsIgnoreCase(accessAny))) { |
|
328 |
return null; |
|
329 |
} |
|
330 |
||
331 |
String protocol; |
|
332 |
if (!systemId.contains(":")) { |
|
333 |
protocol = "file"; |
|
334 |
} else { |
|
335 |
URL url = new URL(systemId); |
|
336 |
protocol = url.getProtocol(); |
|
337 |
if (protocol.equalsIgnoreCase("jar")) { |
|
338 |
String path = url.getPath(); |
|
339 |
protocol = path.substring(0, path.indexOf(":")); |
|
340 |
} else if (protocol.equalsIgnoreCase("jrt")) { |
|
341 |
// if the systemId is "jrt" then allow access if "file" allowed |
|
342 |
protocol = "file"; |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
if (isProtocolAllowed(protocol, allowedProtocols)) { |
|
347 |
//access allowed |
|
348 |
return null; |
|
349 |
} else { |
|
350 |
return protocol; |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Check if the protocol is in the allowed list of protocols. The check |
|
356 |
* is case-insensitive while ignoring whitespaces. |
|
357 |
* |
|
358 |
* @param protocol a protocol |
|
359 |
* @param allowedProtocols a list of allowed protocols |
|
360 |
* @return true if the protocol is in the list |
|
361 |
*/ |
|
362 |
private static boolean isProtocolAllowed(String protocol, String allowedProtocols) { |
|
363 |
if (allowedProtocols == null) { |
|
364 |
return false; |
|
365 |
} |
|
366 |
String temp[] = allowedProtocols.split(","); |
|
367 |
for (String t : temp) { |
|
368 |
t = t.trim(); |
|
369 |
if (t.equalsIgnoreCase(protocol)) { |
|
370 |
return true; |
|
371 |
} |
|
372 |
} |
|
373 |
return false; |
|
374 |
} |
|
375 |
||
376 |
public static ClassLoader getContextClassLoader() { |
|
377 |
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> { |
|
378 |
ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
|
379 |
if (cl == null) |
|
380 |
cl = ClassLoader.getSystemClassLoader(); |
|
381 |
return cl; |
|
382 |
}); |
|
383 |
} |
|
384 |
||
385 |
||
386 |
public static ClassLoader getSystemClassLoader() { |
|
387 |
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> { |
|
388 |
ClassLoader cl = null; |
|
389 |
try { |
|
390 |
cl = ClassLoader.getSystemClassLoader(); |
|
391 |
} catch (SecurityException ex) { |
|
392 |
} |
|
393 |
return cl; |
|
394 |
}); |
|
395 |
} |
|
396 |
||
397 |
public static ClassLoader getParentClassLoader(final ClassLoader cl) { |
|
398 |
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> { |
|
399 |
ClassLoader parent = null; |
|
400 |
try { |
|
401 |
parent = cl.getParent(); |
|
402 |
} catch (SecurityException ex) { |
|
403 |
} |
|
404 |
||
405 |
// eliminate loops in case of the boot |
|
406 |
// ClassLoader returning itself as a parent |
|
407 |
return (parent == cl) ? null : parent; |
|
408 |
}); |
|
409 |
} |
|
410 |
||
411 |
||
412 |
// Used for debugging purposes |
|
413 |
public static String getClassSource(Class<?> cls) { |
|
414 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> { |
|
415 |
CodeSource cs = cls.getProtectionDomain().getCodeSource(); |
|
416 |
if (cs != null) { |
|
417 |
URL loc = cs.getLocation(); |
|
418 |
return loc != null ? loc.toString() : "(no location)"; |
|
419 |
} else { |
|
420 |
return "(no code source)"; |
|
421 |
} |
|
422 |
}); |
|
423 |
} |
|
424 |
||
425 |
// ---------------- For SAX ---------------------- |
|
426 |
/** |
|
427 |
* Returns the current thread's context class loader, or the system class loader |
|
428 |
* if the context class loader is null. |
|
429 |
* @return the current thread's context class loader, or the system class loader |
|
430 |
* @throws SecurityException |
|
431 |
*/ |
|
432 |
public static ClassLoader getClassLoader() throws SecurityException{ |
|
433 |
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>)() -> { |
|
434 |
ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
|
435 |
if (cl == null) { |
|
436 |
cl = ClassLoader.getSystemClassLoader(); |
|
437 |
} |
|
438 |
||
439 |
return cl; |
|
440 |
}); |
|
441 |
} |
|
442 |
||
443 |
public static InputStream getResourceAsStream(final ClassLoader cl, final String name) |
|
444 |
{ |
|
445 |
return AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> { |
|
446 |
InputStream ris; |
|
447 |
if (cl == null) { |
|
448 |
ris = SecuritySupport.class.getResourceAsStream(name); |
|
449 |
} else { |
|
450 |
ris = cl.getResourceAsStream(name); |
|
451 |
} |
|
452 |
return ris; |
|
453 |
}); |
|
454 |
} |
|
33542 | 455 |
} |