Skip to content

Commit e5fe24f

Browse files
Vasilii IakliushinGitLab
authored andcommitted
Merge branch 'duo-edit-20260106-104411' into 'main'
Fix linting warnings in client package See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1356 Merged-by: Vasilii Iakliushin <viakliushin@gitlab.com> Approved-by: Stan Hu <stanhu@gmail.com>
2 parents 1830673 + a8a8910 commit e5fe24f

5 files changed

Lines changed: 27 additions & 26 deletions

File tree

client/httpclient.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"crypto/x509"
88
"errors"
99
"fmt"
10+
"math"
1011
"net"
1112
"net/http"
1213
"os"
@@ -202,9 +203,9 @@ func buildHTTPTransport(gitlabURL string) (*http.Transport, string) {
202203
}
203204

204205
func readTimeout(timeoutSeconds uint64) time.Duration {
205-
if timeoutSeconds == 0 {
206+
if timeoutSeconds == 0 || timeoutSeconds > math.MaxInt64 {
206207
timeoutSeconds = defaultReadTimeoutSeconds
207208
}
208209

209-
return time.Duration(timeoutSeconds) * time.Second
210+
return time.Duration(timeoutSeconds) * time.Second // #nosec G115
210211
}

client/httpsclient_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestSuccessfulRequests(t *testing.T) {
6060

6161
responseBody, err := io.ReadAll(response.Body)
6262
require.NoError(t, err)
63-
require.Equal(t, string(responseBody), "Hello")
63+
require.Equal(t, "Hello", string(responseBody))
6464
})
6565
}
6666
}
@@ -103,10 +103,13 @@ func TestFailedRequests(t *testing.T) {
103103
require.Error(t, err)
104104
require.ErrorIs(t, err, ErrCafileNotFound)
105105
} else {
106-
_, err = client.Get(context.Background(), "/hello")
106+
response, err := client.Get(context.Background(), "/hello")
107+
if response != nil {
108+
response.Body.Close()
109+
}
107110
require.Error(t, err)
108111

109-
require.Equal(t, err.Error(), tc.expectedError)
112+
require.Equal(t, tc.expectedError, err.Error())
110113
}
111114
})
112115
}

client/testserver/gitalyserver.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package testserver provides test server implementations for testing GitLab Shell client functionality.
12
package testserver
23

