|
1 | 1 | /*
|
2 |
| - * Copyright 2015-2016 the original author or authors. |
| 2 | + * Copyright 2015-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.jackson2;
|
18 | 18 |
|
| 19 | +import com.fasterxml.jackson.annotation.JacksonAnnotation; |
19 | 20 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
20 |
| -import com.fasterxml.jackson.databind.Module; |
21 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
22 |
| -import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder; |
| 21 | +import com.fasterxml.jackson.databind.*; |
| 22 | +import com.fasterxml.jackson.databind.cfg.MapperConfig; |
| 23 | +import com.fasterxml.jackson.databind.jsontype.*; |
23 | 24 | import org.apache.commons.logging.Log;
|
24 | 25 | import org.apache.commons.logging.LogFactory;
|
| 26 | +import org.springframework.core.annotation.AnnotationUtils; |
25 | 27 | import org.springframework.util.ClassUtils;
|
26 | 28 |
|
27 |
| -import java.util.ArrayList; |
28 |
| -import java.util.Arrays; |
29 |
| -import java.util.List; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.*; |
30 | 31 |
|
31 | 32 | /**
|
32 | 33 | * This utility class will find all the SecurityModules in classpath.
|
@@ -65,7 +66,7 @@ public static void enableDefaultTyping(ObjectMapper mapper) {
|
65 | 66 | if(mapper != null) {
|
66 | 67 | TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null);
|
67 | 68 | if (typeBuilder == null) {
|
68 |
| - mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); |
| 69 | + mapper.setDefaultTyping(createWhitelistedDefaultTyping()); |
69 | 70 | }
|
70 | 71 | }
|
71 | 72 | }
|
@@ -103,4 +104,111 @@ public static List<Module> getModules(ClassLoader loader) {
|
103 | 104 | }
|
104 | 105 | return modules;
|
105 | 106 | }
|
| 107 | + |
| 108 | + /** |
| 109 | + * Creates a TypeResolverBuilder that performs whitelisting. |
| 110 | + * @return a TypeResolverBuilder that performs whitelisting. |
| 111 | + */ |
| 112 | + private static TypeResolverBuilder<? extends TypeResolverBuilder> createWhitelistedDefaultTyping() { |
| 113 | + TypeResolverBuilder<? extends TypeResolverBuilder> result = new WhitelistTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL); |
| 114 | + result = result.init(JsonTypeInfo.Id.CLASS, null); |
| 115 | + result = result.inclusion(JsonTypeInfo.As.PROPERTY); |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * An implementation of {@link ObjectMapper.DefaultTypeResolverBuilder} that overrides the {@link TypeIdResolver} |
| 121 | + * with {@link WhitelistTypeIdResolver}. |
| 122 | + * @author Rob Winch |
| 123 | + */ |
| 124 | + static class WhitelistTypeResolverBuilder extends ObjectMapper.DefaultTypeResolverBuilder { |
| 125 | + |
| 126 | + public WhitelistTypeResolverBuilder(ObjectMapper.DefaultTyping defaultTyping) { |
| 127 | + super(defaultTyping); |
| 128 | + } |
| 129 | + |
| 130 | + protected TypeIdResolver idResolver(MapperConfig<?> config, |
| 131 | + JavaType baseType, Collection<NamedType> subtypes, boolean forSer, boolean forDeser) { |
| 132 | + TypeIdResolver result = super.idResolver(config, baseType, subtypes, forSer, forDeser); |
| 133 | + return new WhitelistTypeIdResolver(result); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * A {@link TypeIdResolver} that delegates to an existing implementation and throws an IllegalStateException if the |
| 139 | + * class being looked up is not whitelisted, does not provide an explicit mixin, and is not annotated with Jackson |
| 140 | + * mappings. See https://github.com/spring-projects/spring-security/issues/4370 |
| 141 | + */ |
| 142 | + static class WhitelistTypeIdResolver implements TypeIdResolver { |
| 143 | + private static final Set<String> WHITELIST_CLASS_NAMES = Collections.unmodifiableSet(new HashSet(Arrays.asList( |
| 144 | + "java.util.ArrayList", |
| 145 | + "java.util.Collections$EmptyMap", |
| 146 | + "java.util.Date", |
| 147 | + "java.util.TreeMap", |
| 148 | + "org.springframework.security.core.context.SecurityContextImpl" |
| 149 | + ))); |
| 150 | + |
| 151 | + private final TypeIdResolver delegate; |
| 152 | + |
| 153 | + WhitelistTypeIdResolver(TypeIdResolver delegate) { |
| 154 | + this.delegate = delegate; |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public void init(JavaType baseType) { |
| 159 | + delegate.init(baseType); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String idFromValue(Object value) { |
| 164 | + return delegate.idFromValue(value); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String idFromValueAndType(Object value, Class<?> suggestedType) { |
| 169 | + return delegate.idFromValueAndType(value, suggestedType); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public String idFromBaseType() { |
| 174 | + return delegate.idFromBaseType(); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public JavaType typeFromId(DatabindContext context, String id) throws IOException { |
| 179 | + DeserializationConfig config = (DeserializationConfig) context.getConfig(); |
| 180 | + JavaType result = delegate.typeFromId(context, id); |
| 181 | + String className = result.getRawClass().getName(); |
| 182 | + if(isWhitelisted(className)) { |
| 183 | + return delegate.typeFromId(context, id); |
| 184 | + } |
| 185 | + boolean isExplicitMixin = config.findMixInClassFor(result.getRawClass()) != null; |
| 186 | + if(isExplicitMixin) { |
| 187 | + return result; |
| 188 | + } |
| 189 | + JacksonAnnotation jacksonAnnotation = AnnotationUtils.findAnnotation(result.getRawClass(), JacksonAnnotation.class); |
| 190 | + if(jacksonAnnotation != null) { |
| 191 | + return result; |
| 192 | + } |
| 193 | + throw new IllegalArgumentException("The class with " + id + " and name of " + className + " is not whitelisted. " + |
| 194 | + "If you believe this class is safe to deserialize, please provide an explicit mapping using Jackson annotations or by providing a Mixin. " + |
| 195 | + "If the serialization is only done by a trusted source, you can also enable default typing. " + |
| 196 | + "See https://github.com/spring-projects/spring-security/issues/4370 for details"); |
| 197 | + } |
| 198 | + |
| 199 | + private boolean isWhitelisted(String id) { |
| 200 | + return WHITELIST_CLASS_NAMES.contains(id); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public String getDescForKnownTypeIds() { |
| 205 | + return delegate.getDescForKnownTypeIds(); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + public JsonTypeInfo.Id getMechanism() { |
| 210 | + return delegate.getMechanism(); |
| 211 | + } |
| 212 | + |
| 213 | + } |
106 | 214 | }
|
0 commit comments