Skip to content

fix(auth): prevent mask_secret panic on multi-byte UTF-8 secrets#591

Merged
jpoehnelt merged 1 commit intomainfrom
fix/mask-secret-utf8-panic
Mar 23, 2026
Merged

fix(auth): prevent mask_secret panic on multi-byte UTF-8 secrets#591
jpoehnelt merged 1 commit intomainfrom
fix/mask-secret-utf8-panic

Conversation

@jpoehnelt
Copy link
Copy Markdown
Member

Summary

Fixes a runtime panic in mask_secret() when a credential string contains multi-byte UTF-8 characters (accented letters, emoji, CJK, etc.) near the masking boundary.

Root Cause

The original code used byte-level slicing (&s[..4], &s[s.len()-4..]) on a Rust &str. Rust strings are UTF-8, where a single character can be 1–4 bytes. If byte offset 4 lands inside a multi-byte character, Rust panics with byte index is not a char boundary.

Fix

  • s.len()s.chars().count() (count characters, not bytes)
  • &s[..4]s.chars().take(4).collect() (first 4 characters)
  • &s[s.len()-4..]s.chars().skip(n-4).collect() (last 4 characters)

Tests

Added mask_secret_multibyte_utf8 test covering accented Latin, Greek alphabet, and short multi-byte strings. All 750 tests pass with zero regressions.

Impact

  • No breaking changes — output is identical for ASCII inputs
  • No performance impact — called at most twice per export on short strings
  • Single function, single file, single caller (handle_export)

Checklist

  • My code follows the AGENTS.md guidelines
  • I have run cargo fmt --all
  • I have run cargo clippy -- -D warnings and resolved all warnings
  • I have added tests that prove my fix is effective
  • I have provided a Changeset file

Use char-based indexing instead of byte-offset slicing in mask_secret()
to prevent panics when secrets contain multi-byte UTF-8 characters
(accented letters, emoji, CJK, etc.).
@jpoehnelt jpoehnelt added area: auth area: core Core CLI parsing, commands, error handling, utilities labels Mar 23, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 23, 2026

🦋 Changeset detected

Latest commit: 6a6a092

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 a critical runtime panic in the mask_secret utility by refactoring its string manipulation logic. The change ensures that secrets containing multi-byte UTF-8 characters are masked correctly without causing crashes, improving the robustness and internationalization support of the application.

Highlights

  • Prevented panic with multi-byte UTF-8 secrets: The mask_secret function now correctly handles multi-byte UTF-8 characters by switching from byte-level slicing to character-based indexing, resolving a runtime panic when masking secrets containing such characters.
  • Enhanced string manipulation logic: The mask_secret function was refactored to use s.chars().count(), s.chars().take(), and s.chars().skip() for safe and accurate character counting and slicing, ensuring correct masking regardless of character byte length.
  • Improved test coverage: A new test case, mask_secret_multibyte_utf8, was added to specifically validate the fix for various multi-byte UTF-8 scenarios, ensuring robustness and preventing regressions.
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.

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 addresses a panic that occurred in the mask_secret function when processing multi-byte UTF-8 secrets. The fix involves changing the string manipulation from byte-offset slicing to character-based indexing, ensuring correct handling of UTF-8 characters. New test cases have been added to validate this behavior with various multi-byte UTF-8 strings. There is no feedback to provide.

@googleworkspace-bot googleworkspace-bot added cla: yes This human has signed the Contributor License Agreement. and removed area: core Core CLI parsing, commands, error handling, utilities labels Mar 23, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.02%. Comparing base (b4d5e26) to head (6a6a092).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #591   +/-   ##
=======================================
  Coverage   69.01%   69.02%           
=======================================
  Files          42       42           
  Lines       19292    19298    +6     
=======================================
+ Hits        13315    13321    +6     
  Misses       5977     5977           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions bot added the gemini: reviewed Gemini Code Assist has reviewed the latest changes label Mar 23, 2026
@jpoehnelt jpoehnelt merged commit d679401 into main Mar 23, 2026
34 checks passed
@jpoehnelt jpoehnelt deleted the fix/mask-secret-utf8-panic branch March 23, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: auth cla: yes This human has signed the Contributor License Agreement. gemini: reviewed Gemini Code Assist has reviewed the latest changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants