Skip to content

Commit bcf89fe

Browse files
authored
[Improvement-17311][RPC] Validate the args type in RPC method (#17312)
1 parent 3b1d631 commit bcf89fe

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

dolphinscheduler-extract/dolphinscheduler-extract-base/src/main/java/org/apache/dolphinscheduler/extract/base/server/JdkDynamicServerHandler.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import java.util.concurrent.RejectedExecutionException;
3434

3535
import lombok.extern.slf4j.Slf4j;
36+
37+
import com.google.common.collect.Lists;
38+
3639
import io.netty.channel.Channel;
3740
import io.netty.channel.ChannelConfig;
3841
import io.netty.channel.ChannelHandler;
@@ -100,6 +103,11 @@ private void processReceived(final Channel channel, final Transporter transporte
100103
if (standardRpcRequest.getArgs() == null || standardRpcRequest.getArgs().length == 0) {
101104
args = null;
102105
} else {
106+
if (!methodInvoker.isParameterTypeValidated(standardRpcRequest.getArgsTypes())) {
107+
throw new IllegalArgumentException(
108+
"Parameter types: " + Lists.newArrayList(standardRpcRequest.getArgsTypes())
109+
+ " do not match the method signature.");
110+
}
103111
args = new Object[standardRpcRequest.getArgs().length];
104112
for (int i = 0; i < standardRpcRequest.getArgs().length; i++) {
105113
args[i] = JsonSerializer.deserialize(standardRpcRequest.getArgs()[i],

dolphinscheduler-extract/dolphinscheduler-extract-base/src/main/java/org/apache/dolphinscheduler/extract/base/server/ServerMethodInvoker.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ interface ServerMethodInvoker {
2525

2626
Object invoke(final Object... arg) throws Throwable;
2727

28+
boolean isParameterTypeValidated(Class<?>[] argsTypes);
2829
}

dolphinscheduler-extract/dolphinscheduler-extract-base/src/main/java/org/apache/dolphinscheduler/extract/base/server/ServerMethodInvokerImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import java.lang.reflect.InvocationTargetException;
2121
import java.lang.reflect.Method;
22+
import java.util.List;
23+
24+
import com.google.common.collect.Lists;
2225

2326
class ServerMethodInvokerImpl implements ServerMethodInvoker {
2427

@@ -28,10 +31,13 @@ class ServerMethodInvokerImpl implements ServerMethodInvoker {
2831

2932
private final String methodIdentify;
3033

34+
private final List<Class<?>> parameterTypes;
35+
3136
ServerMethodInvokerImpl(Object serviceBean, Method method) {
3237
this.serviceBean = serviceBean;
3338
this.method = method;
3439
this.methodIdentify = method.toGenericString();
40+
this.parameterTypes = Lists.newArrayList(method.getParameterTypes());
3541
}
3642

3743
@Override
@@ -44,6 +50,26 @@ public Object invoke(Object... args) throws Throwable {
4450
}
4551
}
4652

53+
@Override
54+
public boolean isParameterTypeValidated(Class<?>[] argsTypes) {
55+
if (argsTypes == null || argsTypes.length == 0) {
56+
return parameterTypes.isEmpty();
57+
}
58+
if (parameterTypes.size() != argsTypes.length) {
59+
return false;
60+
}
61+
for (int i = 0; i < parameterTypes.size(); i++) {
62+
Class<?> argType = argsTypes[i];
63+
if (argType == null) {
64+
continue;
65+
}
66+
if (!parameterTypes.get(i).isAssignableFrom(argType)) {
67+
return false;
68+
}
69+
}
70+
return true;
71+
}
72+
4773
@Override
4874
public String getMethodIdentify() {
4975
return methodIdentify;

dolphinscheduler-extract/dolphinscheduler-extract-base/src/test/java/org/apache/dolphinscheduler/extract/base/client/ClientsTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424
import org.apache.dolphinscheduler.extract.base.RpcMethod;
2525
import org.apache.dolphinscheduler.extract.base.RpcMethodRetryStrategy;
2626
import org.apache.dolphinscheduler.extract.base.RpcService;
27+
import org.apache.dolphinscheduler.extract.base.config.NettyClientConfig;
2728
import org.apache.dolphinscheduler.extract.base.config.NettyServerConfig;
2829
import org.apache.dolphinscheduler.extract.base.exception.MethodInvocationException;
2930
import org.apache.dolphinscheduler.extract.base.exception.RemoteException;
3031
import org.apache.dolphinscheduler.extract.base.server.SpringServerMethodInvokerDiscovery;
32+
import org.apache.dolphinscheduler.extract.base.utils.Host;
3133

3234
import org.apache.commons.lang3.RandomUtils;
3335
import org.apache.commons.lang3.StringUtils;
3436

37+
import java.lang.reflect.Method;
3538
import java.time.Duration;
3639

3740
import org.awaitility.Awaitility;
@@ -98,6 +101,20 @@ public void testVoid() {
98101
assertDoesNotThrow(proxyClient::voidMethod);
99102
}
100103

104+
@Test
105+
public void testIllegalArgsType() throws Throwable {
106+
final Method method = IService.class.getMethod("ping", String.class);
107+
final SyncClientMethodInvoker methodInvoker = new SyncClientMethodInvoker(Host.of(serverAddress), method,
108+
new NettyRemotingClient(new NettyClientConfig()));
109+
final Integer[] args = {1}; // Invalid argument type, should be String;
110+
MethodInvocationException methodInvocationException = assertThrows(
111+
MethodInvocationException.class,
112+
() -> methodInvoker.invoke(null, method, args));
113+
assertEquals("Parameter types: [class java.lang.Integer] do not match the method signature.",
114+
methodInvocationException.getMessage());
115+
116+
}
117+
101118
@AfterEach
102119
public void tearDown() {
103120
springServerMethodInvokerDiscovery.close();

0 commit comments

Comments
 (0)