summaryrefslogtreecommitdiff
path: root/internal/sshd/connection_test.go
diff options
context:
space:
mode:
authorIgor Drozdov <idrozdov@gitlab.com>2021-04-26 06:37:54 +0000
committerIgor Drozdov <idrozdov@gitlab.com>2021-04-26 06:37:54 +0000
commit9778116c4f213cd7015ac4ebb00ba65f996568f5 (patch)
tree90aeb2d92d5aadcbfaa0f1e2cb812df5cda859e3 /internal/sshd/connection_test.go
parent39792693a2a2d06669103714e7fa9da83b0e9b12 (diff)
parent8cd3599f820b3b877626c5802471bfb85218ab16 (diff)
downloadgitlab-shell-9778116c4f213cd7015ac4ebb00ba65f996568f5.tar.gz
Merge branch '511-be-safe-against-panics' into 'main'
sshd: Recover from per-session and per-connection panics Closes #511 See merge request gitlab-org/gitlab-shell!464
Diffstat (limited to 'internal/sshd/connection_test.go')
-rw-r--r--internal/sshd/connection_test.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/internal/sshd/connection_test.go b/internal/sshd/connection_test.go
new file mode 100644
index 0000000..03e9209
--- /dev/null
+++ b/internal/sshd/connection_test.go
@@ -0,0 +1,49 @@
+package sshd
+
+import (
+ "context"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "golang.org/x/crypto/ssh"
+)
+
+type fakeNewChannel struct {
+ channelType string
+ extraData []byte
+}
+
+func (f *fakeNewChannel) Accept() (ssh.Channel, <-chan *ssh.Request, error) {
+ return nil, nil, nil
+}
+
+func (f *fakeNewChannel) Reject(reason ssh.RejectionReason, message string) error {
+ return nil
+}
+
+func (f *fakeNewChannel) ChannelType() string {
+ return f.channelType
+}
+
+func (f *fakeNewChannel) ExtraData() []byte {
+ return f.extraData
+}
+
+func TestPanicDuringSessionIsRecovered(t *testing.T) {
+ numSessions := 0
+ conn := newConnection(1, "127.0.0.1:50000")
+
+ newChannel := &fakeNewChannel{channelType: "session"}
+ chans := make(chan ssh.NewChannel, 1)
+ chans <- newChannel
+
+ require.NotPanics(t, func() {
+ conn.handle(context.Background(), chans, func(context.Context, ssh.Channel, <-chan *ssh.Request) {
+ numSessions += 1
+ close(chans)
+ panic("This is a panic")
+ })
+ })
+
+ require.Equal(t, numSessions, 1)
+}