Skip to content

Commit d3ae493

Browse files
bradseilerHonor
andcommitted
fix(relay): separate pre-auth policy from admission
Co-authored-by: Honor <e0bf479eeebe47dde1e35705212a4df70616a7bc834d907ddfb18cf0405f8b1b@seiler-test.communities.public.builderlabstaging.xyz> Signed-off-by: Brad Seiler <[email protected]>
1 parent adcb864 commit d3ae493

7 files changed

Lines changed: 50 additions & 29 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ impl axum::extract::FromRequestParts<Arc<AppState>> for GitAuth {
9595
// missing credentials → 401 + WWW-Authenticate challenge;
9696
// malformed credentials (bad base64, bad UTF-8) → 401 without challenge;
9797
// regardless of whether the Host resolves to a known community. No
98-
// database work for syntactically bad requests in Off mode.
98+
// tenant-binding work for syntactically bad requests. The policy read
99+
// still comes first so a required row cannot take the legacy path.
99100
//
100101
// Enforce: the header syntax is validated
101102
// inside the NIP-FI admission closure below, where proof failures are
@@ -4327,7 +4328,7 @@ mod off_mode_precedence_tests {
43274328
// and bad-UTF8 do NOT (those use `into_response()` without the header).
43284329
#[tokio::test]
43294330
#[ignore = "requires Postgres"]
4330-
async fn off_mode_unmapped_host_missing_auth_returns_401_before_db() {
4331+
async fn off_mode_unmapped_host_missing_auth_returns_401_before_tenant_binding() {
43314332
let Some(state) = off_mode_state().await else {
43324333
panic!("local Postgres not reachable");
43334334
};
@@ -4370,7 +4371,7 @@ mod off_mode_precedence_tests {
43704371
// Falsifying mutation: delete transport.rs:101-104 → 404.
43714372
#[tokio::test]
43724373
#[ignore = "requires Postgres"]
4373-
async fn off_mode_unmapped_host_wrong_scheme_returns_401_before_db() {
4374+
async fn off_mode_unmapped_host_wrong_scheme_returns_401_before_tenant_binding() {
43744375
let Some(state) = off_mode_state().await else {
43754376
panic!("local Postgres not reachable");
43764377
};
@@ -4410,7 +4411,7 @@ mod off_mode_precedence_tests {
44104411
// Falsifying mutation: delete transport.rs:101-104 → 404.
44114412
#[tokio::test]
44124413
#[ignore = "requires Postgres"]
4413-
async fn off_mode_unmapped_host_invalid_base64_returns_401_before_db() {
4414+
async fn off_mode_unmapped_host_invalid_base64_returns_401_before_tenant_binding() {
44144415
let Some(state) = off_mode_state().await else {
44154416
panic!("local Postgres not reachable");
44164417
};
@@ -4444,7 +4445,7 @@ mod off_mode_precedence_tests {
44444445
// Falsifying mutation: delete transport.rs:101-104 → 404.
44454446
#[tokio::test]
44464447
#[ignore = "requires Postgres"]
4447-
async fn off_mode_unmapped_host_bad_utf8_returns_401_before_db() {
4448+
async fn off_mode_unmapped_host_bad_utf8_returns_401_before_tenant_binding() {
44484449
let Some(state) = off_mode_state().await else {
44494450
panic!("local Postgres not reachable");
44504451
};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,7 @@ mod route_integration_tests {
25142514
}
25152515

25162516
#[tokio::test]
2517+
#[allow(clippy::result_large_err)] // Real Axum admission closure.
25172518
async fn required_off_and_shadow_validate_real_assertion_crypto() {
25182519
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
25192520
for mode in [buzz_auth::NipFiMode::Off, buzz_auth::NipFiMode::Shadow] {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ pub(crate) async fn nip11_response(
398398
response
399399
}
400400

401+
#[allow(clippy::result_large_err)] // Axum responses carry the discovery denial contract.
401402
pub(crate) async fn nip11_document(
402403
state: &crate::state::AppState,
403404
raw_host: &str,

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,12 +608,7 @@ pub(crate) async fn admit_nip_fi_http_on_state<X, F>(
608608
where
609609
F: FnOnce() -> Result<Nip98Proof<X>, Response<Body>>,
610610
{
611-
if crate::nip_fi_policy::unmapped_request() {
612-
return Err(http_denial(
613-
buzz_auth::DenialClass::AuthorizationUnavailable,
614-
));
615-
}
616-
let mode = crate::nip_fi_policy::mode(state, headers).await;
611+
let mode = crate::nip_fi_policy::admission_mode(state, headers).await?;
617612
let verifier = state.nip_fi_verifier.as_deref();
618613
let communities = state.nip_fi_communities();
619614
match state.nip_fi_deny_map.as_deref() {

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub(crate) fn effective(global: NipFiMode, required: bool) -> NipFiMode {
5151
global.with_requirement(required)
5252
}
5353

54+
#[allow(clippy::result_large_err)] // Axum responses are the ingress error contract.
5455
pub(crate) async fn resolve(
5556
state: &crate::state::AppState,
5657
headers: &HeaderMap,
@@ -96,32 +97,52 @@ pub(crate) async fn resolve(
9697
})
9798
}
9899

99-
/// An unmapped request may retain legacy pre-auth error ordering, but must
100-
/// never obtain a shared admission proof if a handler omits its host check.
101-
pub(crate) fn unmapped_request() -> bool {
102-
REQUEST_POLICY.try_with(|p| !p.mapped).unwrap_or(false)
103-
}
104-
105-
/// Admission callers share the router's decision. Direct callers fail closed
106-
/// when policy is unavailable, even if the global rollout mode is Off/Shadow.
100+
/// Preserve route-specific rejection ordering for an authoritative host miss.
101+
/// This is not admission: the full gate below still requires a mapped policy.
107102
pub(crate) async fn mode(state: &crate::state::AppState, headers: &HeaderMap) -> NipFiMode {
108103
if state.config.nip_fi.mode.denies_unconditionally() {
109104
return NipFiMode::unavailable();
110105
}
111106
match resolve(state, headers).await {
112-
Ok(p)
113-
if p.stored_required
114-
&& (state.nip_fi_verifier.is_none()
115-
|| state.nip_fi_deny_map.is_none()
116-
|| state.config.nip_fi.max_connection_lifetime().is_none()) =>
117-
{
118-
NipFiMode::unavailable()
119-
}
120-
Ok(p) => p.mode,
107+
Ok(policy) => configured_mode(state, policy),
108+
Err(response) if response.status() == StatusCode::NOT_FOUND => state.config.nip_fi.mode,
121109
Err(_) => NipFiMode::unavailable(),
122110
}
123111
}
124112

113+
/// A shared admission proof is impossible without a serving host policy,
114+
/// even when a route preserves legacy unauthenticated error precedence.
115+
#[allow(clippy::result_large_err)] // Axum responses are the ingress error contract.
116+
pub(crate) async fn admission_mode(
117+
state: &crate::state::AppState,
118+
headers: &HeaderMap,
119+
) -> Result<NipFiMode, Response<Body>> {
120+
if state.config.nip_fi.mode.denies_unconditionally() {
121+
return Ok(NipFiMode::unavailable());
122+
}
123+
let policy = resolve(state, headers)
124+
.await
125+
.map_err(|_| crate::nip_fi_core::http_denial(DenialClass::AuthorizationUnavailable))?;
126+
if !policy.mapped {
127+
return Err(crate::nip_fi_core::http_denial(
128+
DenialClass::AuthorizationUnavailable,
129+
));
130+
}
131+
Ok(configured_mode(state, policy))
132+
}
133+
134+
fn configured_mode(state: &crate::state::AppState, policy: Policy) -> NipFiMode {
135+
if policy.stored_required
136+
&& (state.nip_fi_verifier.is_none()
137+
|| state.nip_fi_deny_map.is_none()
138+
|| state.config.nip_fi.max_connection_lifetime().is_none())
139+
{
140+
NipFiMode::unavailable()
141+
} else {
142+
policy.mode
143+
}
144+
}
145+
125146
fn not_found() -> Response<Body> {
126147
(
127148
StatusCode::NOT_FOUND,

‎crates/buzz-relay/src/nip_fi_policy/postgres_tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async fn signed_unsigned_free_controls_and_required_discovery_use_real_host_rows
5757
state.nip_fi_deny_map = Some(Arc::new(buzz_auth::NipFiDenyMap::new(100, vec![])));
5858
let state = Arc::new(state);
5959
let app = crate::router::build_router(state.clone());
60-
let body = br#"{"kinds":[1]}"#;
60+
let body = br#"[{"kinds":[1]}]"#;
6161
for host in [&free, &required] {
6262
for signed in [false, true] {
6363
let mut request = Request::post("/count")

‎crates/buzz-relay/src/nip_fi_policy/tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::result_large_err)] // Test closures return the real Axum admission response.
2+
13
use super::*;
24
use crate::nip_fi_core::tests::ScriptedVerifier;
35
use crate::nip_fi_http::{admit_nip_fi_http_on_state, Nip98Proof};

0 commit comments

Comments
 (0)