Skip to content

Fix DSN parsing error for special characters in passwords#20

Merged
intel352 merged 4 commits intomainfrom
copilot/fix-19
Jul 7, 2025
Merged

Fix DSN parsing error for special characters in passwords#20
intel352 merged 4 commits intomainfrom
copilot/fix-19

Conversation

Copy link
Contributor

Copilot AI commented Jul 7, 2025

Problem

The database module was failing to parse DSN strings containing special characters in passwords. For example, this DSN would cause a parsing error:

postgresql://someuser:8jKwouNHdI!u6a?kx(UuQ-Bgm34P@some-dev-backend.cluster.us-east-1.rds.amazonaws.com/some_backend

The error occurred because Go's url.Parse() function treats special characters like !, ?, (, ) as URL syntax instead of literal password characters, resulting in:

parse "postgresql://someuser:8jKwouNHdI!u6a?kx(UuQ-Bgm34P@...": invalid port ":8jKwouNHdI!u6a" after host

Solution

Enhanced the DSN parsing logic in the database module to handle special characters in passwords by:

  1. Preprocessing DSNs before parsing: Added preprocessDSNForParsing() function that intelligently identifies the password portion and URL-encodes special characters
  2. Smart credential boundary detection: Uses the last @ symbol to separate credentials from host, handling cases where passwords contain @ symbols
  3. Consistent handling across functions: Updated both extractEndpointFromDSN() and replaceDSNPassword() to use the same preprocessing approach

Changes

  • Modified extractEndpointFromDSN() to preprocess DSNs with special characters
  • Modified replaceDSNPassword() to handle special characters consistently
  • Added preprocessDSNForParsing() helper function for URL-encoding password portions
  • Added comprehensive test coverage for various special character scenarios

Test Coverage

Added extensive test cases covering:

  • The exact DSN from the original issue
  • Passwords with @ symbols, query parameters, and complex special characters
  • Edge cases like multiple @ symbols and URL-like structures in passwords
  • AWS IAM authentication scenarios with special character passwords

All existing tests continue to pass, ensuring no regressions.

Fixes #19.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: intel352 <77607+intel352@users.noreply.github.com>
Copilot AI changed the title [WIP] Database module - DSN parsing error Fix DSN parsing error for special characters in passwords Jul 7, 2025
Copilot AI requested a review from intel352 July 7, 2025 15:13
@codecov
Copy link

codecov bot commented Jul 7, 2025

Codecov Report

Attention: Patch coverage is 78.18182% with 12 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
modules/database/aws_iam_auth.go 78.18% 8 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@intel352
Copy link
Contributor

intel352 commented Jul 7, 2025

@copilot Linter errors are showing up, please fix any linter errors and ensure tests are still passing.

Co-authored-by: intel352 <77607+intel352@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 7, 2025

@copilot Linter errors are showing up, please fix any linter errors and ensure tests are still passing.

Fixed linter formatting errors (trailing whitespace) in aws_iam_auth.go and confirmed all tests are still passing. Commit: 118b615

@intel352
Copy link
Contributor

intel352 commented Jul 7, 2025

@copilot

  Error: /home/runner/work/modular/modular/modules/database/dsn_special_chars_test.go:13:1: File is not properly formatted (gofmt)

Co-authored-by: intel352 <77607+intel352@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 7, 2025

@copilot

...

Fixed gofmt formatting issues in dsn_special_chars_test.go by removing trailing whitespace and fixing struct field alignment. All tests are passing and the file now passes gofmt validation. Commit: 88e2363

@intel352 intel352 marked this pull request as ready for review July 7, 2025 16:12
@intel352 intel352 merged commit 82e64b1 into main Jul 7, 2025
12 checks passed
@intel352 intel352 deleted the copilot/fix-19 branch July 7, 2025 16:12
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.

Database module - DSN parsing error

2 participants