fraiseql-wire 2.2.0

Streaming JSON query engine for Postgres 17
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Connection string parsing
//!
//! Supports formats:
//! * postgres://[user[:password]@][host][:port][/database]
//! * <postgres:///database> (Unix socket, local)
//! * <postgres:///database?host=/path/to/socket> (Unix socket, custom directory)

use crate::connection::ConnectionConfig;
use crate::{Result, WireError};
use std::path::{Component, Path, PathBuf};
use zeroize::Zeroizing;

/// Maximum byte length for a Unix socket directory path.
///
/// Linux's `sun_path` field is 108 bytes; 4096 is the broader POSIX PATH_MAX.
/// Any path longer than this cannot be a valid socket directory.
const MAX_SOCKET_DIR_BYTES: usize = 4096;

/// Validate a Unix socket directory path supplied via the `host` query parameter.
///
/// # Errors
///
/// Returns `WireError::Config` if:
/// - `dir` is longer than `MAX_SOCKET_DIR_BYTES`
/// - `dir` is not an absolute path (does not start with `/`)
/// - `dir` contains a `..` component (path traversal)
fn validate_socket_dir(dir: &str) -> Result<()> {
    if dir.len() > MAX_SOCKET_DIR_BYTES {
        return Err(WireError::Config(format!(
            "Unix socket directory path is too long ({} bytes, max {MAX_SOCKET_DIR_BYTES})",
            dir.len()
        )));
    }

    let p = Path::new(dir);
    if !p.is_absolute() {
        return Err(WireError::Config(format!(
            "Unix socket directory must be an absolute path (got {dir:?})"
        )));
    }

    if p.components().any(|c| c == Component::ParentDir) {
        return Err(WireError::Config(format!(
            "Unix socket directory must not contain '..' components (got {dir:?})"
        )));
    }

    Ok(())
}

/// Parsed connection info
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    /// Transport type
    pub transport: TransportType,
    /// Host (for TCP)
    pub host: Option<String>,
    /// Port (for TCP)
    pub port: Option<u16>,
    /// Unix socket path
    pub unix_socket: Option<PathBuf>,
    /// Database name
    pub database: String,
    /// Username
    pub user: String,
    /// Password (zeroed on drop for security)
    pub password: Option<Zeroizing<String>>,
}

/// Transport type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TransportType {
    /// TCP socket
    Tcp,
    /// Unix domain socket
    Unix,
}

/// Resolve the default Unix socket directory
fn resolve_default_socket_dir() -> Option<String> {
    // Try standard locations in order (Linux convention)
    for dir in &["/run/postgresql", "/var/run/postgresql", "/tmp"] {
        if Path::new(dir).is_dir() {
            return Some((*dir).to_string());
        }
    }
    None
}

/// Extract a query parameter value from a query string
fn parse_query_param(query_string: &str, param: &str) -> Option<String> {
    if query_string.is_empty() {
        return None;
    }

    // Remove leading '?' if present
    let query = query_string.trim_start_matches('?');

    // Find the parameter
    for pair in query.split('&') {
        if let Some((key, value)) = pair.split_once('=') {
            if key == param {
                return Some(value.to_string());
            }
        }
    }
    None
}

/// Construct the full Unix socket path
fn construct_socket_path(socket_dir: &str, port: u16) -> PathBuf {
    PathBuf::from(format!("{}/.s.PGSQL.{}", socket_dir, port))
}

impl ConnectionInfo {
    /// Parse connection string
    ///
    /// # Errors
    ///
    /// Returns [`WireError::Config`] if the string does not start with `postgres://` or
    /// `postgresql://`, or if the host/port/database fields cannot be parsed.
    pub fn parse(s: &str) -> Result<Self> {
        // Simple parser (production code would use url crate)
        if !s.starts_with("postgres://") && !s.starts_with("postgresql://") {
            return Err(WireError::Config(
                "connection string must start with postgres://".into(),
            ));
        }

        let rest = s
            .strip_prefix("postgres://")
            .or_else(|| s.strip_prefix("postgresql://"))
            .expect("prefix check above guarantees one of these prefixes is present");

        // Check if Unix socket (starts with / or no host)
        if rest.starts_with('/') || rest.starts_with("///") {
            return Self::parse_unix(rest);
        }

        Self::parse_tcp(rest)
    }

