This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 3b2da1d0f0 fix(global): handle unknown rpc types safely (#7195)
3b2da1d0f0 is described below
commit 3b2da1d0f06cbf21c96c90779761a3170045cef5
Author: Liming Deng <[email protected]>
AuthorDate: Thu Oct 1 06:28:44 2026 +0800
fix(global): handle unknown rpc types safely (#7195)
---
.../plugin/global/DefaultShenyuContextBuilder.java | 12 +++++++++--
.../global/DefaultShenyuContextBuilderTest.java | 24 ++++++++++++++++++++++
2 files changed, 34 insertions(+), 2 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
b/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
index 2ce2536f1b..e8efcd2cf3 100644
---
a/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
+++
b/shenyu-plugin/shenyu-plugin-global/src/main/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilder.java
@@ -22,6 +22,7 @@ import org.apache.commons.lang3.tuple.Pair;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextBuilder;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
@@ -59,14 +60,21 @@ public class DefaultShenyuContextBuilder implements
ShenyuContextBuilder {
@Override
public ShenyuContext build(final ServerWebExchange exchange) {
Pair<String, MetaData> buildData = buildData(exchange);
- return
decoratorMap.get(buildData.getLeft()).decorator(buildDefaultContext(exchange.getRequest()),
buildData.getRight());
+ ShenyuContextDecorator decorator =
decoratorMap.get(buildData.getLeft());
+ if (Objects.isNull(decorator)) {
+ decorator = decoratorMap.get(RpcTypeEnum.HTTP.getName());
+ }
+ if (Objects.isNull(decorator)) {
+ throw new ShenyuException("No ShenyuContextDecorator available for
rpc type: " + buildData.getLeft());
+ }
+ return decorator.decorator(buildDefaultContext(exchange.getRequest()),
buildData.getRight());
}
private Pair<String, MetaData> buildData(final ServerWebExchange exchange)
{
ServerHttpRequest request = exchange.getRequest();
HttpHeaders headers = request.getHeaders();
String rpcType = headers.getFirst(RPC_TYPE);
- if (StringUtils.isNotEmpty(rpcType)) {
+ if (StringUtils.isNotEmpty(rpcType) &&
decoratorMap.containsKey(rpcType)) {
return Pair.of(rpcType, new MetaData());
}
String upgrade = headers.getFirst(UPGRADE);
diff --git
a/shenyu-plugin/shenyu-plugin-global/src/test/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilderTest.java
b/shenyu-plugin/shenyu-plugin-global/src/test/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilderTest.java
index d6442f0f1d..92551c4715 100644
---
a/shenyu-plugin/shenyu-plugin-global/src/test/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilderTest.java
+++
b/shenyu-plugin/shenyu-plugin-global/src/test/java/org/apache/shenyu/plugin/global/DefaultShenyuContextBuilderTest.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.plugin.global;
import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.plugin.api.context.ShenyuContext;
import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator;
import
org.apache.shenyu.plugin.global.fixture.FixtureHttpShenyuContextDecorator;
@@ -34,6 +35,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* The Test Case For DefaultShenyuContextBuilder.
@@ -61,6 +63,28 @@ public final class DefaultShenyuContextBuilderTest {
assertEquals(RpcTypeEnum.HTTP.getName(), shenyuContext.getRpcType());
}
+ @Test
+ public void testBuildWithUnknownRpcTypeFallsBackToHttp() {
+ MockServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost:8080/http")
+ .header("rpc_type", "unknown")
+ .build());
+
+ ShenyuContext shenyuContext =
defaultShenyuContextBuilder.build(exchange);
+
+ assertNotNull(shenyuContext);
+ assertEquals(RpcTypeEnum.HTTP.getName(), shenyuContext.getRpcType());
+ }
+
+ @Test
+ public void testBuildWithoutFallbackDecorator() {
+ DefaultShenyuContextBuilder contextBuilder = new
DefaultShenyuContextBuilder(new HashMap<>());
+ MockServerWebExchange exchange =
MockServerWebExchange.from(MockServerHttpRequest.get("http://localhost:8080/http")
+ .header("rpc_type", "unknown")
+ .build());
+
+ assertThrows(ShenyuException.class, () ->
contextBuilder.build(exchange));
+ }
+
@Test
public void testBuildWithWebSocketUpgradeHeaderValueCaseInsensitive() {
// RFC 6455 requires the Upgrade header value to be compared
case-insensitively