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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package ssh
import (
"context"
"fmt"
"sync"
"gitlab.com/gitlab-org/gitaly/v15/internal/command"
"gitlab.com/gitlab-org/gitaly/v15/internal/git"
"gitlab.com/gitlab-org/gitaly/v15/internal/git/pktline"
"gitlab.com/gitlab-org/gitaly/v15/internal/helper"
"gitlab.com/gitlab-org/gitaly/v15/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/v15/streamio"
)
func (s *server) SSHUploadArchive(stream gitalypb.SSHService_SSHUploadArchiveServer) error {
req, err := stream.Recv() // First request contains Repository only
if err != nil {
return helper.ErrInternal(err)
}
if err = validateFirstUploadArchiveRequest(req); err != nil {
return helper.ErrInvalidArgument(err)
}
if err = s.sshUploadArchive(stream, req); err != nil {
return helper.ErrInternal(err)
}
return nil
}
func (s *server) sshUploadArchive(stream gitalypb.SSHService_SSHUploadArchiveServer, req *gitalypb.SSHUploadArchiveRequest) error {
ctx, cancelCtx := context.WithCancel(stream.Context())
defer cancelCtx()
repoPath, err := s.locator.GetRepoPath(req.Repository)
if err != nil {
return err
}
stdin := streamio.NewReader(func() ([]byte, error) {
request, err := stream.Recv()
return request.GetStdin(), err
})
var m sync.Mutex
stdout := streamio.NewSyncWriter(&m, func(p []byte) error {
return stream.Send(&gitalypb.SSHUploadArchiveResponse{Stdout: p})
})
stderr := streamio.NewSyncWriter(&m, func(p []byte) error {
return stream.Send(&gitalypb.SSHUploadArchiveResponse{Stderr: p})
})
cmd, monitor, err := monitorStdinCommand(ctx, s.gitCmdFactory, stdin, stdout, stderr, git.SubCmd{
Name: "upload-archive",
Args: []string{repoPath},
})
if err != nil {
return err
}
timeoutTicker := helper.NewTimerTicker(s.uploadArchiveRequestTimeout)
// upload-archive expects a list of options terminated by a flush packet:
// https://github.com/git/git/blob/v2.22.0/builtin/upload-archive.c#L38
//
// Place a timeout on receiving the flush packet to mitigate use-after-check
// attacks
go monitor.Monitor(ctx, pktline.PktFlush(), timeoutTicker, cancelCtx)
if err := cmd.Wait(); err != nil {
// When waiting for the packfile negotiation to end times out we'll cancel the local
// context, but not cancel the overall RPC's context. Our cancelhandler middleware
// thus cannot observe the fact that we're cancelling the context, and neither do we
// provide any valuable information to the caller that we do indeed kill the command
// because of our own internal timeout.
//
// We thus need to special-case the situation where we cancel our own context in
// order to provide that information and return a proper gRPC error code.
if ctx.Err() != nil && stream.Context().Err() == nil {
return helper.ErrDeadlineExceededf("waiting for packfile negotiation: %w", ctx.Err())
}
if status, ok := command.ExitStatus(err); ok {
if sendErr := stream.Send(&gitalypb.SSHUploadArchiveResponse{
ExitStatus: &gitalypb.ExitStatus{Value: int32(status)},
}); sendErr != nil {
return sendErr
}
return fmt.Errorf("SSHUploadPack: %v", err)
}
return fmt.Errorf("wait cmd: %v", err)
}
return stream.Send(&gitalypb.SSHUploadArchiveResponse{
ExitStatus: &gitalypb.ExitStatus{Value: 0},
})
}
func validateFirstUploadArchiveRequest(req *gitalypb.SSHUploadArchiveRequest) error {
if req.Stdin != nil {
return fmt.Errorf("non-empty stdin in first request")
}
return nil
}
|