llb.Git omits known SSH hosts for IPv6 URLs without an explicit port
Contributing guidelines and issue reporting guide
- Read the contributing guidelines and issue reporting guide.
- Searched the documentation and existing issues; no matching issue found. Related PR: #7164.
- Included environment information and a runnable reproducer.
Bug description
When constructing an SSH Git source with llb.Git("ssh://git@[::1]/repo.git", ""), automatic SSH host-key scanning fails even when an SSH server is listening on [::1]:22. The generated source has no git.knownsshhosts attribute. The equivalent URL with explicit :22 successfully scans the same server and includes the correct host key.
gitutil.ParseURL supplies [::1] as the host. SSHKeyScan adds the default port using net.JoinHostPort, producing [[::1]]:22. The resulting address error is ignored by the best-effort scan in llb.Git.
Expected: both URL forms produce a Git source containing the same server's known-hosts entry.
This is the requested follow-up to #7164. The bug was found during AI-assisted code inspection and then verified with the exported BuildKit APIs below. This reproduces client-side LLB construction; it does not run a Git clone or a BuildKit solve.
Reproduction
The program starts a temporary SSH server on IPv6 loopback port 22, generates its host key in memory, and compares the scanned and serialized keys with that known key. It needs no SSH credentials, Git repository, SSH daemon installation, or BuildKit daemon. Run it inside the container below so it does not use the host's port 22; the container has no external network or published ports.
- Check out the tested upstream revision:
git clone https://github.com/moby/buildkit.git buildkit-ipv6-repro
cd buildkit-ipv6-repro
git checkout 3bcbbc94a52991ee79490d32d7a375cfe5716330- Save the following as
repro.goin this checkout:
package main
import (
"context"
"crypto/ed25519"
"crypto/rand"
"fmt"
"net"
"strings"
"time"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/gitutil"
"github.com/moby/buildkit/util/sshutil"
"golang.org/x/crypto/ssh"
)
func must(err error) {
if err != nil {
panic(err)
}
}
func main() {
_, privateKey, err := ed25519.GenerateKey(rand.Reader)
must(err)
signer, err := ssh.NewSignerFromKey(privateKey)
must(err)
config := &ssh.ServerConfig{NoClientAuth: true}
config.AddHostKey(signer)
listener, err := net.Listen("tcp6", "[::1]:22")
must(err)
defer listener.Close()
go func() {
for {
conn, err := listener.Accept()
if err != nil {
return
}
go func() {
defer conn.Close()
must(conn.SetDeadline(time.Now().Add(5 * time.Second)))
server, _, _, err := ssh.NewServerConn(conn, config)
if err == nil {
server.Close()
}
}()
}
}()
want := "::1 " + strings.TrimSpace(string(ssh.MarshalAuthorizedKey(signer.PublicKey())))
for _, rawURL := range []string{"ssh://git@[::1]/repo.git", "ssh://git@[::1]:22/repo.git"} {
remote, err := gitutil.ParseURL(rawURL)
must(err)
key, scanErr := sshutil.SSHKeyScan(remote.Host)
fmt.Printf("URL: %s\nSSHKeyScan: key matches=%t, error=%v\n", rawURL, key == want, scanErr)
state := llb.Git(rawURL, "")
definition, err := state.Marshal(context.Background())
must(err)
for _, encoded := range definition.Def {
var op pb.Op
must(op.UnmarshalVT(encoded))
if source := op.GetSource(); source != nil {
key, present := source.Attrs[pb.AttrKnownSSHHosts]
fmt.Printf("llb.Git: git.knownsshhosts present=%t, key matches=%t\n", present, key == want)
}
}
}
}- Run with the vendored dependencies and Go version required by this revision:
docker run --rm --network none \
--mount "type=bind,src=$PWD,dst=/src,readonly" \
--workdir /src \
--env GOTOOLCHAIN=local --env GOPROXY=off \
golang:1.26.8-bookworm \
go run -mod=vendor /src/repro.goObserved output on unmodified upstream:
URL: ssh://git@[::1]/repo.git
SSHKeyScan: key matches=false, error=dial tcp: address [[::1]]:22: missing port in address
llb.Git: git.knownsshhosts present=false, key matches=false
URL: ssh://git@[::1]:22/repo.git
SSHKeyScan: key matches=true, error=<nil>
llb.Git: git.knownsshhosts present=true, key matches=trueApplying only the three-line bracket normalization from #7164 to that same revision, the identical reproducer produces:
URL: ssh://git@[::1]/repo.git
SSHKeyScan: key matches=true, error=<nil>
llb.Git: git.knownsshhosts present=true, key matches=true
URL: ssh://git@[::1]:22/repo.git
SSHKeyScan: key matches=true, error=<nil>
llb.Git: git.knownsshhosts present=true, key matches=trueExplicitly including :22 is a workaround for the demonstrated case.
Version information
- BuildKit client library:
3bcbbc94a52991ee79490d32d7a375cfe5716330(master checked on September 28, 2026). - Proposed change tested: the production diff from #7164 (
e5298d0401a74aaa87a8c08bf8a8a4bcdfc72f39) applied to the revision above. - Go:
go1.26.8 linux/amd64. - Container image:
golang:1.26.8-bookworm, digestsha256:a688600ca24f8a4d3ca77f95b0dd40704a9fc787c826660eb7ba0b641b8b175d. - Docker Engine 29.5.3 / Docker Desktop 4.78.0; Linux
6.6.87.2-microsoft-standard-WSL2, x86_64. - No
buildkitd/buildctlprocess is involved: the symptom occurs while constructing the client-side source definition.
Source: moby/buildkit