fix: split DSN credentials on the last @ of the authority - #394
Merged
Conversation
SafeURL took the first '@' as the userinfo separator. '@' is a legal password character, so a password carrying one was cut short there and its tail folded into the hostname. For postgres://user:pa@ss@dbhost:5432/mydb the password became "pa" and the host "ss@dbhost". This is not connector-specific: SafeURL parses the DSN for all five connectors, so any of them rejects a password containing '@'. The symptom is a DNS failure (getaddrinfo EAI_FAIL on the mangled host) or, when the truncated prefix happens to be a valid password elsewhere, a plain login failure — neither points at the password. It also weakens obfuscateDSNPassword, which masks the DSN before it is logged and relies on the same split. Given the DSN above it produced postgres://user:**@ss@dbhost:5432/mydb leaving the part of the password after the '@' in the string. DBHub prints that line per source at startup, so the tail reached the server's log — and, on the stdio transport, the client's. The exposure is limited to an operator's own password on a host that already holds the config, and only arises in a deployment that cannot connect at all, so this is filed as a bug rather than through the advisory process; say the word if you would rather handle it the other way. Standard URL parsing ends userinfo at the last '@' of the authority, so match that. The search is bounded by the start of the path, because the path is still attached at this point and may legitimately contain '@' (a database named "a@b"): an unbounded lastIndexOf would then pick that one as the separator. Percent-encoding already worked, since the parser decodes username and password. This makes the unencoded form work too, which is what a DSN assembled from an environment variable ends up carrying. Tests cover the multi-'@' password, '@' in the path (with and without credentials), the percent-encoded form, the case with a port, and the masking above, which had no test for this at all. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes DSN parsing in SafeURL so credentials are split on the last @ in the authority (bounded to the start of the path), which correctly supports passwords containing @ and prevents accidental hostname mangling and incomplete DSN masking.
Changes:
- Update
SafeURLauthority parsing to uselastIndexOf('@')bounded by the path start. - Add unit tests covering
@in passwords (single/multiple),@in path, percent-encoded@, and port preservation. - Add a regression test ensuring
obfuscateDSNPasswordfully masks passwords containing@.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/utils/safe-url.ts | Fixes authority credential splitting to handle @ in passwords safely (bounded by path start). |
| src/utils/tests/safe-url.test.ts | Adds coverage for @ in password/path scenarios and port preservation. |
| src/utils/tests/dsn-obfuscate.test.ts | Adds regression coverage to ensure password masking remains complete when passwords contain @. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SafeURLtakes the first@as the userinfo separator.@is a legal password character, so a password carrying one is cut short there and its tail is folded into the hostname:Scope
Not connector-specific.
SafeURLparses the DSN for all five connectors, so any of them rejects a password containing@:ss@dbhostpass@dbhostpass@dbhostpass@dbhostpaThe symptom is a DNS failure on the mangled host (
getaddrinfo EAI_FAIL), or — when the truncated prefix happens to be a valid password for that account — a plainLogin failed. Neither points at the password, which makes this slow to diagnose from the outside.It also weakens the DSN masking
obfuscateDSNPasswordmasks the DSN before it is logged, and relies on the same split. Given the DSN above it produced:The part of the password after the
@stays in the string. DBHub prints that line per source at startup, so the tail reaches the server's log — and on the stdio transport, the client's.I have filed this as a bug rather than through the advisory process, because the exposure is narrow: it is the operator's own password, on a host that already holds the config file containing it in full, and it only arises in a deployment that cannot connect at all — a password with
@means DBHub fails fast and never serves anything. Happy to move it if you would rather handle it the other way.The fix
Standard URL parsing ends userinfo at the last
@of the authority, so match that.The search is bounded by the start of the path, because the path is still attached at that point and may legitimately contain
@— a database nameda@b. An unboundedlastIndexOfwould then pick that one as the separator, trading one bug for another.Percent-encoding already worked, since the parser decodes username and password. This makes the unencoded form work too, which is what a DSN assembled from an environment variable ends up carrying:
Testing
Six cases added for
SafeURL: a password with one@, with several,@in the path both with and without credentials, the percent-encoded form, and a password with@alongside an explicit port — which the previous behaviour also mangled.One case added for
obfuscateDSNPassword, which had no coverage for this at all.Verified to fail against the previous behaviour: reverting the one-line change turns four of the six
SafeURLcases red, and the masking case too.Full unit suite green on top of this branch (952 passing; the one failure is a pre-existing Windows-only SSH-config test that fails identically on an unmodified checkout), and no new TypeScript errors — 137 before and after.