Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nil pointer dereference when connecting to SSH based URLs with invalid proxy address #900

Closed
anandf opened this issue Nov 1, 2023 · 2 comments · Fixed by #901
Closed
Labels
bug Something isn't working

Comments

@anandf
Copy link
Contributor

anandf commented Nov 1, 2023

When trying to list references from a remote git repository, through a socks5 proxy, there is a nil pointer dereference operation causing the code to panic. In the below code, I am setting proxy server url as socks5://127.0.0.1:1080. If the proxy server is running, then the code behaves correctly, but if the proxy server is not running, the code panics at this line of code session, err := gitClient.NewUploadPackSession(ep, auth)

Reproducer code:

package main

import (
	"fmt"

	"github.com/go-git/go-git/v5/plumbing/transport"
	"github.com/go-git/go-git/v5/plumbing/transport/client"
	"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)

func main() {
	ep, err := transport.NewEndpoint("git@github.com:anandf/openshift-gitops-microshift.git")
	if err != nil {
		fmt.Println(err)
		return
	}
	ep.Proxy = transport.ProxyOptions{URL: "socks5://127.0.0.1:1080"}
	gitClient, err := client.NewClient(ep)
	if err != nil {
		fmt.Println("Error:" + err.Error())
		return
	}
	auth, err := ssh.NewPublicKeysFromFile("git", "/home/anjoseph/.ssh/id_rsa", "")
	if err != nil {
		fmt.Println("Error:" + err.Error())
		return
	}
	session, err := gitClient.NewUploadPackSession(ep, auth)
	if err != nil {
		fmt.Println("Error:" + err.Error())
		return
	}
	ar, err := session.AdvertisedReferences()
	if err != nil {
		fmt.Println("Error:" + err.Error())
		return
	}
	allRefs, err := ar.AllReferences()
	if err != nil {
		fmt.Println("Error:" + err.Error())
		return
	}
	fmt.Println(allRefs)
	err = session.Close()
	if err != nil {
		fmt.Println("Error:" + err.Error())
		return
	}
}
@pjbgf pjbgf added the bug Something isn't working label Nov 1, 2023
@pjbgf
Copy link
Member

pjbgf commented Nov 1, 2023

@anandf thank you for reporting this. Do you mind sharing the stack trace please?

@anandf
Copy link
Contributor Author

anandf commented Nov 1, 2023

yes, I have the code fix as well. Will be submitting a PR.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x8cdac6]

goroutine 1 [running]:
golang.org/x/crypto/ssh.exchangeVersions({0x0, 0x0}, {0xc000205594, 0xa, 0xa})
	/home/anjoseph/go/pkg/mod/golang.org/x/crypto@v0.14.0/ssh/transport.go:302 +0x1c6
golang.org/x/crypto/ssh.(*connection).clientHandshake(0xc00023a280, {0xc000205550, 0xd}, 0xc000218340)
	/home/anjoseph/go/pkg/mod/golang.org/x/crypto@v0.14.0/ssh/client.go:100 +0x22b
golang.org/x/crypto/ssh.NewClientConn({0x0, 0x0}, {0xc000205550, 0xd}, 0xc00011fba0)
	/home/anjoseph/go/pkg/mod/golang.org/x/crypto@v0.14.0/ssh/client.go:83 +0x1fa
github.com/go-git/go-git/v5/plumbing/transport/ssh.dial({0x9acd5b, 0x3}, {0xc000205550, 0xd}, {{0x9b4675, 0x17}, {0x0, 0x0}, {0x0, 0x0}}, ...)
	/home/anjoseph/go/pkg/mod/github.com/go-git/go-git/v5@v5.10.0/plumbing/transport/ssh/common.go:197 +0xaa5
github.com/go-git/go-git/v5/plumbing/transport/ssh.(*command).connect(0xc000168730)
	/home/anjoseph/go/pkg/mod/github.com/go-git/go-git/v5@v5.10.0/plumbing/transport/ssh/common.go:143 +0x499
github.com/go-git/go-git/v5/plumbing/transport/ssh.(*runner).Command(0xc000118578, {0x9b07e2, 0xf}, 0xc000140370, {0xa09528, 0xc0001d4000})
	/home/anjoseph/go/pkg/mod/github.com/go-git/go-git/v5@v5.10.0/plumbing/transport/ssh/common.go:55 +0x176
github.com/go-git/go-git/v5/plumbing/transport/internal/common.(*client).newSession(0xc0001a0090, {0x9b07e2, 0xf}, 0xc000140370, {0xa09528, 0xc0001d4000})
	/home/anjoseph/go/pkg/mod/github.com/go-git/go-git/v5@v5.10.0/plumbing/transport/internal/common/common.go:116 +0xb0
github.com/go-git/go-git/v5/plumbing/transport/internal/common.(*client).NewUploadPackSession(0xc0001a0090, 0xc000140370, {0xa09528, 0xc0001d4000})
	/home/anjoseph/go/pkg/mod/github.com/go-git/go-git/v5@v5.10.0/plumbing/transport/internal/common/common.go:93 +0x85
main.main()
	/home/anjoseph/go/src/github.com/anandf/socks5-example/main.go:25 +0x58e

anandf added a commit to anandf/go-git that referenced this issue Nov 1, 2023
…nreachable proxy server is set. Fixes go-git#900

Signed-off-by: Anand Francis Joseph <anjoseph@redhat.com>
anandf added a commit to anandf/go-git that referenced this issue Nov 2, 2023
…nreachable proxy server is set. Fixes go-git#900

Signed-off-by: Anand Francis Joseph <anjoseph@redhat.com>
@pjbgf pjbgf closed this as completed in #901 Nov 2, 2023
pjbgf added a commit that referenced this issue Nov 2, 2023
plumbing: transport/ssh, Fix nil pointer dereference caused when an unreachable proxy server is set. Fixes #900
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants