41230
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
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.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.io;
|
|
27 |
|
|
28 |
import java.security.AccessController;
|
|
29 |
import java.security.PrivilegedAction;
|
|
30 |
import java.security.Security;
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Objects;
|
|
34 |
import java.util.Optional;
|
|
35 |
import java.util.function.Function;
|
|
36 |
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Filter classes, array lengths, and graph metrics during deserialization.
|
|
40 |
* If set on an {@link ObjectInputStream}, the {@link #checkInput checkInput(FilterInfo)}
|
|
41 |
* method is called to validate classes, the length of each array,
|
|
42 |
* the number of objects being read from the stream, the depth of the graph,
|
|
43 |
* and the total number of bytes read from the stream.
|
|
44 |
* <p>
|
|
45 |
* A filter can be set via {@link ObjectInputStream#setObjectInputFilter setObjectInputFilter}
|
|
46 |
* for an individual ObjectInputStream.
|
|
47 |
* A filter can be set via {@link Config#setSerialFilter(ObjectInputFilter) Config.setSerialFilter}
|
|
48 |
* to affect every {@code ObjectInputStream} that does not otherwise set a filter.
|
|
49 |
* <p>
|
|
50 |
* A filter determines whether the arguments are {@link Status#ALLOWED ALLOWED}
|
|
51 |
* or {@link Status#REJECTED REJECTED} and should return the appropriate status.
|
|
52 |
* If the filter cannot determine the status it should return
|
|
53 |
* {@link Status#UNDECIDED UNDECIDED}.
|
|
54 |
* Filters should be designed for the specific use case and expected types.
|
|
55 |
* A filter designed for a particular use may be passed a class that is outside
|
|
56 |
* of the scope of the filter. If the purpose of the filter is to black-list classes
|
|
57 |
* then it can reject a candidate class that matches and report UNDECIDED for others.
|
|
58 |
* A filter may be called with class equals {@code null}, {@code arrayLength} equal -1,
|
|
59 |
* the depth, number of references, and stream size and return a status
|
|
60 |
* that reflects only one or only some of the values.
|
|
61 |
* This allows a filter to specific about the choice it is reporting and
|
|
62 |
* to use other filters without forcing either allowed or rejected status.
|
|
63 |
*
|
|
64 |
* <p>
|
|
65 |
* Typically, a custom filter should check if a process-wide filter
|
|
66 |
* is configured and defer to it if so. For example,
|
|
67 |
* <pre>{@code
|
|
68 |
* ObjectInputFilter.Status checkInput(FilterInfo info) {
|
|
69 |
* ObjectInputFilter serialFilter = ObjectInputFilter.Config.getSerialFilter();
|
|
70 |
* if (serialFilter != null) {
|
|
71 |
* ObjectInputFilter.Status status = serialFilter.checkInput(info);
|
|
72 |
* if (status != ObjectInputFilter.Status.UNDECIDED) {
|
|
73 |
* // The process-wide filter overrides this filter
|
|
74 |
* return status;
|
|
75 |
* }
|
|
76 |
* }
|
|
77 |
* if (info.serialClass() != null &&
|
|
78 |
* Remote.class.isAssignableFrom(info.serialClass())) {
|
|
79 |
* return Status.REJECTED; // Do not allow Remote objects
|
|
80 |
* }
|
|
81 |
* return Status.UNDECIDED;
|
|
82 |
* }
|
|
83 |
*}</pre>
|
|
84 |
* <p>
|
|
85 |
* Unless otherwise noted, passing a {@code null} argument to a
|
|
86 |
* method in this interface and its nested classes will cause a
|
|
87 |
* {@link NullPointerException} to be thrown.
|
|
88 |
*
|
|
89 |
* @see ObjectInputStream#setObjectInputFilter(ObjectInputFilter)
|
|
90 |
* @since 9
|
|
91 |
*/
|
|
92 |
@FunctionalInterface
|
|
93 |
public interface ObjectInputFilter {
|
|
94 |
|
|
95 |
/**
|
|
96 |
* Check the class, array length, number of object references, depth,
|
|
97 |
* stream size, and other available filtering information.
|
|
98 |
* Implementations of this method check the contents of the object graph being created
|
|
99 |
* during deserialization. The filter returns {@link Status#ALLOWED Status.ALLOWED},
|
|
100 |
* {@link Status#REJECTED Status.REJECTED}, or {@link Status#UNDECIDED Status.UNDECIDED}.
|
|
101 |
*
|
|
102 |
* @param filterInfo provides information about the current object being deserialized,
|
|
103 |
* if any, and the status of the {@link ObjectInputStream}
|
|
104 |
* @return {@link Status#ALLOWED Status.ALLOWED} if accepted,
|
|
105 |
* {@link Status#REJECTED Status.REJECTED} if rejected,
|
|
106 |
* {@link Status#UNDECIDED Status.UNDECIDED} if undecided.
|
|
107 |
* @since 9
|
|
108 |
*/
|
|
109 |
Status checkInput(FilterInfo filterInfo);
|
|
110 |
|
|
111 |
/**
|
|
112 |
* FilterInfo provides access to information about the current object
|
|
113 |
* being deserialized and the status of the {@link ObjectInputStream}.
|
|
114 |
* @since 9
|
|
115 |
*/
|
|
116 |
interface FilterInfo {
|
|
117 |
/**
|
|
118 |
* The class of an object being deserialized.
|
|
119 |
* For arrays, it is the array type.
|
|
120 |
* For example, the array class name of a 2 dimensional array of strings is
|
|
121 |
* "{@code [[Ljava.lang.String;}".
|
|
122 |
* To check the array's element type, iteratively use
|
|
123 |
* {@link Class#getComponentType() Class.getComponentType} while the result
|
|
124 |
* is an array and then check the class.
|
|
125 |
* The {@code serialClass is null} in the case where a new object is not being
|
|
126 |
* created and to give the filter a chance to check the depth, number of
|
|
127 |
* references to existing objects, and the stream size.
|
|
128 |
*
|
|
129 |
* @return class of an object being deserialized; may be null
|
|
130 |
*/
|
|
131 |
Class<?> serialClass();
|
|
132 |
|
|
133 |
/**
|
|
134 |
* The number of array elements when deserializing an array of the class.
|
|
135 |
*
|
|
136 |
* @return the non-negative number of array elements when deserializing
|
|
137 |
* an array of the class, otherwise -1
|
|
138 |
*/
|
|
139 |
long arrayLength();
|
|
140 |
|
|
141 |
/**
|
|
142 |
* The current depth.
|
|
143 |
* The depth starts at {@code 1} and increases for each nested object and
|
|
144 |
* decrements when each nested object returns.
|
|
145 |
*
|
|
146 |
* @return the current depth
|
|
147 |
*/
|
|
148 |
long depth();
|
|
149 |
|
|
150 |
/**
|
|
151 |
* The current number of object references.
|
|
152 |
*
|
|
153 |
* @return the non-negative current number of object references
|
|
154 |
*/
|
|
155 |
long references();
|
|
156 |
|
|
157 |
/**
|
|
158 |
* The current number of bytes consumed.
|
|
159 |
* @implSpec {@code streamBytes} is implementation specific
|
|
160 |
* and may not be directly related to the object in the stream
|
|
161 |
* that caused the callback.
|
|
162 |
*
|
|
163 |
* @return the non-negative current number of bytes consumed
|
|
164 |
*/
|
|
165 |
long streamBytes();
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* The status of a check on the class, array length, number of references,
|
|
170 |
* depth, and stream size.
|
|
171 |
*
|
|
172 |
* @since 9
|
|
173 |
*/
|
|
174 |
enum Status {
|
|
175 |
/**
|
|
176 |
* The status is undecided, not allowed and not rejected.
|
|
177 |
*/
|
|
178 |
UNDECIDED,
|
|
179 |
/**
|
|
180 |
* The status is allowed.
|
|
181 |
*/
|
|
182 |
ALLOWED,
|
|
183 |
/**
|
|
184 |
* The status is rejected.
|
|
185 |
*/
|
|
186 |
REJECTED;
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* A utility class to set and get the process-wide filter or create a filter
|
|
191 |
* from a pattern string. If a process-wide filter is set, it will be
|
|
192 |
* used for each {@link ObjectInputStream} that does not set its own filter.
|
|
193 |
* <p>
|
|
194 |
* When setting the filter, it should be stateless and idempotent,
|
|
195 |
* reporting the same result when passed the same arguments.
|
|
196 |
* <p>
|
|
197 |
* The filter is configured using the {@link java.security.Security}
|
|
198 |
* property {@code jdk.serialFilter} and can be overridden by
|
|
199 |
* the System property {@code jdk.serialFilter}.
|
|
200 |
*
|
|
201 |
* The syntax is the same as for the {@link #createFilter(String) createFilter} method.
|
|
202 |
*
|
|
203 |
* @since 9
|
|
204 |
*/
|
|
205 |
final class Config {
|
|
206 |
/* No instances. */
|
|
207 |
private Config() {}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Lock object for process-wide filter.
|
|
211 |
*/
|
|
212 |
private final static Object serialFilterLock = new Object();
|
|
213 |
|
|
214 |
/**
|
|
215 |
* Debug: Logger
|
|
216 |
*/
|
|
217 |
private final static System.Logger configLog;
|
|
218 |
|
|
219 |
/**
|
|
220 |
* Logger for debugging.
|
|
221 |
*/
|
|
222 |
static void filterLog(System.Logger.Level level, String msg, Object... args) {
|
|
223 |
if (configLog != null) {
|
|
224 |
configLog.log(level, msg, args);
|
|
225 |
}
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* The name for the process-wide deserialization filter.
|
|
230 |
* Used as a system property and a java.security.Security property.
|
|
231 |
*/
|
|
232 |
private final static String SERIAL_FILTER_PROPNAME = "jdk.serialFilter";
|
|
233 |
|
|
234 |
/**
|
|
235 |
* The process-wide filter; may be null.
|
|
236 |
* Lookup the filter in java.security.Security or
|
|
237 |
* the system property.
|
|
238 |
*/
|
|
239 |
private final static ObjectInputFilter configuredFilter;
|
|
240 |
|
|
241 |
static {
|
|
242 |
configuredFilter = AccessController
|
|
243 |
.doPrivileged((PrivilegedAction<ObjectInputFilter>) () -> {
|
|
244 |
String props = System.getProperty(SERIAL_FILTER_PROPNAME);
|
|
245 |
if (props == null) {
|
|
246 |
props = Security.getProperty(SERIAL_FILTER_PROPNAME);
|
|
247 |
}
|
|
248 |
if (props != null) {
|
|
249 |
System.Logger log =
|
|
250 |
System.getLogger("java.io.serialization");
|
|
251 |
log.log(System.Logger.Level.INFO,
|
|
252 |
"Creating serialization filter from {0}", props);
|
|
253 |
try {
|
|
254 |
return createFilter(props);
|
|
255 |
} catch (RuntimeException re) {
|
|
256 |
log.log(System.Logger.Level.ERROR,
|
|
257 |
"Error configuring filter: {0}", re);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
return null;
|
|
261 |
});
|
|
262 |
configLog = (configuredFilter != null) ? System.getLogger("java.io.serialization") : null;
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Current configured filter.
|
|
267 |
*/
|
|
268 |
private static ObjectInputFilter serialFilter = configuredFilter;
|
|
269 |
|
|
270 |
/**
|
|
271 |
* Returns the process-wide serialization filter or {@code null} if not configured.
|
|
272 |
*
|
|
273 |
* @return the process-wide serialization filter or {@code null} if not configured
|
|
274 |
*/
|
|
275 |
public static ObjectInputFilter getSerialFilter() {
|
|
276 |
synchronized (serialFilterLock) {
|
|
277 |
return serialFilter;
|
|
278 |
}
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* Set the process-wide filter if it has not already been configured or set.
|
|
283 |
*
|
|
284 |
* @param filter the serialization filter to set as the process-wide filter; not null
|
|
285 |
* @throws SecurityException if there is security manager and the
|
|
286 |
* {@code SerializablePermission("serialFilter")} is not granted
|
|
287 |
* @throws IllegalStateException if the filter has already been set {@code non-null}
|
|
288 |
*/
|
|
289 |
public static void setSerialFilter(ObjectInputFilter filter) {
|
|
290 |
Objects.requireNonNull(filter, "filter");
|
|
291 |
SecurityManager sm = System.getSecurityManager();
|
|
292 |
if (sm != null) {
|
|
293 |
sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
|
|
294 |
}
|
|
295 |
synchronized (serialFilterLock) {
|
|
296 |
if (serialFilter != null) {
|
|
297 |
throw new IllegalStateException("Serial filter can only be set once");
|
|
298 |
}
|
|
299 |
serialFilter = filter;
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
/**
|
|
304 |
* Returns an ObjectInputFilter from a string of patterns.
|
|
305 |
* <p>
|
|
306 |
* Patterns are separated by ";" (semicolon). Whitespace is significant and
|
|
307 |
* is considered part of the pattern.
|
|
308 |
* If a pattern includes an equals assignment, "{@code =}" it sets a limit.
|
|
309 |
* If a limit appears more than once the last value is used.
|
|
310 |
* <ul>
|
|
311 |
* <li>maxdepth={@code value} - the maximum depth of a graph</li>
|
|
312 |
* <li>maxrefs={@code value} - the maximum number of internal references</li>
|
|
313 |
* <li>maxbytes={@code value} - the maximum number of bytes in the input stream</li>
|
|
314 |
* <li>maxarray={@code value} - the maximum array length allowed</li>
|
|
315 |
* </ul>
|
|
316 |
* <p>
|
|
317 |
* Other patterns match or reject class or package name
|
|
318 |
* as returned from {@link Class#getName() Class.getName()} and
|
|
319 |
* if an optional module name is present
|
|
320 |
* {@link java.lang.reflect.Module#getName() class.getModule().getName()}.
|
|
321 |
* Note that for arrays the element type is used in the pattern,
|
|
322 |
* not the array type.
|
|
323 |
* <ul>
|
|
324 |
* <li>If the pattern starts with "!", the class is rejected if the remaining pattern is matched;
|
|
325 |
* otherwise the class is allowed if the pattern matches.
|
|
326 |
* <li>If the pattern contains "/", the non-empty prefix up to the "/" is the module name;
|
|
327 |
* if the module name matches the module name of the class then
|
|
328 |
* the remaining pattern is matched with the class name.
|
|
329 |
* If there is no "/", the module name is not compared.
|
|
330 |
* <li>If the pattern ends with ".**" it matches any class in the package and all subpackages.
|
|
331 |
* <li>If the pattern ends with ".*" it matches any class in the package.
|
|
332 |
* <li>If the pattern ends with "*", it matches any class with the pattern as a prefix.
|
|
333 |
* <li>If the pattern is equal to the class name, it matches.
|
|
334 |
* <li>Otherwise, the pattern is not matched.
|
|
335 |
* </ul>
|
|
336 |
* <p>
|
|
337 |
* The resulting filter performs the limit checks and then
|
|
338 |
* tries to match the class, if any. If any of the limits are exceeded,
|
|
339 |
* the filter returns {@link Status#REJECTED Status.REJECTED}.
|
|
340 |
* If the class is an array type, the class to be matched is the element type.
|
|
341 |
* Arrays of any number of dimensions are treated the same as the element type.
|
|
342 |
* For example, a pattern of "{@code !example.Foo}",
|
|
343 |
* rejects creation of any instance or array of {@code example.Foo}.
|
|
344 |
* The first pattern that matches, working from left to right, determines
|
|
345 |
* the {@link Status#ALLOWED Status.ALLOWED}
|
|
346 |
* or {@link Status#REJECTED Status.REJECTED} result.
|
|
347 |
* If nothing matches, the result is {@link Status#UNDECIDED Status.UNDECIDED}.
|
|
348 |
*
|
|
349 |
* @param pattern the pattern string to parse; not null
|
|
350 |
* @return a filter to check a class being deserialized; may be null;
|
|
351 |
* {@code null} if no patterns
|
|
352 |
* @throws IllegalArgumentException
|
|
353 |
* if a limit is missing the name, or the long value
|
|
354 |
* is not a number or is negative,
|
|
355 |
* or the module name is missing if the pattern contains "/"
|
|
356 |
* or if the package is missing for ".*" and ".**"
|
|
357 |
*/
|
|
358 |
public static ObjectInputFilter createFilter(String pattern) {
|
|
359 |
Objects.requireNonNull(pattern, "pattern");
|
|
360 |
return Global.createFilter(pattern);
|
|
361 |
}
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Implementation of ObjectInputFilter that performs the checks of
|
|
365 |
* the process-wide serialization filter. If configured, it will be
|
|
366 |
* used for all ObjectInputStreams that do not set their own filters.
|
|
367 |
*
|
|
368 |
*/
|
|
369 |
final static class Global implements ObjectInputFilter {
|
|
370 |
/**
|
|
371 |
* The pattern used to create the filter.
|
|
372 |
*/
|
|
373 |
private final String pattern;
|
|
374 |
/**
|
|
375 |
* The list of class filters.
|
|
376 |
*/
|
|
377 |
private final List<Function<Class<?>, Status>> filters;
|
|
378 |
/**
|
|
379 |
* Maximum allowed bytes in the stream.
|
|
380 |
*/
|
|
381 |
private long maxStreamBytes;
|
|
382 |
/**
|
|
383 |
* Maximum depth of the graph allowed.
|
|
384 |
*/
|
|
385 |
private long maxDepth;
|
|
386 |
/**
|
|
387 |
* Maximum number of references in a graph.
|
|
388 |
*/
|
|
389 |
private long maxReferences;
|
|
390 |
/**
|
|
391 |
* Maximum length of any array.
|
|
392 |
*/
|
|
393 |
private long maxArrayLength;
|
|
394 |
|
|
395 |
/**
|
|
396 |
* Returns an ObjectInputFilter from a string of patterns.
|
|
397 |
*
|
|
398 |
* @param pattern the pattern string to parse
|
|
399 |
* @return a filter to check a class being deserialized; not null
|
|
400 |
* @throws IllegalArgumentException if the parameter is malformed
|
|
401 |
* if the pattern is missing the name, the long value
|
|
402 |
* is not a number or is negative.
|
|
403 |
*/
|
|
404 |
static ObjectInputFilter createFilter(String pattern) {
|
|
405 |
Global filter = new Global(pattern);
|
|
406 |
return filter.isEmpty() ? null : filter;
|
|
407 |
}
|
|
408 |
|
|
409 |
/**
|
|
410 |
* Construct a new filter from the pattern String.
|
|
411 |
*
|
|
412 |
* @param pattern a pattern string of filters
|
|
413 |
* @throws IllegalArgumentException if the pattern is malformed
|
|
414 |
*/
|
|
415 |
private Global(String pattern) {
|
|
416 |
this.pattern = pattern;
|
|
417 |
|
|
418 |
maxArrayLength = Long.MAX_VALUE; // Default values are unlimited
|
|
419 |
maxDepth = Long.MAX_VALUE;
|
|
420 |
maxReferences = Long.MAX_VALUE;
|
|
421 |
maxStreamBytes = Long.MAX_VALUE;
|
|
422 |
|
|
423 |
String[] patterns = pattern.split(";");
|
|
424 |
filters = new ArrayList<>(patterns.length);
|
|
425 |
for (int i = 0; i < patterns.length; i++) {
|
|
426 |
String p = patterns[i];
|
|
427 |
int nameLen = p.length();
|
|
428 |
if (nameLen == 0) {
|
|
429 |
continue;
|
|
430 |
}
|
|
431 |
if (parseLimit(p)) {
|
|
432 |
// If the pattern contained a limit setting, i.e. type=value
|
|
433 |
continue;
|
|
434 |
}
|
|
435 |
boolean negate = p.charAt(0) == '!';
|
|
436 |
int poffset = negate ? 1 : 0;
|
|
437 |
|
|
438 |
// isolate module name, if any
|
|
439 |
int slash = p.indexOf('/', poffset);
|
|
440 |
if (slash == poffset) {
|
|
441 |
throw new IllegalArgumentException("module name is missing in: \"" + pattern + "\"");
|
|
442 |
}
|
|
443 |
final String moduleName = (slash >= 0) ? p.substring(poffset, slash) : null;
|
|
444 |
poffset = (slash >= 0) ? slash + 1 : poffset;
|
|
445 |
|
|
446 |
final Function<Class<?>, Status> patternFilter;
|
|
447 |
if (p.endsWith("*")) {
|
|
448 |
// Wildcard cases
|
|
449 |
if (p.endsWith(".*")) {
|
|
450 |
// Pattern is a package name with a wildcard
|
|
451 |
final String pkg = p.substring(poffset, nameLen - 1);
|
|
452 |
if (pkg.length() < 2) {
|
|
453 |
throw new IllegalArgumentException("package missing in: \"" + pattern + "\"");
|
|
454 |
}
|
|
455 |
if (negate) {
|
|
456 |
// A Function that fails if the class starts with the pattern, otherwise don't care
|
|
457 |
patternFilter = c -> matchesPackage(c, pkg) ? Status.REJECTED : Status.UNDECIDED;
|
|
458 |
} else {
|
|
459 |
// A Function that succeeds if the class starts with the pattern, otherwise don't care
|
|
460 |
patternFilter = c -> matchesPackage(c, pkg) ? Status.ALLOWED : Status.UNDECIDED;
|
|
461 |
}
|
|
462 |
} else if (p.endsWith(".**")) {
|
|
463 |
// Pattern is a package prefix with a double wildcard
|
|
464 |
final String pkgs = p.substring(poffset, nameLen - 2);
|
|
465 |
if (pkgs.length() < 2) {
|
|
466 |
throw new IllegalArgumentException("package missing in: \"" + pattern + "\"");
|
|
467 |
}
|
|
468 |
if (negate) {
|
|
469 |
// A Function that fails if the class starts with the pattern, otherwise don't care
|
|
470 |
patternFilter = c -> c.getName().startsWith(pkgs) ? Status.REJECTED : Status.UNDECIDED;
|
|
471 |
} else {
|
|
472 |
// A Function that succeeds if the class starts with the pattern, otherwise don't care
|
|
473 |
patternFilter = c -> c.getName().startsWith(pkgs) ? Status.ALLOWED : Status.UNDECIDED;
|
|
474 |
}
|
|
475 |
} else {
|
|
476 |
// Pattern is a classname (possibly empty) with a trailing wildcard
|
|
477 |
final String className = p.substring(poffset, nameLen - 1);
|
|
478 |
if (negate) {
|
|
479 |
// A Function that fails if the class starts with the pattern, otherwise don't care
|
|
480 |
patternFilter = c -> c.getName().startsWith(className) ? Status.REJECTED : Status.UNDECIDED;
|
|
481 |
} else {
|
|
482 |
// A Function that succeeds if the class starts with the pattern, otherwise don't care
|
|
483 |
patternFilter = c -> c.getName().startsWith(className) ? Status.ALLOWED : Status.UNDECIDED;
|
|
484 |
}
|
|
485 |
}
|
|
486 |
} else {
|
|
487 |
final String name = p.substring(poffset);
|
|
488 |
if (name.isEmpty()) {
|
|
489 |
throw new IllegalArgumentException("class or package missing in: \"" + pattern + "\"");
|
|
490 |
}
|
|
491 |
// Pattern is a class name
|
|
492 |
if (negate) {
|
|
493 |
// A Function that fails if the class equals the pattern, otherwise don't care
|
|
494 |
patternFilter = c -> c.getName().equals(name) ? Status.REJECTED : Status.UNDECIDED;
|
|
495 |
} else {
|
|
496 |
// A Function that succeeds if the class equals the pattern, otherwise don't care
|
|
497 |
patternFilter = c -> c.getName().equals(name) ? Status.ALLOWED : Status.UNDECIDED;
|
|
498 |
}
|
|
499 |
}
|
|
500 |
// If there is a moduleName, combine the module name check with the package/class check
|
|
501 |
if (moduleName == null) {
|
|
502 |
filters.add(patternFilter);
|
|
503 |
} else {
|
|
504 |
filters.add(c -> moduleName.equals(c.getModule().getName()) ? patternFilter.apply(c) : Status.UNDECIDED);
|
|
505 |
}
|
|
506 |
}
|
|
507 |
}
|
|
508 |
|
|
509 |
/**
|
|
510 |
* Returns if this filter has any checks.
|
|
511 |
* @return {@code true} if the filter has any checks, {@code false} otherwise
|
|
512 |
*/
|
|
513 |
private boolean isEmpty() {
|
|
514 |
return filters.isEmpty() &&
|
|
515 |
maxArrayLength == Long.MAX_VALUE &&
|
|
516 |
maxDepth == Long.MAX_VALUE &&
|
|
517 |
maxReferences == Long.MAX_VALUE &&
|
|
518 |
maxStreamBytes == Long.MAX_VALUE;
|
|
519 |
}
|
|
520 |
|
|
521 |
/**
|
|
522 |
* Parse out a limit for one of maxarray, maxdepth, maxbytes, maxreferences.
|
|
523 |
*
|
|
524 |
* @param pattern a string with a type name, '=' and a value
|
|
525 |
* @return {@code true} if a limit was parsed, else {@code false}
|
|
526 |
* @throws IllegalArgumentException if the pattern is missing
|
|
527 |
* the name, the Long value is not a number or is negative.
|
|
528 |
*/
|
|
529 |
private boolean parseLimit(String pattern) {
|
|
530 |
int eqNdx = pattern.indexOf('=');
|
|
531 |
if (eqNdx < 0) {
|
|
532 |
// not a limit pattern
|
|
533 |
return false;
|
|
534 |
}
|
|
535 |
String valueString = pattern.substring(eqNdx + 1);
|
|
536 |
if (pattern.startsWith("maxdepth=")) {
|
|
537 |
maxDepth = parseValue(valueString);
|
|
538 |
} else if (pattern.startsWith("maxarray=")) {
|
|
539 |
maxArrayLength = parseValue(valueString);
|
|
540 |
} else if (pattern.startsWith("maxrefs=")) {
|
|
541 |
maxReferences = parseValue(valueString);
|
|
542 |
} else if (pattern.startsWith("maxbytes=")) {
|
|
543 |
maxStreamBytes = parseValue(valueString);
|
|
544 |
} else {
|
|
545 |
throw new IllegalArgumentException("unknown limit: " + pattern.substring(0, eqNdx));
|
|
546 |
}
|
|
547 |
return true;
|
|
548 |
}
|
|
549 |
|
|
550 |
/**
|
|
551 |
* Parse the value of a limit and check that it is non-negative.
|
|
552 |
* @param string inputstring
|
|
553 |
* @return the parsed value
|
|
554 |
* @throws IllegalArgumentException if parsing the value fails or the value is negative
|
|
555 |
*/
|
|
556 |
private static long parseValue(String string) throws IllegalArgumentException {
|
|
557 |
// Parse a Long from after the '=' to the end
|
|
558 |
long value = Long.parseLong(string);
|
|
559 |
if (value < 0) {
|
|
560 |
throw new IllegalArgumentException("negative limit: " + string);
|
|
561 |
}
|
|
562 |
return value;
|
|
563 |
}
|
|
564 |
|
|
565 |
/**
|
|
566 |
* {@inheritDoc}
|
|
567 |
*/
|
|
568 |
@Override
|
|
569 |
public Status checkInput(FilterInfo filterInfo) {
|
|
570 |
if (filterInfo.references() < 0
|
|
571 |
|| filterInfo.depth() < 0
|
|
572 |
|| filterInfo.streamBytes() < 0
|
|
573 |
|| filterInfo.references() > maxReferences
|
|
574 |
|| filterInfo.depth() > maxDepth
|
|
575 |
|| filterInfo.streamBytes() > maxStreamBytes) {
|
|
576 |
return Status.REJECTED;
|
|
577 |
}
|
|
578 |
|
|
579 |
Class<?> clazz = filterInfo.serialClass();
|
|
580 |
if (clazz != null) {
|
|
581 |
if (clazz.isArray()) {
|
|
582 |
if (filterInfo.arrayLength() >= 0 && filterInfo.arrayLength() > maxArrayLength) {
|
|
583 |
// array length is too big
|
|
584 |
return Status.REJECTED;
|
|
585 |
}
|
|
586 |
do {
|
|
587 |
// Arrays are decided based on the component type
|
|
588 |
clazz = clazz.getComponentType();
|
|
589 |
} while (clazz.isArray());
|
|
590 |
}
|
|
591 |
|
|
592 |
if (clazz.isPrimitive()) {
|
|
593 |
// Primitive types are undecided; let someone else decide
|
|
594 |
return Status.UNDECIDED;
|
|
595 |
} else {
|
|
596 |
// Find any filter that allowed or rejected the class
|
|
597 |
final Class<?> cl = clazz;
|
|
598 |
Optional<Status> status = filters.stream()
|
|
599 |
.map(f -> f.apply(cl))
|
|
600 |
.filter(p -> p != Status.UNDECIDED)
|
|
601 |
.findFirst();
|
|
602 |
return status.orElse(Status.UNDECIDED);
|
|
603 |
}
|
|
604 |
}
|
|
605 |
return Status.UNDECIDED;
|
|
606 |
}
|
|
607 |
|
|
608 |
/**
|
|
609 |
* Returns {@code true} if the class is in the package.
|
|
610 |
*
|
|
611 |
* @param c a class
|
|
612 |
* @param pkg a package name (including the trailing ".")
|
|
613 |
* @return {@code true} if the class is in the package,
|
|
614 |
* otherwise {@code false}
|
|
615 |
*/
|
|
616 |
private static boolean matchesPackage(Class<?> c, String pkg) {
|
|
617 |
String n = c.getName();
|
|
618 |
return n.startsWith(pkg) && n.lastIndexOf('.') == pkg.length() - 1;
|
|
619 |
}
|
|
620 |
|
|
621 |
/**
|
|
622 |
* Returns the pattern used to create this filter.
|
|
623 |
* @return the pattern used to create this filter
|
|
624 |
*/
|
|
625 |
@Override
|
|
626 |
public String toString() {
|
|
627 |
return pattern;
|
|
628 |
}
|
|
629 |
}
|
|
630 |
}
|
|
631 |
}
|