jaxp/src/java.xml/share/classes/com/sun/org/apache/xerces/internal/utils/SecuritySupport.java
changeset 39907 db51759e3695
parent 31284 e9b8469adb9a
equal deleted inserted replaced
39906:230d872f56ea 39907:db51759e3695
     1 /*
     1 /*
     2  * reserved comment block
     2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT REMOVE OR ALTER!
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  */
     4  *
     5 /*
     5  * This code is free software; you can redistribute it and/or modify it
     6  * Copyright 2002,2004 The Apache Software Foundation.
     6  * under the terms of the GNU General Public License version 2 only, as
     7  *
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * Licensed under the Apache License, Version 2.0 (the "License");
     8  * particular file as subject to the "Classpath" exception as provided
     9  * you may not use this file except in compliance with the License.
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  * You may obtain a copy of the License at
    10  *
    11  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  *      http://www.apache.org/licenses/LICENSE-2.0
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  *
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * Unless required by applicable law or agreed to in writing, software
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * distributed under the License is distributed on an "AS IS" BASIS,
    15  * accompanied this code).
    16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  *
    17  * See the License for the specific language governing permissions and
    17  * You should have received a copy of the GNU General Public License version
    18  * limitations under the License.
    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.
    19  */
    24  */
    20 
    25 
    21 package com.sun.org.apache.xerces.internal.utils;
    26 package com.sun.org.apache.xerces.internal.utils;
    22 
    27 
    23 import java.io.File;
    28 import java.io.File;
    46 
    51 
    47     private static final SecuritySupport securitySupport = new SecuritySupport();
    52     private static final SecuritySupport securitySupport = new SecuritySupport();
    48 
    53 
    49     /**
    54     /**
    50      * Return an instance of this class.
    55      * Return an instance of this class.
       
    56      * @return an instance of this class
    51      */
    57      */
    52     public static SecuritySupport getInstance() {
    58     public static SecuritySupport getInstance() {
    53         return securitySupport;
    59         return securitySupport;
    54     }
    60     }
    55 
    61 
    56     static ClassLoader getContextClassLoader() {
    62     static ClassLoader getContextClassLoader() {
    57         return (ClassLoader)
    63         return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
    58         AccessController.doPrivileged(new PrivilegedAction() {
    64             ClassLoader cl = null;
    59             public Object run() {
    65             try {
    60                 ClassLoader cl = null;
    66                 cl = Thread.currentThread().getContextClassLoader();
    61                 try {
    67             } catch (SecurityException ex) { }
    62                     cl = Thread.currentThread().getContextClassLoader();
    68             return cl;
    63                 } catch (SecurityException ex) { }
       
    64                 return cl;
       
    65             }
       
    66         });
    69         });
    67     }
    70     }
    68 
    71 
    69     static ClassLoader getSystemClassLoader() {
    72     static ClassLoader getSystemClassLoader() {
    70         return (ClassLoader)
    73         return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
    71         AccessController.doPrivileged(new PrivilegedAction() {
    74             ClassLoader cl = null;
    72             public Object run() {
    75             try {
    73                 ClassLoader cl = null;
    76                 cl = ClassLoader.getSystemClassLoader();
    74                 try {
    77             } catch (SecurityException ex) {}
    75                     cl = ClassLoader.getSystemClassLoader();
    78             return cl;
    76                 } catch (SecurityException ex) {}
       
    77                 return cl;
       
    78             }
       
    79         });
    79         });
    80     }
    80     }
    81 
    81 
    82     static ClassLoader getParentClassLoader(final ClassLoader cl) {
    82     static ClassLoader getParentClassLoader(final ClassLoader cl) {
    83         return (ClassLoader)
    83         return AccessController.doPrivileged((PrivilegedAction<ClassLoader>) () -> {
    84         AccessController.doPrivileged(new PrivilegedAction() {
    84             ClassLoader parent = null;
    85             public Object run() {
    85             try {
    86                 ClassLoader parent = null;
    86                 parent = cl.getParent();
    87                 try {
    87             } catch (SecurityException ex) {}
    88                     parent = cl.getParent();
    88 
    89                 } catch (SecurityException ex) {}
    89             // eliminate loops in case of the boot
    90 
    90             // ClassLoader returning itself as a parent
    91                 // eliminate loops in case of the boot
    91             return (parent == cl) ? null : parent;
    92                 // ClassLoader returning itself as a parent
       
    93                 return (parent == cl) ? null : parent;
       
    94             }
       
    95         });
    92         });
    96     }
    93     }
    97 
    94 
    98     public static String getSystemProperty(final String propName) {
    95     public static String getSystemProperty(final String propName) {
    99         return (String)
    96         return AccessController.doPrivileged((PrivilegedAction<String>) () ->
   100         AccessController.doPrivileged(new PrivilegedAction() {
    97                 System.getProperty(propName));
   101             public Object run() {
       
   102                 return System.getProperty(propName);
       
   103             }
       
   104         });
       
   105     }
    98     }
   106 
    99 
   107     static FileInputStream getFileInputStream(final File file)
   100     static FileInputStream getFileInputStream(final File file)
   108     throws FileNotFoundException
   101     throws FileNotFoundException
   109     {
   102     {
   110         try {
   103         try {
   111             return (FileInputStream)
   104             return AccessController.doPrivileged(
   112             AccessController.doPrivileged(new PrivilegedExceptionAction() {
   105                     (PrivilegedExceptionAction<FileInputStream>)() ->
   113                 public Object run() throws FileNotFoundException {
   106                     new FileInputStream(file));
   114                     return new FileInputStream(file);
       
   115                 }
       
   116             });
       
   117         } catch (PrivilegedActionException e) {
   107         } catch (PrivilegedActionException e) {
   118             throw (FileNotFoundException)e.getException();
   108             throw (FileNotFoundException)e.getException();
   119         }
   109         }
   120     }
   110     }
   121 
   111 
   133      * @param bundle the base name of the resource bundle, a fully qualified class name
   123      * @param bundle the base name of the resource bundle, a fully qualified class name
   134      * @param locale the locale for which a resource bundle is desired
   124      * @param locale the locale for which a resource bundle is desired
   135      * @return a resource bundle for the given base name and locale
   125      * @return a resource bundle for the given base name and locale
   136      */
   126      */
   137     public static ResourceBundle getResourceBundle(final String bundle, final Locale locale) {
   127     public static ResourceBundle getResourceBundle(final String bundle, final Locale locale) {
   138         return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
   128         return AccessController.doPrivileged((PrivilegedAction<ResourceBundle>) () -> {
   139             public ResourceBundle run() {
   129             try {
       
   130                 return PropertyResourceBundle.getBundle(bundle, locale);
       
   131             } catch (MissingResourceException e) {
   140                 try {
   132                 try {
   141                     return PropertyResourceBundle.getBundle(bundle, locale);
   133                     return PropertyResourceBundle.getBundle(bundle, new Locale("en", "US"));
   142                 } catch (MissingResourceException e) {
   134                 } catch (MissingResourceException e2) {
   143                     try {
   135                     throw new MissingResourceException(
   144                         return PropertyResourceBundle.getBundle(bundle, new Locale("en", "US"));
   136                             "Could not load any resource bundle by " + bundle, bundle, "");
   145                     } catch (MissingResourceException e2) {
       
   146                         throw new MissingResourceException(
       
   147                                 "Could not load any resource bundle by " + bundle, bundle, "");
       
   148                     }
       
   149                 }
   137                 }
   150             }
   138             }
   151         });
   139         });
   152     }
   140     }
   153 
   141 
   154     static boolean getFileExists(final File f) {
   142     static boolean getFileExists(final File f) {
   155         return ((Boolean)
   143         return (AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
   156                 AccessController.doPrivileged(new PrivilegedAction() {
   144                 f.exists()));
   157                     public Object run() {
       
   158                         return f.exists() ? Boolean.TRUE : Boolean.FALSE;
       
   159                     }
       
   160                 })).booleanValue();
       
   161     }
   145     }
   162 
   146 
   163     static long getLastModified(final File f) {
   147     static long getLastModified(final File f) {
   164         return ((Long)
   148         return (AccessController.doPrivileged((PrivilegedAction<Long>) () ->
   165                 AccessController.doPrivileged(new PrivilegedAction() {
   149                 f.lastModified()));
   166                     public Object run() {
       
   167                         return new Long(f.lastModified());
       
   168                     }
       
   169                 })).longValue();
       
   170     }
   150     }
   171 
   151 
   172     /**
   152     /**
   173      * Strip off path from an URI
   153      * Strip off path from an URI
   174      *
   154      *
   191      *
   171      *
   192      * @param systemId the Id of the URI
   172      * @param systemId the Id of the URI
   193      * @param allowedProtocols a list of allowed protocols separated by comma
   173      * @param allowedProtocols a list of allowed protocols separated by comma
   194      * @param accessAny keyword to indicate allowing any protocol
   174      * @param accessAny keyword to indicate allowing any protocol
   195      * @return the name of the protocol if rejected, null otherwise
   175      * @return the name of the protocol if rejected, null otherwise
       
   176      * @throws java.io.IOException
   196      */
   177      */
   197     public static String checkAccess(String systemId, String allowedProtocols, String accessAny) throws IOException {
   178     public static String checkAccess(String systemId, String allowedProtocols, String accessAny) throws IOException {
   198         if (systemId == null || (allowedProtocols != null &&
   179         if (systemId == null || (allowedProtocols != null &&
   199                 allowedProtocols.equalsIgnoreCase(accessAny))) {
   180                 allowedProtocols.equalsIgnoreCase(accessAny))) {
   200             return null;
   181             return null;
   201         }
   182         }
   202 
   183 
   203         String protocol;
   184         String protocol;
   204         if (systemId.indexOf(":")==-1) {
   185         if (!systemId.contains(":")) {
   205             protocol = "file";
   186             protocol = "file";
   206         } else {
   187         } else {
   207             URL url = new URL(systemId);
   188             URL url = new URL(systemId);
   208             protocol = url.getProtocol();
   189             protocol = url.getProtocol();
   209             if (protocol.equalsIgnoreCase("jar")) {
   190             if (protocol.equalsIgnoreCase("jar")) {
   247 
   228 
   248     /**
   229     /**
   249      * Read JAXP system property in this order: system property,
   230      * Read JAXP system property in this order: system property,
   250      * $java.home/conf/jaxp.properties if the system property is not specified
   231      * $java.home/conf/jaxp.properties if the system property is not specified
   251      *
   232      *
   252      * @param propertyId the Id of the property
   233      * @param sysPropertyId the Id of the property
   253      * @return the value of the property
   234      * @return the value of the property
   254      */
   235      */
   255     public static String getJAXPSystemProperty(String sysPropertyId) {
   236     public static String getJAXPSystemProperty(String sysPropertyId) {
   256         String accessExternal = getSystemProperty(sysPropertyId);
   237         String value = getSystemProperty(sysPropertyId);
   257         if (accessExternal == null) {
   238         if (value == null) {
   258             accessExternal = readJAXPProperty(sysPropertyId);
   239             value = readJAXPProperty(sysPropertyId);
   259         }
   240         }
   260         return accessExternal;
   241         return value;
   261     }
   242     }
   262 
   243 
   263      /**
   244      /**
   264      * Read from $java.home/conf/jaxp.properties for the specified property
   245      * Read from $java.home/conf/jaxp.properties for the specified property
   265      * The program
   246      * The program
   286                 }
   267                 }
   287             }
   268             }
   288             value = cacheProps.getProperty(propertyId);
   269             value = cacheProps.getProperty(propertyId);
   289 
   270 
   290         }
   271         }
   291         catch (Exception ex) {}
   272         catch (IOException ex) {}
   292         finally {
   273         finally {
   293             if (is != null) {
   274             if (is != null) {
   294                 try {
   275                 try {
   295                     is.close();
   276                     is.close();
   296                 } catch (IOException ex) {}
   277                 } catch (IOException ex) {}