Skip to content

fix(auth): prevent --help on auth subcommands from triggering OAuth flow#835

Open
nuthalapativarun wants to merge 1 commit into
googleworkspace:mainfrom
nuthalapativarun:fix/782-auth-help-flag
Open

fix(auth): prevent --help on auth subcommands from triggering OAuth flow#835
nuthalapativarun wants to merge 1 commit into
googleworkspace:mainfrom
nuthalapativarun:fix/782-auth-help-flag

Conversation

@nuthalapativarun
Copy link
Copy Markdown

Description

gws auth login --help, gws auth setup --help, and gws auth status --help were triggering the full OAuth/setup flow instead of printing help text. The top-level gws auth --help was already handled correctly.

The fix adds an args_request_help helper that checks for --help/-h in the raw args slice, plus guards at the subcommand dispatch level in handle_auth_command so help requests exit cleanly without any network I/O or credential resolution:

  • login --help / -h: prints login subcommand help via build_login_subcommand().print_help() and returns Ok(())
  • status --help / -h: prints status subcommand help and returns Ok(()) without querying credentials or calling the network
  • setup --help / -h: already handled by parse_setup_args (which has its own clap parser without disable_help_flag); the existing delegation to run_setup correctly passes --help in the trailing args

Closes #782

Checklist:

  • My code follows the AGENTS.md guidelines (no generated google-* crates).
  • I have run cargo fmt --all to format the code perfectly.
  • I have run cargo clippy -- -D warnings and resolved all warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have provided a Changeset file (e.g. via pnpx changeset) to document my changes.

Adds an `args_request_help` helper and guards in `handle_auth_command`
so that `gws auth login --help`, `gws auth status --help`, and
`gws auth setup --help` print help text and exit cleanly instead of
entering the OAuth or gcloud setup flow.

The `setup` subcommand already delegated to `parse_setup_args` which
handles `--help` internally (via its own clap parser without
`disable_help_flag`). The `login` and `status` subcommands now have
explicit guards that check for `--help`/`-h` in the raw args before
dispatching to any network I/O.

Closes googleworkspace#782
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 2, 2026

🦋 Changeset detected

Latest commit: 6cf4af6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@googleworkspace/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where invoking help flags on specific authentication subcommands incorrectly triggered the full OAuth or setup flow. By implementing a pre-dispatch check for help flags, the CLI now correctly displays help documentation and exits cleanly, improving user experience and preventing unnecessary side effects.

Highlights

  • Help Flag Handling: Introduced a helper function args_request_help to detect --help or -h flags in CLI arguments before command execution.
  • Subcommand Guarding: Updated handle_auth_command to intercept help requests for login and status subcommands, preventing unintended OAuth flows or network I/O.
  • Test Coverage: Added comprehensive unit and integration tests to verify that help flags correctly display help text without triggering authentication or network processes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request aims to prevent --help or -h flags on auth subcommands (such as login and status) from triggering OAuth flows or network requests. It introduces a helper function args_request_help, adds guard match arms to handle help requests, and includes several unit tests. However, the reviewer points out that the new guard match arms for the login and status subcommands are redundant and unreachable. Because these subcommands do not disable the help flag, clap's built-in parser already intercepts the help flags and handles them before reaching the subcommand matching logic. The reviewer recommends removing these redundant match arms.

Comment on lines +460 to 491
Some(("login", _)) if args_request_help(args) => {
// Print login-specific help without starting any OAuth flow.
build_login_subcommand()
.print_help()
.map_err(|e| GwsError::Validation(format!("Failed to print help: {e}")))?;
Ok(())
}
Some(("login", sub_m)) => {
let (scope_mode, services_filter) = parse_login_args(sub_m);

handle_login_inner(scope_mode, services_filter).await
}
Some(("setup", sub_m)) => {
// Collect remaining args and delegate to setup's own clap parser.
// setup uses disable_help_flag(true) + trailing_var_arg so --help/-h
// lands in the captured args; parse_setup_args handles it internally.
let setup_args: Vec<String> = sub_m
.get_many::<String>("args")
.map(|vals| vals.cloned().collect())
.unwrap_or_default();
crate::setup::run_setup(&setup_args).await
}
Some(("status", _)) if args_request_help(args) => {
// Print status-specific help without querying credentials or the network.
auth_command()
.find_subcommand_mut("status")
.unwrap()
.print_help()
.map_err(|e| GwsError::Validation(format!("Failed to print help: {e}")))?;
Ok(())
}
Some(("status", _)) => handle_status().await,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Redundant and Unreachable Dead Code

The new guard match arms for login and status subcommands are completely redundant and unreachable dead code.

Why this is redundant:

  1. When a user runs gws auth login --help or gws auth status --help, handle_auth_command is called with args containing "--help" or "-h".
  2. auth_command().try_get_matches_from(...) is called with these arguments.
  3. Since the login and status subcommands do not have .disable_help_flag(true) set, clap's built-in parser automatically intercepts the --help/-h flag and returns a clap::error::Error with ErrorKind::DisplayHelp.
  4. This error is immediately caught by the Err(e) if e.kind() == ErrorKind::DisplayHelp match arm at lines 447-455 (which prints the help message and returns Ok(()) directly).
  5. As a result, the execution never reaches the match matches.subcommand() block for these help requests.

Recommendation:

Remove these redundant match arms. The existing integration tests added in this PR will still pass perfectly, proving that clap's built-in help handling is already fully sufficient.

        Some(("login", sub_m)) => {
            let (scope_mode, services_filter) = parse_login_args(sub_m);

            handle_login_inner(scope_mode, services_filter).await
        }
        Some(("setup", sub_m)) => {
            // Collect remaining args and delegate to setup's own clap parser.
            // setup uses disable_help_flag(true) + trailing_var_arg so --help/-h
            // lands in the captured args; parse_setup_args handles it internally.
            let setup_args: Vec<String> = sub_m
                .get_many::<String>("args")
                .map(|vals| vals.cloned().collect())
                .unwrap_or_default();
            crate::setup::run_setup(&setup_args).await
        }
        Some(("status", _)) => handle_status().await,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gws auth login --help (and -h) is silently ignored — starts the OAuth flow instead of printing help

2 participants