Skip to content

Commit 2712794

Browse files
authored
fix(cli): sanitize remote url (#729)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent 7b1e792 commit 2712794

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

internal/attestation/crafter/crafter.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"io"
2323
"net/http"
24+
"net/url"
2425
"os"
2526
"path/filepath"
2627
"strings"
@@ -340,15 +341,37 @@ func gracefulGitRepoHead(path string) (*HeadCommit, error) {
340341
continue
341342
}
342343

344+
remoteURI, err := sanitizeRemoteURL(r.Config().URLs[0])
345+
if err != nil {
346+
return nil, fmt.Errorf("sanitizing remote url: %w", err)
347+
}
348+
343349
c.Remotes = append(c.Remotes, &CommitRemote{
344350
Name: r.Config().Name,
345-
URL: r.Config().URLs[0],
351+
URL: remoteURI,
346352
})
347353
}
348354

349355
return c, nil
350356
}
351357

358+
// Clear any basic auth credentials from the remote URL
359+
func sanitizeRemoteURL(remoteURL string) (string, error) {
360+
uri, err := url.Parse(remoteURL)
361+
if err != nil {
362+
// check if it's a valid git@ url
363+
if strings.HasPrefix(remoteURL, "git@") {
364+
return remoteURL, nil
365+
}
366+
367+
return "", fmt.Errorf("parsing remote url: %w", err)
368+
}
369+
370+
// clear basic auth credentials
371+
uri.User = nil
372+
return uri.String(), nil
373+
}
374+
352375
func initialCraftingState(cwd string, schema *schemaapi.CraftingSchema, wf *api.WorkflowMetadata, dryRun bool, runnerType schemaapi.CraftingSchema_Runner_RunnerType, jobURL string) (*api.CraftingState, error) {
353376
// Get git commit hash
354377
headCommit, err := gracefulGitRepoHead(cwd)

internal/attestation/crafter/crafter_unit_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,49 @@ type crafterUnitSuite struct {
3434
suite.Suite
3535
}
3636

37+
func (s *crafterUnitSuite) TestSanitizeRemoteURI() {
38+
testCases := []struct {
39+
name string
40+
uri string
41+
want string
42+
wantErr bool
43+
}{
44+
{
45+
name: "ssh",
46+
uri: "[email protected]:skynet.git",
47+
want: "[email protected]:skynet.git",
48+
},
49+
{
50+
name: "https",
51+
uri: "https://cyberdyne.com/skynet.git",
52+
want: "https://cyberdyne.com/skynet.git",
53+
},
54+
{
55+
name: "https with user",
56+
uri: "https://demo-user:[email protected]/skynet.git",
57+
want: "https://cyberdyne.com/skynet.git",
58+
},
59+
{
60+
name: "invalid uri",
61+
uri: "https://demo-user@pass:cyberdyne.com/skynet.git",
62+
wantErr: true,
63+
},
64+
}
65+
66+
for _, tc := range testCases {
67+
s.Run(tc.name, func() {
68+
got, err := sanitizeRemoteURL(tc.uri)
69+
if tc.wantErr {
70+
s.Error(err)
71+
return
72+
}
73+
74+
require.NoError(s.T(), err)
75+
s.Equal(tc.want, got)
76+
})
77+
}
78+
}
79+
3780
func (s *crafterUnitSuite) TestGitRepoHead() {
3881
initRepo := func(withCommit bool) func(string) (*HeadCommit, error) {
3982
return func(repoPath string) (*HeadCommit, error) {

0 commit comments

Comments
 (0)