Skip to content

Commit

Permalink
plumbing: transport/ssh, Fix nil pointer dereference caused when an u…
Browse files Browse the repository at this point in the history
…nreachable proxy server is set. Fixes #900

Signed-off-by: Anand Francis Joseph <anjoseph@redhat.com>
  • Loading branch information
anandf committed Nov 1, 2023
1 parent 6252084 commit 690fdf0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 5 additions & 5 deletions plumbing/transport/ssh/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func dial(network, addr string, proxyOpts transport.ProxyOptions, config *ssh.Cl
defer cancel()

var conn net.Conn
var err error
var dialErr error

if proxyOpts.URL != "" {
proxyUrl, err := proxyOpts.FullURL()
Expand All @@ -186,12 +186,12 @@ func dial(network, addr string, proxyOpts transport.ProxyOptions, config *ssh.Cl
return nil, fmt.Errorf("expected ssh proxy dialer to be of type %s; got %s",
reflect.TypeOf(ctxDialer), reflect.TypeOf(dialer))
}
conn, err = ctxDialer.DialContext(ctx, "tcp", addr)
conn, dialErr = ctxDialer.DialContext(ctx, "tcp", addr)
} else {
conn, err = proxy.Dial(ctx, network, addr)
conn, dialErr = proxy.Dial(ctx, network, addr)
}
if err != nil {
return nil, err
if dialErr != nil {
return nil, dialErr
}

c, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
Expand Down
22 changes: 22 additions & 0 deletions plumbing/transport/ssh/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ func (s *SuiteCommon) TestIssue70(c *C) {
c.Assert(err, IsNil)
}

/*
Given, an endpoint to a git server with a socks5 proxy URL,
When, the socks5 proxy server is not reachable,
Then, there should not be any panic and an error with appropriate message should be returned.
Related issue : https://github.com/go-git/go-git/pull/900
*/
func (s *SuiteCommon) TestInvalidSocks5Proxy(c *C) {
ep, err := transport.NewEndpoint("git@github.com:foo/bar.git")
c.Assert(err, IsNil)
ep.Proxy.URL = "socks5://127.0.0.1:1080"

auth, err := NewPublicKeys("foo", testdata.PEMBytes["rsa"], "")
c.Assert(err, IsNil)
c.Assert(auth, NotNil)

ps, err := DefaultClient.NewUploadPackSession(ep, auth)
//Since the proxy server is not running, we expect an error.
c.Assert(ps, IsNil)
c.Assert(err, NotNil)
c.Assert(err, ErrorMatches, "socks connect .* dial tcp 127.0.0.1:1080: connect: connection refused")
}

type mockSSHConfig struct {
Values map[string]map[string]string
}
Expand Down

0 comments on commit 690fdf0

Please sign in to comment.