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
7 changed files with 174 additions and 4 deletions

No files matched your search

+1 -1
View File
@@ -108,7 +108,7 @@ class SettingsController < ApplicationController
def set_nostr_pubkey
signed_event = Nostr::Event.new(**nostr_event_from_params)
is_valid_sig = signed_event.verify_signature
is_valid_sig = NostrManager::VerifySignedEvent.call(event: signed_event)
is_valid_auth = NostrManager::VerifyAuth.call(
event: signed_event,
challenge: session[:shared_secret]
+1 -1
View File
@@ -23,7 +23,7 @@ class Users::SessionsController < Devise::SessionsController
def nostr_login
signed_event = Nostr::Event.new(**nostr_event_from_params)
is_valid_sig = signed_event.verify_signature
is_valid_sig = NostrManager::VerifySignedEvent.call(event: signed_event)
is_valid_auth = NostrManager::VerifyAuth.call(
event: signed_event,
challenge: session[:shared_secret]
+6 -2
View File
@@ -1,5 +1,7 @@
module NostrManager
class VerifyAuth < NostrManagerService
AUTH_KIND = 22242
def initialize(event:, challenge:)
@event = event
@challenge_expected = challenge
@@ -7,9 +9,11 @@ module NostrManager
end
def call
return false unless @event.kind == AUTH_KIND
tags = parse_tags(@event.tags)
site_given = tags[:site].first
challenge_given = tags[:challenge].first
site_given = tags[:site]&.first
challenge_given = tags[:challenge]&.first
site_given == @site_expected &&
challenge_given == @challenge_expected
@@ -0,0 +1,29 @@
module NostrManager
class VerifySignedEvent < NostrManagerService
def initialize(event:, allowed_kinds: nil)
@event = event
@allowed_kinds = allowed_kinds
end
def call
return false if @event.id.blank? || @event.pubkey.blank? || @event.sig.blank?
return false unless event_id_matches?
return false unless signature_valid?
return false if @allowed_kinds.present? && !@allowed_kinds.include?(@event.kind)
true
end
private
def signature_valid?
@event.verify_signature
rescue StandardError
false
end
def event_id_matches?
Digest::SHA256.hexdigest(JSON.dump(@event.serialize)) == @event.id
end
end
end
+23
View File
@@ -127,5 +127,28 @@ RSpec.describe "Settings", type: :request do
expect(flash[:alert]).to eq("Public key could not be verified")
end
end
context "With a forged event body" do
before do
expect(LdapManager::UpdateNostrKey).not_to receive(:call)
forged_event = auth_event.merge("content" => "injected")
post set_nostr_pubkey_settings_path,
params: { signed_event: forged_event }.to_json,
headers: {
"CONTENT_TYPE" => "application/json",
"HTTP_ACCEPT" => "application/json"
}
end
it "returns a 422 status" do
expect(response).to have_http_status(422)
end
it "informs the user about the failure" do
expect(flash[:alert]).to eq("Public key could not be verified")
end
end
end
end
+23
View File
@@ -90,5 +90,28 @@ RSpec.describe "Devise login sessions", type: :request do
expect(flash[:alert]).to eq("Login verification failed")
end
end
context "With a forged event body" do
before do
expect(LdapManager::FetchUserByNostrKey).not_to receive(:call)
forged_event = auth_event.merge("content" => "injected")
post users_nostr_login_path,
params: { signed_event: forged_event }.to_json,
headers: {
"CONTENT_TYPE" => "application/json",
"HTTP_ACCEPT" => "application/json"
}
end
it "returns an unauthorized status" do
expect(response).to have_http_status(401)
end
it "informs the user about the failure" do
expect(flash[:alert]).to eq("Login verification failed")
end
end
end
end
@@ -0,0 +1,91 @@
require 'rails_helper'
RSpec.describe NostrManager::VerifySignedEvent, type: :model do
let(:keypair) do
Nostr::KeyPair.new(
private_key: Nostr::PrivateKey.new(Setting.nostr_private_key),
public_key: Nostr::PublicKey.new(Setting.nostr_public_key)
)
end
let(:nostr_user) { Nostr::User.new(keypair: keypair) }
let(:event) do
nostr_user.create_event(kind: 22242, content: "", tags: [["challenge", "abc"]])
end
describe "with a correctly signed event" do
it "is accepted" do
expect(described_class.call(event: event)).to be(true)
end
end
describe "with an injected event body but a valid id/sig pair" do
let(:forged) do
Nostr::Event.new(
id: event.id, sig: event.sig, pubkey: event.pubkey,
created_at: event.created_at, kind: event.kind, tags: event.tags,
content: "injected"
)
end
it "still passes the raw signature check" do
expect(forged.verify_signature).to be(true)
end
it "is rejected because the id no longer matches the body" do
expect(described_class.call(event: forged)).to be(false)
end
end
describe "with a tampered signature" do
let(:forged) do
Nostr::Event.new(
id: event.id, sig: "0" * 128, pubkey: event.pubkey,
created_at: event.created_at, kind: event.kind, tags: event.tags,
content: event.content
)
end
it "is rejected" do
expect(described_class.call(event: forged)).to be(false)
end
end
describe "with a malformed signature" do
let(:malformed) do
Nostr::Event.new(
id: event.id, sig: "abc", pubkey: event.pubkey,
created_at: event.created_at, kind: event.kind, tags: event.tags,
content: event.content
)
end
it "is rejected instead of raising" do
expect(described_class.call(event: malformed)).to be(false)
end
end
describe "with missing id, pubkey or signature" do
let(:unsigned) do
Nostr::Event.new(
pubkey: event.pubkey, created_at: event.created_at,
kind: event.kind, tags: [], content: ""
)
end
it "is rejected" do
expect(described_class.call(event: unsigned)).to be(false)
end
end
describe "with an allowed_kinds restriction" do
it "accepts an event of an allowed kind" do
expect(described_class.call(event: event, allowed_kinds: [22242])).to be(true)
end
it "rejects an event of a disallowed kind" do
expect(described_class.call(event: event, allowed_kinds: [0, 10002, 10063])).to be(false)
end
end
end