Skip to content

Commit e8fdd20

Browse files
committed
go/vcs: fix command injection in VCS path
Apply same change as CL 94656 did for cmd/go/internal/get, but for golang.org/x/tools/go/vcs, to help keep them in sync. It indirectly includes changes from CL 94603, since CL 94656 was rebased on top of CL 94603. Updates golang/go#23867. Helps golang/go#11490. Change-Id: I33eca1aba19f47bbe3e83d4ef9f9cc9a9c9ae975 Reviewed-on: https://go-review.googlesource.com/94899 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent db9df82 commit e8fdd20

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

go/vcs/vcs.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"fmt"
2222
"log"
23+
"net/url"
2324
"os"
2425
"os/exec"
2526
"path/filepath"
@@ -566,8 +567,8 @@ func RepoRootForImportDynamic(importPath string, verbose bool) (*RepoRoot, error
566567
}
567568
}
568569

569-
if !strings.Contains(metaImport.RepoRoot, "://") {
570-
return nil, fmt.Errorf("%s: invalid repo root %q; no scheme", urlStr, metaImport.RepoRoot)
570+
if err := validateRepoRoot(metaImport.RepoRoot); err != nil {
571+
return nil, fmt.Errorf("%s: invalid repo root %q: %v", urlStr, metaImport.RepoRoot, err)
571572
}
572573
rr := &RepoRoot{
573574
VCS: ByCmd(metaImport.VCS),
@@ -580,6 +581,19 @@ func RepoRootForImportDynamic(importPath string, verbose bool) (*RepoRoot, error
580581
return rr, nil
581582
}
582583

584+
// validateRepoRoot returns an error if repoRoot does not seem to be
585+
// a valid URL with scheme.
586+
func validateRepoRoot(repoRoot string) error {
587+
url, err := url.Parse(repoRoot)
588+
if err != nil {
589+
return err
590+
}
591+
if url.Scheme == "" {
592+
return errors.New("no scheme")
593+
}
594+
return nil
595+
}
596+
583597
// metaImport represents the parsed <meta name="go-import"
584598
// content="prefix vcs reporoot" /> tags from HTML files.
585599
type metaImport struct {

go/vcs/vcs_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,47 @@ func TestParseMetaGoImports(t *testing.T) {
140140
}
141141
}
142142
}
143+
144+
func TestValidateRepoRoot(t *testing.T) {
145+
tests := []struct {
146+
root string
147+
ok bool
148+
}{
149+
{
150+
root: "",
151+
ok: false,
152+
},
153+
{
154+
root: "http://",
155+
ok: true,
156+
},
157+
{
158+
root: "git+ssh://",
159+
ok: true,
160+
},
161+
{
162+
root: "http#://",
163+
ok: false,
164+
},
165+
{
166+
root: "-config",
167+
ok: false,
168+
},
169+
{
170+
root: "-config://",
171+
ok: false,
172+
},
173+
}
174+
175+
for _, test := range tests {
176+
err := validateRepoRoot(test.root)
177+
ok := err == nil
178+
if ok != test.ok {
179+
want := "error"
180+
if test.ok {
181+
want = "nil"
182+
}
183+
t.Errorf("validateRepoRoot(%q) = %q, want %s", test.root, err, want)
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)