1111import java .io .InputStream ;
1212import java .lang .annotation .Annotation ;
1313import java .security .AccessController ;
14+ import java .security .PrivilegedAction ;
1415import java .security .PrivilegedExceptionAction ;
1516import java .util .Collections ;
1617import java .util .List ;
3738import org .hibernate .validator .internal .metadata .raw .ConstrainedType ;
3839import org .hibernate .validator .internal .util .logging .Log ;
3940import org .hibernate .validator .internal .util .logging .LoggerFactory ;
41+ import org .hibernate .validator .internal .util .privilegedactions .GetClassLoader ;
4042import org .hibernate .validator .internal .util .privilegedactions .NewJaxbContext ;
43+ import org .hibernate .validator .internal .util .privilegedactions .SetContextClassLoader ;
4144import org .hibernate .validator .internal .util .privilegedactions .Unmarshal ;
4245
4346import static org .hibernate .validator .internal .util .CollectionHelper .newArrayList ;
@@ -173,10 +176,10 @@ public final void parse(Set<InputStream> mappingStreams) {
173176 }
174177
175178 private ConstraintMappingsType unmarshal (JAXBContext jc , InputStream in ) throws JAXBException {
176- ClassLoader previousTccl = Thread . currentThread (). getContextClassLoader ( );
179+ ClassLoader previousTccl = run ( GetClassLoader . fromContext () );
177180
178181 try {
179- Thread . currentThread (). setContextClassLoader ( ValidationXmlParser .class .getClassLoader () );
182+ run ( SetContextClassLoader . action ( XmlMappingParser .class .getClassLoader () ) );
180183
181184 XMLEventReader xmlEventReader = xmlParserHelper .createXmlEventReader ( "constraint mapping file" , new CloseIgnoringInputStream ( in ) );
182185 String schemaVersion = xmlParserHelper .getSchemaVersion ( "constraint mapping file" , xmlEventReader );
@@ -189,7 +192,7 @@ private ConstraintMappingsType unmarshal(JAXBContext jc, InputStream in) throws
189192 return getValidationConfig ( xmlEventReader , unmarshaller );
190193 }
191194 finally {
192- Thread . currentThread (). setContextClassLoader ( previousTccl );
195+ run ( SetContextClassLoader . action ( previousTccl ) );
193196 }
194197 }
195198
@@ -382,6 +385,16 @@ private String getSchemaResourceName(String schemaVersion) {
382385 return schemaResource ;
383386 }
384387
388+ /**
389+ * Runs the given privileged action, using a privileged block if required.
390+ * <p>
391+ * <b>NOTE:</b> This must never be changed into a publicly available method to avoid execution of arbitrary
392+ * privileged actions within HV's protection domain.
393+ */
394+ private static <T > T run (PrivilegedAction <T > action ) {
395+ return System .getSecurityManager () != null ? AccessController .doPrivileged ( action ) : action .run ();
396+ }
397+
385398 /**
386399 * Runs the given privileged action, using a privileged block if required.
387400 * <p>
0 commit comments