1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package hook
import (
"context"
"io"
"net"
"testing"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/helper"
"google.golang.org/grpc/metadata"
)
func TestSidechannel(t *testing.T) {
// Client side
ctxOut, wt, err := SetupSidechannel(
context.Background(),
func(c *net.UnixConn) error {
_, err := io.WriteString(c, "ping")
return err
},
)
require.NoError(t, err)
defer wt.Close()
// Server side
ctxIn := helper.OutgoingToIncoming(ctxOut)
c, err := GetSidechannel(ctxIn)
require.NoError(t, err)
defer c.Close()
buf, err := io.ReadAll(c)
require.NoError(t, err)
require.Equal(t, "ping", string(buf))
// Client side
require.NoError(t, wt.Wait())
}
func TestGetSidechannel(t *testing.T) {
testCases := []string{
"foobar",
"sc.foo/../../bar",
"foo/../../bar",
"/etc/passwd",
}
for _, tc := range testCases {
t.Run(tc, func(t *testing.T) {
ctx := metadata.NewIncomingContext(
context.Background(),
map[string][]string{sidechannelHeader: {tc}},
)
_, err := GetSidechannel(ctx)
require.Error(t, err)
require.Equal(t, &errInvalidSidechannelAddress{tc}, err)
})
}
}
|