11
11
import java .io .InputStream ;
12
12
import java .lang .annotation .Annotation ;
13
13
import java .security .AccessController ;
14
+ import java .security .PrivilegedAction ;
14
15
import java .security .PrivilegedExceptionAction ;
15
16
import java .util .Collections ;
16
17
import java .util .List ;
37
38
import org .hibernate .validator .internal .metadata .raw .ConstrainedType ;
38
39
import org .hibernate .validator .internal .util .logging .Log ;
39
40
import org .hibernate .validator .internal .util .logging .LoggerFactory ;
41
+ import org .hibernate .validator .internal .util .privilegedactions .GetClassLoader ;
40
42
import org .hibernate .validator .internal .util .privilegedactions .NewJaxbContext ;
43
+ import org .hibernate .validator .internal .util .privilegedactions .SetContextClassLoader ;
41
44
import org .hibernate .validator .internal .util .privilegedactions .Unmarshal ;
42
45
43
46
import static org .hibernate .validator .internal .util .CollectionHelper .newArrayList ;
@@ -173,10 +176,10 @@ public final void parse(Set<InputStream> mappingStreams) {
173
176
}
174
177
175
178
private ConstraintMappingsType unmarshal (JAXBContext jc , InputStream in ) throws JAXBException {
176
- ClassLoader previousTccl = Thread . currentThread (). getContextClassLoader ( );
179
+ ClassLoader previousTccl = run ( GetClassLoader . fromContext () );
177
180
178
181
try {
179
- Thread . currentThread (). setContextClassLoader ( ValidationXmlParser .class .getClassLoader () );
182
+ run ( SetContextClassLoader . action ( XmlMappingParser .class .getClassLoader () ) );
180
183
181
184
XMLEventReader xmlEventReader = xmlParserHelper .createXmlEventReader ( "constraint mapping file" , new CloseIgnoringInputStream ( in ) );
182
185
String schemaVersion = xmlParserHelper .getSchemaVersion ( "constraint mapping file" , xmlEventReader );
@@ -189,7 +192,7 @@ private ConstraintMappingsType unmarshal(JAXBContext jc, InputStream in) throws
189
192
return getValidationConfig ( xmlEventReader , unmarshaller );
190
193
}
191
194
finally {
192
- Thread . currentThread (). setContextClassLoader ( previousTccl );
195
+ run ( SetContextClassLoader . action ( previousTccl ) );
193
196
}
194
197
}
195
198
@@ -382,6 +385,16 @@ private String getSchemaResourceName(String schemaVersion) {
382
385
return schemaResource ;
383
386
}
384
387
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
+
385
398
/**
386
399
* Runs the given privileged action, using a privileged block if required.
387
400
* <p>
0 commit comments