Skip to content

Commit 51ad528

Browse files
committed
Enforce NIP-OA authorization time bounds
Signed-off-by: Jordan Mecom <[email protected]>
1 parent a373078 commit 51ad528

11 files changed

Lines changed: 325 additions & 48 deletions

File tree

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

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,28 @@ pub(crate) async fn enforce_http_admission(
5555
}
5656
}
5757

58+
/// Values retained from an already-verified bridge authentication event.
59+
#[derive(Debug)]
60+
pub(crate) struct VerifiedBridgeAuth {
61+
pub(crate) pubkey: nostr::PublicKey,
62+
pub(crate) event_id_bytes: [u8; 32],
63+
pub(crate) signed_created_at: Option<u64>,
64+
}
65+
66+
type BridgeAuthResult = Result<VerifiedBridgeAuth, (StatusCode, Json<Value>)>;
67+
5868
/// Verify bridge auth: NIP-98 (production) or X-Pubkey (dev mode).
5969
///
60-
/// Returns the authenticated public key and an event ID for replay detection.
61-
/// For X-Pubkey dev mode, the event ID is a zero hash (no replay concern).
70+
/// Returns the authenticated public key, an event ID for replay detection, and
71+
/// the verified signed auth timestamp. For X-Pubkey dev mode, the event ID is
72+
/// a zero hash and the timestamp is absent.
6273
pub(crate) fn verify_bridge_auth(
6374
headers: &HeaderMap,
6475
method: &str,
6576
url: &str,
6677
body: Option<&[u8]>,
6778
require_auth_token: bool,
68-
) -> Result<(nostr::PublicKey, [u8; 32]), (StatusCode, Json<Value>)> {
79+
) -> BridgeAuthResult {
6980
verify_bridge_auth_with_options(headers, method, url, body, require_auth_token, false)
7081
}
7182

@@ -76,7 +87,7 @@ pub(crate) fn verify_bridge_auth_with_options(
7687
body: Option<&[u8]>,
7788
require_auth_token: bool,
7889
require_payload: bool,
79-
) -> Result<(nostr::PublicKey, [u8; 32]), (StatusCode, Json<Value>)> {
90+
) -> BridgeAuthResult {
8091
// Try NIP-98 first (Authorization: Nostr <base64>)
8192
if let Some(auth_str) = headers
8293
.get("authorization")
@@ -111,7 +122,11 @@ pub(crate) fn verify_bridge_auth_with_options(
111122
let pubkey = buzz_auth::verify_nip98_event(&event_json, url, method, body)
112123
.map_err(|e| api_error(StatusCode::UNAUTHORIZED, &format!("NIP-98: {e}")))?;
113124

114-
return Ok((pubkey, event_id_bytes));
125+
return Ok(VerifiedBridgeAuth {
126+
pubkey,
127+
event_id_bytes,
128+
signed_created_at: Some(event.created_at.as_secs()),
129+
});
115130
}
116131

117132
// Dev-mode fallback: X-Pubkey header (only when require_auth_token is false)
@@ -120,7 +135,11 @@ pub(crate) fn verify_bridge_auth_with_options(
120135
let pubkey = nostr::PublicKey::from_hex(hex_val)
121136
.map_err(|_| api_error(StatusCode::UNAUTHORIZED, "invalid X-Pubkey hex"))?;
122137
// Zero event ID — no replay detection needed for dev mode
123-
return Ok((pubkey, [0u8; 32]));
138+
return Ok(VerifiedBridgeAuth {
139+
pubkey,
140+
event_id_bytes: [0u8; 32],
141+
signed_created_at: None,
142+
});
124143
}
125144
}
126145

@@ -723,7 +742,11 @@ pub async fn submit_event(
723742
})?;
724743

