Skip to content

Commit

Permalink
Validate that bearer token is not used over http
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Ihle <[email protected]>
  • Loading branch information
blurpy committed Jan 17, 2023
1 parent 0c792a6 commit f077586
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
9 changes: 6 additions & 3 deletions git/gogit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,12 @@ func (g *Client) validateUrl(u string) error {
return errors.New("URL cannot contain credentials when using HTTP")
}

if httpOrEmpty && g.authOpts != nil &&
(g.authOpts.Username != "" || g.authOpts.Password != "") {
return errors.New("basic auth cannot be sent over HTTP")
if httpOrEmpty && g.authOpts != nil {
if g.authOpts.Username != "" || g.authOpts.Password != "" {
return errors.New("basic auth cannot be sent over HTTP")
} else if g.authOpts.BearerToken != "" {
return errors.New("bearer token cannot be sent over HTTP")
}
}

return nil
Expand Down
28 changes: 25 additions & 3 deletions git/gogit/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ func TestValidateUrl(t *testing.T) {
transport git.TransportType
username string
password string
bearerToken string
url string
credentialsOverHttp bool
expectedError string
Expand Down Expand Up @@ -687,6 +688,26 @@ func TestValidateUrl(t *testing.T) {
password: "pass",
url: "https://url",
},
{
name: "blocked: bearer token over http",
transport: git.HTTP,
bearerToken: "token",
url: "http://url",
expectedError: "bearer token cannot be sent over HTTP",
},
{
name: "allowed: bearer token over http with insecure enabled",
transport: git.HTTP,
bearerToken: "token",
url: "http://url",
credentialsOverHttp: true,
},
{
name: "allowed: bearer token over https",
transport: git.HTTPS,
bearerToken: "token",
url: "https://url",
},
}

for _, tt := range tests {
Expand All @@ -699,9 +720,10 @@ func TestValidateUrl(t *testing.T) {
}

ggc, err := NewClient(t.TempDir(), &git.AuthOptions{
Transport: tt.transport,
Username: tt.username,
Password: tt.password,
Transport: tt.transport,
Username: tt.username,
Password: tt.password,
BearerToken: tt.bearerToken,
}, opts...)
g.Expect(err).ToNot(HaveOccurred())

Expand Down
20 changes: 17 additions & 3 deletions git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
name string
username string
password string
bearerToken string
allowCredentialsOverHttp bool
transformURL func(string) string
expectCloneErr string
Expand All @@ -1009,6 +1010,11 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
password: "pass",
expectCloneErr: "basic auth cannot be sent over HTTP",
},
{
name: "blocked: bearer token over HTTP",
bearerToken: "token",
expectCloneErr: "bearer token cannot be sent over HTTP",
},
{
name: "blocked: URL based credential over HTTP (name)",
transformURL: func(s string) string {
Expand Down Expand Up @@ -1069,6 +1075,13 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
allowCredentialsOverHttp: true,
expectRequest: true,
},
{
name: "allowed: bearer token over HTTP",
bearerToken: "token",
expectCloneErr: "unable to clone",
allowCredentialsOverHttp: true,
expectRequest: true,
},
{
name: "allowed: URL based credential over HTTP (name)",
transformURL: func(s string) string {
Expand Down Expand Up @@ -1129,9 +1142,10 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
}

ggc, err := NewClient(tmpDir, &git.AuthOptions{
Transport: git.HTTP,
Username: tt.username,
Password: tt.password,
Transport: git.HTTP,
Username: tt.username,
Password: tt.password,
BearerToken: tt.bearerToken,
}, opts...)

g.Expect(err).ToNot(HaveOccurred())
Expand Down

0 comments on commit f077586

Please sign in to comment.