fix(connection): scan for any .s.PGSQL.<port> socket, not just 5432#728
fix(connection): scan for any .s.PGSQL.<port> socket, not just 5432#728
Conversation
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #728 +/- ##
==========================================
- Coverage 68.75% 68.74% -0.01%
==========================================
Files 46 46
Lines 31061 31093 +32
==========================================
+ Hits 21354 21372 +18
- Misses 9707 9721 +14 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
REVVerdict: REQUEST_CHANGES The direction is correct — auto-detecting non-5432 ports is worth having, and the fast path / slow path structure is sound. Three issues need fixing before merge, one of which is a correctness bug. Issue 1 — Wrong existence check for sockets (correctness bug)// Verify it is actually a socket, not a stale file.
if entry.path().exists() {
The right check is: use std::os::unix::fs::FileTypeExt;
if entry.path().metadata()
.map(|m| m.file_type().is_socket())
.unwrap_or(false)
{
return ((*dir).to_owned(), port);
}This is exactly what libpq does: it calls Note: The fast path ( Issue 2 — Double
|
| # | Severity | Description |
|---|---|---|
| 1 | Bug | exists() does not verify socket type; use is_socket() |
| 2 | Correctness risk | Double default_host_port() call can return inconsistent host/port pair |
| 3 | Correctness risk | Slow path is non-deterministic; sort or document |
| 4 | Noise | Cargo.lock version bump unrelated to this change |
Fix issues 1-3, then this is good to merge.
|
Follow-up REV on current HEAD (5878245). Three issues from the original review remain open. 1. is_socket() not used — .exists() does not distinguish socket from regular file Fast path (line ~378): if path.join(".s.PGSQL.5432").exists() {Slow path (line ~392): if entry.path().exists() {Both still use Fix: add 2. default_host_port() called twice — TOCTOU window
Fix: call 3. Slow path is non-deterministic The slow path returns on the first matching entry from if entry.path().exists() {
return ((*dir).to_owned(), port);
}
Fix: collect all valid ports into a The doc comment says "the first one found" which correctly documents the current (non-deterministic) behavior, but that is the behavior that should change, not just be documented. No other issues — the |
c2eead5 to
5a2feab
Compare
…terministic port sort - Replace exists() with metadata().file_type().is_socket() in both the fast path (port 5432) and slow path to correctly reject non-socket files. - Introduce default_host_port() -> (String, u16) replacing default_host() so host and port are derived from a single directory scan. - Call default_host_port() once in resolve_params() and thread the results into resolve_host() and resolve_port() to eliminate the TOCTOU race from two independent read_dir() scans. - Collect all matching socket ports in the slow path, sort ascending, and return the lowest for deterministic behavior when multiple sockets exist.
5a2feab to
d72dc94
Compare
Problem
B1 audit test fails: when
rpgis invoked with no host/port and a Postgres socket exists in/tmpat a non-standard port (e.g./tmp/.s.PGSQL.5437),default_host()(nowdefault_host_port()) only checked for.s.PGSQL.5432— hardcoded — and fell back tolocalhost:5432(connection refused).Fix
Replace
default_host() -> Stringwithdefault_host_port() -> (String, u16):.s.PGSQL.5432first (libpq-compatible, O(1)).s.PGSQL.<N>socket file, excluding.lockfiles, and return the first valid one with its port numberBoth
resolve_host()andresolve_port()updated to usedefault_host_port()as their fallback.Tests
rpg -c 'SHOW port'→ connects via socket at port 5437)test_defaultsupdated to assert againstdefault_host_port()result rather than hardcoded 5432 (environment-aware)Fixes B1 audit test.