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

fix: clean user information from remote url if it contains username and token #2457

Merged
merged 1 commit into from Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions internal/pipe/git/git.go
Expand Up @@ -2,6 +2,7 @@ package git

import (
"fmt"
"net/url"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -82,10 +83,20 @@ func getGitInfo() (context.GitInfo, error) {
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get commit date: %w", err)
}
url, err := getURL()
gitURL, err := getURL()
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't get remote URL: %w", err)
}

if strings.HasPrefix(gitURL, "https://") {
u, err := url.Parse(gitURL)
if err != nil {
return context.GitInfo{}, fmt.Errorf("couldn't parse remote URL: %w", err)
}
u.User = nil
gitURL = u.String()
}

tag, err := getTag()
if err != nil {
return context.GitInfo{
Expand All @@ -94,7 +105,7 @@ func getGitInfo() (context.GitInfo, error) {
FullCommit: full,
ShortCommit: short,
CommitDate: date,
URL: url,
URL: gitURL,
CurrentTag: "v0.0.0",
}, ErrNoTag
}
Expand All @@ -105,7 +116,7 @@ func getGitInfo() (context.GitInfo, error) {
FullCommit: full,
ShortCommit: short,
CommitDate: date,
URL: url,
URL: gitURL,
}, nil
}

Expand Down
22 changes: 22 additions & 0 deletions internal/pipe/git/git_test.go
Expand Up @@ -115,6 +115,28 @@ func TestDirty(t *testing.T) {
})
}

func TestRemoteURLContainsWithUsernameAndToken(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "https://gitlab-ci-token:SyYhsAghYFTvMoxw7GAg@gitlab.private.com/platform/base/poc/kink.git/releases/tag/v0.1.4")
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.NoError(t, Pipe{}.Run(ctx))
}

func TestRemoteURLContainsWithUsernameAndTokenWithInvalidURL(t *testing.T) {
testlib.Mktmp(t)
testlib.GitInit(t)
testlib.GitRemoteAdd(t, "https://gitlab-ci-token:SyYhsAghYFTvMoxw7GAggitlab.com/platform/base/poc/kink.git/releases/tag/v0.1.4")
testlib.GitAdd(t)
testlib.GitCommit(t, "commit2")
testlib.GitTag(t, "v0.0.1")
ctx := context.New(config.Project{})
require.Error(t, Pipe{}.Run(ctx))
}

func TestShallowClone(t *testing.T) {
folder := testlib.Mktmp(t)
require.NoError(
Expand Down