Skip to content

Commit b1dfb7b

Browse files
garyrussellartembilan
authored andcommitted
INT-1926: Option to disallow arbitrary routing
JIRA: https://jira.spring.io/browse/INT-1926 Add an option to mapping routers to disable falling back to the channel key as the channel name.
1 parent ef1d7be commit b1dfb7b

File tree

5 files changed

+125
-13
lines changed

5 files changed

+125
-13
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/RouterSpec.java

+15
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,21 @@ public RouterSpec<K, R> suffix(String suffix) {
109109
return _this();
110110
}
111111

112+
/**
113+
* By default, if a resolved channel key does not exist in the channel map, the key
114+
* itself is used as the channel name, which we will attempt to resolve to a channel.
115+
* Invoke this method to disable this feature. This could be useful to prevent
116+
* malicious actors from generating a message that could cause the message to be
117+
* routed to an unexpected channel, such as one upstream of the router, which would
118+
* cause a stack overflow.
119+
* @return the router spec.
120+
* @since 5.2
121+
*/
122+
public RouterSpec<K, R> noChannelKeyFallback() {
123+
this.handler.setChannelKeyFallback(false);
124+
return _this();
125+
}
126+
112127
/**
113128
* @param key the key.
114129
* @param channelName the channelName.

spring-integration-core/src/main/java/org/springframework/integration/router/AbstractMappingMessageRouter.java

+30-12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ protected boolean removeEldestEntry(Entry<String, MessageChannel> eldest) {
7474

7575
private boolean resolutionRequired = true;
7676

77+
private boolean channelKeyFallback = true;
78+
7779
private volatile Map<String, String> channelMappings = new LinkedHashMap<>();
7880

7981

@@ -116,6 +118,20 @@ public void setResolutionRequired(boolean resolutionRequired) {
116118
this.resolutionRequired = resolutionRequired;
117119
}
118120

121+
/**
122+
* When true (default), if a resolved channel key does not exist in the channel map,
123+
* the key itself is used as the channel name, which we will attempt to resolve to a
124+
* channel. Set to false to disable this feature. This could be useful to prevent
125+
* malicious actors from generating a message that could cause the message to be
126+
* routed to an unexpected channel, such as one upstream of the router, which would
127+
* cause a stack overflow.
128+
* @param channelKeyFallback false to disable the fall back.
129+
* @since 5.2
130+
*/
131+
public void setChannelKeyFallback(boolean channelKeyFallback) {
132+
this.channelKeyFallback = channelKeyFallback;
133+
}
134+
119135
/**
120136
* Set a limit for how many dynamic channels are retained (for reporting purposes).
121137
* When the limit is exceeded, the oldest channel is discarded.
@@ -241,23 +257,25 @@ private void addChannelFromString(Collection<MessageChannel> channels, String ch
241257

242258
// if the channelMappings contains a mapping, we'll use the mapped value
243259
// otherwise, the String-based channelKey itself will be used as the channel name
244-
String channelName = channelKey;
260+
String channelName = this.channelKeyFallback ? channelKey : null;
245261
boolean mapped = false;
246262
if (this.channelMappings.containsKey(channelKey)) {
247263
channelName = this.channelMappings.get(channelKey);
248264
mapped = true;
249265
}
250-
if (this.prefix != null) {
251-
channelName = this.prefix + channelName;
252-
}
253-
if (this.suffix != null) {
254-
channelName = channelName + this.suffix;
255-
}
256-
MessageChannel channel = resolveChannelForName(channelName, message);
257-
if (channel != null) {
258-
channels.add(channel);
259-
if (!mapped && this.dynamicChannels.get(channelName) == null) {
260-
this.dynamicChannels.put(channelName, channel);
266+
if (channelName != null) {
267+
if (this.prefix != null) {
268+
channelName = this.prefix + channelName;
269+
}
270+
if (this.suffix != null) {
271+
channelName = channelName + this.suffix;
272+
}
273+
MessageChannel channel = resolveChannelForName(channelName, message);
274+
if (channel != null) {
275+
channels.add(channel);
276+
if (!mapped && this.dynamicChannels.get(channelName) == null) {
277+
this.dynamicChannels.put(channelName, channel);
278+
}
261279
}
262280
}
263281
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2019 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+
* https://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.dsl.routers;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.Collections;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.integration.channel.QueueChannel;
29+
import org.springframework.integration.config.EnableIntegration;
30+
import org.springframework.integration.dsl.IntegrationFlow;
31+
import org.springframework.messaging.support.GenericMessage;
32+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
33+
34+
/**
35+
* @author Gary Russell
36+
* @since 5.2
37+
*
38+
*/
39+
@SpringJUnitConfig
40+
public class NoFallbackAllowedTests {
41+
42+
@Test
43+
void noStackOverflow(@Autowired Config config) {
44+
config.flow()
45+
.getInputChannel()
46+
.send(new GenericMessage<>("foo", Collections.singletonMap("whereTo", "flow.input")));
47+
assertThat(config.queue().receive(0)).isNotNull();
48+
}
49+
50+
@Configuration
51+
@EnableIntegration
52+
public static class Config {
53+
54+
@Bean
55+
public IntegrationFlow flow() {
56+
return f -> f.route("headers.whereTo", r -> r
57+
.noChannelKeyFallback()
58+
.defaultOutputChannel(queue()));
59+
}
60+
61+
@Bean
62+
public QueueChannel queue() {
63+
return new QueueChannel();
64+
}
65+
66+
}
67+
68+
}

