@@ -118,14 +118,30 @@ async fn check_nip98_replay_with_guard(
118118 }
119119}
120120
121- /// Reconstruct the canonical URL for NIP-98 verification from the relay config.
122- fn canonical_url ( relay_url : & str , path : & str ) -> String {
123- let base = relay_url
124- . trim ( )
125- . trim_end_matches ( '/' )
126- . replace ( "wss://" , "https://" )
127- . replace ( "ws://" , "http://" ) ;
128- format ! ( "{base}{path}" )
121+ /// Construct the NIP-98 `u`-tag expected URL for a request bound to `tenant`.
122+ ///
123+ /// Conformance row 44 obligation: "NIP-98 `u` URL host must match
124+ /// `req.community`." Host comes from the resolved [`TenantContext`] — the
125+ /// same host the row-zero seam already bound from the request `Host` header —
126+ /// and the scheme comes from the deployment's configured relay URL so
127+ /// `ws`/`wss` deployments map to `http`/`https` consistently with how the
128+ /// client signs the URL it is actually hitting.
129+ ///
130+ /// Critically, this does NOT use `config_relay_url`'s host. `config.relay_url`
131+ /// is one static string per deployment; under multi-tenant a relay serves many
132+ /// hosts, only one of which would match. Using it as the URL match key would
133+ /// (a) accept a NIP-98 event signed for community A's host when the request
134+ /// arrives at community B's host (host-binding side door — verify_nip98 would
135+ /// pass and the relay would proceed against the wrong tenant's auth context),
136+ /// and (b) reject every legitimate request whose community host isn't the
137+ /// single configured one. Substituting `tenant.host()` closes both directions.
138+ fn nip98_expected_url ( config_relay_url : & str , tenant : & TenantContext , path : & str ) -> String {
139+ let scheme = if config_relay_url. trim_start ( ) . starts_with ( "wss://" ) {
140+ "https"
141+ } else {
142+ "http"
143+ } ;
144+ format ! ( "{scheme}://{}{path}" , tenant. host( ) )
129145}
130146
131147/// Extract a channel UUID from a single filter's `#h` tag.
@@ -206,7 +222,7 @@ pub async fn submit_event(
206222 )
207223 } ) ?;
208224
209- let url = canonical_url ( & state. config . relay_url , "/events" ) ;
225+ let url = nip98_expected_url ( & state. config . relay_url , & tenant , "/events" ) ;
210226 let ( pubkey, event_id_bytes) = verify_bridge_auth (
211227 & headers,
212228 "POST" ,
@@ -295,7 +311,7 @@ pub async fn query_events(
295311 )
296312 } ) ?;
297313
298- let url = canonical_url ( & state. config . relay_url , "/query" ) ;
314+ let url = nip98_expected_url ( & state. config . relay_url , & tenant , "/query" ) ;
299315 let ( pubkey, event_id_bytes) = verify_bridge_auth (
300316 & headers,
301317 "POST" ,
@@ -570,7 +586,7 @@ pub async fn count_events(
570586 )
571587 } ) ?;
572588
573- let url = canonical_url ( & state. config . relay_url , "/count" ) ;
589+ let url = nip98_expected_url ( & state. config . relay_url , & tenant , "/count" ) ;
574590 let ( pubkey, event_id_bytes) = verify_bridge_auth (
575591 & headers,
576592 "POST" ,
@@ -1244,6 +1260,151 @@ mod tests {
12441260 ) ;
12451261 }
12461262
1263+ /// Build a signed NIP-98 event JSON string for `url` + `method`, mirroring
1264+ /// `buzz_auth::nip98::tests::make_nip98_event` so the bridge tests don't
1265+ /// reach into buzz-auth's test scope.
1266+ fn build_nip98_event_json ( keys : & Keys , url : & str , method : & str ) -> String {
1267+ let tags = vec ! [
1268+ Tag :: parse( [ "u" , url] ) . expect( "u tag" ) ,
1269+ Tag :: parse( [ "method" , method] ) . expect( "method tag" ) ,
1270+ ] ;
1271+ let event = EventBuilder :: new ( Kind :: HttpAuth , "" )
1272+ . tags ( tags)
1273+ . sign_with_keys ( keys)
1274+ . expect ( "sign NIP-98 event" ) ;
1275+ serde_json:: to_string ( & event) . expect ( "serialize" )
1276+ }
1277+
1278+ /// Build a `HeaderMap` with the NIP-98 event base64-encoded in
1279+ /// `Authorization: Nostr <base64>`, matching the production bridge auth
1280+ /// header shape.
1281+ fn nip98_auth_headers ( event_json : & str ) -> axum:: http:: HeaderMap {
1282+ use base64:: engine:: general_purpose:: STANDARD as BASE64 ;
1283+ let mut headers = axum:: http:: HeaderMap :: new ( ) ;
1284+ let value = format ! ( "Nostr {}" , BASE64 . encode( event_json. as_bytes( ) ) ) ;
1285+ headers. insert (
1286+ axum:: http:: header:: AUTHORIZATION ,
1287+ value. parse ( ) . expect ( "valid header value" ) ,
1288+ ) ;
1289+ headers
1290+ }
1291+
1292+ /// Row 44 obligation: a NIP-98 event signed against community A's host
1293+ /// MUST be rejected at the bridge when the request resolves to community
1294+ /// B's host. The conformance text in `docs/multi-tenant-conformance.md`
1295+ /// states: "NIP-98 `u` URL host must match `req.community`". Before this
1296+ /// gap closed, `expected_url` was derived from `state.config.relay_url`
1297+ /// (one static string per deployment), so any request to *any* host on a
1298+ /// multi-tenant deployment would verify against community A's URL — both
1299+ /// admitting cross-host forgeries (event signed for A presented at B) and
1300+ /// rejecting every legitimate request whose community host wasn't the
1301+ /// single configured one.
1302+ ///
1303+ /// This test bites if `nip98_expected_url` is reverted to use
1304+ /// `config.relay_url`'s host (the original `canonical_url` behavior).
1305+ #[ test]
1306+ fn verify_bridge_auth_rejects_nip98_event_signed_for_wrong_communitys_host ( ) {
1307+ let keys = Keys :: generate ( ) ;
1308+ // Client signs an event for community A's host, then presents it at a
1309+ // request whose `Host` header resolved to community B.
1310+ let signed_url = "https://host-a.example/events" ;
1311+ let event_json = build_nip98_event_json ( & keys, signed_url, "POST" ) ;
1312+ let headers = nip98_auth_headers ( & event_json) ;
1313+
1314+ let config_relay_url = "wss://host-a.example" ; // doesn't matter — only used for scheme.
1315+ let tenant_b = fresh_tenant ( "host-b.example" ) ;
1316+ let expected_url = nip98_expected_url ( config_relay_url, & tenant_b, "/events" ) ;
1317+
1318+ let ( status, body) = verify_bridge_auth ( & headers, "POST" , & expected_url, Some ( b"" ) , true )
1319+ . expect_err (
1320+ "cross-host NIP-98 event MUST be rejected — row 44: `u` URL host \
1321+ must match req.community",
1322+ ) ;
1323+ assert_eq ! (
1324+ status,
1325+ StatusCode :: UNAUTHORIZED ,
1326+ "cross-host rejection must be a 401, not silently admitted"
1327+ ) ;
1328+ let msg = body
1329+ . get ( "error" )
1330+ . and_then ( |v| v. as_str ( ) )
1331+ . unwrap_or_default ( ) ;
1332+ assert ! (
1333+ msg. contains( "URL mismatch" ) ,
1334+ "rejection must carry the URL-mismatch signal so callers can \
1335+ distinguish it from other auth failures; got body = {body:?}"
1336+ ) ;
1337+ }
1338+
1339+ /// Positive control for the cross-host test: a NIP-98 event signed for
1340+ /// host A MUST be accepted at a request whose tenant resolved to host A.
1341+ /// Without this, the cross-host test could be passing vacuously (e.g. if
1342+ /// `nip98_expected_url` always produced a URL no event could match).
1343+ #[ test]
1344+ fn verify_bridge_auth_accepts_nip98_event_signed_for_matching_host ( ) {
1345+ let keys = Keys :: generate ( ) ;
1346+ let signed_url = "https://host-a.example/events" ;
1347+ let event_json = build_nip98_event_json ( & keys, signed_url, "POST" ) ;
1348+ let headers = nip98_auth_headers ( & event_json) ;
1349+
1350+ // Configured relay URL deliberately differs in host from the request's
1351+ // tenant host — proving the helper uses `tenant.host()`, not the config.
1352+ let config_relay_url = "wss://other-config-host.example" ;
1353+ let tenant_a = fresh_tenant ( "host-a.example" ) ;
1354+ let expected_url = nip98_expected_url ( config_relay_url, & tenant_a, "/events" ) ;
1355+
1356+ let ( pubkey, _event_id_bytes) =
1357+ verify_bridge_auth ( & headers, "POST" , & expected_url, Some ( b"" ) , true )
1358+ . expect ( "matching-host NIP-98 event must verify" ) ;
1359+ assert_eq ! (
1360+ pubkey,
1361+ keys. public_key( ) ,
1362+ "returned pubkey must be the signer's"
1363+ ) ;
1364+ }
1365+
1366+ /// `nip98_expected_url` derives host from `tenant`, not from
1367+ /// `config_relay_url`. Pin both directions: changing the tenant's host
1368+ /// changes the output; changing the config's host does NOT.
1369+ #[ test]
1370+ fn nip98_expected_url_uses_tenant_host_not_config_host ( ) {
1371+ let tenant_a = fresh_tenant ( "host-a.example" ) ;
1372+ let tenant_b = fresh_tenant ( "host-b.example" ) ;
1373+
1374+ let url_a = nip98_expected_url ( "wss://config-host.example" , & tenant_a, "/events" ) ;
1375+ let url_b = nip98_expected_url ( "wss://config-host.example" , & tenant_b, "/events" ) ;
1376+ assert_eq ! ( url_a, "https://host-a.example/events" ) ;
1377+ assert_eq ! ( url_b, "https://host-b.example/events" ) ;
1378+
1379+ // Same tenant, two different config hosts → output is identical.
1380+ // (If config-host ever leaked into the URL, this assertion would bite.)
1381+ let url_a_alt_config =
1382+ nip98_expected_url ( "wss://different-config.example" , & tenant_a, "/events" ) ;
1383+ assert_eq ! (
1384+ url_a, url_a_alt_config,
1385+ "config-relay-url's host MUST NOT influence the NIP-98 expected URL — \
1386+ only its scheme contributes"
1387+ ) ;
1388+ }
1389+
1390+ /// `nip98_expected_url` derives scheme from `config_relay_url`'s prefix:
1391+ /// `wss://` → `https`, everything else → `http`. Deployments that run
1392+ /// `ws://` in dev/test still need a NIP-98 URL the client can sign against.
1393+ #[ test]
1394+ fn nip98_expected_url_derives_scheme_from_config ( ) {
1395+ let tenant = fresh_tenant ( "host-a.example" ) ;
1396+ assert_eq ! (
1397+ nip98_expected_url( "wss://config.example" , & tenant, "/events" ) ,
1398+ "https://host-a.example/events" ,
1399+ "wss:// production config → https:// URL"
1400+ ) ;
1401+ assert_eq ! (
1402+ nip98_expected_url( "ws://config.example" , & tenant, "/events" ) ,
1403+ "http://host-a.example/events" ,
1404+ "ws:// dev config → http:// URL"
1405+ ) ;
1406+ }
1407+
12471408 /// Build a kind:30174 engram envelope authored by `agent`, tagged with `owner`.
12481409 fn engram_envelope ( agent : & Keys , owner_hex : & str ) -> buzz_core:: StoredEvent {
12491410 let d_tag = Tag :: custom (
0 commit comments