|
| 1 | +/* |
| 2 | + * Copyright 2002-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.support.converter; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.ObjectInputStream; |
| 22 | +import java.io.ObjectStreamClass; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.LinkedHashSet; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.springframework.beans.DirectFieldAccessor; |
| 28 | +import org.springframework.core.ConfigurableObjectInputStream; |
| 29 | +import org.springframework.core.NestedIOException; |
| 30 | +import org.springframework.core.convert.converter.Converter; |
| 31 | +import org.springframework.core.serializer.DefaultDeserializer; |
| 32 | +import org.springframework.core.serializer.Deserializer; |
| 33 | +import org.springframework.core.serializer.support.SerializationFailedException; |
| 34 | +import org.springframework.util.Assert; |
| 35 | +import org.springframework.util.PatternMatchUtils; |
| 36 | + |
| 37 | +/** |
| 38 | + * A {@link Converter} that delegates to a |
| 39 | + * {@link org.springframework.core.serializer.Deserializer} to convert data in a byte |
| 40 | + * array to an object. By default, if using a {@link DefaultDeserializer} all |
| 41 | + * classes/packages are deserialized. If you receive data from untrusted sources, consider |
| 42 | + * adding trusted classes/packages using {@link #setWhiteListPatterns(String...)} or |
| 43 | + * {@link #addWhiteListPatterns(String...)}. |
| 44 | + * |
| 45 | + * @author Gary Russell |
| 46 | + * @author Mark Fisher |
| 47 | + * @author Juergen Hoeller |
| 48 | + * @since 4.2.13 |
| 49 | + */ |
| 50 | +public class WhiteListDeserializingConverter implements Converter<byte[], Object> { |
| 51 | + |
| 52 | + private final Deserializer<Object> deserializer; |
| 53 | + |
| 54 | + private final ClassLoader defaultDeserializerClassLoader; |
| 55 | + |
| 56 | + private final boolean usingDefaultDeserializer; |
| 57 | + |
| 58 | + private final Set<String> whiteListPatterns = new LinkedHashSet<String>(); |
| 59 | + |
| 60 | + |
| 61 | + /** |
| 62 | + * Create a {@code WhiteListDeserializingConverter} with default |
| 63 | + * {@link java.io.ObjectInputStream} configuration, using the "latest user-defined |
| 64 | + * ClassLoader". |
| 65 | + */ |
| 66 | + public WhiteListDeserializingConverter() { |
| 67 | + this(new DefaultDeserializer()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Create a {@code WhiteListDeserializingConverter} for using an |
| 72 | + * {@link java.io.ObjectInputStream} with the given {@code ClassLoader}. |
| 73 | + * @param classLoader the class loader to use for deserialization. |
| 74 | + */ |
| 75 | + public WhiteListDeserializingConverter(ClassLoader classLoader) { |
| 76 | + this(new DefaultDeserializer(classLoader)); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create a {@code WhiteListDeserializingConverter} that delegates to the provided |
| 81 | + * {@link Deserializer}. |
| 82 | + * @param deserializer the deserializer to use. |
| 83 | + */ |
| 84 | + public WhiteListDeserializingConverter(Deserializer<Object> deserializer) { |
| 85 | + Assert.notNull(deserializer, "Deserializer must not be null"); |
| 86 | + this.deserializer = deserializer; |
| 87 | + if (deserializer instanceof DefaultDeserializer) { |
| 88 | + ClassLoader classLoader = null; |
| 89 | + try { |
| 90 | + classLoader = (ClassLoader) new DirectFieldAccessor(deserializer).getPropertyValue("classLoader"); |
| 91 | + } |
| 92 | + catch (Exception e) { |
| 93 | + // no-op |
| 94 | + } |
| 95 | + this.defaultDeserializerClassLoader = classLoader; |
| 96 | + this.usingDefaultDeserializer = true; |
| 97 | + } |
| 98 | + else { |
| 99 | + this.defaultDeserializerClassLoader = null; |
| 100 | + this.usingDefaultDeserializer = false; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Set simple patterns for allowable packages/classes for deserialization. |
| 106 | + * The patterns will be applied in order until a match is found. |
| 107 | + * A class can be fully qualified or a wildcard '*' is allowed at the |
| 108 | + * beginning or end of the class name. |
| 109 | + * Examples: {@code com.foo.*}, {@code *.MyClass}. |
| 110 | + * @param whiteListPatterns the patterns. |
| 111 | + */ |
| 112 | + public void setWhiteListPatterns(String... whiteListPatterns) { |
| 113 | + this.whiteListPatterns.clear(); |
| 114 | + Collections.addAll(this.whiteListPatterns, whiteListPatterns); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Add package/class patterns to the white list. |
| 119 | + * @param patterns the patterns to add. |
| 120 | + * @see #setWhiteListPatterns(String...) |
| 121 | + */ |
| 122 | + public void addWhiteListPatterns(String... patterns) { |
| 123 | + Collections.addAll(this.whiteListPatterns, patterns); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Object convert(byte[] source) { |
| 128 | + ByteArrayInputStream byteStream = new ByteArrayInputStream(source); |
| 129 | + try { |
| 130 | + if (this.usingDefaultDeserializer) { |
| 131 | + return deserialize(byteStream); |
| 132 | + } |
| 133 | + else { |
| 134 | + return this.deserializer.deserialize(byteStream); |
| 135 | + } |
| 136 | + } |
| 137 | + catch (Throwable ex) { |
| 138 | + throw new SerializationFailedException("Failed to deserialize payload. " + |
| 139 | + "Is the byte array a result of corresponding serialization for " + |
| 140 | + this.deserializer.getClass().getSimpleName() + "?", ex); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + protected Object deserialize(ByteArrayInputStream inputStream) throws IOException { |
| 145 | + try { |
| 146 | + ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, |
| 147 | + this.defaultDeserializerClassLoader) { |
| 148 | + |
| 149 | + @Override |
| 150 | + protected Class<?> resolveClass(ObjectStreamClass classDesc) |
| 151 | + throws IOException, ClassNotFoundException { |
| 152 | + Class<?> clazz = super.resolveClass(classDesc); |
| 153 | + checkWhiteList(clazz); |
| 154 | + return clazz; |
| 155 | + } |
| 156 | + |
| 157 | + }; |
| 158 | + return objectInputStream.readObject(); |
| 159 | + } |
| 160 | + catch (ClassNotFoundException ex) { |
| 161 | + throw new NestedIOException("Failed to deserialize object type", ex); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + protected void checkWhiteList(Class<?> clazz) throws IOException { |
| 166 | + if (this.whiteListPatterns.isEmpty()) { |
| 167 | + return; |
| 168 | + } |
| 169 | + if (clazz.isArray() || clazz.isPrimitive() || clazz.equals(String.class) |
| 170 | + || Number.class.isAssignableFrom(clazz)) { |
| 171 | + return; |
| 172 | + } |
| 173 | + String className = clazz.getName(); |
| 174 | + for (String pattern : this.whiteListPatterns) { |
| 175 | + if (PatternMatchUtils.simpleMatch(pattern, className)) { |
| 176 | + return; |
| 177 | + } |
| 178 | + } |
| 179 | + throw new SecurityException("Attempt to deserialize unauthorized " + clazz); |
| 180 | + } |
| 181 | + |
| 182 | +} |
0 commit comments