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 1 commit
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
17 changes: 17 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,19 @@ func (a *PersistentAuth) Challenge(ctx context.Context) error {
return nil
}

// Best effort to remove url path, query args and fragments from the host
func (a *PersistentAuth) cleanHost() {
parsedHost, err := url.Parse(a.Host)
if err != nil {
return
}
parsedHost.RawPath = ""
parsedHost.RawQuery = ""
parsedHost.Path = ""
parsedHost.Fragment = ""
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
a.Host = parsedHost.String()
}

func (a *PersistentAuth) init(ctx context.Context) error {
if a.Host == "" && a.AccountID == "" {
return ErrFetchCredentials
Expand All @@ -156,6 +170,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
29 changes: 29 additions & 0 deletions libs/auth/oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,32 @@ func TestChallengeFailed(t *testing.T) {
assert.EqualError(t, err, "authorize: access_denied: Policy evaluation failed for this request")
})
}

func TestPerisistentAuthCleanHost(t *testing.T) {
shreyas-goenka marked this conversation as resolved.
Show resolved Hide resolved
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?query", "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", "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"},
} {
p := &PersistentAuth{
Host: tcases.in,
}
p.cleanHost()
assert.Equal(t, tcases.out, p.Host)
}
}
Loading