Skip to content

Commit 11297dd

Browse files
TheSentinel454Elrondclaude
committed
fix(relay): reject hosts longer than any community's before resolving
The tenant-host gate keeps an entry for every missed host, mapped or not, and unauthenticated paths such as NIP-05 reach the resolver with a caller-chosen host of any length. The gate cache bounds entries, not bytes, so invalid traffic could pin up to 10,000 long strings per pod. communities.host is VARCHAR(255), so a longer host can never map. bind_community now rejects it as UnmappedHost next to the empty-host check, before any resolver, cache, gate or lookup sees it. The limit counts characters, as the column does, so no storable host is refused. MAX_HOST_LEN moves to buzz-core::tenant and provisioning uses the same constant. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 9911c99 commit 11297dd

5 files changed

Lines changed: 43 additions & 9 deletions

File tree

‎Justfile‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,11 @@ test-unit:
639639
# Invitation drain/cutoff boundary contracts are infra-free.
640640
cargo nextest run -p buzz-relay --lib -E 'test(/^invite_token::/) + test(/^config::invite_cutoff_tests::/)'
641641
# Relay read caches: the admitted-connection index, the shared-read
642-
# gates (open set, restriction, tenant host), tenant host cache and
643-
# the ban disconnect's restriction-cache drop. Infra-free; the
642+
# gates (open set, restriction, tenant host), tenant host cache, the
643+
# overlong-host rejection and the ban disconnect's restriction-cache
644+
# drop. Infra-free; the
644645
# database URL refuses at once. Keep scripts/run-tests.sh in step.
645-
cargo nextest run -p buzz-relay --lib -E 'test(=state::tests::admitted_index_follows_every_key_change) + test(/^shared_reads::tests::/) + test(=state::tests::concurrent_open_channel_misses_share_the_in_flight_read) + test(=state::tests::concurrent_restriction_and_host_misses_share_the_in_flight_read) + test(=tenant::tests::app_state_resolver_serves_fresh_cached_hosts_only) + test(=state::tests::disconnect_drops_the_cached_restriction_row)'
646+
cargo nextest run -p buzz-relay --lib -E 'test(=state::tests::admitted_index_follows_every_key_change) + test(/^shared_reads::tests::/) + test(=state::tests::concurrent_open_channel_misses_share_the_in_flight_read) + test(=state::tests::concurrent_restriction_and_host_misses_share_the_in_flight_read) + test(=tenant::tests::app_state_resolver_serves_fresh_cached_hosts_only) + test(=tenant::tests::an_overlong_host_fails_closed_before_the_resolver) + test(=state::tests::disconnect_drops_the_cached_restriction_row)'
646647
# Retain partially consumed mesh receives across housekeeping ticks.
647648
cargo nextest run -p buzz-relay --lib -E 'test(=mesh_boot::tests::demo_echo_retains_pending_receive_across_housekeeping_ticks)'
648649
# boot_lifecycle spawns the real relay binary and asserts its startup

‎crates/buzz-core/src/tenant.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ impl TenantContext {
9797
}
9898
}
9999

100+
/// Longest host a community can have, in characters: `communities.host` is
101+
/// `VARCHAR(255)`. A longer host maps to no community.
102+
pub const MAX_HOST_LEN: usize = 255;
103+
100104
/// Normalize a connection `Host` into the canonical form used as the community
101105
/// lookup key.
102106
///

‎crates/buzz-relay/src/handlers/community_provisioning.rs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ use std::sync::Arc;
3030
use serde::{Deserialize, Serialize};
3131
use tracing::info;
3232

33-
use buzz_core::tenant::{normalize_host, TenantContext};
33+
use buzz_core::tenant::{normalize_host, TenantContext, MAX_HOST_LEN};
3434
use url::{Host, Url};
3535

3636
use crate::state::AppState;
3737

38-
/// Maximum accepted authority length. Matches `communities.host VARCHAR(255)`.
39-
const MAX_HOST_LEN: usize = 255;
40-
4138
/// JSON body for `POST /operator/communities`.
4239
#[derive(Debug, Deserialize)]
4340
pub struct ProvisionCommunityRequest {

‎crates/buzz-relay/src/tenant.rs‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! (`Db::resolve_host`); the relay depends on the trait, not the query, so the
1515
//! binding is testable without a database.
1616
17-
use buzz_core::tenant::{normalize_host, CommunityId, TenantContext};
17+
use buzz_core::tenant::{normalize_host, CommunityId, TenantContext, MAX_HOST_LEN};
1818

1919
/// Resolves a normalized connection host to its community, or `None` when the
2020
/// host maps to no community on this deployment.
@@ -81,7 +81,10 @@ pub async fn bind_community<R: HostResolver>(
8181
// bind to a misconfigured empty-host community. Reuse `UnmappedHost` (not a
8282
// distinct variant) so the rejection is byte-identical to any other unmapped
8383
// 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 {
8588
return Err(BindError::UnmappedHost);
8689
}
8790
match resolver.resolve_host(&host).await {
@@ -380,6 +383,34 @@ mod tests {
380383
}
381384
}
382385

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+
383414
/// The request-path resolver serves a mapped host from the cache only
384415
/// while the entry is younger than `TENANT_HOST_CACHE_MAX_AGE`; past
385416
/// that it rereads, and a failed reread fails closed. The database is

‎scripts/run-tests.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ run_unit_tests() {
341341
state::tests::concurrent_open_channel_misses_share_the_in_flight_read \
342342
state::tests::concurrent_restriction_and_host_misses_share_the_in_flight_read \
343343
tenant::tests::app_state_resolver_serves_fresh_cached_hosts_only \
344+
tenant::tests::an_overlong_host_fails_closed_before_the_resolver \
344345
state::tests::disconnect_drops_the_cached_restriction_row
345346

346347
run_test_step "buzz-relay router tests" \

0 commit comments

Comments
 (0)