src/reference/asciidoc/router.adoc

+6-1
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,11 @@ That map's setter method is exposed as a public method along with the 'setChanne
11221122
These let you change, add, and remove router mappings at runtime, as long as you have a reference to the router itself.
11231123
It also means that you could expose these same configuration options through JMX (see <<./jmx.adoc#jmx,JMX Support>>) or the Spring Integration control bus (see <<./control-bus.adoc#control-bus,Control Bus>>) functionality. 
11241124

1125+
IMPORTANT: Falling back to the channel key as the channel name is flexible and convenient.
1126+
However, if you don't trust the message creator, a malicious actor (who has knowledge of the system) could create a message that is routed to an unexpected channel.
1127+
For example, if the key is set to the channel name of the router's input channel, such a message would be routed back to the router, eventually resulting in a stack overflow error.
1128+
You may therefore wish to disable this feature (set the `channelKeyFallback` property to `false`), and change the mappings instead if needed.
1129+
11251130
[[dynamic-routers-control-bus]]
11261131
===== Manage Router Mappings using the Control Bus
11271132

@@ -1256,7 +1261,7 @@ The `routingSlipIndex` remains unchanged.
12561261
* When the `routingSlipIndex` exceeds the size of the routing slip `path` list, the algorithm moves to the default behavior for the standard `replyChannel` header.
12571262

12581263
[[process-manager]]
1259-
===== Process Manager Enterprise Integration Pattern
1264+
==== Process Manager Enterprise Integration Pattern
12601265

12611266
Enterprise integration patterns include the https://www.enterpriseintegrationpatterns.com/ProcessManager.html[process manager] pattern.
12621267
You can now easily implement this pattern by using custom process manager logic encapsulated in a `RoutingSlipRouteStrategy` within the routing slip.

src/reference/asciidoc/whats-new.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,9 @@ See <<./mongodb.adoc#mongodb, MongoDB Support>> for more information.
130130

131131
Simple Apache Avro transformers are now provided.
132132
See <<./transformers.adoc#avro-transformers, Avro Transformers>> for more information.
133+
134+
[[x5.2-routers]]
135+
==== Router Changes
136+
137+
You can now disable falling back to the channel key as the channel bean name.
138+
See <<./router.adoc#dynamic-routers, Dynamic Routers>> for more information.

0 commit comments

Comments
 (0)