Skip to content

Commit 8af2d91

Browse files
wpfleger96Duncan
andauthored
refactor(relay): bind HTTP tenants through one shadow-aware helper (#8062)
Adds `bind_tenant()`, a single helper that resolves the community from the request `Host` and records the NIP-FI shadow verdict when binding fails. Ten protected HTTP handlers previously duplicated the Host lookup and called `observe_unbound` in their own failure branches, so each one had to remember the observer call. Moving those ten handlers onto the helper makes recording the verdict part of binding itself, and `observe_unbound` becomes private to the shadow module. Each caller keeps its existing rejection response, so Off/Enforce behavior is unchanged. `bind_community()` stays directly callable. Routes exempt from NIP-FI (invite claim, webhooks, NIP-05 and NIP-11 metadata) keep using it, and the WebSocket and audio upgrades record their own verdicts before they bind. A new protected HTTP handler should bind through `bind_tenant()`. 🤖 Authored by Duncan (agent) on behalf of Will. Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: Duncan <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
1 parent df3f28a commit 8af2d91

8 files changed

Lines changed: 48 additions & 88 deletions

File tree

‎crates/buzz-relay/src/api/bridge.rs‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -905,14 +905,9 @@ pub async fn submit_event(
905905
// before any tenant-scoped write, identical to the WS door in `router.rs`.
906906
// Unmapped host or lookup failure fails closed with a generic 404 — never a
907907
// default tenant, never echoing the host.
908-
let raw_host = headers
909-
.get(axum::http::header::HOST)
910-
.and_then(|v| v.to_str().ok())
911-
.unwrap_or("");
912-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
908+
let tenant = crate::nip_fi_shadow::bind_tenant(&state, &headers)
913909
.await
914-
.map_err(|_| {
915-
crate::nip_fi_shadow::observe_unbound(&state, &headers);
910+
.ok_or_else(|| {
916911
api_error(
917912
StatusCode::NOT_FOUND,
918913
"relay: no community is configured for this host",
@@ -1236,14 +1231,9 @@ pub async fn query_events(
12361231
// An unmapped host or lookup failure fails closed with a generic 404 — never
12371232
// a default tenant, never echoing the host (so an unauthenticated caller
12381233
// cannot probe which communities exist on this deployment).
1239-
let raw_host = headers
1240-
.get(axum::http::header::HOST)
1241-
.and_then(|v| v.to_str().ok())
1242-
.unwrap_or("");
1243-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
1234+
let tenant = crate::nip_fi_shadow::bind_tenant(&state, &headers)
12441235
.await
1245-
.map_err(|_| {
1246-
crate::nip_fi_shadow::observe_unbound(&state, &headers);
1236+
.ok_or_else(|| {
12471237
api_error(
12481238
StatusCode::NOT_FOUND,
12491239
"relay: no community is configured for this host",
@@ -1871,14 +1861,9 @@ pub async fn count_events(
18711861
// before any tenant-scoped read, identical to the WS door in `router.rs`
18721862
// and `query_events`/`submit_event` above. Fail-closed; never a default
18731863
// tenant, never echoing the host.
1874-
let raw_host = headers
1875-
.get(axum::http::header::HOST)
1876-
.and_then(|v| v.to_str().ok())
1877-
.unwrap_or("");
1878-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
1864+
let tenant = crate::nip_fi_shadow::bind_tenant(&state, &headers)
18791865
.await
1880-
.map_err(|_| {
1881-
crate::nip_fi_shadow::observe_unbound(&state, &headers);
1866+
.ok_or_else(|| {
18821867
api_error(
18831868
StatusCode::NOT_FOUND,
18841869
"relay: no community is configured for this host",
@@ -2690,14 +2675,9 @@ async fn authorize_moderation_read(
26902675
path: &str,
26912676
raw_query: Option<&str>,
26922677
) -> Result<TenantContext, Response> {
2693-
let raw_host = headers
2694-
.get(axum::http::header::HOST)
2695-
.and_then(|v| v.to_str().ok())
2696-
.unwrap_or("");
2697-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
2678+
let tenant = crate::nip_fi_shadow::bind_tenant(state, headers)
26982679
.await
2699-
.map_err(|_| {
2700-
crate::nip_fi_shadow::observe_unbound(state, headers);
2680+
.ok_or_else(|| {
27012681
api_error(
27022682
StatusCode::NOT_FOUND,
27032683
"relay: no community is configured for this host",

‎crates/buzz-relay/src/api/gifs.rs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::time::Duration;
1414

1515
use axum::{
1616
extract::State,
17-
http::{header, HeaderMap, StatusCode},
17+
http::{HeaderMap, StatusCode},
1818
response::{IntoResponse, Json, Response},
1919
};
2020
use futures_util::StreamExt;
@@ -125,14 +125,9 @@ async fn authenticate(
125125
path: &str,
126126
body: &[u8],
127127
) -> Result<(buzz_core::TenantContext, nostr::PublicKey), Response> {
128-
let raw_host = headers
129-
.get(header::HOST)
130-
.and_then(|value| value.to_str().ok())
131-
.unwrap_or("");
132-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
128+
let tenant = crate::nip_fi_shadow::bind_tenant(state, headers)
133129
.await
134-
.map_err(|_| {
135-
crate::nip_fi_shadow::observe_unbound(state, headers);
130+
.ok_or_else(|| {
136131
api_error(
137132
StatusCode::NOT_FOUND,
138133
"relay: no community is configured for this host",
@@ -401,7 +396,7 @@ mod tests {
401396
use super::*;
402397
use axum::{
403398
body::Body,
404-
http::{Request, StatusCode},
399+
http::{header, Request, StatusCode},
405400
routing::get,
406401
Router,
407402
};

‎crates/buzz-relay/src/api/git/settings.rs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::Arc;
1010
use axum::{
1111
body::Bytes,
1212
extract::{DefaultBodyLimit, Path, State},
13-
http::{header, HeaderMap, StatusCode},
13+
http::{HeaderMap, StatusCode},
1414
response::{IntoResponse, Response},
1515
routing::get,
1616
Json, Router,
@@ -167,16 +167,9 @@ async fn authenticate(
167167
path: &str,
168168
body: Option<&[u8]>,
169169
) -> Result<SettingsAuth, Response> {
170-
let host = headers
171-
.get(header::HOST)
172-
.and_then(|h| h.to_str().ok())
173-
.unwrap_or("");
174-
let tenant = crate::tenant::bind_community(&state.db, host)
170+
let tenant = crate::nip_fi_shadow::bind_tenant(state, headers)
175171
.await
176-
.map_err(|_| {
177-
crate::nip_fi_shadow::observe_unbound(state, headers);
178-
error(StatusCode::NOT_FOUND, "repository not found")
179-
})?;
172+
.ok_or_else(|| error(StatusCode::NOT_FOUND, "repository not found"))?;
180173
let url = bridge::nip98_expected_url(&state.config.relay_url, &tenant, path);
181174
let method = if body.is_some() { "POST" } else { "GET" };
182175
let require_payload = body.is_some();

‎crates/buzz-relay/src/api/git/transport.rs‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,9 @@ impl axum::extract::FromRequestParts<Arc<AppState>> for GitAuth {
116116
// headers; the signed `u` tag is checked against the host that resolved
117117
// through the authoritative communities table, not a deployment-global
118118
// `config.relay_url` and not any client-supplied community value.
119-
let raw_host = parts
120-
.headers
121-
.get(header::HOST)
122-
.and_then(|v| v.to_str().ok())
123-
.unwrap_or("");
124-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
119+
let tenant = crate::nip_fi_shadow::bind_tenant(state, &parts.headers)
125120
.await
126-
.map_err(|_| {
127-
crate::nip_fi_shadow::observe_unbound(state, &parts.headers);
128-
(StatusCode::NOT_FOUND, "repository not found").into_response()
129-
})?;
121+
.ok_or_else(|| (StatusCode::NOT_FOUND, "repository not found").into_response())?;
130122
let expected_url = git_expected_url(
131123
&state.config.relay_url,
132124
&tenant,

‎crates/buzz-relay/src/api/invites.rs‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,20 +293,12 @@ async fn mint_invite_checked(
293293
) -> axum::response::Response {
294294
use axum::response::IntoResponse as _;
295295

296-
let raw_host = headers
297-
.get(axum::http::header::HOST)
298-
.and_then(|v| v.to_str().ok())
299-
.unwrap_or("");
300-
let tenant = match crate::tenant::bind_community(&state.db, raw_host).await {
301-
Ok(t) => t,
302-
Err(_) => {
303-
crate::nip_fi_shadow::observe_unbound(&state, &headers);
304-
return api_error(
305-
StatusCode::NOT_FOUND,
306-
"relay: no community is configured for this host",
307-
)
308-
.into_response();
309-
}
296+
let Some(tenant) = crate::nip_fi_shadow::bind_tenant(&state, &headers).await else {
297+
return api_error(
298+
StatusCode::NOT_FOUND,
299+
"relay: no community is configured for this host",
300+
)
301+
.into_response();
310302
};
311303

312304
let url = bridge::nip98_expected_url(&state.config.relay_url, &tenant, "/api/invites");

‎crates/buzz-relay/src/api/media.rs‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -649,16 +649,9 @@ async fn bind_media_read_tenant(
649649
state: &AppState,
650650
headers: &HeaderMap,
651651
) -> Result<TenantContext, MediaError> {
652-
let raw_host = headers
653-
.get(header::HOST)
654-
.and_then(|v| v.to_str().ok())
655-
.unwrap_or("");
656-
crate::tenant::bind_community(&state.db, raw_host)
652+
crate::nip_fi_shadow::bind_tenant(state, headers)
657653
.await
658-
.map_err(|_| {
659-
crate::nip_fi_shadow::observe_unbound(state, headers);
660-
MediaError::NotFound
661-
})
654+
.ok_or(MediaError::NotFound)
662655
}
663656

664657
/// Extract and signature-verify the Blossom auth event for a GET/HEAD read.

‎crates/buzz-relay/src/api/workflows.rs‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,9 @@ async fn authorize_workflow_read(
4949
raw_query: Option<&str>,
5050
workflow_id: Uuid,
5151
) -> Result<TenantContext, Response> {
52-
let raw_host = headers
53-
.get(axum::http::header::HOST)
54-
.and_then(|value| value.to_str().ok())
55-
.unwrap_or("");
56-
let tenant = crate::tenant::bind_community(&state.db, raw_host)
52+
let tenant = crate::nip_fi_shadow::bind_tenant(state, headers)
5753
.await
58-
.map_err(|_| {
59-
crate::nip_fi_shadow::observe_unbound(state, headers);
54+
.ok_or_else(|| {
6055
api_error(
6156
StatusCode::NOT_FOUND,
6257
"relay: no community is configured for this host",

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,31 @@ pub(crate) fn observe_strict_proof<E>(
155155
}
156156
}
157157

158+
/// Bind the request's tenant from its Host header, as every HTTP handler's
159+
/// row zero does. `None` means the Host bound no community; the caller keeps
160+
/// its own legacy rejection, and shadow records enforce's verdict first.
161+
pub(crate) async fn bind_tenant(
162+
state: &crate::state::AppState,
163+
headers: &HeaderMap,
164+
) -> Option<buzz_core::TenantContext> {
165+
let raw_host = headers
166+
.get(axum::http::header::HOST)
167+
.and_then(|v| v.to_str().ok())
168+
.unwrap_or("");
169+
let tenant = crate::tenant::bind_community(&state.db, raw_host)
170+
.await
171+
.ok();
172+
if tenant.is_none() {
173+
observe_unbound(state, headers);
174+
}
175+
tenant
176+
}
177+
158178
/// In shadow mode, record the verdict enforce reaches for a request whose
159179
/// Host bound no tenant: `community` when the Host maps to no configured
160180
/// community; an admit when enforce's guard passed it, since the handler's
161181
/// own rejection is no NIP-FI denial; nothing outside a guarded route.
162-
pub(crate) fn observe_unbound(state: &crate::state::AppState, headers: &HeaderMap) {
182+
fn observe_unbound(state: &crate::state::AppState, headers: &HeaderMap) {
163183
let nip_fi = &state.config.nip_fi;
164184
if nip_fi.mode.observes_only() {
165185
let verdict = match crate::nip_fi_core::resolve_community(headers, &nip_fi.communities) {

0 commit comments

Comments
 (0)