fix(gitlab): parse glab's token label by structure, not exact wording (#3038)
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -93,7 +94,7 @@ func (c *GLabClient) RunAuthDiagnostics(ctx context.Context) *AuthDiagnostics {
|
||||
|
||||
// glabReadToken extracts the token glab uses for the given hostname. glab's
|
||||
// `auth status -t` prints the token to stderr; this function captures both
|
||||
// streams and parses the line "Token: <value>".
|
||||
// streams and parses the token status line.
|
||||
func glabReadToken(ctx context.Context, hostname string) (string, error) {
|
||||
cctx, cancel := context.WithTimeout(ctx, glabAuthTimeout)
|
||||
defer cancel()
|
||||
@@ -114,34 +115,47 @@ func glabReadToken(ctx context.Context, hostname string) (string, error) {
|
||||
return parseGlabToken(stdout.String() + "\n" + stderr.String()), nil
|
||||
}
|
||||
|
||||
// glabTokenLabels are the label variants `glab auth status -t` has used for
|
||||
// the token line. glab >= 1.x prints "Token found:"; older builds printed a
|
||||
// bare "Token:". Longest first so "Token found:" is not mis-split by "Token:".
|
||||
var glabTokenLabels = []string{"token found:", "token:"}
|
||||
// glabTokenLinePattern matches a token status line by structure, rather than
|
||||
// by exact wording. The token label must start the line after any status
|
||||
// marker, which prevents diagnostic text from becoming a credential.
|
||||
var glabTokenLinePattern = regexp.MustCompile(`(?i)^[^[:alnum:]_]*token\b([^:\n]*):\s*(\S.*)$`)
|
||||
|
||||
func isGlabTokenMetadataLabel(label string) bool {
|
||||
fields := strings.Fields(strings.ToLower(label))
|
||||
if len(fields) == 0 {
|
||||
return false
|
||||
}
|
||||
switch strings.Trim(fields[0], "():") {
|
||||
case "scope", "scopes", "type", "endpoint", "permission", "permissions":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// parseGlabToken finds the token line in the combined output of
|
||||
// `glab auth status -t` and returns the token (or "" if none).
|
||||
func parseGlabToken(output string) string {
|
||||
for _, line := range strings.Split(output, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
lower := strings.ToLower(line)
|
||||
idx, label := -1, ""
|
||||
for _, candidate := range glabTokenLabels {
|
||||
if at := strings.Index(lower, candidate); at >= 0 {
|
||||
idx, label = at, candidate
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
m := glabTokenLinePattern.FindStringSubmatch(line)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
token := strings.TrimSpace(line[idx+len(label):])
|
||||
if isGlabTokenMetadataLabel(m[1]) {
|
||||
continue
|
||||
}
|
||||
token := strings.TrimSpace(m[2])
|
||||
// glab sometimes prefixes lines with ANSI / arrows; strip leading
|
||||
// non-alphanumerics until we hit token characters.
|
||||
token = strings.TrimLeft(token, " \t-→>")
|
||||
if token != "" && token != "<no token>" {
|
||||
return token
|
||||
// Without -t (or a masked display), the value is a run of asterisks
|
||||
// rather than an absent line — reject that instead of "authenticating"
|
||||
// with a redacted placeholder.
|
||||
if token == "" || token == "<no token>" || strings.Trim(token, "*") == "" {
|
||||
return ""
|
||||
}
|
||||
return token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -48,6 +48,59 @@ func TestParseGlabToken_NoTokenFound(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// glab 1.114 prints "Token found in configuration file (plaintext):" —
|
||||
// neither of the two prior exact labels ("Token:", "Token found:") is a
|
||||
// substring of this line, which is what silently broke token extraction
|
||||
// (and surfaced as "GitLab connection unavailable" in the UI) even though
|
||||
// `glab auth status` reported the user authenticated.
|
||||
func TestParseGlabToken_TokenFoundInConfigFileLabel(t *testing.T) {
|
||||
output := `gitlab.example.com
|
||||
✓ Logged in to gitlab.example.com as example-user (/home/example/.config/glab-cli/config.yml)
|
||||
✓ REST API Endpoint: https://gitlab.example.com/api/v4/
|
||||
✓ Token found in configuration file (plaintext): glpat-AAAAA-BBBBB
|
||||
! To store this token more securely, run glab auth login --hostname gitlab.example.com to move it into the operating system keyring.`
|
||||
got := parseGlabToken(output)
|
||||
if got != "glpat-AAAAA-BBBBB" {
|
||||
t.Errorf("got %q, want glpat-AAAAA-BBBBB", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A future relabel this pattern was NOT written to anticipate verbatim —
|
||||
// only structurally ("mentions token, ends in a colon then a value") — so a
|
||||
// wording nobody has shipped yet should still parse.
|
||||
func TestParseGlabToken_HypotheticalFutureLabel(t *testing.T) {
|
||||
got := parseGlabToken("✓ Token (from OS keyring): glpat-future-wording")
|
||||
if got != "glpat-future-wording" {
|
||||
t.Errorf("got %q, want glpat-future-wording", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Without -t (or when the CLI redacts on display), glab prints the token as
|
||||
// a run of asterisks rather than omitting the line. That must not be mistaken
|
||||
// for a real token.
|
||||
func TestParseGlabToken_MaskedTokenIsNotAToken(t *testing.T) {
|
||||
got := parseGlabToken("✓ Token found in configuration file (plaintext): **************************")
|
||||
if got != "" {
|
||||
t.Errorf("got %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGlabToken_MaskedTokenDoesNotFallThroughToScopes(t *testing.T) {
|
||||
output := `gitlab.example.com
|
||||
✓ Logged in to gitlab.example.com as example-user (/home/example/.config/glab-cli/config.yml)
|
||||
✓ Token found in configuration file (plaintext): **************************
|
||||
✓ Token scopes: api`
|
||||
if got := parseGlabToken(output); got != "" {
|
||||
t.Errorf("got %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGlabToken_DiagnosticLineIsNotAToken(t *testing.T) {
|
||||
if got := parseGlabToken("error: token exchange failed: unauthorized"); got != "" {
|
||||
t.Errorf("got %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseGlabToken_Empty(t *testing.T) {
|
||||
if got := parseGlabToken(""); got != "" {
|
||||
t.Errorf("got %q, want empty", got)
|
||||
|
||||
Reference in New Issue
Block a user