Skip to content

Commit 3980b32

Browse files
garyrussellartembilan
authored andcommitted
Change Defaults
**cherry-pick to 3.0.x, 2.4.x** (cherry picked from commit 09c612c) # Conflicts: # spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java
1 parent dcc49ba commit 3980b32

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ subprojects { subproject ->
293293
if (name ==~ /(testAll)/) {
294294
systemProperty 'RUN_LONG_INTEGRATION_TESTS', 'true'
295295
}
296+
environment "SPRING_AMQP_DESERIALIZATION_TRUST_ALL", "true"
297+
296298
useJUnitPlatform()
297299
}
298300

spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,17 @@
3737
*/
3838
public final class SerializationUtils {
3939

40+
private static final String TRUST_ALL_ENV = "SPRING_AMQP_DESERIALIZATION_TRUST_ALL";
41+
42+
private static final String TRUST_ALL_PROP = "spring.amqp.deserialization.trust.all";
43+
44+
private static final boolean TRUST_ALL;
45+
46+
static {
47+
TRUST_ALL = Boolean.parseBoolean(System.getenv(TRUST_ALL_ENV))
48+
|| Boolean.parseBoolean(System.getProperty(TRUST_ALL_PROP));
49+
}
50+
4051
private SerializationUtils() {
4152
}
4253

@@ -136,11 +147,12 @@ protected Class<?> resolveClass(ObjectStreamClass classDesc)
136147
* @since 2.1
137148
*/
138149
public static void checkAllowedList(Class<?> clazz, Set<String> patterns) {
139-
if (ObjectUtils.isEmpty(patterns)) {
150+
if (TRUST_ALL && ObjectUtils.isEmpty(patterns)) {
140151
return;
141152
}
142153
if (clazz.isArray() || clazz.isPrimitive() || clazz.equals(String.class)
143-
|| Number.class.isAssignableFrom(clazz)) {
154+
|| Number.class.isAssignableFrom(clazz)
155+
|| String.class.equals(clazz)) {
144156
return;
145157
}
146158
String className = clazz.getName();
@@ -149,7 +161,10 @@ public static void checkAllowedList(Class<?> clazz, Set<String> patterns) {
149161
return;
150162
}
151163
}
152-
throw new SecurityException("Attempt to deserialize unauthorized " + clazz);
164+
throw new SecurityException("Attempt to deserialize unauthorized " + clazz
165+
+ "; add allowed class name patterns to the message converter or, if you trust the message orginiator, "
166+
+ "set environment variable '"
167+
+ TRUST_ALL_ENV + "' or system property '" + TRUST_ALL_PROP + "' to true");
153168
}
154169

155170
}

spring-amqp/src/test/java/org/springframework/amqp/support/converter/AllowedListDeserializingMessageConverterTests.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
1717
package org.springframework.amqp.support.converter;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.assertj.core.api.Assertions.fail;
20+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2121

2222
import java.io.Serializable;
2323
import java.util.Collections;
@@ -40,7 +40,11 @@ public void testAllowedList() throws Exception {
4040
SerializerMessageConverter converter = new SerializerMessageConverter();
4141
TestBean testBean = new TestBean("foo");
4242
Message message = converter.toMessage(testBean, new MessageProperties());
43-
Object fromMessage = converter.fromMessage(message);
43+
// when env var not set
44+
// assertThatExceptionOfType(SecurityException.class).isThrownBy(() -> converter.fromMessage(message));
45+
Object fromMessage;
46+
// when env var set.
47+
fromMessage = converter.fromMessage(message);
4448
assertThat(fromMessage).isEqualTo(testBean);
4549

4650
converter.setAllowedListPatterns(Collections.singletonList("*"));
@@ -54,15 +58,8 @@ public void testAllowedList() throws Exception {
5458
fromMessage = converter.fromMessage(message);
5559
assertThat(fromMessage).isEqualTo(testBean);
5660

57-
try {
58-
converter.setAllowedListPatterns(Collections.singletonList("foo.*"));
59-
fromMessage = converter.fromMessage(message);
60-
assertThat(fromMessage).isEqualTo(testBean);
61-
fail("Expected SecurityException");
62-
}
63-
catch (SecurityException e) {
64-
65-
}
61+
converter.setAllowedListPatterns(Collections.singletonList("foo.*"));
62+
assertThatExceptionOfType(SecurityException.class).isThrownBy(() -> converter.fromMessage(message));
6663
}
6764

6865
@SuppressWarnings("serial")

src/reference/asciidoc/amqp.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4321,14 +4321,15 @@ consider configuring which packages and classes are allowed to be deserialized.
43214321
This applies to both the `SimpleMessageConverter` and `SerializerMessageConverter` when it is configured to use a
43224322
`DefaultDeserializer` either implicitly or via configuration.
43234323
4324-
By default, the allowed list is empty, meaning all classes are deserialized.
4324+
By default, the allowed list is empty, meaning no classes will be deserialized.
43254325
43264326
You can set a list of patterns, such as `thing1.*`, `thing1.thing2.Cat` or `*.MySafeClass`.
43274327
43284328
The patterns are checked in order until a match is found.
43294329
If there is no match, a `SecurityException` is thrown.
43304330
43314331
You can set the patterns using the `allowedListPatterns` property on these converters.
4332+
Alternatively, if you trust all message originators, you can set the environment variable `SPRING_AMQP_DESERIALIZATION_TRUST_ALL` or system property `spring.amqp.deserialization.trust.all` to `true`.
43324333
====
43334334

43344335
[[message-properties-converters]]

0 commit comments

Comments
 (0)