    fn parse_unix(rest: &str) -> Result<Self> {
        // Format: postgres:///database or postgres:///database?host=/path/to/socket&port=5432
        // Split database name from query parameters
        let (path, query_string) = if let Some(q_pos) = rest.find('?') {
            let (p, q) = rest.split_at(q_pos);
            (p, q)
        } else {
            (rest, "")
        };

        let path = path.trim_start_matches('/');

        let database = if path.is_empty() {
            whoami::username()
        } else {
            path.to_string()
        };

        // Parse port from query parameters (default: 5432)
        let port = parse_query_param(query_string, "port")
            .and_then(|p| p.parse::<u16>().ok())
            .unwrap_or(5432);

        // Determine socket directory
        let socket_dir = if let Some(custom_dir) = parse_query_param(query_string, "host") {
            // Validate before use: must be absolute, no traversal, within length limit.
            validate_socket_dir(&custom_dir)?;
            custom_dir
        } else {
            // Use default socket directory
            resolve_default_socket_dir().ok_or_else(|| {
                WireError::Config(
                    "could not locate Unix socket directory. Set host query parameter explicitly."
                        .into(),
                )
            })?
        };

        let unix_socket = Some(construct_socket_path(&socket_dir, port));

        Ok(Self {
            transport: TransportType::Unix,
            host: None,
            port: Some(port),
            unix_socket,
            database,
            user: whoami::username(),
            password: None,
        })
    }

    fn parse_tcp(rest: &str) -> Result<Self> {
        // Format: [user[:password]@]host[:port][/database]
        let (auth, rest) = if let Some(pos) = rest.find('@') {
            let (auth, rest) = rest.split_at(pos);
            (Some(auth), &rest[1..])
        } else {
            (None, rest)
        };

        let (user, password) = if let Some(auth) = auth {
            if let Some(pos) = auth.find(':') {
                let (user, pass) = auth.split_at(pos);
                (
                    user.to_string(),
                    Some(Zeroizing::new(pass[1..].to_string())),
                )
            } else {
                (auth.to_string(), None)
            }
        } else {
            (whoami::username(), None)
        };

        let (host_port, database) = if let Some(pos) = rest.find('/') {
            let (hp, db) = rest.split_at(pos);
            (hp, db[1..].to_string())
        } else {
            (rest, whoami::username())
        };

        let (host, port) = if let Some(pos) = host_port.find(':') {
            let (host, port) = host_port.split_at(pos);
            let port = port[1..]
                .parse()
                .map_err(|_| WireError::Config("invalid port".into()))?;
            (host.to_string(), port)
        } else {
            (host_port.to_string(), 5432)
        };

        Ok(Self {
            transport: TransportType::Tcp,
            host: Some(host),
            port: Some(port),
            unix_socket: None,
            database,
            user,
            password,
        })
    }

