Skip to content

Commit

Permalink
Escape git usernames with backslashes in .gitconfig
Browse files Browse the repository at this point in the history
Fixes tektoncd#3887

The proper format for a username like `foo\bar` in `.gitconfig` is `username = foo\\bar` - i.e., the `\` needs to be escaped.

Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>
  • Loading branch information
abayer authored and tekton-robot committed Oct 28, 2021
1 parent 6ac0cbc commit 5859a82
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/credentials/gitcreds/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ type basicEntry struct {
}

func (be *basicEntry) configBlurb(u string) string {
return fmt.Sprintf("[credential %q]\n username = %s\n", u, be.username)
return fmt.Sprintf("[credential %q]\n username = %s\n", u, be.escapedUsername())
}

func (be *basicEntry) escapedUsername() string {
if strings.Contains(be.username, "\\") {
return strings.ReplaceAll(be.username, "\\", "\\\\")
}
return be.username
}

func newBasicEntry(u, secret string) (*basicEntry, error) {
Expand Down
53 changes: 53 additions & 0 deletions pkg/credentials/gitcreds/creds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,56 @@ func TestMatchingAnnotations(t *testing.T) {
}
}
}

func TestBasicBackslashInUsername(t *testing.T) {
credentials.VolumePath, _ = ioutil.TempDir("", "")
dir := credentials.VolumeName("foo")
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
t.Fatalf("os.MkdirAll(%s) = %v", dir, err)
}
if err := ioutil.WriteFile(filepath.Join(dir, corev1.BasicAuthUsernameKey), []byte(`foo\bar\banana`), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(username) = %v", err)
}
if err := ioutil.WriteFile(filepath.Join(dir, corev1.BasicAuthPasswordKey), []byte("baz"), 0777); err != nil {
t.Fatalf("ioutil.WriteFile(password) = %v", err)
}

fs := flag.NewFlagSet("test", flag.ContinueOnError)
AddFlags(fs)
err := fs.Parse([]string{
"-basic-git=foo=https://github.com",
})
if err != nil {
t.Fatalf("flag.CommandLine.Parse() = %v", err)
}

os.Setenv("HOME", credentials.VolumePath)
if err := NewBuilder().Write(credentials.VolumePath); err != nil {
t.Fatalf("Write() = %v", err)
}

b, err := ioutil.ReadFile(filepath.Join(credentials.VolumePath, ".gitconfig"))
if err != nil {
t.Fatalf("ioutil.ReadFile(.gitconfig) = %v", err)
}

expectedGitConfig := `[credential]
helper = store
[credential "https://github.com"]
username = foo\\bar\\banana
`
if string(b) != expectedGitConfig {
t.Errorf("got: %v, wanted: %v", string(b), expectedGitConfig)
}

b, err = ioutil.ReadFile(filepath.Join(credentials.VolumePath, ".git-credentials"))
if err != nil {
t.Fatalf("ioutil.ReadFile(.git-credentials) = %v", err)
}

expectedGitCredentials := `https://foo%5Cbar%5Cbanana:baz@github.com
`
if string(b) != expectedGitCredentials {
t.Errorf("got: %v, wanted: %v", string(b), expectedGitCredentials)
}
}

0 comments on commit 5859a82

Please sign in to comment.