You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2022/10/10 10:47:07 UTC
[shenyu] branch master updated: [ISSUE #3808] Checking for empty parameter for plugins. (#4064)
This is an automated email from the ASF dual-hosted git repository.
zhangzicheng 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 e14a76dc8 [ISSUE #3808] Checking for empty parameter for plugins. (#4064)
e14a76dc8 is described below
commit e14a76dc80a5bc13068fccb7aa5a32bb63d847d3
Author: lahmxu <la...@gmail.com>
AuthorDate: Mon Oct 10 18:46:57 2022 +0800
[ISSUE #3808] Checking for empty parameter for plugins. (#4064)
* [type:fix plugin] check empty parameters
* [type:fix plugin] modify the test code.
---
.../shenyu/common/utils/ParamCheckUtils.java | 19 +++++++++++---
.../shenyu/common/utils/ParamCheckUtilsTest.java | 29 +++++++++++++++++-----
.../dubbo/proxy/AlibabaDubboProxyService.java | 2 +-
.../dubbo/proxy/ApacheDubboProxyService.java | 2 +-
.../plugin/motan/proxy/MotanProxyService.java | 2 ++
.../sofa/param/SofaParamResolveServiceImpl.java | 2 ++
.../shenyu/plugin/sofa/proxy/SofaProxyService.java | 14 ++---------
.../param/SofaBodyParamResolveServiceTest.java | 8 ++++--
.../shenyu/plugin/tars/util/PrxInfoUtil.java | 2 ++
.../protocol/grpc/constant/GrpcConstants.java | 5 ++++
.../shenyu/protocol/grpc/message/JsonMessage.java | 2 ++
11 files changed, 62 insertions(+), 25 deletions(-)
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ParamCheckUtils.java b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ParamCheckUtils.java
index 157a15a6e..5e9170599 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ParamCheckUtils.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/utils/ParamCheckUtils.java
@@ -17,18 +17,31 @@
package org.apache.shenyu.common.utils;
+import org.apache.shenyu.common.exception.ShenyuException;
+
/**
* The type Param check utils.
*/
public class ParamCheckUtils {
/**
- * Dubbo body is empty boolean.
+ * Body is empty boolean.
*
* @param body the body
* @return the boolean
*/
- public static boolean dubboBodyIsEmpty(final String body) {
- return null == body || "".equals(body) || "{}".equals(body) || "null".equals(body);
+ public static boolean bodyIsEmpty(final String body) {
+ return null == body || "".equals(body) || "null".equals(body);
+ }
+
+ /**
+ * Check params length.
+ * @param argsLength params length.
+ * @param typesLength types length.
+ */
+ public static void checkParamsLength(final Integer argsLength, final Integer typesLength) {
+ if (argsLength < typesLength) {
+ throw new ShenyuException("args.length < types.length");
+ }
}
}
diff --git a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ParamCheckUtilsTest.java b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ParamCheckUtilsTest.java
index e21528772..14ae3cf24 100644
--- a/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ParamCheckUtilsTest.java
+++ b/shenyu-common/src/test/java/org/apache/shenyu/common/utils/ParamCheckUtilsTest.java
@@ -17,9 +17,13 @@
package org.apache.shenyu.common.utils;
+import org.apache.shenyu.common.exception.ShenyuException;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
@@ -28,11 +32,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public final class ParamCheckUtilsTest {
@Test
- public void testDubboBodyIsEmpty() {
- assertTrue(ParamCheckUtils.dubboBodyIsEmpty(null));
- assertTrue(ParamCheckUtils.dubboBodyIsEmpty(""));
- assertTrue(ParamCheckUtils.dubboBodyIsEmpty("{}"));
- assertTrue(ParamCheckUtils.dubboBodyIsEmpty("null"));
- assertFalse(ParamCheckUtils.dubboBodyIsEmpty("123"));
+ public void testBodyIsEmpty() {
+ assertTrue(ParamCheckUtils.bodyIsEmpty(null));
+ assertTrue(ParamCheckUtils.bodyIsEmpty(""));
+ assertTrue(ParamCheckUtils.bodyIsEmpty("null"));
+ assertFalse(ParamCheckUtils.bodyIsEmpty("123"));
+ }
+
+ @Test
+ public void testcheckParamsLength() {
+ assertDoesNotThrow(() -> {
+ ParamCheckUtils.checkParamsLength(2, 2); });
+ }
+
+ @Test
+ public void testcheckParamsLengthException() {
+ Throwable exception = assertThrows(ShenyuException.class, () -> {
+ ParamCheckUtils.checkParamsLength(1, 2);
+ });
+ assertEquals("args.length < types.length", exception.getMessage());
}
}
diff --git a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/proxy/AlibabaDubboProxyService.java b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/proxy/AlibabaDubboProxyService.java
index 61837b825..9ab66261e 100644
--- a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/proxy/AlibabaDubboProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/proxy/AlibabaDubboProxyService.java
@@ -71,7 +71,7 @@ public class AlibabaDubboProxyService {
try {
GenericService genericService = reference.get();
Pair<String[], Object[]> pair;
- if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.dubboBodyIsEmpty(body)) {
+ if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.bodyIsEmpty(body)) {
pair = new ImmutablePair<>(new String[]{}, new Object[]{});
} else {
pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes());
diff --git a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
index 6d4190f40..b2f2ec82d 100644
--- a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
@@ -70,7 +70,7 @@ public class ApacheDubboProxyService {
}
GenericService genericService = reference.get();
Pair<String[], Object[]> pair;
- if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.dubboBodyIsEmpty(body)) {
+ if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.bodyIsEmpty(body)) {
pair = new ImmutablePair<>(new String[]{}, new Object[]{});
} else {
pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes());
diff --git a/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java b/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
index 52cfd7017..5810c80fc 100644
--- a/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-motan/src/main/java/org/apache/shenyu/plugin/motan/proxy/MotanProxyService.java
@@ -31,6 +31,7 @@ import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.enums.ResultEnum;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.common.utils.ParamCheckUtils;
import org.apache.shenyu.common.utils.Singleton;
import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
import org.apache.shenyu.plugin.motan.cache.ApplicationConfigCache;
@@ -91,6 +92,7 @@ public class MotanProxyService {
int num = motanParamInfo.getParamTypes().length;
params = new Object[num];
Map<String, Object> bodyMap = GsonUtils.getInstance().convertToMap(body);
+ ParamCheckUtils.checkParamsLength(bodyMap.size(), motanParamInfo.getParamNames().length);
for (int i = 0; i < num; i++) {
params[i] = bodyMap.get(motanParamInfo.getParamNames()[i]).toString();
}
diff --git a/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java b/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java
index c494ba2c5..4d5bf3a79 100644
--- a/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java
+++ b/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/param/SofaParamResolveServiceImpl.java
@@ -26,6 +26,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.common.utils.ParamCheckUtils;
import org.springframework.lang.NonNull;
import java.util.ArrayList;
@@ -45,6 +46,7 @@ public class SofaParamResolveServiceImpl implements SofaParamResolveService {
List<Object> values = new ArrayList<>();
final List<Object> params = new ArrayList<>(GsonUtils.getInstance().toObjectMap(body).values());
for (int paramIndex = 0; paramIndex < parameterTypeStrings.length; paramIndex++) {
+ ParamCheckUtils.checkParamsLength(params.size(), parameterTypeStrings.length);
final String[] parameter = StringUtils.split(parameterTypeStrings[paramIndex], "#");
parameterTypeArr.add(parameter[0]);
values.add(convertToParameterValue(params.get(paramIndex), parameter));
diff --git a/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/proxy/SofaProxyService.java b/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/proxy/SofaProxyService.java
index 6bbeb0c5d..51f5b8d46 100644
--- a/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/proxy/SofaProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-sofa/src/main/java/org/apache/shenyu/plugin/sofa/proxy/SofaProxyService.java
@@ -31,6 +31,7 @@ import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.ResultEnum;
import org.apache.shenyu.common.exception.ShenyuException;
+import org.apache.shenyu.common.utils.ParamCheckUtils;
import org.apache.shenyu.plugin.sofa.cache.ApplicationConfigCache;
import org.apache.shenyu.plugin.sofa.param.SofaParamResolveService;
import org.springframework.web.server.ServerWebExchange;
@@ -72,7 +73,7 @@ public class SofaProxyService {
}
Pair<String[], Object[]> pair;
- if (StringUtils.isBlank(metaData.getParameterTypes()) || parameterIsNone(body)) {
+ if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.bodyIsEmpty(body)) {
pair = new ImmutablePair<>(new String[]{}, new Object[]{});
} else {
pair = sofaParamResolveService.buildParameter(body, metaData.getParameterTypes());
@@ -107,15 +108,4 @@ public class SofaProxyService {
return ret;
})).onErrorMap(ShenyuException::new);
}
-
- /**
- * check request body.<br>
- * if none [null, none, none json, null string].
- *
- * @param parameterBody parameter body
- * @return check result
- */
- private boolean parameterIsNone(final String parameterBody) {
- return null == parameterBody || "".equals(parameterBody) || "{}".equals(parameterBody) || "null".equals(parameterBody);
- }
}
diff --git a/shenyu-plugin/shenyu-plugin-sofa/src/test/java/org/apache/shenyu/plugin/sofa/param/SofaBodyParamResolveServiceTest.java b/shenyu-plugin/shenyu-plugin-sofa/src/test/java/org/apache/shenyu/plugin/sofa/param/SofaBodyParamResolveServiceTest.java
index 917f1f9d2..79558b317 100644
--- a/shenyu-plugin/shenyu-plugin-sofa/src/test/java/org/apache/shenyu/plugin/sofa/param/SofaBodyParamResolveServiceTest.java
+++ b/shenyu-plugin/shenyu-plugin-sofa/src/test/java/org/apache/shenyu/plugin/sofa/param/SofaBodyParamResolveServiceTest.java
@@ -27,6 +27,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.lang.NonNull;
import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -59,8 +61,10 @@ public final class SofaBodyParamResolveServiceTest {
parameterTypes = "org.apache.shenyu.web.rpc.DubboMultiParameterResolveServiceImplTest.Student[]";
pair = impl.buildParameter(body, parameterTypes);
assertLeftAndRightSame(pair, 1);
- assertIsStudent(pair.getRight()[0], true);
-
+ List<Student> right = new ArrayList<>((Collection) pair.getRight()[0]);
+ assertIsStudent(right.get(0), true);
+ assertIsStudent(right.get(1), true);
+
//language=JSON
body = "{\"ids\":[\"123\",\"456\"],\"name\":\"hello world\"}\n";
parameterTypes = "java.lang.Integer[],java.lang.String";
diff --git a/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java b/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java
index ab2c40041..1441959fb 100644
--- a/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java
+++ b/shenyu-plugin/shenyu-plugin-tars/src/main/java/org/apache/shenyu/plugin/tars/util/PrxInfoUtil.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.plugin.tars.util;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.common.utils.ParamCheckUtils;
import java.util.HashMap;
import java.util.Map;
@@ -143,6 +144,7 @@ public final class PrxInfoUtil {
*/
public static Object[] getParamArray(final Class<?>[] paramTypes, final String[] paramNames, final String body) {
Map<String, Object> bodyMap = GsonUtils.getInstance().convertToMap(body);
+ ParamCheckUtils.checkParamsLength(bodyMap.size(), paramNames.length);
Object[] param = new Object[paramNames.length];
for (int i = 0; i < paramNames.length; i++) {
String paramName = paramNames[i];
diff --git a/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/constant/GrpcConstants.java b/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/constant/GrpcConstants.java
index 9c8d2a6cb..368a889a8 100644
--- a/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/constant/GrpcConstants.java
+++ b/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/constant/GrpcConstants.java
@@ -37,4 +37,9 @@ public class GrpcConstants {
*/
public static final String JSON_DESCRIPTOR_PROTO_FIELD_NAME = "data";
+ /**
+ * json descriptor proto field num.
+ */
+ public static final Integer JSON_DESCRIPTOR_PROTO_FIELD_NUM = 1;
+
}
diff --git a/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/message/JsonMessage.java b/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/message/JsonMessage.java
index 949b8e725..ca979ed29 100644
--- a/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/message/JsonMessage.java
+++ b/shenyu-protocol/shenyu-protocol-grpc/src/main/java/org/apache/shenyu/protocol/grpc/message/JsonMessage.java
@@ -25,6 +25,7 @@ import com.google.protobuf.DynamicMessage;
import com.google.protobuf.ExtensionRegistryLite;
import io.grpc.MethodDescriptor;
import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.common.utils.ParamCheckUtils;
import org.apache.shenyu.protocol.grpc.constant.GrpcConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -83,6 +84,7 @@ public class JsonMessage {
* @return DynamicMessageList
*/
public static List<DynamicMessage> buildJsonMessageList(final Map<String, Object> jsonParamMap) {
+ ParamCheckUtils.checkParamsLength(jsonParamMap.size(), GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NUM);
JsonArray jsonParams = (JsonArray) jsonParamMap.get(GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME);
List<DynamicMessage> jsonMessageList = new ArrayList<>(jsonParams.size());
jsonParams.forEach(jsonParam -> {