34
import (
@@ -18,11 +19,13 @@ import (
1819
"google.golang.org/grpc/metadata"
1920
)
2021

22+
// TestGitalyServer is a test implementation of the Gitaly SSH service server.
2123
type TestGitalyServer struct {
2224
ReceivedMD metadata.MD
2325
pb.UnimplementedSSHServiceServer
2426
}
2527

28+
// SSHReceivePack handles the SSH receive pack RPC for testing.
2629
func (s *TestGitalyServer) SSHReceivePack(stream pb.SSHService_SSHReceivePackServer) error {
2730
req, err := stream.Recv()
2831
if err != nil {
@@ -35,12 +38,15 @@ func (s *TestGitalyServer) SSHReceivePack(stream pb.SSHService_SSHReceivePackSer
3538
return stream.Send(&pb.SSHReceivePackResponse{Stdout: response})
3639
}
3740

41+
// SSHUploadPackWithSidechannel handles the SSH upload pack with sidechannel RPC for testing.
3842
func (s *TestGitalyServer) SSHUploadPackWithSidechannel(ctx context.Context, req *pb.SSHUploadPackWithSidechannelRequest) (*pb.SSHUploadPackWithSidechannelResponse, error) {
3943
conn, err := client.OpenServerSidechannel(ctx)
4044
if err != nil {
4145
return nil, err
4246
}
43-
defer conn.Close()
47+
defer func() {
48+
_ = conn.Close()
49+
}()
4450

4551
s.ReceivedMD, _ = metadata.FromIncomingContext(ctx)
4652

@@ -55,6 +61,7 @@ func (s *TestGitalyServer) SSHUploadPackWithSidechannel(ctx context.Context, req
5561
return &pb.SSHUploadPackWithSidechannelResponse{}, nil
5662
}
5763

64+
// SSHUploadArchive handles the SSH upload archive RPC for testing.
5865
func (s *TestGitalyServer) SSHUploadArchive(stream pb.SSHService_SSHUploadArchiveServer) error {
5966
req, err := stream.Recv()
6067
if err != nil {
@@ -67,6 +74,7 @@ func (s *TestGitalyServer) SSHUploadArchive(stream pb.SSHService_SSHUploadArchiv
6774
return stream.Send(&pb.SSHUploadArchiveResponse{Stdout: response})
6875
}
6976

77+
// StartGitalyServer starts a test Gitaly server for the specified network type.
7078
func StartGitalyServer(t *testing.T, network string) (string, *TestGitalyServer) {
7179
t.Helper()
7280

@@ -116,7 +124,7 @@ func doStartTestServer(t *testing.T, network string, path string) (string, *Test
116124
pb.RegisterSSHServiceServer(server, &testServer)
117125

118126
go func() {
119-
require.NoError(t, server.Serve(listener))
127+
_ = server.Serve(listener)
120128
}()
121129
t.Cleanup(func() { server.GracefulStop() })
122130

client/testserver/testserver.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212
"path"
1313
"path/filepath"
1414
"testing"
15+
"time"
1516

1617
"github.com/stretchr/testify/require"
1718
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/testhelper"
1819
)
1920

21+
// TestRequestHandler defines a test HTTP request handler with a path and handler function.
2022
type TestRequestHandler struct {
2123
Path string
2224
Handler func(w http.ResponseWriter, r *http.Request)
@@ -44,12 +46,15 @@ func StartSocketHTTPServer(t *testing.T, handlers []TestRequestHandler) string {
4446
require.NoError(t, err)
4547

4648
server := http.Server{
47-
Handler: buildHandler(handlers),
49+
Handler: buildHandler(handlers),
50+
ReadHeaderTimeout: 10 * time.Second,
4851
// We'll put this server through some nasty stuff we don't want
4952
// in our test output
5053
ErrorLog: log.New(io.Discard, "", 0),
5154
}
52-
go server.Serve(socketListener)
55+
go func() {
56+
_ = server.Serve(socketListener)
57+
}()
5358

5459
url := "http+unix://" + testSocket
5560

@@ -114,7 +119,7 @@ func StartHTTPSServer(t *testing.T, handlers []TestRequestHandler, clientCAPath
114119
}
115120

116121
if clientCAPath != "" {
117-
caCert, err := os.ReadFile(clientCAPath)
122+
caCert, err := os.ReadFile(filepath.Clean(clientCAPath))
118123
require.NoError(t, err)
119124

120125
caCertPool := x509.NewCertPool()

support/lint_last_known_acceptable.txt

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
client/httpclient.go:209:22: G115: integer overflow conversion uint64 -> int64 (gosec)
2-
client/httpsclient_test.go:63:4: expected-actual: need to reverse actual and expected values (testifylint)
3-
client/httpsclient_test.go:106:24: response body must be closed (bodyclose)
4-
client/httpsclient_test.go:109:5: expected-actual: need to reverse actual and expected values (testifylint)
5-
client/testserver/gitalyserver.go:1:1: package-comments: should have a package comment (revive)
6-
client/testserver/gitalyserver.go:21:6: exported: exported type TestGitalyServer should have comment or be unexported (revive)
7-
client/testserver/gitalyserver.go:26:1: exported: exported method TestGitalyServer.SSHReceivePack should have comment or be unexported (revive)
8-
client/testserver/gitalyserver.go:38:1: exported: exported method TestGitalyServer.SSHUploadPackWithSidechannel should have comment or be unexported (revive)
9-
client/testserver/gitalyserver.go:43:18: Error return value of `conn.Close` is not checked (errcheck)
10-
client/testserver/gitalyserver.go:58:1: exported: exported method TestGitalyServer.SSHUploadArchive should have comment or be unexported (revive)
11-
client/testserver/gitalyserver.go:70:1: exported: exported function StartGitalyServer should have comment or be unexported (revive)
12-
client/testserver/gitalyserver.go:119:3: go-require: require must only be used in the goroutine running the test function (testifylint)
13-
client/testserver/testserver.go:20:6: exported: exported type TestRequestHandler should have comment or be unexported (revive)
14-
client/testserver/testserver.go:46:12: G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server (gosec)
15-
client/testserver/testserver.go:52:17: Error return value of `server.Serve` is not checked (errcheck)
16-
client/testserver/testserver.go:117:18: G304: Potential file inclusion via variable (gosec)
171
internal/command/command.go:1:1: package-comments: should have a package comment (revive)
182
internal/command/command.go:15:6: exported: exported type Command should have comment or be unexported (revive)
193
internal/command/command.go:19:6: exported: exported type LogMetadata should have comment or be unexported (revive)

0 commit comments

Comments
 (0)