/
Valid.java
580 lines (551 loc) · 20.6 KB
/
Valid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.river.api.io;
import java.io.InvalidObjectException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Utilities for validating invariants.
* <p>
* Collections de-serialized by AtomicMarshalInputStream are always safe,
* that is, they do not call hashCode or equals on elements. These collections
* are not intended to be used as Collections in object form and must be replaced
* during construction. Although Comparator's contained in SortedMap's and SortedSet's
* are serialized, they are not used by any of the following methods. It is
* generally recommended, although not compulsory, to prefer constructing a Comparator
* when constructing defensive copies, instead of using the de-serialized Comparator.
* <p>
* Before using a de-serialized Comparator, it should be type checked, to
* ensure it is suitable for comparison of types of elements contained in
* a SortedSet or SortedMap.
* <p>
* Since Java 8, HashMap key's and HashSet elements that implement Comparable
* are provided with improved protection against hash collision DOS.
* <p>
* It is recommended that when using Set's or Map's, that
* Comparable keys or elements, or a Comparator, be used to avoid hash collisions.
* Remember to type check all keys, values and elements first, convenience
* methods have been provided here to ease type checks in Collections.
* <p>
* Note that an attacker may deliberately generate a hash collision to ensure
* equals is called and try to install the collection in another object that
* later invokes it. This is why type checking of all elements is so important.
* <p>
* When considering the security of a collection, remember that an attacker
* may try to obtain a reference to the collection, as such, defensive copies
* of collections should not be allowed to leak through your API during
* de-serialization, as your object is accessible via shared references.
* <p>
* Users are cautioned against using Maps and Sets that use hashing functions
* when keys or elements contained therein are not type specific.
*
* @author peter
*/
public class Valid {
private static Method clone;
static {
try {
clone = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>(){
@Override
public Method run() throws Exception {
return Object.class.getDeclaredMethod("clone", new Class [0]);
}
});
} catch (PrivilegedActionException ex) {
Exception cause = ex.getException();
if (cause instanceof SecurityException) throw (SecurityException) cause;
throw new Error(cause);
}
}
/**
* Type checks an object is an instance of type and returns it cast as
* the type if true, otherwise throws an InvalidObjectException.
*
* @param <T> instance type to return.
* @param type to check instance.
* @param o Object instance to type check.
* @return a type cast instance of o.
* @throws InvalidObjectException
*/
public static <T> T isInstance(Class<T> type, Object o) throws InvalidObjectException{
if (o == null) return null;
if (type == null) throw new IllegalArgumentException("type cannot be null");
if (type.isInstance(o)) return (T) o;
throw new InvalidObjectException("Argument must be an instance of " + type);
}
/**
* Checks class of an Object is equal to the Class type and returns it as
* that type, if true and throws an InvalidObjectException if false.
*
* @param <T> type cast object as.
* @param type
* @param o the Object.
* @return the Object cast as type.
* @throws InvalidObjectException
*/
public static <T> T hasClass(Class<T> type, Object o) throws InvalidObjectException{
if (o == null) return null;
if (type == null) throw new IllegalArgumentException("type cannot be null");
if (type.equals(o.getClass())) return (T) o;
throw new InvalidObjectException("Argument must be an instance of " + type);
}
/**
* Convenience method to copy and type check all elements from the
* source collection, into the destination collection.
*
* Instances of java.util.Collection will be replaced in the stream
* by a safe limited functionality immutable Collection instance
* that must be replaced during deserialization.
*
* Do not use this to validate collections that use hashing and do
* not protect adequately against hash collisions. Since Java 8,
* HashSet protects against hash collisions, only if elements are Comparable.
*
* @param <T> Collection or subtype.
* @param <E> Element type.
* @param source Collection containing unchecked elements.
* @param destination Empty Collection to populate with checked elements.
* @param type
* @return the populated destination collection, or null if source is null.
* @throws java.io.InvalidObjectException if invariant checks fail.
* @throws NullPointerException if any parameter, other than source, is null.
*/
public static <T extends Collection<E>, E> T copyCol(T source, T destination, Class<E> type) throws InvalidObjectException {
if (source == null) return null;
if (destination == null) throw new NullPointerException("destination cannot be null");
if (type == null) throw new NullPointerException("type cannot be null");
try {
Collection typeCheckedView = Collections.checkedCollection(destination, type);
typeCheckedView.addAll(source);
} catch (ClassCastException ex){
throwIOE(ex);
} catch (NullPointerException ex){
throwIOE(ex);
} catch (IllegalArgumentException ex){
throwIOE(ex);
}
return destination;
}
/**
* Convenience method to copy and type check all elements from the
* source collection, into the destination collection.
* <p>
* Instances of java.util.Set have been replaced in the stream
* by a safe limited functionality immutable Set instance
* that must be replaced during deserialization.
* <p>
* This method checks for hash collisions before populating the destination set.
*
* @param <T> Set or subtype.
* @param <E> Element type.
* @param source Collection containing unchecked elements.
* @param destination Empty Collection to populate with checked elements.
* @param type Element type.
* @param allowableHashCollisions number of hash collisions allowed per bucket.
* @return the populated destination collection, or null if source is null.
* @throws java.io.InvalidObjectException if invariant checks fail.
* @throws NullPointerException if any parameter (other than comp or source) is null.
* @throws IllegalArgumentException if allowableHashCollisions is negative.
*/
public static <T extends Set<E>, E> T copySet(T source, T destination, Class<E> type, int allowableHashCollisions) throws InvalidObjectException {
if (source == null) return null;
if (destination == null) throw new NullPointerException("destination cannot be null");
if (type == null) throw new NullPointerException("type cannot be null");
if (allowableHashCollisions < 0) throw
new IllegalArgumentException("allowableHashCollisions is negative");
hashCollision(source, allowableHashCollisions);
try {
Set typeCheckedView = Collections.checkedSet(
destination,
type
);
typeCheckedView.addAll(source);
} catch (ClassCastException ex){
throwIOE(ex);
} catch (NullPointerException ex){
throwIOE(ex);
} catch (IllegalArgumentException ex){
throwIOE(ex);
}
return destination;
}
private static CloneNotSupportedException thro(Throwable cause) {
CloneNotSupportedException ex = new CloneNotSupportedException("Clone unsuccessful");
ex.initCause(cause);
return ex;
}
/**
* Convenience method to check that an object is non null.
* @param <T>
* @param obj
* @param message reason for exception.
* @return obj if non null.
* @throws InvalidObjectException with a NullPointerException as its cause.
*/
public static <T> T notNull(T obj, String message) throws InvalidObjectException {
if (obj != null) {
return obj;
}
InvalidObjectException ex = new InvalidObjectException(message);
ex.initCause(new NullPointerException("Object was null"));
throw ex;
}
/**
* Checks all elements in an array for null values, if the arry parameter
* is not null.
*
* @param <T>
* @param arry the array
* @param message the message for the InvalidObjectException
* @return the array or null if arry is null.
* @throws InvalidObjectException if array contains null elements.
*/
public static <T> T[] nullElement(T[] arry, String message) throws InvalidObjectException{
if (arry == null) return null;
for (int i = 0, l = arry.length; i < l; i++){
if (arry[i] == null) throw new InvalidObjectException(message);
}
return arry;
}
/**
* Convenience method to perform a deep copy of an array containing
* Cloneable objects
*
* @param <T>
* @param arry - may be null or contain null elements.
* @return A deep clone of arry, or null if arry is null.
* @throws CloneNotSupportedException
*/
public static <T> T[] deepCopy(T[] arry) throws CloneNotSupportedException {
if (arry == null) {
return null;
}
T[] cpy = copy(arry);
for (int i = 0, l = cpy.length; i < l; i++) {
cpy[i] = copy(cpy[i]);
}
return cpy;
}
/**
* Convenience method to create a shallow copy of an array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param <T> type
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static <T> T[] copy(T[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a byte array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static byte[] copy(byte[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a boolean array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static boolean[] copy(boolean[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a char array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static char[] copy(char[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a short array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static short[] copy(short[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a int array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static int[] copy(int[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a long array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static long[] copy(long[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a double array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static double[] copy(double[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to create a copy of a float array
* if non null.
* <p>
* Since arrays are mutable, an attacker can retain a reference
* to a de-serialized array, that allows an attacker to mutate that
* array.
*
* @param arry that will be cloned.
* @return A clone of arry, or null if arry is null.
*/
public static float[] copy(float[] arry) {
if (arry == null) {
return null;
}
return arry.clone();
}
/**
* Convenience method to copy Cloneable objects.
*
* @param <T>
* @param obj
* @return a clone of obj if non null, otherwise null;
* @throws CloneNotSupportedException
*/
public static <T> T copy(T obj) throws CloneNotSupportedException {
if (obj == null) {
return null;
}
try {
return (T) clone.invoke(obj, (Object) null);
} catch (IllegalAccessException ex) {
throw thro(ex);
} catch (IllegalArgumentException ex) {
throw thro(ex);
} catch (InvocationTargetException ex) {
throw thro(ex);
}
}
/**
* Convenience method to copy and type check all keys and values from
* the source map, into the destination map.
* <p>
* Note, this shouldn't be used to populate maps that don't have protection
* against hash collisions. Note that HashMap and ConcurrentHashMap,
* since Java 8 defend against hash collisions, but only if keys are Comparable.
*
* @param <T> Map or subtype.
* @param <K> key type.
* @param <V> value type.
* @param source any map containing unchecked keys and values.
* @param destination a map into which checked values and keys are to be copied.
* @param key Class of key to type check.
* @param val Class of value to type check.
* @return the populated destination map, or null if source is null.
* @throws java.io.InvalidObjectException if invariant checks fail.
* @throws ClassCastException
* @throws NullPointerException if any parameter other than source is null.
*/
public static <T extends Map<K, V>, K, V> T copyMap
(
T source,
T destination,
Class<K> key,
Class<V> val
)
throws InvalidObjectException
{
if (source == null) return null;
if (destination == null) throw new NullPointerException("destination cannot be null");
if (key == null) throw new NullPointerException("key cannot be null");
if (val == null) throw new NullPointerException("val cannot be null");
try {
Map<K, V> typeCheckedView = Collections.checkedMap(destination, key, val);
typeCheckedView.putAll(source);
} catch (ClassCastException ex){
throwIOE(ex);
} catch (NullPointerException ex){
throwIOE(ex);
} catch (IllegalArgumentException ex){
throwIOE(ex);
}
return destination;
}
/**
* Convenience method to copy and type check all keys and values from
* the source map, into the destination map.
*
* This method checks for hash collisions before populating the destination
* map.
*
* @param <T> Map or subtype.
* @param <K> key type.
* @param <V> value type.
* @param source any map containing unchecked keys and values.
* @param destination a map into which checked values and keys are to be copied.
* @param key Class of key to type check.
* @param val Class of value to type check.
* @param allowableHashCollisions the number of hash collisions allowed per bucket.
* @return the populated destination map, or null if source is null.
* @throws java.io.InvalidObjectException if invariant checks fail.
* @throws NullPointerException if any parameter (other than comp and source) is null.
* @throws IllegalArgumentException if allowableHashCollisions is negative.
*/
public static <T extends Map<K, V>, K, V> T copyMap
(
T source,
T destination,
Class<K> key,
Class<V> val,
int allowableHashCollisions
) throws InvalidObjectException
{
if (source == null) return null;
if (destination == null) throw new NullPointerException("destination cannot be null");
if (key == null) throw new NullPointerException("key cannot be null");
if (val == null) throw new NullPointerException("val cannot be null");
if (allowableHashCollisions < 0) throw new IllegalArgumentException("allowableHashCollisions is negative");
hashCollision(source.keySet(), allowableHashCollisions);
try {
Map<K, V> typeCheckedView = Collections.checkedMap(
destination,
key,
val
);
typeCheckedView.putAll(source);
} catch (ClassCastException ex){
throwIOE(ex);
} catch (NullPointerException ex){
throwIOE(ex);
} catch (IllegalArgumentException ex){
throwIOE(ex);
}
return destination;
}
public static void throwIOE(Exception cause) throws InvalidObjectException{
InvalidObjectException ioe = new InvalidObjectException("Input validation failed: ");
ioe.initCause(cause);
throw ioe;
}
private static void hashCollision(Collection col, int hashCollisionLimit) throws InvalidObjectException{
Map<Integer,Integer> count = new HashMap<Integer,Integer>(col.size());
Iterator it = col.iterator();
while (it.hasNext()){
Object t = it.next();
Integer hash = t.hashCode();
Integer i = count.get(hash);
if (i == null) {
count.put(hash,1);
} else {
i++;
count.put(hash,i);
}
}
Iterator<Entry<Integer,Integer>> countIt = count.entrySet().iterator();
while (countIt.hasNext()){
Entry<Integer,Integer> en = countIt.next();
if( en.getValue() > hashCollisionLimit) throw new InvalidObjectException("Too many hash collisions");
}
}
}