sofastack / sofa-rpc

SOFARPC is a high-performance, high-extensibility, production-level Java RPC framework.
https://www.sofastack.tech/sofa-rpc/docs/Home
Apache License 2.0
3.81k stars 1.16k forks source link

用户手动构造SofaResponse时并且errorMsg为空字符串场景会导致异常 #1425

Open wangchengming666 opened 4 days ago

wangchengming666 commented 4 days ago

在使用sofa-hessian-go这个库时,使用hessian序列化的场景下,用户手动构造com.alipay.sofa.rpc.core.response.SofaResponse会导致异常。

SofaRPCResponse的结构体如下

type SofaRPCResponse struct {
    IsError       bool              `hessian:"isError"`
    ErrorMsg      string            `hessian:"errorMsg"`
    AppResponse   interface{}       `hessian:"appResponse"`
    ResponseProps map[string]string `hessian:"responseProps"`
}

func (s *SofaRPCResponse) GetJavaClassName() string {
    return "com.alipay.sofa.rpc.core.response.SofaResponse"
}

可以看到errorMsg为string类型,在go里string会被初始化为空字符串。此时用户想自定义appResponse这个属性为一个xxxRuntimeException,并没有设置errorMsg这个属性。

那么在com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg里,仅仅判断了errorMsg为null的场景并没有判断为空字符串的场景,那么就会将isError设置为true,导致异常类型为SofaRpcException而不是用户手动设置的xxxRuntimeException

解决方案: 可以在com.alipay.sofa.rpc.core.response.SofaResponse#setErrorMsg里使用StringUtils.isEmpty进行判断