725744
let url = nip98_expected_url(&state.config.relay_url, &tenant, "/events");
726-
let (pubkey, event_id_bytes) = verify_bridge_auth(
745+
let VerifiedBridgeAuth {
746+
pubkey,
747+
event_id_bytes,
748+
signed_created_at,
749+
} = verify_bridge_auth(
727750
&headers,
728751
"POST",
729752
&url,
@@ -736,8 +759,16 @@ pub async fn submit_event(
736759
// runs inside the helper. The thin wrapper here owns the single terminal
737760
// attribution line so it fires for every outcome, including admission/
738761
// replay/membership failures that previously returned before any log fired.
739-
let outcome =
740-
submit_event_authed(&state, &tenant, &headers, &body, pubkey, event_id_bytes).await;
762+
let outcome = submit_event_authed(
763+
&state,
764+
&tenant,
765+
&headers,
766+
&body,
767+
pubkey,
768+
event_id_bytes,
769+
signed_created_at,
770+
)
771+
.await;
741772

742773
match &outcome {
743774
SubmitOutcome::Ok { accepted, kind, .. } => {
@@ -846,6 +877,7 @@ async fn submit_event_authed(
846877
body: &[u8],
847878
pubkey: nostr::PublicKey,
848879
event_id_bytes: [u8; 32],
880+
signed_auth_created_at: Option<u64>,
849881
) -> SubmitOutcome {
850882
// Admission and replay checks fire before body parse — a 429 or replay
851883
// reject on a malformed body must still be attributed.
@@ -894,12 +926,17 @@ async fn submit_event_authed(
894926
tenant.community(),
895927
&pubkey_bytes,
896928
auth_tag,
929+
signed_auth_created_at,
897930
)
898931
.await
899932
{
900933
Ok(owner) => owner.or_else(|| {
901934
if !state.config.require_relay_membership {
902-
super::relay_members::extract_nip_oa_owner(&pubkey_bytes, auth_tag)
935+
super::relay_members::extract_nip_oa_owner(
936+
&pubkey_bytes,
937+
auth_tag,
938+
signed_auth_created_at,
939+
)
903940
} else {
904941
None
905942
}
@@ -994,7 +1031,11 @@ pub async fn query_events(
9941031
})?;
9951032

9961033
let url = nip98_expected_url(&state.config.relay_url, &tenant, "/query");
997-
let (pubkey, event_id_bytes) = verify_bridge_auth(
1034+
let VerifiedBridgeAuth {
1035+
pubkey,
1036+
event_id_bytes,
1037+
signed_created_at,
1038+
} = verify_bridge_auth(
9981039
&headers,
9991040
"POST",
10001041
&url,
@@ -1007,8 +1048,16 @@ pub async fn query_events(
10071048
// helper. The single terminal attribution line fires here from the Result
10081049
// so every outcome — including admission/replay/membership failures that
10091050
// previously returned before any log — is attributed.
1010-
let result =
1011-
query_events_authed(&state, &tenant, &headers, &body, pubkey, event_id_bytes).await;
1051+
let result = query_events_authed(
1052+
&state,
1053+
&tenant,
1054+
&headers,
1055+
&body,
1056+
pubkey,
1057+
event_id_bytes,
1058+
signed_created_at,
1059+
)
1060+
.await;
10121061
match &result {
10131062
Ok(Json(Value::Array(events))) => {
10141063
tracing::info!(
@@ -1044,6 +1093,7 @@ async fn query_events_authed(
10441093
body: &[u8],
10451094
pubkey: nostr::PublicKey,
10461095
event_id_bytes: [u8; 32],
1096+
signed_auth_created_at: Option<u64>,
10471097
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
10481098
enforce_http_admission(state, tenant, &pubkey).await?;
10491099
check_nip98_replay(state, tenant, event_id_bytes).await?;
@@ -1055,6 +1105,7 @@ async fn query_events_authed(
10551105
tenant.community(),
10561106
&pubkey_bytes,
10571107
auth_tag,
1108+
signed_auth_created_at,
10581109
)
10591110
.await?;
10601111

@@ -1523,7 +1574,11 @@ pub async fn count_events(
15231574
})?;
15241575

15251576
let url = nip98_expected_url(&state.config.relay_url, &tenant, "/count");
1526-
let (pubkey, event_id_bytes) = verify_bridge_auth(
1577+
let VerifiedBridgeAuth {
1578+
pubkey,
1579+
event_id_bytes,
1580+
signed_created_at,
1581+
} = verify_bridge_auth(
15271582
&headers,
15281583
"POST",
15291584
&url,
@@ -1536,8 +1591,16 @@ pub async fn count_events(
15361591
// helper. The single terminal attribution line fires here from the Result
15371592
// so every outcome — including admission/replay/membership failures that
15381593
// previously returned before any log — is attributed.
1539-
let result =
1540-
count_events_authed(&state, &tenant, &headers, &body, pubkey, event_id_bytes).await;
1594+
let result = count_events_authed(
1595+
&state,
1596+
&tenant,
1597+
&headers,
1598+
&body,
1599+
pubkey,
1600+
event_id_bytes,
1601+
signed_created_at,
1602+
)
1603+
.await;
15411604
match &result {
15421605
Ok(Json(value)) => {
15431606
let count = value.get("count").and_then(Value::as_u64);
@@ -1571,6 +1634,7 @@ async fn count_events_authed(
15711634
body: &[u8],
15721635
pubkey: nostr::PublicKey,
15731636
event_id_bytes: [u8; 32],
1637+
signed_auth_created_at: Option<u64>,
15741638
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
15751639
enforce_http_admission(state, tenant, &pubkey).await?;
15761640
check_nip98_replay(state, tenant, event_id_bytes).await?;
@@ -1582,6 +1646,7 @@ async fn count_events_authed(
15821646
tenant.community(),
15831647
&pubkey_bytes,
15841648
auth_tag,
1649+
signed_auth_created_at,
15851650
)
15861651
.await?;
15871652

@@ -2309,8 +2374,11 @@ async fn authorize_moderation_read(
23092374
_ => path.to_string(),
23102375
};
23112376
let url = nip98_expected_url(&state.config.relay_url, &tenant, &path_with_query);
2312-
let (pubkey, event_id_bytes) =
2313-
verify_bridge_auth(headers, "GET", &url, None, state.config.require_auth_token)?;
2377+
let VerifiedBridgeAuth {
2378+
pubkey,
2379+
event_id_bytes,
2380+
..
2381+
} = verify_bridge_auth(headers, "GET", &url, None, state.config.require_auth_token)?;
23142382
check_nip98_replay(state, &tenant, event_id_bytes).await?;
23152383
let pubkey_bytes = pubkey.to_bytes().to_vec();
23162384

@@ -2921,14 +2989,21 @@ mod tests {
29212989
let tenant_a = fresh_tenant("host-a.example");
29222990
let expected_url = nip98_expected_url(config_relay_url, &tenant_a, "/events");
29232991

2924-
let (pubkey, _event_id_bytes) =
2925-
verify_bridge_auth(&headers, "POST", &expected_url, Some(b""), true)
2926-
.expect("matching-host NIP-98 event must verify");
2992+
let VerifiedBridgeAuth {
2993+
pubkey,
2994+
signed_created_at,
2995+
..
2996+
} = verify_bridge_auth(&headers, "POST", &expected_url, Some(b""), true)
2997+
.expect("matching-host NIP-98 event must verify");
29272998
assert_eq!(
29282999
pubkey,
29293000
keys.public_key(),
29303001
"returned pubkey must be the signer's"
29313002
);
3003+
assert!(
3004+
signed_created_at.is_some(),
3005+
"verified NIP-98 auth must retain its signed timestamp"
3006+
);
29323007
}
29333008

29343009
/// Mirror of the query-reconstruction `authorize_moderation_read` performs
@@ -2970,7 +3045,7 @@ mod tests {
29703045
Some("limit=20&status=open"),
29713046
);
29723047

2973-
let (pubkey, _event_id_bytes) =
3048+
let VerifiedBridgeAuth { pubkey, .. } =
29743049
verify_bridge_auth(&headers, "GET", &expected_url, None, true)
29753050
.expect("query-bearing moderation read must verify against the same query");
29763051
assert_eq!(pubkey, keys.public_key());
@@ -3027,7 +3102,7 @@ mod tests {
30273102
Some("limit=20"),
30283103
);
30293104

3030-
let (pubkey, _event_id_bytes) =
3105+
let VerifiedBridgeAuth { pubkey, .. } =
30313106
verify_bridge_auth(&headers, "GET", &expected_url, None, true)
30323107
.expect("audit query-bearing read must verify");
30333108
assert_eq!(pubkey, keys.public_key());
@@ -3052,7 +3127,7 @@ mod tests {
30523127
);
30533128
assert_eq!(expected_url, "https://host-a.example/moderation/restricted");
30543129

3055-
let (pubkey, _event_id_bytes) =
3130+
let VerifiedBridgeAuth { pubkey, .. } =
30563131
verify_bridge_auth(&headers, "GET", &expected_url, None, true)
30573132
.expect("query-less restricted read must verify against the bare path");
30583133
assert_eq!(pubkey, keys.public_key());

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ async fn authenticate(
138138
})?;
139139

140140
let expected_url = bridge::nip98_expected_url(&state.config.relay_url, &tenant, path);
141-
let (pubkey, event_id_bytes) = bridge::verify_bridge_auth_with_options(
141+
let bridge::VerifiedBridgeAuth {
142+
pubkey,
143+
event_id_bytes,
144+
signed_created_at,
145+
} = bridge::verify_bridge_auth_with_options(
142146
headers,
143147
"POST",
144148
&expected_url,
@@ -155,6 +159,7 @@ async fn authenticate(
155159
headers
156160
.get("x-auth-tag")
157161
.and_then(|value| value.to_str().ok()),
162+
signed_created_at,
158163
)
159164
.await?;
160165

0 commit comments

Comments
 (0)