Skip to content

fix: redact database password from tracing span output#2415

Merged
helio-frota merged 1 commit into
guacsec:mainfrom
ctron:feature/hide_connect_options_url_1
Jun 26, 2026
Merged

fix: redact database password from tracing span output#2415
helio-frota merged 1 commit into
guacsec:mainfrom
ctron:feature/hide_connect_options_url_1

Conversation

@ctron

@ctron ctron commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Add HideString newtype that wraps a Debug value and replaces all occurrences of a given string with "***" in the Debug output. Use it in the #[instrument] on Database::new to prevent the password from leaking through the url field in tracing spans.

Summary by Sourcery

Redact sensitive database passwords from tracing spans when constructing a Database instance.

Enhancements:

  • Introduce a generic HideString debug wrapper that masks a specified substring as "***" in Debug output.
  • Apply the HideString wrapper to the database configuration in Database::new tracing instrumentation to prevent password leakage.
  • Expose the new redact module from the common crate for reuse across the codebase.

Tests:

  • Add unit and rstest-based tests verifying HideString redacts sensitive values in strings and structured types like URLs.

@ctron ctron requested review from desmax74 and helio-frota June 26, 2026 08:33
@ctron ctron added the backport release/0.5.z Backport (0.5.z) label Jun 26, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Introduces a generic HideString debug wrapper to redact sensitive substrings (like DB passwords) from Debug output and wires it into Database::new tracing instrumentation so database URLs in spans no longer expose passwords.

Sequence diagram for Database::new tracing with HideString redaction

sequenceDiagram
    actor Caller
    participant Database
    participant tracing

    Caller->>Database: new(database)
    activate Database
    Database->>tracing: HideString(database, database.password.0)
    tracing-->>Database: span_created_with_redacted_database_field
    Database-->>Caller: Result<Database>
    deactivate Database
Loading

File-Level Changes

Change Details Files
Redact database password from tracing span output for Database::new.
  • Extend #[instrument] on Database::new to skip the database argument from automatic capture.
  • Add a custom span field database that uses the HideString wrapper to redact the password from the database config when logging.
  • Keep error logging level configuration unchanged while enhancing span safety.
common/src/db/mod.rs
Expose a new redact module and generic HideString Debug wrapper for redacting substrings from Debug output.
  • Export a new redact module from the common crate so it can be used elsewhere.
  • Implement the HideString<'a, T: Debug> wrapper type that formats its inner value via Debug and replaces all occurrences of a target substring with "".
  • Handle the empty hide-string case by delegating directly to the wrapped Debug impl without allocation or replacement.
  • Add rstest-based tests to verify redaction behavior for plain strings and for a config struct containing a reqwest::Url and password, ensuring passwords in URLs and fields are replaced with "".
common/src/lib.rs
common/src/redact.rs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ctron ctron requested a review from mrrajan June 26, 2026 08:33

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've left some high level feedback:

  • The HideString Debug implementation allocates a String and does a full replace on every call; if this will be used in hot paths it may be worth constraining it to cheaper, more structured redaction (e.g., only extracting/redacting the URL field or using a custom formatter instead of format!).
  • Using a plain replace on the debug output means any occurrence of the password substring (including in non-sensitive fields) will be masked; consider clarifying that this behavior is intentional or narrowing the redaction to known sensitive parts of the structure.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `HideString` `Debug` implementation allocates a `String` and does a full replace on every call; if this will be used in hot paths it may be worth constraining it to cheaper, more structured redaction (e.g., only extracting/redacting the URL field or using a custom formatter instead of `format!`).
- Using a plain `replace` on the debug output means any occurrence of the password substring (including in non-sensitive fields) will be masked; consider clarifying that this behavior is intentional or narrowing the redaction to known sensitive parts of the structure.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Add HideString newtype that wraps a Debug value and replaces all
occurrences of a given string with "***" in the Debug output. Use it
in the #[instrument] on Database::new to prevent the password from
leaking through the url field in tracing spans.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ctron ctron force-pushed the feature/hide_connect_options_url_1 branch from 24e1042 to 4758f86 Compare June 26, 2026 09:18
@codecov

codecov Bot commented Jun 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.31%. Comparing base (b5c7762) to head (4758f86).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2415      +/-   ##
==========================================
+ Coverage   71.28%   71.31%   +0.03%     
==========================================
  Files         449      450       +1     
  Lines       27146    27161      +15     
  Branches    27146    27161      +15     
==========================================
+ Hits        19352    19371      +19     
+ Misses       6662     6652      -10     
- Partials     1132     1138       +6     

☔ View full report in Codecov by Harness.
📢 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.

@ctron ctron enabled auto-merge June 26, 2026 11:23
@ctron ctron added this pull request to the merge queue Jun 26, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Jun 26, 2026
@helio-frota helio-frota added this pull request to the merge queue Jun 26, 2026
Merged via the queue into guacsec:main with commit 0bb3674 Jun 26, 2026
12 of 13 checks passed
@github-project-automation github-project-automation Bot moved this to Done in Trustify Jun 26, 2026
@trustify-ci-bot

Copy link
Copy Markdown

Successfully created backport PR for release/0.5.z:

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

Labels

backport release/0.5.z Backport (0.5.z)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants