Skip to content

fix: percent-encode usernames in Postgres connection URLs#34

Closed
cocoa-claw[bot] wants to merge 1 commit intomasterfrom
fix/username-encoding-issue-33
Closed

fix: percent-encode usernames in Postgres connection URLs#34
cocoa-claw[bot] wants to merge 1 commit intomasterfrom
fix/username-encoding-issue-33

Conversation

@cocoa-claw
Copy link
Copy Markdown
Contributor

@cocoa-claw cocoa-claw Bot commented Mar 29, 2026

Fixes #33

Problem

Postgres usernames containing @ (and other special characters) were not being percent-encoded when building connection URIs, causing the @ to be misinterpreted as the delimiter between userinfo and host.

Example: user@domain.com would produce postgres://user@domain.com@localhost/mydb which parses incorrectly.

Solution

URL-encode the username in to_url() using the existing urlencoding::encode() helper.

Changes

  • Encode username in to_url()
  • Add test_connection_to_url_with_at_in_username() test
  • Add test_connection_url_round_trip_with_special_username() test

Verification

  • Username user@domain.com now produces: postgres://user%40domain.com@localhost/mydb
  • Round-trip test confirms: to_url()from_url() preserves the original username

@cocoa-claw cocoa-claw Bot force-pushed the fix/username-encoding-issue-33 branch 4 times, most recently from 00c598b to fadd91d Compare March 29, 2026 20:29
@fcoury
Copy link
Copy Markdown
Owner

fcoury commented Mar 29, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fadd91d308

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/tsql/src/config/connections.rs Outdated
// Add user
url.push_str(&self.user);
// Add user (percent-encoded to handle special characters like @)
url.push_str(&urlencoding::encode(&self.user));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid double-encoding legacy stored usernames

Encoding self.user unconditionally in to_url() regresses existing saved connections whose user field is already percent-encoded (for example, entries created earlier via URL paste/import). Those entries will now serialize as ...user%2540domain.com..., so Postgres receives user%40domain.com as the literal username and authentication fails. The new decode path in from_url() only fixes newly parsed URLs, so previously persisted configs are still affected unless you normalize/migrate legacy user values before this encode step.

Useful? React with 👍 / 👎.

Fixes issue #33 where usernames containing @ (and other special characters)
would break the connection URI parsing.

Changes:
- URL-encode the username in to_url() using the existing urlencoding::encode()
- Add tests for usernames with @ and other special characters
- Verify round-trip encoding/decoding works correctly

Before: user@domain.com@localhost (parsed incorrectly)
After:  user%40domain.com@localhost (parsed correctly)
@cocoa-claw cocoa-claw Bot force-pushed the fix/username-encoding-issue-33 branch from fadd91d to c36f745 Compare March 29, 2026 21:16
@fcoury
Copy link
Copy Markdown
Owner

fcoury commented Mar 29, 2026

@codex review

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c36f7458ea

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +280 to +283
let decoded_user = urlencoding::decode(&self.user)
.map(|s| s.into_owned())
.unwrap_or_else(|_| self.user.clone());
url.push_str(&urlencoding::encode(&decoded_user));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Stop decoding usernames before URL encoding

to_url() now runs urlencoding::decode(&self.user) before encoding, which can silently rewrite valid usernames that contain literal percent sequences. A concrete failure path is postgres://user%2540team@host/db: from_url() stores user%40team, but to_url() decodes that to user@team and emits user%40team, changing the account identity on reconnect and causing authentication to target the wrong role. Usernames should be percent-encoded from the stored value without a pre-decode step.

Useful? React with 👍 / 👎.

@fcoury fcoury added the bug Something isn't working label Apr 18, 2026
@fcoury
Copy link
Copy Markdown
Owner

fcoury commented Apr 18, 2026

Closing in favor of #40, which reimplements the fix from a fresh master branch and preserves literal percent sequences in stored usernames.

@fcoury fcoury closed this Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection manager: Postgres usernames are not encoded and can break the connection URI

1 participant