From c7b4a1b92ef74f1040c9de93997636ecbb549c83 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 29 Mar 2021 00:14:47 +0100 Subject: [PATCH] Prevent Context.RemoteAddr() from exploding when called from ConnCallback When calling ctx.RemoteAddr() within ConnCallback one gets a panic due to unsafe casting within the accessor. I understand it's not a valid scenario as such, but I accidentally called a logger that expected that checked for RemoteAddr() in ssh.Context panic: interface conversion: interface is nil, not net.Addr Signed-off-by: Alejandro Mery --- context.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 2f61a40..505a43d 100644 --- a/context.go +++ b/context.go @@ -140,7 +140,10 @@ func (ctx *sshContext) ServerVersion() string { } func (ctx *sshContext) RemoteAddr() net.Addr { - return ctx.Value(ContextKeyRemoteAddr).(net.Addr) + if addr, ok := ctx.Value(ContextKeyRemoteAddr).(net.Addr); ok { + return addr + } + return nil } func (ctx *sshContext) LocalAddr() net.Addr {