    /// Convert to `ConnectionConfig`
    pub fn to_config(&self) -> ConnectionConfig {
        let mut config = ConnectionConfig::new(&self.database, &self.user);
        if let Some(ref password) = self.password {
            // SECURITY: Extract password string from Zeroizing wrapper
            config = config.password(password.as_str());
        }
        config
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)] // Reason: test code, panics are acceptable
    use super::*;

    #[test]
    fn test_parse_tcp_full() {
        let info = ConnectionInfo::parse("postgres://user:pass@localhost:5433/mydb").unwrap();
        assert_eq!(info.transport, TransportType::Tcp);
        assert_eq!(info.host, Some("localhost".to_string()));
        assert_eq!(info.port, Some(5433));
        assert_eq!(info.database, "mydb");
        assert_eq!(info.user, "user");
        assert_eq!(info.password.as_ref().map(|p| p.as_str()), Some("pass"));
    }

    #[test]
    fn test_parse_tcp_minimal() {
        let info = ConnectionInfo::parse("postgres://localhost/mydb").unwrap();
        assert_eq!(info.transport, TransportType::Tcp);
        assert_eq!(info.host, Some("localhost".to_string()));
        assert_eq!(info.port, Some(5432));
        assert_eq!(info.database, "mydb");
    }

    #[test]
    fn test_parse_unix() {
        let info = ConnectionInfo::parse("postgres:///mydb").unwrap();
        assert_eq!(info.transport, TransportType::Unix);
        assert_eq!(info.database, "mydb");
        assert_eq!(info.port, Some(5432)); // Default port
                                           // Socket path should contain the database name and port
        assert!(info.unix_socket.is_some());
        let path = info.unix_socket.unwrap();
        assert!(path.to_string_lossy().contains(".s.PGSQL.5432"));
    }

    #[test]
    fn test_parse_unix_socket_path_construction() {
        let info = ConnectionInfo::parse("postgres:///mydb").unwrap();
        let socket_path = info.unix_socket.unwrap();
        // Socket path should end with .s.PGSQL.5432
        assert!(socket_path.to_string_lossy().ends_with(".s.PGSQL.5432"));
    }

    #[test]
    fn test_parse_unix_with_custom_directory() {
        let info = ConnectionInfo::parse("postgres:///mydb?host=/custom/path").unwrap();
        assert_eq!(info.transport, TransportType::Unix);
        assert_eq!(info.database, "mydb");
        assert_eq!(info.port, Some(5432));
        let socket_path = info.unix_socket.unwrap();
        assert_eq!(socket_path, PathBuf::from("/custom/path/.s.PGSQL.5432"));
    }

    #[test]
    fn test_parse_unix_with_custom_port() {
        let info = ConnectionInfo::parse("postgres:///mydb?host=/tmp&port=5433").unwrap();
        assert_eq!(info.transport, TransportType::Unix);
        assert_eq!(info.database, "mydb");
        assert_eq!(info.port, Some(5433));
        let socket_path = info.unix_socket.unwrap();
        assert_eq!(socket_path, PathBuf::from("/tmp/.s.PGSQL.5433"));
    }

    #[test]
    fn test_construct_socket_path() {
        let path = construct_socket_path("/run/postgresql", 5432);
        assert_eq!(path, PathBuf::from("/run/postgresql/.s.PGSQL.5432"));

        let path = construct_socket_path("/var/run/postgresql", 5433);
        assert_eq!(path, PathBuf::from("/var/run/postgresql/.s.PGSQL.5433"));
    }

    #[test]
    fn test_parse_query_param() {
        let host = parse_query_param("?host=/tmp", "host");
        assert_eq!(host, Some("/tmp".to_string()));

        let port = parse_query_param("?host=/tmp&port=5433", "port");
        assert_eq!(port, Some("5433".to_string()));

        let missing = parse_query_param("?host=/tmp", "port");
        assert_eq!(missing, None);

        let empty = parse_query_param("", "host");
        assert_eq!(empty, None);
    }

    #[test]
    fn test_parse_unix_default_database() {
        // When no database specified, should use username
        let info = ConnectionInfo::parse("postgres:///").unwrap();
        assert_eq!(info.transport, TransportType::Unix);
        // Database should be the username (from whoami)
        assert!(!info.database.is_empty());
    }

    #[test]
    fn test_password_field_present() {
        // Verify password field exists and is properly handled (and zeroed on drop)
        let info = ConnectionInfo::parse("postgres://user:secret@localhost/db").unwrap();
        assert_eq!(info.password.as_ref().map(|p| p.as_str()), Some("secret"));
    }

    // ── Socket-dir validation tests ────────────────────────────────────────────

    #[test]
    fn test_valid_socket_dir_accepted() {
        validate_socket_dir("/run/postgresql")
            .unwrap_or_else(|e| panic!("expected Ok for /run/postgresql: {e}"));
        validate_socket_dir("/tmp").unwrap_or_else(|e| panic!("expected Ok for /tmp: {e}"));
        validate_socket_dir("/var/run/postgresql")
            .unwrap_or_else(|e| panic!("expected Ok for /var/run/postgresql: {e}"));
    }

    #[test]
    fn test_relative_socket_dir_rejected() {
        let err = validate_socket_dir("run/postgresql").unwrap_err();
        assert!(matches!(err, WireError::Config(_)));
        let msg = err.to_string();
        assert!(msg.contains("absolute"), "error must say 'absolute': {msg}");
    }

    #[test]
    fn test_dot_dot_in_socket_dir_rejected() {
        let err = validate_socket_dir("/run/../etc").unwrap_err();
        assert!(matches!(err, WireError::Config(_)));
        let msg = err.to_string();
        assert!(msg.contains(".."), "error must mention '..': {msg}");
    }

    #[test]
    fn test_socket_dir_too_long_rejected() {
        // 4097-byte path must be rejected by the length guard.
        let long = format!("/{}", "a".repeat(4096));
        let err = validate_socket_dir(&long).unwrap_err();
        assert!(matches!(err, WireError::Config(_)));
        let msg = err.to_string();
        assert!(msg.contains("4096"), "error must mention the limit: {msg}");
    }

    #[test]
    fn test_connection_string_rejects_traversal_in_host_param() {
        let result = ConnectionInfo::parse("postgres:///mydb?host=/run/../etc");
        assert!(result.is_err(), "path traversal in host must be rejected");
    }

    #[test]
    fn test_connection_string_rejects_relative_host_param() {
        let result = ConnectionInfo::parse("postgres:///mydb?host=relative/path");
        assert!(result.is_err(), "relative host param must be rejected");
    }
}