@@ -8,8 +8,8 @@ import net.corda.core.contracts.TransactionVerificationException
8
8
import net.corda.core.contracts.TransactionVerificationException.OverlappingAttachmentsException
9
9
import net.corda.core.contracts.TransactionVerificationException.PackageOwnershipException
10
10
import net.corda.core.crypto.SecureHash
11
- import net.corda.core.internal.JAVA_1_2_CLASS_FILE_FORMAT_MAJOR_VERSION
12
11
import net.corda.core.internal.JAVA_17_CLASS_FILE_FORMAT_MAJOR_VERSION
12
+ import net.corda.core.internal.JAVA_1_2_CLASS_FILE_FORMAT_MAJOR_VERSION
13
13
import net.corda.core.internal.JarSignatureCollector
14
14
import net.corda.core.internal.NamedCacheFactory
15
15
import net.corda.core.internal.PlatformVersionSwitches
@@ -118,16 +118,11 @@ class AttachmentsClassLoader(attachments: List<Attachment>,
118
118
// Reset the value to prevent Error due to a factory already defined
119
119
factoryField.set(null , null )
120
120
// Set our custom factory and wrap the current one into it
121
- URL .setURLStreamHandlerFactory(
122
- // Set the factory to a decorator
123
- object : URLStreamHandlerFactory {
124
- // route between our own and the pre-existing factory
125
- override fun createURLStreamHandler (protocol : String ): URLStreamHandler ? {
126
- return AttachmentURLStreamHandlerFactory .createURLStreamHandler(protocol)
127
- ? : existingFactory.createURLStreamHandler(protocol)
128
- }
129
- }
130
- )
121
+ URL .setURLStreamHandlerFactory { protocol ->
122
+ // route between our own and the pre-existing factory
123
+ AttachmentURLStreamHandlerFactory .createURLStreamHandler(protocol)
124
+ ? : existingFactory.createURLStreamHandler(protocol)
125
+ }
131
126
}
132
127
}
133
128
}
@@ -158,9 +153,7 @@ class AttachmentsClassLoader(attachments: List<Attachment>,
158
153
checkAttachments(attachments)
159
154
}
160
155
161
- private class AttachmentHashContext (
162
- val txId : SecureHash ,
163
- val buffer : ByteArray = ByteArray (DEFAULT_BUFFER_SIZE ))
156
+ private class AttachmentHashContext (val buffer : ByteArray = ByteArray (DEFAULT_BUFFER_SIZE ))
164
157
165
158
private fun hash (inputStream : InputStream , ctx : AttachmentHashContext ) : SecureHash .SHA256 {
166
159
val md = MessageDigest .getInstance(SecureHash .SHA2_256 )
@@ -189,7 +182,7 @@ class AttachmentsClassLoader(attachments: List<Attachment>,
189
182
// This function attempts to strike a balance between security and usability when it comes to the no-overlap rule.
190
183
// TODO - investigate potential exploits.
191
184
private fun shouldCheckForNoOverlap (path : String , targetPlatformVersion : Int ): Boolean {
192
- require(path.toLowerCase () == path)
185
+ require(path.lowercase () == path)
193
186
require(! path.contains(' \\ ' ))
194
187
195
188
return when {
@@ -234,7 +227,7 @@ class AttachmentsClassLoader(attachments: List<Attachment>,
234
227
// claim their parts of the Java package namespace via registration with the zone operator.
235
228
236
229
val classLoaderEntries = mutableMapOf<String , SecureHash >()
237
- val ctx = AttachmentHashContext (sampleTxId )
230
+ val ctx = AttachmentHashContext ()
238
231
for (attachment in attachments) {
239
232
// We may have been given an attachment loaded from the database in which case, important info like
240
233
// signers is already calculated.
@@ -270,7 +263,7 @@ class AttachmentsClassLoader(attachments: List<Attachment>,
270
263
// filesystem tries to be case insensitive. This may break developers who attempt to use ProGuard.
271
264
//
272
265
// Also convert to Unix path separators as all resource/class lookups will expect this.
273
- val path = entry.name.toLowerCase (Locale .US ).replace(' \\ ' , ' /' )
266
+ val path = entry.name.lowercase (Locale .US ).replace(' \\ ' , ' /' )
274
267
275
268
// Namespace ownership. We only check class files: resources are loaded relative to a JAR anyway.
276
269
if (path.endsWith(" .class" )) {
@@ -285,7 +278,7 @@ class AttachmentsClassLoader(attachments: List<Attachment>,
285
278
for ((namespace, pubkey) in params.packageOwnership) {
286
279
// Note that due to the toLowerCase() call above, we'll be comparing against a lowercased
287
280
// version of the ownership claim.
288
- val ns = namespace.toLowerCase (Locale .US )
281
+ val ns = namespace.lowercase (Locale .US )
289
282
// We need an additional . to avoid matching com.foo.Widget against com.foobar.Zap
290
283
if (pkgName == ns || pkgName.startsWith(" $ns ." )) {
291
284
if (pubkey !in signers)
@@ -358,7 +351,7 @@ object AttachmentsClassLoaderBuilder {
358
351
val attachmentIds = attachments.mapTo(LinkedHashSet (), Attachment ::id)
359
352
360
353
val cache = attachmentsClassLoaderCache ? : fallBackCache
361
- val cachedSerializationContext = cache.computeIfAbsent(AttachmentsClassLoaderKey (attachmentIds, params), Function { key ->
354
+ val cachedSerializationContext = cache.computeIfAbsent(AttachmentsClassLoaderKey (attachmentIds, params)) { key ->
362
355
// Create classloader and load serializers, whitelisted classes
363
356
val transactionClassLoader = AttachmentsClassLoader (attachments, key.params, txId, isAttachmentTrusted, parent)
364
357
val serializers = try {
@@ -380,9 +373,9 @@ object AttachmentsClassLoaderBuilder {
380
373
.withWhitelist(whitelistedClasses)
381
374
.withCustomSerializers(serializers)
382
375
.withoutCarpenter()
383
- })
376
+ }
384
377
385
- val serializationContext = cachedSerializationContext.withProperties(mapOf< Any , Any > (
378
+ val serializationContext = cachedSerializationContext.withProperties(mapOf (
386
379
// Duplicate the SerializationContext from the cache and give
387
380
// it these extra properties, just for this transaction.
388
381
// However, keep a strong reference to the cached SerializationContext so we can
@@ -489,7 +482,6 @@ class AttachmentsClassLoaderCacheImpl(cacheFactory: NamedCacheFactory) : Singlet
489
482
private val toBeClosed = ConcurrentHashMap .newKeySet<ToBeClosed >()
490
483
private val expiryQueue = ReferenceQueue <SerializationContext >()
491
484
492
- @Suppress(" TooGenericExceptionCaught" )
493
485
private fun purgeExpiryQueue () {
494
486
// Close the AttachmentsClassLoader for every SerializationContext
495
487
// that has already been garbage-collected.
0 commit comments