fix(relay): keep the NIP-11 document readable under a CORS allowlist - #6195
Open
guptasiddharth wants to merge 2 commits into
Open
fix(relay): keep the NIP-11 document readable under a CORS allowlist#6195guptasiddharth wants to merge 2 commits into
guptasiddharth wants to merge 2 commits into
Conversation
NIP-11 requires the relay information document to be readable cross-origin. `build_cors_layer` wraps every route, so setting `BUZZ_CORS_ORIGINS` — which `deploy/compose/.env.example` tells operators to do — also narrows `GET /` with `Accept: application/nostr+json` and `GET /info` to the allowlist, and browser clients hosted anywhere else can no longer read the document. The request still returns 200, so the failure is invisible server-side. Serve the document with permissive CORS headers on the response instead of splitting the route: `/` is content-negotiated with the WebSocket upgrade and cannot be split off, and tower_http's CORS layer overwrites only the header names it emits — so an allowlisted origin still gets its own value echoed back (exactly one), while every other origin keeps the permissive one. This is the pattern the NIP-05 document already uses. The allowlist keeps governing /events, /query, /count, and the media surface. Fixes block#5550 Signed-off-by: guptasiddharth <guptasiddharth12@gmail.com>
`just test-unit` enumerates packages explicitly because nothing in CI runs `cargo test --workspace`, and buzz-relay is not on that list. The backend integration job archives the package's lib tests (`cargo nextest archive -p buzz-relay --lib`) but only ever selects `api::invites::tests` and `handlers::relay_admin::tests`, so `router::tests` and `nip11::tests` are compiled by clippy and the archive build, then executed by nothing. Select those two modules in the unit job, where they belong: both are pure in-process axum + tower coverage with no Postgres or Redis. The rest of the package's lib set is infra-backed and stays where it is. Mirrored into scripts/run-tests.sh, which the recipe falls back to when cargo-nextest is absent — the two lists must stay in step, as the surrounding comments note. Verified: 23 tests run, 23 passed, no services running. Signed-off-by: guptasiddharth <guptasiddharth12@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5550.
Problem
NIP-11 makes cross-origin readability of the relay information document a MUST:
build_cors_layeris applied to the whole merged router (router.rs:200), so whenBUZZ_CORS_ORIGINSis set the allowlist that (correctly) protects the authenticated REST surface also narrows the relay information document —GET /withAccept: application/nostr+jsonandGET /info. Browser-based clients hosted anywhere else can no longer read it.This is on the documented path, not an exotic config:
deploy/compose/.env.exampleshipsBUZZ_CORS_ORIGINS=https://buzz.example.com, so following the deployment guide produces a relay that fails the MUST. The response is200either way and only the header differs, so it fails silently from the server's perspective.Fix
Serve the document with permissive CORS headers set on the response (
relay_info_responseinnip11.rs), used by both the/infohandler and the content-negotiated/branch.I did not split the document into its own permissively-layered
Routeras the issue suggested, because/is content-negotiated with the WebSocket upgrade — the NIP-11 branch is selected byAccept, not by path, so it cannot be routed away from the WS handler. Setting the headers on the response covers both entry points and composes correctly with the existing layer:tower_http's CORS layer overwrites only the header names it emits, soAccess-Control-Allow-Origin, never two.This is the same pattern the NIP-05 document already uses (
api/nip05.rs), so the two public documents now behave alike.Nothing else is widened:
BUZZ_CORS_ORIGINSkeeps governing/events,/query,/count, the media endpoints and the rest of the surface. The document carries no private data — capabilities, limits, and the relay's public key — andauth_required/restricted_writescontinue to gate everything that matters.Tests
Three tests in
router::tests, driving the realbuild_cors_layerwith a configured allowlist:nip11_document_stays_readable_from_any_origin_under_a_cors_allowlist— a foreign origin gets all three headers on/info. Fails onmain(left: [],right: ["*"]), which is the reported symptom.allowlisted_origin_gets_exactly_one_allow_origin_on_the_nip11_document— guards the duplicate-header hazard.cors_allowlist_still_narrows_the_authenticated_surface— a bridge route still gets noAccess-Control-Allow-Originfor a foreign origin, so the exemption cannot silently widen.Second commit: making those tests actually run
While checking which job would execute them, I found that nothing does.
just test-unitenumerates packages explicitly (because nothing in CI runscargo test --workspace) and buzz-relay is not on the list; the backend integration job archives the package's lib tests but only ever selectsapi::invites::testsandhandlers::relay_admin::tests. Sorouter::testsandnip11::testsare compiled by clippy and the archive build, then executed by nothing — a regression test that never runs would not have guarded this fix.The second commit selects those two modules in the unit job, where they belong: both are pure in-process axum + tower coverage needing no Postgres or Redis (23 tests, verified passing with no services up). The rest of the package's lib set is infra-backed and stays where it is. Mirrored into
scripts/run-tests.sh, the recipe's fallback when cargo-nextest is absent, since the surrounding comments there ask for the two lists to stay in step.I put this in
just test-unitrather than adding a step toci.ymlon purpose: these tests need no infra, and.github/workflows/ci.ymlis in thehashFileskey for the relay-artifacts cache, so editing it would invalidate that cache for everyone. Happy to move it if you'd rather have it elsewhere — or to drop the commit entirely if the coverage gap is deliberate.Manual check against the issue's repro, with
BUZZ_CORS_ORIGINS=https://relay.example.comset:Related
Searched open PRs before starting — no PR referenced #5550. Closest in the area: none touching
build_cors_layerornip11.rs.One adjacent observation, not changed here: NIP-05 requires
Access-Control-Allow-Origin: *on/.well-known/nostr.jsonand the handler already sets it, so that one is conformant today — it just gets there by the same mechanism this PR now uses for NIP-11.