Skip to main content

reddb_wire/
conn_string.rs

1//! Connection-string parser shared across `reddb`, `reddb-client`,
2//! `red_client`, and every language driver.
3//!
4//! Pure function over a string; no I/O, no allocation beyond what the
5//! returned [`ConnectionTarget`] needs. The grammar is defined by
6//! `docs/clients/connection-strings.md`; this module is the canonical
7//! parser and is the single source of truth consumed by the rest of
8//! the workspace.
9//!
10//! The parser ports the logic that previously lived in
11//! `drivers/rust/src/connect.rs` (which keeps a thin re-export layer
12//! for backwards compatibility while drivers migrate over). Cluster
13//! URIs (`grpc://primary,replica:port`), default ports per scheme,
14//! and the `?route=primary` override behave identically to the
15//! original.
16
17use std::path::PathBuf;
18
19use url::Url;
20
21/// Stable error code for parser failures.
22///
23/// Mirrors the `ErrorCode` shape used by the language drivers so that
24/// downstream wrappers can map 1:1 without information loss.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum ParseErrorKind {
27    /// The input was empty.
28    Empty,
29    /// `url::Url` rejected the string, or a transport-specific
30    /// invariant (missing host, empty cluster entry, bad port…) was
31    /// violated.
32    InvalidUri,
33    /// The scheme is not in the documented vocabulary.
34    UnsupportedScheme,
35    /// A DoS guardrail in [`ConnStringLimits`] was tripped.
36    /// `message` carries the limit name + the offending value so
37    /// downstream wrappers can surface the structured detail.
38    LimitExceeded,
39}
40
41impl ParseErrorKind {
42    pub fn as_str(self) -> &'static str {
43        match self {
44            ParseErrorKind::Empty => "EMPTY",
45            ParseErrorKind::InvalidUri => "INVALID_URI",
46            ParseErrorKind::UnsupportedScheme => "UNSUPPORTED_SCHEME",
47            ParseErrorKind::LimitExceeded => "LIMIT_EXCEEDED",
48        }
49    }
50}
51
52/// Error returned by [`parse`].
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct ParseError {
55    pub kind: ParseErrorKind,
56    pub message: String,
57}
58
59impl ParseError {
60    pub fn new(kind: ParseErrorKind, message: impl Into<String>) -> Self {
61        Self {
62            kind,
63            message: message.into(),
64        }
65    }
66}
67
68impl std::fmt::Display for ParseError {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(f, "{}: {}", self.kind.as_str(), self.message)
71    }
72}
73
74impl std::error::Error for ParseError {}
75
76/// Default port per documented scheme. Centralised so other crates
77/// (the connector, server-side dispatch) can stay consistent.
78pub const DEFAULT_PORT_RED: u16 = 5050;
79pub const DEFAULT_PORT_GRPC: u16 = 5055;
80
81/// DoS guardrails applied by [`parse`] before any URI work happens.
82///
83/// The connection-string parser is the only entry point an attacker
84/// can reach BEFORE auth, so every limit here is enforced eagerly
85/// and surfaces as a structured [`ParseErrorKind::LimitExceeded`]
86/// error rather than a panic, hang, or unbounded allocation.
87#[derive(Debug, Clone, Copy, PartialEq, Eq)]
88pub struct ConnStringLimits {
89    /// Maximum length of the input URI in bytes. Default `8 KiB`.
90    pub max_uri_bytes: usize,
91    /// Maximum number of `key=value` query parameters. Default `32`.
92    pub max_query_params: usize,
93    /// Maximum number of comma-separated cluster hosts allowed in a
94    /// `red://`/`reds://`/`grpc://` cluster URI. Default `64`.
95    pub max_cluster_hosts: usize,
96}
97
98impl Default for ConnStringLimits {
99    fn default() -> Self {
100        Self {
101            max_uri_bytes: 8 * 1024,
102            max_query_params: 32,
103            max_cluster_hosts: 64,
104        }
105    }
106}
107
108/// Normalised target produced by [`parse`].
109///
110/// Variants intentionally mirror the public Rust client target shape
111/// so callers can keep a thin compatibility layer without duplicating
112/// parser behavior.
113#[derive(Debug, Clone, PartialEq, Eq)]
114pub enum ConnectionTarget {
115    /// `memory://` — ephemeral, in-memory backend.
116    Memory,
117    /// `file:///abs/path` — embedded engine on disk.
118    File { path: PathBuf },
119    /// Single remote endpoint over `grpc://` or `grpcs://`. Stored
120    /// as a normalised `http://host:port` string because tonic's
121    /// `Endpoint` consumes that form.
122    Grpc { endpoint: String },
123    /// Multi-host gRPC URI: primary + read replicas. Writes hit the
124    /// primary; reads round-robin across replicas unless
125    /// `force_primary` is set.
126    GrpcCluster {
127        primary: String,
128        replicas: Vec<String>,
129        force_primary: bool,
130    },
131    /// `http://host:port` / `https://host:port` — REST endpoint.
132    Http { base_url: String },
133    /// `red://host:port` (plain TCP) or `reds://host:port` (TLS).
134    /// RedWire binary frame protocol per ADR 0001. The connector
135    /// speaks framed binary directly; it does NOT route through
136    /// tonic.
137    RedWire { host: String, port: u16, tls: bool },
138}
139
140/// Parse a connection URI into a [`ConnectionTarget`] under the
141/// default DoS limits.
142///
143/// Pure function, no side effects. Behaviour matches
144/// `drivers/rust/src/connect.rs::parse` 1:1 with two additions:
145///   - Mixed-case schemes (e.g. `Red://`, `REDS://`) are normalised
146///     to lowercase before dispatch.
147///   - Inputs exceeding [`ConnStringLimits`] return a structured
148///     [`ParseErrorKind::LimitExceeded`] error instead of being
149///     processed.
150pub fn parse(uri: &str) -> Result<ConnectionTarget, ParseError> {
151    parse_with_limits(uri, ConnStringLimits::default())
152}
153
154/// Return true for documented embedded aliases that must not resolve to
155/// a remote transport target.
156///
157/// This is intentionally separate from [`parse`]: legacy clients may need
158/// to reject embedded targets before mapping `red://host` onto a remote
159/// compatibility transport.
160pub fn is_embedded_connection_uri(uri: &str) -> bool {
161    let trimmed = uri.trim();
162    matches!(
163        trimmed,
164        "red://" | "red:" | "red:///" | "red://:memory" | "red://:memory:"
165    ) || trimmed.starts_with("red:///")
166}
167
168/// Same as [`parse`] but with caller-supplied DoS guardrails.
169/// Useful for tests that need tighter limits or for callers (a
170/// future admin tool, an offline validator) that need to relax the
171/// defaults.
172pub fn parse_with_limits(
173    uri: &str,
174    limits: ConnStringLimits,
175) -> Result<ConnectionTarget, ParseError> {
176    if uri.is_empty() {
177        return Err(ParseError::new(
178            ParseErrorKind::Empty,
179            "empty connection string",
180        ));
181    }
182
183    if uri.len() > limits.max_uri_bytes {
184        return Err(ParseError::new(
185            ParseErrorKind::LimitExceeded,
186            format!(
187                "max_uri_bytes exceeded: limit={} actual={}",
188                limits.max_uri_bytes,
189                uri.len(),
190            ),
191        ));
192    }
193
194    // Lowercase the scheme so `Red://Host`, `REDS://Host`, etc.
195    // dispatch identically to the canonical lowercase forms. The
196    // host and path retain original casing — host is downcased by
197    // `url::Url` for IDN per spec, path stays verbatim.
198    let normalised = normalise_scheme(uri);
199    let uri = normalised.as_str();
200
201    if uri == "memory://" || uri == "memory:" {
202        return Ok(ConnectionTarget::Memory);
203    }
204
205    if let Some(rest) = uri.strip_prefix("file://") {
206        if rest.is_empty() {
207            return Err(ParseError::new(
208                ParseErrorKind::InvalidUri,
209                "file:// URI is missing a path",
210            ));
211        }
212        return Ok(ConnectionTarget::File {
213            path: PathBuf::from(rest),
214        });
215    }
216
217    if let Some(cluster) = try_parse_grpc_cluster(uri, &limits)? {
218        return Ok(cluster);
219    }
220
221    let parsed = Url::parse(uri)
222        .map_err(|e| ParseError::new(ParseErrorKind::InvalidUri, format!("{e}: {uri}")))?;
223
224    enforce_query_param_limit(&parsed, &limits)?;
225
226    match parsed.scheme() {
227        "red" | "reds" => {
228            let host = parsed.host_str().ok_or_else(|| {
229                ParseError::new(ParseErrorKind::InvalidUri, "red:// URI is missing a host")
230            })?;
231            let port = parsed.port().unwrap_or(DEFAULT_PORT_RED);
232            Ok(ConnectionTarget::RedWire {
233                host: host.to_string(),
234                port,
235                tls: parsed.scheme() == "reds",
236            })
237        }
238        "grpc" | "grpcs" => {
239            let host = parsed.host_str().ok_or_else(|| {
240                ParseError::new(ParseErrorKind::InvalidUri, "grpc:// URI is missing a host")
241            })?;
242            let port = parsed.port().unwrap_or(DEFAULT_PORT_GRPC);
243            Ok(ConnectionTarget::Grpc {
244                endpoint: format!("http://{host}:{port}"),
245            })
246        }
247        "http" | "https" => {
248            let host = parsed.host_str().ok_or_else(|| {
249                ParseError::new(
250                    ParseErrorKind::InvalidUri,
251                    "http(s):// URI is missing a host",
252                )
253            })?;
254            let scheme = parsed.scheme();
255            let port = parsed
256                .port()
257                .unwrap_or(if scheme == "https" { 443 } else { 80 });
258            Ok(ConnectionTarget::Http {
259                base_url: format!("{scheme}://{host}:{port}"),
260            })
261        }
262        other => Err(ParseError::new(
263            ParseErrorKind::UnsupportedScheme,
264            format!("unsupported scheme: {other}"),
265        )),
266    }
267}
268
269/// Lowercase only the scheme portion (everything before the first
270/// `:`), leaving host/path/query untouched. Returns the original
271/// string when no scheme separator is present so the downstream
272/// `Url::parse` path produces the canonical "missing scheme" error
273/// instead of being masked here.
274fn normalise_scheme(uri: &str) -> String {
275    match uri.find(':') {
276        Some(i) => {
277            let scheme = &uri[..i];
278            // Only ASCII alphanumerics + `+ . -` are valid scheme
279            // bytes per RFC 3986. If the prefix violates that we
280            // leave it alone so `Url::parse` can produce the
281            // structured error.
282            if scheme.is_empty()
283                || !scheme
284                    .bytes()
285                    .all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'.' || b == b'-')
286            {
287                return uri.to_string();
288            }
289            let mut out = String::with_capacity(uri.len());
290            out.push_str(&scheme.to_ascii_lowercase());
291            out.push_str(&uri[i..]);
292            out
293        }
294        None => uri.to_string(),
295    }
296}
297
298fn enforce_query_param_limit(url: &Url, limits: &ConnStringLimits) -> Result<(), ParseError> {
299    let Some(q) = url.query() else {
300        return Ok(());
301    };
302    if q.is_empty() {
303        return Ok(());
304    }
305    let count = q.split('&').count();
306    if count > limits.max_query_params {
307        return Err(ParseError::new(
308            ParseErrorKind::LimitExceeded,
309            format!(
310                "max_query_params exceeded: limit={} actual={}",
311                limits.max_query_params, count,
312            ),
313        ));
314    }
315    Ok(())
316}
317
318/// Try to parse a multi-host gRPC URI. `Ok(None)` means "this is a
319/// single-host URI — fall through to the standard parser".
320fn try_parse_grpc_cluster(
321    uri: &str,
322    limits: &ConnStringLimits,
323) -> Result<Option<ConnectionTarget>, ParseError> {
324    let (rest, default_port) = if let Some(r) = uri.strip_prefix("grpc://") {
325        (r, DEFAULT_PORT_GRPC)
326    } else if let Some(r) = uri.strip_prefix("grpcs://") {
327        (r, DEFAULT_PORT_GRPC)
328    } else if let Some(r) = uri
329        .strip_prefix("red://")
330        .or_else(|| uri.strip_prefix("reds://"))
331    {
332        (r, DEFAULT_PORT_RED)
333    } else {
334        return Ok(None);
335    };
336
337    let (host_part, query_part) = match rest.find('?') {
338        Some(i) => (&rest[..i], Some(&rest[i + 1..])),
339        None => (rest, None),
340    };
341
342    if !host_part.contains(',') {
343        return Ok(None);
344    }
345
346    let raw_count = host_part.split(',').count();
347    if raw_count > limits.max_cluster_hosts {
348        return Err(ParseError::new(
349            ParseErrorKind::LimitExceeded,
350            format!(
351                "max_cluster_hosts exceeded: limit={} actual={}",
352                limits.max_cluster_hosts, raw_count,
353            ),
354        ));
355    }
356
357    let mut endpoints: Vec<String> = Vec::with_capacity(raw_count);
358    for raw in host_part.split(',') {
359        let raw = raw.trim();
360        if raw.is_empty() {
361            return Err(ParseError::new(
362                ParseErrorKind::InvalidUri,
363                "grpc cluster URI has an empty host entry",
364            ));
365        }
366        // Bracketed IPv6 literal: `[::1]:5050` or `[::1]`.
367        let (host, port) = if let Some(after_bracket) = raw.strip_prefix('[') {
368            let end = after_bracket.find(']').ok_or_else(|| {
369                ParseError::new(
370                    ParseErrorKind::InvalidUri,
371                    format!("unterminated IPv6 bracket in cluster URI: {raw}"),
372                )
373            })?;
374            let host = &after_bracket[..end];
375            let tail = &after_bracket[end + 1..];
376            let port = if tail.is_empty() {
377                default_port
378            } else if let Some(p) = tail.strip_prefix(':') {
379                p.parse::<u16>().map_err(|_| {
380                    ParseError::new(
381                        ParseErrorKind::InvalidUri,
382                        format!("invalid port in cluster URI: {raw}"),
383                    )
384                })?
385            } else {
386                return Err(ParseError::new(
387                    ParseErrorKind::InvalidUri,
388                    format!("trailing junk after IPv6 bracket in cluster URI: {raw}"),
389                ));
390            };
391            (format!("[{host}]"), port)
392        } else {
393            match raw.rsplit_once(':') {
394                Some((h, p)) => {
395                    let port: u16 = p.parse().map_err(|_| {
396                        ParseError::new(
397                            ParseErrorKind::InvalidUri,
398                            format!("invalid port in cluster URI: {raw}"),
399                        )
400                    })?;
401                    (h.to_string(), port)
402                }
403                None => (raw.to_string(), default_port),
404            }
405        };
406        if host.is_empty() || host == "[]" {
407            return Err(ParseError::new(
408                ParseErrorKind::InvalidUri,
409                "grpc cluster URI has an empty host entry",
410            ));
411        }
412        endpoints.push(format!("http://{host}:{port}"));
413    }
414
415    if let Some(q) = query_part {
416        let qcount = if q.is_empty() {
417            0
418        } else {
419            q.split('&').count()
420        };
421        if qcount > limits.max_query_params {
422            return Err(ParseError::new(
423                ParseErrorKind::LimitExceeded,
424                format!(
425                    "max_query_params exceeded: limit={} actual={}",
426                    limits.max_query_params, qcount,
427                ),
428            ));
429        }
430    }
431
432    let force_primary = query_part
433        .map(|q| {
434            q.split('&').any(|kv| {
435                let mut parts = kv.splitn(2, '=');
436                let k = parts.next().unwrap_or("");
437                let v = parts.next().unwrap_or("");
438                k.eq_ignore_ascii_case("route") && v.eq_ignore_ascii_case("primary")
439            })
440        })
441        .unwrap_or(false);
442
443    let mut iter = endpoints.into_iter();
444    let primary = iter.next().expect("split on ',' yields at least one entry");
445    let replicas: Vec<String> = iter.collect();
446
447    Ok(Some(ConnectionTarget::GrpcCluster {
448        primary,
449        replicas,
450        force_primary,
451    }))
452}