Skip to content

feat(gmail): implement robust draft-only mode with centralized policy enforcement (#424)#499

Open
dumko2001 wants to merge 7 commits intogoogleworkspace:mainfrom
dumko2001:feature/issue-424-gmail-drafts
Open

feat(gmail): implement robust draft-only mode with centralized policy enforcement (#424)#499
dumko2001 wants to merge 7 commits intogoogleworkspace:mainfrom
dumko2001:feature/issue-424-gmail-drafts

Conversation

@dumko2001
Copy link

@dumko2001 dumko2001 commented Mar 15, 2026

Description

Implemented a robust Gmail draft-only mode to prevent accidental email sends. This update addresses a critical security bypass where helper commands (like +send, +reply, +forward) previously circumvented the safety flag.

Key Changes:

  • Centralized Enforcement: Moved policy checks into executor.rs and Gmail's send_raw_email to ensure all interaction layers (raw API and helpers) are protected.
  • Architectural Consolidation: Introduced a unified ExecutionPolicy struct to manage both Model Armor sanitization and Gmail safety settings consistently across all service helpers.
  • Early Rejection: Enforced checks before authentication in main.rs to provide a better user experience for blocked operations.
  • Fixes: Removed a duplicate disclaimer bug in print_usage and resolved several compiler warnings.

Fixes #424

Dry Run Output:

{
  "body": {
    "raw": "VG86IGFsaWNlQGV4YW1wbGUuY29tDQpTdWJqZWN0OiBEcmFmdA0KTUlNRS1WZXJzaW9uOiAxLjANCkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbjsgY2hhcnNldD11dGYtOA0KDQpUaGlzIGlzIHNhZmU="
  },
  "dry_run": true,
  "is_multipart_upload": false,
  "method": "POST",
  "query_params": [],
  "url": "https://gmail.googleapis.com/gmail/v1/users/me/messages/send"
}

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.

@dumko2001 dumko2001 requested a review from jpoehnelt as a code owner March 15, 2026 16:54
@changeset-bot
Copy link

changeset-bot bot commented Mar 15, 2026

🦋 Changeset detected

Latest commit: c051c0c

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 Minor

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

@googleworkspace-bot googleworkspace-bot added area: distribution area: tui area: core Core CLI parsing, commands, error handling, utilities labels Mar 15, 2026
@gemini-code-assist
Copy link
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 introduces a robust 'draft-only' mode for Gmail interactions within the CLI tool, significantly enhancing user control over email sending. The primary goal is to prevent unintended email dispatches by enforcing a policy that allows only draft creation or updates when enabled. This feature is configurable via both a command-line flag and an environment variable, providing flexibility for different workflows, and includes a specific exemption for dry runs to facilitate safe testing.

Highlights

  • Gmail Draft-Only Mode: Implemented a new 'draft-only' mode for Gmail operations to prevent accidental email sends. This mode intercepts all messages.send and drafts.send operations.
  • Activation Methods: The draft-only policy can be activated using a new global --draft-only CLI flag or by setting the GOOGLE_WORKSPACE_GMAIL_DRAFT_ONLY=true environment variable.
  • Send Blocking & Exceptions: When active, actual email transmission is blocked, but creating drafts is still allowed. The --dry-run flag is also exempt from this blocking, enabling testing and oversight.
  • Explicit Draft Creation: Added a --draft flag to Gmail helper commands (+send, +reply, +reply-all, +forward) to explicitly save messages as drafts instead of sending them.
  • Dependency Updates & Testing: Updated various Rust dependencies, including a downgrade of ratatui and the addition of assert_cmd for new integration tests that validate the draft-only functionality.
Changelog
  • .changeset/feat-gmail-draft-only.md
    • Documented the new Gmail draft-only feature, detailing its purpose, activation, and behavior.
  • Cargo.lock
    • Updated numerous package dependencies to their latest compatible versions.
    • Added new dependencies such as assert_cmd, cassowary, difflib, predicates, predicates-core, predicates-tree, paste, termtree, and wait-timeout.
    • Removed several unused dependencies including bit-set, bit-vec, csscolorparser, deltae, euclid, fancy-regex, filedescriptor, finl_unicode, fixedbitset, foldhash, hex, kasuari, lab, line-clipping, mac_address, memmem, memoffset, minimal-lexical, nix, nom, num-derive, ordered-float, pest, pest_derive, pest_generator, pest_meta, phf_codegen, phf_generator, phf_macros, portable-atomic, regex, terminfo, termios, ucd-trie, uuid, vtparse, wezterm-bidi, wezterm-blob-leases, wezterm-color-types, wezterm-dynamic, wezterm-dynamic-derive, and wezterm-input-types.
    • Downgraded ratatui from 0.30.0 to 0.29.0 and compact_str from 0.9.0 to 0.8.1.
    • Adjusted version specifications for darling, instability, itertools, lru, num-conv, strum, strum_macros, time, and time-macros.
  • Cargo.toml
    • Added the env feature to the clap dependency to allow reading environment variables.
    • Updated the ratatui dependency version from 0.30.0 to 0.29.0.
    • Included assert_cmd as a development dependency for integration testing.
  • src/commands.rs
    • Introduced a global --draft-only argument to the CLI, enabling the Gmail draft-only mode.
  • src/helpers/gmail/forward.rs
    • Updated the email forwarding logic to utilize the new send_or_draft_raw_email function, supporting draft creation and the draft-only policy.
  • src/helpers/gmail/mod.rs
    • Added resolve_draft_create_method to dynamically retrieve the Gmail API method for creating drafts.
    • Modified build_raw_send_body to conditionally format the request body for either sending or drafting messages.
    • Renamed send_raw_email to send_or_draft_raw_email and integrated the draft-only policy logic, blocking sends unless it's a dry-run or an explicit draft.
    • Dynamically selected the appropriate API method (send or draft create) based on the command arguments.
    • Updated the OAuth scope resolution to use the scopes of the selected API method.
    • Added a --draft argument to Gmail helper commands (+send, +reply, +reply-all, +forward) to allow explicit draft creation.
    • Enhanced the help text tips to include information about the --draft flag and the GOOGLE_WORKSPACE_GMAIL_DRAFT_ONLY environment variable.
    • Adjusted test cases for build_raw_send_body to accommodate the new is_draft parameter.
  • src/helpers/gmail/reply.rs
    • Updated the email reply logic to use the new send_or_draft_raw_email function, supporting draft creation and the draft-only policy.
  • src/helpers/gmail/send.rs
    • Updated the email sending logic to use the new send_or_draft_raw_email function, supporting draft creation and the draft-only policy.
  • src/main.rs
    • Implemented a check for the global --draft-only flag, preventing direct calls to gmail.users.messages.send or gmail.users.drafts.send unless a dry-run is specified.
  • src/setup_tui.rs
    • Imported the Stylize trait from ratatui::prelude for enhanced text styling in the terminal user interface.
  • tests/draft_only.rs
    • Added new integration tests to verify the functionality of the Gmail draft-only mode, covering scenarios for dry-runs, blocking real sends, allowing explicit drafts, environment variable activation, and blocking raw API sends.
Activity
  • The author, dumko2001, initiated this pull request to introduce a 'draft-only' mode for Gmail operations.
  • The pull request description provides a clear overview of the feature, its activation mechanisms, and an example of dry-run output.
  • The author has confirmed that the code adheres to project guidelines, is properly formatted, passes clippy checks, includes sufficient test coverage, and has a corresponding changeset entry.
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 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 counter productive. 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

@dumko2001
Copy link
Author

/gemini review

Copy link
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 introduces a 'draft-only' mode for Gmail to prevent accidental sending of emails, which is a valuable safety feature. The implementation correctly uses a --draft-only flag and a GOOGLE_WORKSPACE_GMAIL_DRAFT_ONLY environment variable to control this mode. The logic properly intercepts both raw API calls (like messages.send and drafts.send) and the relevant helper commands, while still permitting draft creation and dry runs. The addition of integration tests provides good coverage for the new functionality. The code is well-structured and the changes appear correct. I have not found any issues of high or critical severity in this pull request.

Copy link
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 introduces a 'draft-only' mode for Gmail, a valuable safety feature to prevent accidental email sends. The implementation is robust, with checks at both the high-level helper command layer and the low-level raw API execution layer. The addition of a global --draft-only flag and the GOOGLE_WORKSPACE_GMAIL_DRAFT_ONLY environment variable provides flexible configuration. The new integration tests thoroughly cover the expected behavior of this new mode.

My main concern is the downgrade of the ratatui dependency, which seems out of scope for this feature and has a significant impact on the project's dependencies. I've left a specific comment regarding this.

@dumko2001 dumko2001 force-pushed the feature/issue-424-gmail-drafts branch from 66f2577 to 78c30d6 Compare March 15, 2026 16:58
@dumko2001 dumko2001 changed the title feat(gmail): add draft-only mode to prevent accidental sends feat(gmail): add draft-only mode to prevent accidental sends (#424) Mar 15, 2026
@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
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 introduces a valuable draft-only mode for Gmail to prevent accidental sending of emails. The implementation correctly intercepts both helper commands (like +send) and raw API calls (users.messages.send), with activation via a flag or environment variable. The logic correctly permits draft creation and dry runs while in this mode. My review includes one critical suggestion to improve the robustness of the check for raw API calls, which currently relies on an optional field from the API's discovery document. The proposed change will make this important safety feature more resilient against silent failures.

@dumko2001
Copy link
Author

I've refactored the Gmail draft-only check to use the resolved command path instead of the optional method.id, as suggested. I also confirmed that ratatui is at version 0.30.0. Verified the fix with unit tests and manual CLI execution.

Manual verification result for send (blocked):

{
  "error": {
    "code": 400,
    "message": "Gmail draft-only mode is active. Sending mail is blocked (preparing a draft is still allowed).",
    "reason": "validationError"
  }
}

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
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 introduces a 'draft-only' mode for Gmail to prevent accidental email sending, which is a valuable safety feature. The implementation correctly intercepts send operations at both the raw API and helper command levels, using a new --draft-only flag and a corresponding environment variable. The changes are well-tested with new integration tests. My review includes one suggestion to improve the robustness of the raw API interception logic by using the method ID instead of a hardcoded command path.

@dumko2001 dumko2001 force-pushed the feature/issue-424-gmail-drafts branch from 916803a to 554e7a8 Compare March 15, 2026 17:50
@dumko2001
Copy link
Author

I've updated the implementation to use a hybrid check for Gmail draft-only mode.

Initially, the review suggested avoiding method.id due to its optional nature in the Discovery Document. Subsequently, a follow-up review recommended method.id for its stability across CLI restructurings.

The new hybrid check satisfies both:

  1. It primarily checks method.id for stability.
  2. If method.id is missing, it falls back to checking the command path for robustness.

Verification results:

  • gws gmail users messages send --draft-only -> Blocked (Validation Error)
  • gws gmail users drafts create --draft-only --dry-run -> Allowed (Proceeds past validation)

@googleworkspace-bot
Copy link
Collaborator

/gemini review

Copy link
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 introduces a new +read helper command for the Gmail CLI, enabling users to extract message bodies and headers in plain text or JSON format, with options for HTML output. It also adds a global --draft-only flag and corresponding environment variable to prevent accidental email sending when working with Gmail drafts. A minor bug was introduced by accidentally duplicating the disclaimer message in the print_usage function.

@dumko2001 dumko2001 changed the title feat(gmail): add draft-only mode to prevent accidental sends (#424) feat(gmail): implement robust draft-only mode with centralized policy enforcement (#424) Mar 16, 2026
@googleworkspace-bot
Copy link
Collaborator

/gemini review

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

Labels

area: core Core CLI parsing, commands, error handling, utilities area: distribution area: http

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Gmail draft-only mode for CLI+agent workflows

2 participants