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

Clean host URL in the auth login command #1879

Merged
merged 5 commits into from
Nov 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions libs/auth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"net"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -143,6 +144,26 @@ func (a *PersistentAuth) Challenge(ctx context.Context) error {
return nil
}

// This function cleans up the host URL by only retaining the scheme and the host.
// This function thus removes any path, query arguments, or fragments from the URL.
func (a *PersistentAuth) cleanHost() {
parsedHost, err := url.Parse(a.Host)
if err != nil {
return
}
// when either host or scheme is empty, we don't want to clean it. This is because
// the Go url library parses a raw "abc" string as the path of a URL and cleaning
// it will return thus return an empty string.
if parsedHost.Host == "" || parsedHost.Scheme == "" {
return
}
host := url.URL{
Scheme: parsedHost.Scheme,
Host: parsedHost.Host,
}
a.Host = host.String()
}

func (a *PersistentAuth) init(ctx context.Context) error {
if a.Host == "" && a.AccountID == "" {
return ErrFetchCredentials
Expand All @@ -156,6 +177,9 @@ func (a *PersistentAuth) init(ctx context.Context) error {
if a.browser == nil {
a.browser = browser.OpenURL
}

a.cleanHost()

// try acquire listener, which we also use as a machine-local
// exclusive lock to prevent token cache corruption in the scope
// of developer machine, where this command runs.
Expand Down
34 changes: 34 additions & 0 deletions libs/auth/oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,37 @@ func TestChallengeFailed(t *testing.T) {
assert.EqualError(t, err, "authorize: access_denied: Policy evaluation failed for this request")
})
}

func TestPersistentAuthCleanHost(t *testing.T) {
for _, tcases := range []struct {
in string
out string
}{
{"https://example.com", "https://example.com"},
{"https://example.com/", "https://example.com"},
{"https://example.com/path", "https://example.com"},
{"https://example.com/path/subpath", "https://example.com"},
{"https://example.com/path?query=1", "https://example.com"},
{"https://example.com/path?query=1&other=2", "https://example.com"},
{"https://example.com/path#fragment", "https://example.com"},
{"https://example.com/path?query=1#fragment", "https://example.com"},
{"https://example.com/path?query=1&other=2#fragment", "https://example.com"},
{"https://example.com/path/subpath?query=1", "https://example.com"},
{"https://example.com/path/subpath?query=1&other=2", "https://example.com"},
{"https://example.com/path/subpath#fragment", "https://example.com"},
{"https://example.com/path/subpath?query=1#fragment", "https://example.com"},
{"https://example.com/path/subpath?query=1&other=2#fragment", "https://example.com"},
{"https://example.com/path?query=1%20value&other=2%20value", "https://example.com"},
{"http://example.com/path/subpath?query=1%20value&other=2%20value", "http://example.com"},

// URLs without scheme should be left as is
{"abc", "abc"},
{"abc.com/def", "abc.com/def"},
} {
p := &PersistentAuth{
Host: tcases.in,
}
p.cleanHost()
assert.Equal(t, tcases.out, p.Host)
}
}
Loading