Skip to content

contrib/database/sql: sanitize sensitive data in error messages#3686

Merged
kakkoyun merged 2 commits into
mainfrom
kakkoyun/fix_log_issues
Jun 24, 2025
Merged

contrib/database/sql: sanitize sensitive data in error messages#3686
kakkoyun merged 2 commits into
mainfrom
kakkoyun/fix_log_issues

Conversation

@kakkoyun

Copy link
Copy Markdown
Member

Summary

This PR enhances error handling in the database/sql contrib package by ensuring that sensitive information in database connection strings is properly sanitized before being logged or included in error messages.

Changes

  • Added comprehensive sanitization functions for various DSN formats (MySQL, PostgreSQL, SQL Server)
  • Enhanced error logging to prevent credential exposure in application logs
  • Implemented robust pattern matching for different connection string formats
  • Added test coverage for edge cases and different DSN patterns

Security Enhancement

This change proactively ensures that database credentials and other sensitive connection details are never exposed in error messages or debug logs, following security best practices for credential handling.

Test Coverage

  • New test cases covering various DSN formats and edge cases
  • Verification of sanitization effectiveness across different database drivers

This change ensures that database connection strings and other sensitive
information are properly sanitized in error messages and logs. The implementation
handles various DSN formats including MySQL, PostgreSQL, and SQL Server
connection strings, preventing exposure of credentials in application logs.

Signed-off-by: Kemal Akkoyun <[email protected]>
@kakkoyun
kakkoyun requested review from a team as code owners June 24, 2025 11:46
@github-actions github-actions Bot added the apm:ecosystem contrib/* related feature requests or bugs label Jun 24, 2025
@pr-commenter

pr-commenter Bot commented Jun 24, 2025

Copy link
Copy Markdown

Benchmarks

Benchmark execution time: 2025-06-24 13:48:56

Comparing candidate commit f436fdc in PR branch kakkoyun/fix_log_issues with baseline commit 897f65b in branch main.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 24 metrics, 0 unstable metrics.

@kakkoyun
kakkoyun requested a review from a team June 24, 2025 11:49
@kakkoyun
kakkoyun enabled auto-merge (squash) June 24, 2025 13:36
@kakkoyun

Copy link
Copy Markdown
Member Author

@eliottness could you have look at it?

@hannahkm
hannahkm requested a review from zarirhamza June 24, 2025 13:37
@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Jun 24, 2025

Copy link
Copy Markdown

Datadog Report

Branch report: kakkoyun/fix_log_issues
Commit report: 88e5ece
Test service: dd-trace-go

✅ 0 Failed, 70 Passed, 0 Skipped, 4m 22.43s Total Time

@kakkoyun
kakkoyun merged commit d2b0acd into main Jun 24, 2025
232 of 234 checks passed
@kakkoyun
kakkoyun deleted the kakkoyun/fix_log_issues branch June 24, 2025 14:01

@RomainMuller RomainMuller 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.

TBH I feel this might still fail in ways that expose credentials and I'd rather simplify it if possible...

Comment on lines +196 to +198
keyValueSpacePattern = regexp.MustCompile(`(?i)(password|passwd|pwd)\s*=\s*(.+?)(\s+(?:host|user|port|database|dbname)\s*=)`)
keyValueSemiPattern = regexp.MustCompile(`(?i)(password|passwd|pwd)\s*=\s*([^;]+)(;)`)
keyValuePattern = regexp.MustCompile(`(?i)(password|passwd|pwd)\s*=\s*([^\s;]+)`)

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.

Like to have regex1 links for all regexes so they can easily be tested etc...

These also make me feel a little frisky -- this is effectively a filtering deny-list, so who know about patterns you've not thought about, yet are still credentials?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is just a best effort. I'm aware this solution doesn't cover all possible cases.

Comment on lines +165 to +167
// Fallback: use the comprehensive sanitize() function for all password patterns.
e.URL = sanitize(e.URL)
return e

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.

As noted below, the fallback makes it possible to leak credentials, in particular since the URL is malformed here, there could be typos in strategic places that cause credentials to still be leaked. I'd rather use an opaque string here...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍 Makes sense. It was hard to get the regexes right for the test cases that I have came up with.

Having an opaque string to say indicate that this URL in this error is redacted is fine for me.

Comment on lines +211 to +215
// Look for URL patterns and manually handle them to avoid issues with @ in passwords.
urlStart := strings.Index(msg, "://")
if urlStart == -1 {
return msg
}

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.

This is a fallback for an URL that did not successfully parse isn't it? Yet here we're failing open in cases where the URL is blatantly invalid?

}

// sanitizeURLPasswords sanitizes passwords in URL format (user:pass@host).
func sanitizeURLPasswords(msg string) string {

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.

This feels dangerously close to trying to correctly parse an invalid URL (since url.Parse failed)... parsing valid URLs correctly is already extremely difficult...

genesor pushed a commit that referenced this pull request Jun 24, 2025
genesor pushed a commit that referenced this pull request Jun 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apm:ecosystem contrib/* related feature requests or bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants