Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/jdk/xml/internal/SecuritySupport.java
40948 views
1
/*
2
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
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;
32
import java.net.URL;
33
import java.security.AccessController;
34
import java.security.CodeSource;
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;
40
import java.util.MissingResourceException;
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 {
48
public final static String NEWLINE = System.lineSeparator();
49
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
/**
79
* Reads a system property with privilege
80
*
81
* @param propName the name of the property
82
* @return the value of the property
83
*/
84
@SuppressWarnings("removal")
85
public static String getSystemProperty(final String propName) {
86
return
87
AccessController.doPrivileged(
88
(PrivilegedAction<String>) () -> System.getProperty(propName));
89
}
90
91
/**
92
* Reads a system property with privilege
93
*
94
* @param propName the name of the property
95
* @return the value of the property
96
*/
97
public static String getSystemProperty(final String propName, String defValue) {
98
String value = getSystemProperty(propName);
99
if (value == null) {
100
return defValue;
101
}
102
return value;
103
}
104
105
/**
106
* Reads a system property with specified type.
107
*
108
* @param <T> the type of the property value
109
* @param type the type of the property value
110
* @param propName the name of the property
111
* @param defValue the default value
112
* @return the value of the property, or the default value if no system
113
* property is found
114
*/
115
public static <T> T getSystemProperty(Class<T> type, String propName, String defValue) {
116
String value = getSystemProperty(propName);
117
if (value == null) {
118
value = defValue;
119
}
120
if (Integer.class.isAssignableFrom(type)) {
121
return type.cast(Integer.parseInt(value));
122
} else if (Boolean.class.isAssignableFrom(type)) {
123
return type.cast(Boolean.parseBoolean(value));
124
}
125
return type.cast(value);
126
}
127
128
/**
129
* Reads JAXP system property in this order: system property,
130
* $java.home/conf/jaxp.properties if the system property is not specified
131
*
132
* @param <T> the type of the property value
133
* @param type the type of the property value
134
* @param propName the name of the property
135
* @param defValue the default value
136
* @return the value of the property, or the default value if no system
137
* property is found
138
*/
139
public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {
140
String value = getJAXPSystemProperty(propName);
141
if (value == null) {
142
value = defValue;
143
}
144
if (Integer.class.isAssignableFrom(type)) {
145
return type.cast(Integer.parseInt(value));
146
} else if (Boolean.class.isAssignableFrom(type)) {
147
return type.cast(Boolean.parseBoolean(value));
148
}
149
return type.cast(value);
150
}
151
152
/**
153
* Reads JAXP system property in this order: system property,
154
* $java.home/conf/jaxp.properties if the system property is not specified
155
*
156
* @param propName the name of the property
157
* @return the value of the property
158
*/
159
public static String getJAXPSystemProperty(String propName) {
160
String value = getSystemProperty(propName);
161
if (value == null) {
162
value = readJAXPProperty(propName);
163
}
164
return value;
165
}
166
167
/**
168
* Reads the specified property from $java.home/conf/jaxp.properties
169
*
170
* @param propName the name of the property
171
* @return the value of the property
172
*/
173
public static String readJAXPProperty(String propName) {
174
String value = null;
175
InputStream is = null;
176
try {
177
if (firstTime) {
178
synchronized (cacheProps) {
179
if (firstTime) {
180
String configFile = getSystemProperty("java.home") + File.separator
181
+ "conf" + File.separator + "jaxp.properties";
182
File f = new File(configFile);
183
if (isFileExists(f)) {
184
is = getFileInputStream(f);
185
cacheProps.load(is);
186
}
187
firstTime = false;
188
}
189
}
190
}
191
value = cacheProps.getProperty(propName);
192
193
} catch (IOException ex) {
194
} finally {
195
if (is != null) {
196
try {
197
is.close();
198
} catch (IOException ex) {}
199
}
200
}
201
202
return value;
203
}
204
205
/**
206
* Tests whether the file denoted by this abstract pathname is a directory.
207
* @param f the file to be tested
208
* @return true if it is a directory, false otherwise
209
*/
210
@SuppressWarnings("removal")
211
public static boolean isDirectory(final File f) {
212
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
213
-> f.isDirectory()));
214
}
215
216
/**
217
* Tests whether the file exists.
218
*
219
* @param f the file to be tested
220
* @return true if the file exists, false otherwise
221
*/
222
@SuppressWarnings("removal")
223
public static boolean isFileExists(final File f) {
224
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
225
-> f.exists()));
226
}
227
228
/**
229
* Creates and returns a new FileInputStream from a file.
230
* @param file the specified file
231
* @return the FileInputStream
232
* @throws FileNotFoundException if the file is not found
233
*/
234
@SuppressWarnings("removal")
235
public static FileInputStream getFileInputStream(final File file)
236
throws FileNotFoundException {
237
try {
238
return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) ()
239
-> new FileInputStream(file));
240
} catch (PrivilegedActionException e) {
241
throw (FileNotFoundException) e.getException();
242
}
243
}
244
245
/**
246
* Returns the resource as a stream.
247
* @param name the resource name
248
* @return the resource stream
249
*/
250
@SuppressWarnings("removal")
251
public static InputStream getResourceAsStream(final String name) {
252
return AccessController.doPrivileged((PrivilegedAction<InputStream>) () ->
253
SecuritySupport.class.getResourceAsStream("/"+name));
254
}
255
256
/**
257
* Gets a resource bundle using the specified base name, the default locale, and the caller's class loader.
258
* @param bundle the base name of the resource bundle, a fully qualified class name
259
* @return a resource bundle for the given base name and the default locale
260
*/
261
public static ResourceBundle getResourceBundle(String bundle) {
262
return getResourceBundle(bundle, Locale.getDefault());
263
}
264
265
/**
266
* Gets a resource bundle using the specified base name and locale, and the caller's class loader.
267
* @param bundle the base name of the resource bundle, a fully qualified class name
268
* @param locale the locale for which a resource bundle is desired
269
* @return a resource bundle for the given base name and locale
270
*/
271
@SuppressWarnings("removal")
272
public static ResourceBundle getResourceBundle(final String bundle, final Locale locale) {
273
return AccessController.doPrivileged((PrivilegedAction<ResourceBundle>) () -> {
274
try {
275
return ResourceBundle.getBundle(bundle, locale);
276
} catch (MissingResourceException e) {
277
try {
278
return ResourceBundle.getBundle(bundle, new Locale("en", "US"));
279
} catch (MissingResourceException e2) {
280
throw new MissingResourceException(
281
"Could not load any resource bundle by " + bundle, bundle, "");
282
}
283
}
284
});
285
}
286
287
/**
288
* Checks whether the file exists.
289
* @param f the specified file
290
* @return true if the file exists, false otherwise
291
*/
292
@SuppressWarnings("removal")
293
public static boolean doesFileExist(final File f) {
294
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> f.exists()));
295
}
296
297
/**
298
* Checks the LastModified attribute of a file.
299
* @param f the specified file
300
* @return the LastModified attribute
301
*/
302
@SuppressWarnings("removal")
303
static long getLastModified(final File f) {
304
return (AccessController.doPrivileged((PrivilegedAction<Long>) () -> f.lastModified()));
305
}
306
307
/**
308
* Strip off path from an URI
309
*
310
* @param uri an URI with full path
311
* @return the file name only
312
*/
313
public static String sanitizePath(String uri) {
314
if (uri == null) {
315
return "";
316
}
317
int i = uri.lastIndexOf("/");
318
if (i > 0) {
319
return uri.substring(i+1, uri.length());
320
}
321
return "";
322
}
323
324
/**
325
* Check the protocol used in the systemId against allowed protocols
326
*
327
* @param systemId the Id of the URI
328
* @param allowedProtocols a list of allowed protocols separated by comma
329
* @param accessAny keyword to indicate allowing any protocol
330
* @return the name of the protocol if rejected, null otherwise
331
*/
332
public static String checkAccess(String systemId, String allowedProtocols,
333
String accessAny) throws IOException {
334
if (systemId == null || (allowedProtocols != null &&
335
allowedProtocols.equalsIgnoreCase(accessAny))) {
336
return null;
337
}
338
339
String protocol;
340
if (!systemId.contains(":")) {
341
protocol = "file";
342
} else {
343
URL url = new URL(systemId);
344
protocol = url.getProtocol();
345
if (protocol.equalsIgnoreCase("jar")) {
346
String path = url.getPath();
347
protocol = path.substring(0, path.indexOf(":"));
348
} else if (protocol.equalsIgnoreCase("jrt")) {
349
// if the systemId is "jrt" then allow access if "file" allowed
350
protocol = "file";
351
}
352
}
353
354
if (isProtocolAllowed(protocol, allowedProtocols)) {
355
//access allowed
356
return null;
357
} else {
358
return protocol;
359
}
360
}
361
362
/**
363
* Check if the protocol is in the allowed list of protocols. The check
364
* is case-insensitive while ignoring whitespaces.
365
*
366
* @param protocol a protocol
367
* @param allowedProtocols a list of allowed protocols
368
* @return true if the protocol is in the list
369
*/
370
private static boolean isProtocolAllowed(String protocol, String allowedProtocols) {
371
if (allowedProtocols == null) {
372
return false;
373
}
374
String temp[] = allowedProtocols.split(",");
375
for (String t : temp) {
376
t = t.trim();
377
if (t.equalsIgnoreCase(protocol)) {
378
return true;
379
}
380
}
381
return false;
382
}
383
384
@SuppressWarnings("removal")
385
public static ClassLoader getContextClassLoader() {
386
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
387
ClassLoader cl = Thread.currentThread().getContextClassLoader();
388
if (cl == null)
389
cl = ClassLoader.getSystemClassLoader();
390
return cl;
391
});
392
}
393
394
395
@SuppressWarnings("removal")
396
public static ClassLoader getSystemClassLoader() {
397
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
398
ClassLoader cl = null;
399
try {
400
cl = ClassLoader.getSystemClassLoader();
401
} catch (SecurityException ex) {
402
}
403
return cl;
404
});
405
}
406
407
@SuppressWarnings("removal")
408
public static ClassLoader getParentClassLoader(final ClassLoader cl) {
409
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
410
ClassLoader parent = null;
411
try {
412
parent = cl.getParent();
413
} catch (SecurityException ex) {
414
}
415
416
// eliminate loops in case of the boot
417
// ClassLoader returning itself as a parent
418
return (parent == cl) ? null : parent;
419
});
420
}
421
422
423
// Used for debugging purposes
424
@SuppressWarnings("removal")
425
public static String getClassSource(Class<?> cls) {
426
return AccessController.doPrivileged((PrivilegedAction<String>) () -> {
427
CodeSource cs = cls.getProtectionDomain().getCodeSource();
428
if (cs != null) {
429
URL loc = cs.getLocation();
430
return loc != null ? loc.toString() : "(no location)";
431
} else {
432
return "(no code source)";
433
}
434
});
435
}
436
437
// ---------------- For SAX ----------------------
438
/**
439
* Returns the current thread's context class loader, or the system class loader
440
* if the context class loader is null.
441
* @return the current thread's context class loader, or the system class loader
442
* @throws SecurityException
443
*/
444
@SuppressWarnings("removal")
445
public static ClassLoader getClassLoader() throws SecurityException{
446
return AccessController.doPrivileged((PrivilegedAction<ClassLoader>)() -> {
447
ClassLoader cl = Thread.currentThread().getContextClassLoader();
448
if (cl == null) {
449
cl = ClassLoader.getSystemClassLoader();
450
}
451
452
return cl;
453
});
454
}
455
456
@SuppressWarnings("removal")
457
public static InputStream getResourceAsStream(final ClassLoader cl, final String name)
458
{
459
return AccessController.doPrivileged((PrivilegedAction<InputStream>) () -> {
460
InputStream ris;
461
if (cl == null) {
462
ris = SecuritySupport.class.getResourceAsStream(name);
463
} else {
464
ris = cl.getResourceAsStream(name);
465
}
466
return ris;
467
});
468
}
469
}
470
471