Loading
fix(auth): validate OAuth client ID before browser step
Description
On self-managed and Dedicated instances, OAuth authentication requires an Application ID. Previously, glab only checked that the input was not empty. If a user pasted a personal access token (glpat-), an application secret (gloas-), or a truncated value, the mistake only surfaced after the browser opened, and the command hung indefinitely waiting for a callback that never arrived.
This change validates OAuth application client IDs across all entry points:
- Client IDs must be 64 hexadecimal characters. The same validation rule applies at all three entry points: interactive paste prompt,
glab config set client_id, and when resolving from host configuration orGITLAB_CLIENT_ID. - Setting
client_idto an empty value (glab config set client_id "" --host <host>) is supported to clear a saved ID. - Common mistake prefixes are detected with specific messages:
glpat-values are identified as personal access tokens.gloas-values are identified as application secrets.
- A pre-flight check in
StartFlowqueries the authorization endpoint before opening the browser. If the instance answers HTTP 400 or HTTP 401 withinvalid_client, the command fails fast with an error wrappingErrClientIDRejectedinstead of hanging. This only catches an unknown client ID where/oauth/authorizerejects it before sign-in. On GitLab.com, Workhorse routes unknown client IDs to the IAM Auth service, which returns 401invalid_client. Self-managed and Dedicated instances without the IAM service handle the request in Rails, which redirects to sign-in (302) first, so the check passes and a well-formed but unknown ID still hangs the--webflow, as it does onmain.
Related Issues
Closes #8698 (closed)
How has this been tested?
I added unit tests covering:
- Format validation for 64 hex characters, uppercase hex, whitespace trimming, and invalid characters.
- Prefix detection for personal access tokens (
glpat-), application secrets (gloas-), and other token prefixes. - Configuration validation in
glab config set client_idrejecting invalid formats and tokens, and allowing clearing with an empty string. - Rejection of invalid non-hex client IDs in host configuration and
GITLAB_CLIENT_IDduring OAuth resolution, with whitespace-only values falling through to prompt. - Pre-flight authorization checks verifying fail-fast behavior with
ErrClientIDRejectedon HTTP 400 and HTTP 401invalid_client, with fallback on proxy responses and normal operation on redirects.
The rest runs in the pipeline.
Implemented with AI assistance; I reviewed and tested the changes.
Edited by Jay McCure