[Bug] Unsafe type assertions without comma-ok cause panics (config/registry/protocol) (P1)
Description
Multiple single-value type assertions (x.(T) without the comma-ok form) will panic with panic: interface conversion: ... is nil, not T (or wrong concrete type) when the stored value does not match the expected type. These sit on hot paths such as service discovery and response decoding.
Affected locations
config_center/apollo/impl.go:121—return value.(string), nil(panics when the Apollo value is not a string)registry/protocol/protocol.go:97—return actualReg.(registry.Registry)(panics on type mismatch)registry/directory/directory.go:502,571,589— multiplevalue.(protocolbase.Invoker)assertionsprotocol/dubbo/dubbo_codec.go:148-150—response.Result.(result.RPCResult)(3 occurrences)filter/accesslog/filter.go:140-162—itf.(string)etc. (8 occurrences on attachment values)
Example risk
// config_center/apollo/impl.go:121
return value.(string), nil
// If value holds int/bool/nil, this panics instead of returning an error.
Suggested fix
Replace single-value assertions with the comma-ok form and return a descriptive error (or fall back gracefully):
s, ok := value.(string)
if !ok {
return "", perrors.Errorf("apollo value is not a string: %T", value)
}
return s, nil
Apply the same pattern across the locations listed above.
Severity
P1 — incorrect config / unexpected data types cause hard panics on core RPC and discovery paths.
Environment
- Reproducible on current
developtip (3.3.2 prep).
go vet check: config_center/apollo, registry/protocol, registry/directory, protocol/dubbo, filter/accesslog pass with zero default warnings. Unsafe type assertions without comma-ok are not flagged by default go vet (only the assert analyzer, and not these cases). See #3552 for the full verification note.
看过了 Issue 里列出的 Affected locations,可以简单分成下面几类:
-
config_center/apollo/impl.go:121
Apollo 缓存返回的是any,YAML 或自定义解析器可能产生数字、布尔值等非字符串值。这里直接执行value.(string)会 panic。 -
registry/protocol/protocol.go:97
理论风险,实际风险较低registries是包内私有缓存,写入值来自返回类型已经是registry.Registry的extension.GetRegistry。正常情况下不会出现其他类型,除非扩展错误地返回nil, nil或内部缓存被破坏。这里更多是内部不变量,而不是外部数据直接导致的风险。 -
registry/directory/directory.go:502、571、589
实际风险较低cacheInvokersMap同样是包内私有缓存,只有doCacheInvoker会向里面写入protocolbase.Invoker。正常调用链能够保证类型。盲目使用 comma-ok 并跳过错误值,可能反而掩盖内部状态损坏; -
protocol/dubbo/dubbo_codec.go:148-150
能够复现,但正常生产路径已有约束 它要求response.Result必须是值类型result.RPCResult。如果传入常见的指针类型*result.RPCResult,就会 panic,我已经实际复现。
不过当前 Getty 服务端 handler 的返回类型固定为result.RPCResult,所以正常服务端响应链路不会触发。这里是响应编码,不是 issue 描述的响应解码。 -
filter/accesslog/filter.go:140-162
Invocation attachments 的类型本来就是map[string]any,应用代码和其他 filter 可以写入任意类型。Accesslog 却把 interface、version、group、timestamp、地址等值直接断言为字符串。传入int时我已经复现interface {} is int, not string。日志功能不应该因为 attachment 类型异常导致整个 RPC panic,应该安全转换、忽略异常值或记录告警。
close via #3563