|
14 | 14 | //! (`Db::resolve_host`); the relay depends on the trait, not the query, so the |
15 | 15 | //! binding is testable without a database. |
16 | 16 |
|
17 | | -use buzz_core::tenant::{normalize_host, CommunityId, TenantContext}; |
| 17 | +use buzz_core::tenant::{normalize_host, CommunityId, TenantContext, MAX_HOST_LEN}; |
18 | 18 |
|
19 | 19 | /// Resolves a normalized connection host to its community, or `None` when the |
20 | 20 | /// host maps to no community on this deployment. |
@@ -81,7 +81,10 @@ pub async fn bind_community<R: HostResolver>( |
81 | 81 | // bind to a misconfigured empty-host community. Reuse `UnmappedHost` (not a |
82 | 82 | // distinct variant) so the rejection is byte-identical to any other unmapped |
83 | 83 | // host — an unauthenticated caller cannot probe for an empty-host row. |
84 | | - if host.is_empty() { |
| 84 | + // A host longer than any `communities.host` can hold maps to nothing |
| 85 | + // either; rejecting it here keeps an unauthenticated caller's arbitrarily |
| 86 | + // long host out of the resolver's caches and off the database. |
| 87 | + if host.is_empty() || host.chars().count() > MAX_HOST_LEN { |
85 | 88 | return Err(BindError::UnmappedHost); |
86 | 89 | } |
87 | 90 | match resolver.resolve_host(&host).await { |
@@ -380,6 +383,34 @@ mod tests { |
380 | 383 | } |
381 | 384 | } |
382 | 385 |
|
| 386 | + /// A host longer than `communities.host` can hold fails closed as |
| 387 | + /// unmapped before the request-path resolver sees it: no cache or gate |
| 388 | + /// entry and no lookup. The database is unreachable, so a lookup would |
| 389 | + /// fail as `Lookup`, as it does for a host at the limit. |
| 390 | + /// Mutation: drop the length check, or count bytes past the column's |
| 391 | + /// characters → RED. |
| 392 | + #[tokio::test] |
| 393 | + async fn an_overlong_host_fails_closed_before_the_resolver() { |
| 394 | + let state = crate::state::tests::test_state_with_unreachable_database_url( |
| 395 | + "postgres://[email protected]:1/buzz", |
| 396 | + ) |
| 397 | + .await; |
| 398 | + let overlong = "a".repeat(MAX_HOST_LEN + 1); |
| 399 | + assert!(matches!( |
| 400 | + bind_community(&*state, &overlong).await, |
| 401 | + Err(BindError::UnmappedHost) |
| 402 | + )); |
| 403 | + assert!(state.tenant_host_reads.gate(&overlong).is_none()); |
| 404 | + |
| 405 | + // At the limit, even in multibyte characters, the host is looked up. |
| 406 | + for longest in ["a".repeat(MAX_HOST_LEN), "é".repeat(MAX_HOST_LEN)] { |
| 407 | + assert!(matches!( |
| 408 | + bind_community(&*state, &longest).await, |
| 409 | + Err(BindError::Lookup(_)) |
| 410 | + )); |
| 411 | + } |
| 412 | + } |
| 413 | + |
383 | 414 | /// The request-path resolver serves a mapped host from the cache only |
384 | 415 | /// while the entry is younger than `TENANT_HOST_CACHE_MAX_AGE`; past |
385 | 416 | /// that it rereads, and a failed reread fails closed. The database is |
|
0 commit comments