Skip to content

Fix DSN construction to support passwords with special characters#593

Merged
morgo merged 6 commits intoblock:mainfrom
deanbaker:fix-dsn-special-char-passwords
Mar 2, 2026
Merged

Fix DSN construction to support passwords with special characters#593
morgo merged 6 commits intoblock:mainfrom
deanbaker:fix-dsn-special-char-passwords

Conversation

@deanbaker
Copy link
Contributor

@deanbaker deanbaker commented Feb 9, 2026

Summary

Fixes #592

  • DSN construction via fmt.Sprintf and manual string concatenation breaks when passwords contain special characters (@, ?, &, /) — common in AWS IAM authentication tokens
  • Refactored newDSN() in pkg/dbconn/conn.go and dsn() in pkg/migration/runner.go to use the go-sql-driver/mysql library's structured Config type with ParseDSN() / FormatDSN() instead of manual string building
  • Added test cases covering passwords with @, ?, &, and full AWS IAM-style tokens to verify correct round-tripping

Test plan

  • Unit tests added for passwords with special characters (@, multiple @, mixed special chars, empty password, AWS IAM-style token)
  • Existing tests pass (go test ./pkg/dbconn/... ./pkg/migration/...)
  • Manual verification with an IAM auth token DSN

🤖 Generated with Claude Code

Replace fmt.Sprintf-based DSN construction with mysql.Config.FormatDSN()
in Runner.dsn(), and rewrite newDSN() to set parameters on the parsed
config struct instead of naive string concatenation. The old approach
used strings.Contains(dsn, "?") to detect query params, which broke
when passwords contained "?" (e.g. AWS IAM auth tokens), causing all
session variables and driver options to be absorbed into the DB name.

Also enable AllowCleartextPasswords, which is required for AWS RDS IAM
authentication (mysql_clear_password plugin).
Remove redundant TLS parameter pre-checks that were bypassing DSN
construction. Remove unused createFallbackDSN function. Make
AllowCleartextPasswords conditional on TLS being configured rather
than always enabled. Update tests for corrected mixed-case TLS
parameter behavior and add special character password round-trip tests.
@morgo morgo requested a review from Kiran01bm February 10, 2026 16:36
Empty tls= is indistinguishable from unset after ParseDSN, so the
TLS config should be applied rather than preserving the empty value.
Remove allowNativePasswords=true from expected params since true is
the go-mysql-driver default and FormatDSN() omits default values.
@deanbaker deanbaker marked this pull request as ready for review February 12, 2026 04:56
…n newDSN

- newDSN: replace early return when TLS is set with a guard around only
  the TLS switch block, so session variables (sql_mode, time_zone,
  innodb_lock_wait_timeout, RejectReadOnly, etc.) are always applied
- addTLSParametersToDSN: check cfg.TLSConfig before overwriting to
  preserve user-supplied TLS overrides (e.g. tls=skip-verify)
- Update tests to verify TLS preservation via parsed config rather than
  raw string equality

Amp-Thread-ID: https://ampcode.com/threads/T-019c63df-40f9-740b-8419-c3aa7d83cb32
Co-authored-by: Amp <amp@ampcode.com>
morgo added 2 commits March 2, 2026 10:43
Resolve conflicts in pkg/dbconn/conn.go and pkg/migration/runner_test.go:
- conn.go: Keep PR's cfg.TLSConfig/cfg.FormatDSN() approach (safe for
  special characters in passwords) with main's switch statement structure
- runner_test.go: Keep both TestDSN (from PR) and TestEnumReorder/
  TestSetReorder (from main)
…ve mixed-case TLS test wording

- Remove cfg.AllowNativePasswords = true since it is the default in go-sql-driver
  (https://github.com/go-sql-driver/mysql/blob/v1.9.3/dsn.go#L96)
- Improve test description for mixed-case TLS parameter edge case to explicitly
  call out that uppercase 'TLS' is ignored by ParseDSN and passed through as an
  unknown query parameter
- Remove stale comment referencing AllowNativePasswords in fallback test
@morgo morgo self-requested a review March 2, 2026 18:07
@morgo morgo enabled auto-merge March 2, 2026 18:08
@morgo morgo merged commit 6ea0ded into block:main Mar 2, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DSN construction fails with passwords containing special characters

3 participants