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:

  1. 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 or GITLAB_CLIENT_ID.
  2. Setting client_id to an empty value (glab config set client_id "" --host <host>) is supported to clear a saved ID.
  3. Common mistake prefixes are detected with specific messages:
    • glpat- values are identified as personal access tokens.
    • gloas- values are identified as application secrets.
  4. A pre-flight check in StartFlow queries the authorization endpoint before opening the browser. If the instance answers HTTP 400 or HTTP 401 with invalid_client, the command fails fast with an error wrapping ErrClientIDRejected instead of hanging. This only catches an unknown client ID where /oauth/authorize rejects it before sign-in. On GitLab.com, Workhorse routes unknown client IDs to the IAM Auth service, which returns 401 invalid_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 --web flow, as it does on main.

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_id rejecting invalid formats and tokens, and allowing clearing with an empty string.
  • Rejection of invalid non-hex client IDs in host configuration and GITLAB_CLIENT_ID during OAuth resolution, with whitespace-only values falling through to prompt.
  • Pre-flight authorization checks verifying fail-fast behavior with ErrClientIDRejected on HTTP 400 and HTTP 401 invalid_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

Merge request reports

Loading
Loading