Recompute Nostr event id before trusting signed events #268

Merged
raucao merged 1 commits from bugfix/nostr-event-id-verification into master 2026-11-18 16:37:27 +00:00
Owner

Nostr::Event#verify_signature in the nostr gem only validates the signature over the event's claimed id; it never recomputes the id from the serialized body. A genuine (id, sig, pubkey) triple lifted from any public event can therefore be replayed with injected tags, kind or content and still pass verification.

For POST /users/nostr_login this is an account takeover: an attacker injects their own challenge tag into someone else's signed event and signs in as that key's owner. POST /settings/set_nostr_pubkey is affected the same way.

This adds NostrManager::VerifySignedEvent, which recomputes the id as Digest::SHA256.hexdigest(JSON.dump(event.serialize)) and rejects any mismatch, and uses it in both flows. It also makes VerifyAuth assert kind == 22242 (previously only the tag contents were checked).

Regression tests: a forged body with a valid id/sig pair now fails verification at the service level and returns 422/401 at the endpoints. Full suite green (406 examples, 0 failures).

Found while working on #248.

`Nostr::Event#verify_signature` in the `nostr` gem only validates the signature over the event's claimed `id`; it never recomputes the id from the serialized body. A genuine `(id, sig, pubkey)` triple lifted from any public event can therefore be replayed with injected tags, kind or content and still pass verification. For `POST /users/nostr_login` this is an account takeover: an attacker injects their own `challenge` tag into someone else's signed event and signs in as that key's owner. `POST /settings/set_nostr_pubkey` is affected the same way. This adds `NostrManager::VerifySignedEvent`, which recomputes the id as `Digest::SHA256.hexdigest(JSON.dump(event.serialize))` and rejects any mismatch, and uses it in both flows. It also makes `VerifyAuth` assert `kind == 22242` (previously only the tag contents were checked). Regression tests: a forged body with a valid id/sig pair now fails verification at the service level and returns 422/401 at the endpoints. Full suite green (406 examples, 0 failures). Found while working on #248.
raucao added the
integration
nostr
label 2026-11-18 15:51:56 +00:00
raucao added 1 commit 2026-11-18 15:51:56 +00:00
Recompute Nostr event id before trusting signed events
CI / Test (pull_request) Successful in 44s
Release Drafter / Update release notes draft (pull_request) Successful in 2s
1eccfde70f
The nostr gem's Event#verify_signature only validates the signature over
the event's claimed id and never recomputes the id from the serialized
body. A genuine (id, sig, pubkey) triple lifted from any public event
could therefore be replayed with injected tags, kind or content and still
pass verification. For Nostr login this allows signing in as the key
owner.

Add NostrManager::VerifySignedEvent, which recomputes the id with
Digest::SHA256.hexdigest(JSON.dump(event.serialize)) and rejects any
mismatch, and use it in the pubkey verification and Nostr login flows.
VerifyAuth now also asserts kind 22242.
raucao added the security label 2026-11-18 16:37:20 +00:00
raucao merged commit b2e33edc7b into master 2026-11-18 16:37:27 +00:00
raucao deleted branch bugfix/nostr-event-id-verification 2026-11-18 16:37:28 +00:00
Sign in to join this conversation.