Verify Nostr event IDs before trusting signed events
This commit is contained in:
10 files changed
+168
-56
No files matched your search
@@ -47,3 +47,7 @@ dump.rdb
|
||||
|
||||
# Ignore generated ctags
|
||||
*.tags
|
||||
|
||||
# Ignore local agent tooling artifacts
|
||||
/.agents
|
||||
/skills-lock.json
|
||||
@@ -6,16 +6,3 @@
|
||||
) %>
|
||||
<% end %>
|
||||
|
||||
<% if @status == 1 %>
|
||||
<p class="mt-8">
|
||||
<button class="btn-md btn-blue">
|
||||
Edit my profile
|
||||
</button>
|
||||
</p>
|
||||
<% elsif @status == 2 %>
|
||||
<p class="mt-8">
|
||||
<button class="btn-md btn-blue">
|
||||
Create my profile
|
||||
</button>
|
||||
</p>
|
||||
<% end %>
|
||||
@@ -155,12 +155,15 @@ class SettingsController < ApplicationController
|
||||
|
||||
case params[:step]
|
||||
when "relays"
|
||||
nip65_event = NostrManager::DiscoverUserRelays.call(pubkey: @user.nostr_pubkey)
|
||||
nip65_event = NostrManager::DiscoverUserEvent.call(
|
||||
pubkey: @user.nostr_pubkey, kind: NostrManagerService::RELAY_LIST_KIND
|
||||
)
|
||||
write_relays = nip65_event ? extract_write_relays(nip65_event) : []
|
||||
|
||||
if nip65_event && write_relays.any? && stale?(nip65_event)
|
||||
auth_event = NostrManager::DiscoverUserRelays.call(
|
||||
pubkey: @user.nostr_pubkey, relays: write_relays
|
||||
auth_event = NostrManager::DiscoverUserEvent.call(
|
||||
pubkey: @user.nostr_pubkey, kind: NostrManagerService::RELAY_LIST_KIND,
|
||||
relays: write_relays
|
||||
)
|
||||
nip65_event = auth_event if auth_event
|
||||
write_relays = extract_write_relays(nip65_event) if auth_event
|
||||
@@ -173,8 +176,8 @@ class SettingsController < ApplicationController
|
||||
when "profile"
|
||||
write_relays = Rails.cache.read("#{cache_key}:write_relays") || []
|
||||
relays = write_relays.any? ? write_relays : nil
|
||||
profile = NostrManager::DiscoverUserProfile.call(
|
||||
pubkey: @user.nostr_pubkey, relays: relays
|
||||
profile = NostrManager::DiscoverUserEvent.call(
|
||||
pubkey: @user.nostr_pubkey, kind: NostrManagerService::PROFILE_KIND, relays: relays
|
||||
)
|
||||
Rails.cache.write("#{cache_key}:profile", profile, expires_in: 5.minutes)
|
||||
render json: { next: "render" }
|
||||
|
||||
@@ -16,10 +16,20 @@ module Settings
|
||||
default: ENV["NOSTR_PUBLIC_KEY_PRIMARY_DOMAIN"].presence
|
||||
|
||||
field :nostr_relay_url, type: :string,
|
||||
default: ENV["NOSTR_RELAY_URL"].presence
|
||||
default: ENV["NOSTR_RELAY_URL"].presence,
|
||||
validates: { format: {
|
||||
with: %r{\Awss?://.+},
|
||||
allow_blank: true,
|
||||
message: "must be a ws:// or wss:// URL"
|
||||
} }
|
||||
|
||||
field :nostr_blossom_server_url, type: :string,
|
||||
default: ENV["NOSTR_BLOSSOM_SERVER_URL"].presence
|
||||
default: ENV["NOSTR_BLOSSOM_SERVER_URL"].presence,
|
||||
validates: { format: {
|
||||
with: %r{\Ahttps?://.+},
|
||||
allow_blank: true,
|
||||
message: "must be an http:// or https:// URL"
|
||||
} }
|
||||
|
||||
field :nostr_zaps_relay_limit, type: :integer,
|
||||
default: 12
|
||||
@@ -31,11 +41,6 @@ module Settings
|
||||
wss://relay.primal.net
|
||||
wss://relay.damus.io
|
||||
]
|
||||
|
||||
def self.nostr_relay_url_http
|
||||
self.nostr_relay_url.gsub(/^ws:/, "http:")
|
||||
.gsub(/^wss:/, "https:")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
+5
-7
@@ -1,21 +1,19 @@
|
||||
module NostrManager
|
||||
class DiscoverUserProfile < NostrManagerService
|
||||
def initialize(pubkey:, relays: nil)
|
||||
class DiscoverUserEvent < NostrManagerService
|
||||
def initialize(pubkey:, kind:, relays: nil)
|
||||
@pubkey = pubkey
|
||||
@kind = kind
|
||||
@relays = relays.present? ? relays : Setting.nostr_discovery_relays
|
||||
end
|
||||
|
||||
def call
|
||||
filter = Nostr::Filter.new(
|
||||
authors: [@pubkey],
|
||||
kinds: [0],
|
||||
kinds: [@kind],
|
||||
limit: 1,
|
||||
)
|
||||
|
||||
NostrManager::FetchLatestEvent.call(
|
||||
relays: @relays,
|
||||
filter: filter
|
||||
)
|
||||
NostrManager::FetchLatestEvent.call(relays: @relays, filter: filter)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,21 +0,0 @@
|
||||
module NostrManager
|
||||
class DiscoverUserRelays < NostrManagerService
|
||||
def initialize(pubkey:, relays: nil)
|
||||
@pubkey = pubkey
|
||||
@relays = relays.present? ? relays : Setting.nostr_discovery_relays
|
||||
end
|
||||
|
||||
def call
|
||||
filter = Nostr::Filter.new(
|
||||
authors: [@pubkey],
|
||||
kinds: [10002],
|
||||
limit: 1,
|
||||
)
|
||||
|
||||
NostrManager::FetchLatestEvent.call(
|
||||
relays: @relays,
|
||||
filter: filter
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,10 @@
|
||||
require "nostr"
|
||||
|
||||
class NostrManagerService < ApplicationService
|
||||
PROFILE_KIND = 0
|
||||
RELAY_LIST_KIND = 10002
|
||||
MEDIA_SERVER_LIST_KIND = 10063
|
||||
|
||||
def parse_tags(tags)
|
||||
out = {}
|
||||
# TODO support more than 1 item for each tag type
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
<div>
|
||||
<%= profile.inspect %>
|
||||
</div>
|
||||
@@ -151,4 +151,99 @@ RSpec.describe "Settings", type: :request do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /settings/fetch_nostr_metadata" do
|
||||
before do
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return("abc123")
|
||||
allow_any_instance_of(User).to receive(:display_name).and_return("Mark")
|
||||
end
|
||||
|
||||
context "without a pubkey" do
|
||||
before do
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(nil)
|
||||
end
|
||||
|
||||
it "returns a bad request status" do
|
||||
get nostr_metadata_settings_path,
|
||||
headers: { "HTTP_ACCEPT" => "application/json" }
|
||||
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
|
||||
context "with step=relays" do
|
||||
it "discovers the relay list and asks for the profile next" do
|
||||
expect(NostrManager::DiscoverUserEvent).to receive(:call)
|
||||
.with(pubkey: "abc123", kind: NostrManagerService::RELAY_LIST_KIND)
|
||||
.and_return(nil)
|
||||
|
||||
get nostr_metadata_settings_path(step: "relays"),
|
||||
headers: { "HTTP_ACCEPT" => "application/json" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq("next" => "profile")
|
||||
end
|
||||
|
||||
context "with a stale relay list" do
|
||||
let(:stale_event) do
|
||||
{
|
||||
"id" => "old",
|
||||
"created_at" => 2.hours.ago.to_i,
|
||||
"tags" => [
|
||||
["r", "wss://write.example.com"],
|
||||
["r", "wss://read.example.com", "read"]
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
it "refetches the relay list from the user's write relays" do
|
||||
expect(NostrManager::DiscoverUserEvent).to receive(:call)
|
||||
.with(pubkey: "abc123", kind: NostrManagerService::RELAY_LIST_KIND)
|
||||
.and_return(stale_event)
|
||||
expect(NostrManager::DiscoverUserEvent).to receive(:call)
|
||||
.with(pubkey: "abc123", kind: NostrManagerService::RELAY_LIST_KIND,
|
||||
relays: ["wss://write.example.com"])
|
||||
.and_return(nil)
|
||||
|
||||
get nostr_metadata_settings_path(step: "relays"),
|
||||
headers: { "HTTP_ACCEPT" => "application/json" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq("next" => "profile")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with step=profile" do
|
||||
it "discovers the profile and asks to render next" do
|
||||
expect(NostrManager::DiscoverUserEvent).to receive(:call)
|
||||
.with(pubkey: "abc123", kind: NostrManagerService::PROFILE_KIND, relays: nil)
|
||||
.and_return(nil)
|
||||
|
||||
get nostr_metadata_settings_path(step: "profile"),
|
||||
headers: { "HTTP_ACCEPT" => "application/json" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)).to eq("next" => "render")
|
||||
end
|
||||
end
|
||||
|
||||
context "with step=render" do
|
||||
it "renders the metadata partial" do
|
||||
get nostr_metadata_settings_path(step: "render"),
|
||||
headers: { "HTTP_ACCEPT" => "text/html" }
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
end
|
||||
|
||||
context "with an unknown step" do
|
||||
it "returns a bad request status" do
|
||||
get nostr_metadata_settings_path(step: "bogus"),
|
||||
headers: { "HTTP_ACCEPT" => "application/json" }
|
||||
|
||||
expect(response).to have_http_status(:bad_request)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe NostrManager::DiscoverUserEvent, type: :model do
|
||||
before do
|
||||
allow(Setting).to receive(:nostr_discovery_relays)
|
||||
.and_return(["wss://discovery.example.com"])
|
||||
end
|
||||
|
||||
describe "without explicit relays" do
|
||||
it "queries the discovery relays for the requested kind" do
|
||||
expect(NostrManager::FetchLatestEvent).to receive(:call) do |args|
|
||||
expect(args[:relays]).to eq(["wss://discovery.example.com"])
|
||||
expect(args[:filter].authors).to eq(["abc123"])
|
||||
expect(args[:filter].kinds).to eq([NostrManagerService::RELAY_LIST_KIND])
|
||||
expect(args[:filter].limit).to eq(1)
|
||||
"event"
|
||||
end
|
||||
|
||||
result = described_class.call(
|
||||
pubkey: "abc123", kind: NostrManagerService::RELAY_LIST_KIND
|
||||
)
|
||||
expect(result).to eq("event")
|
||||
end
|
||||
end
|
||||
|
||||
describe "with explicit relays" do
|
||||
it "queries the given relays instead of the discovery relays" do
|
||||
expect(NostrManager::FetchLatestEvent).to receive(:call) do |args|
|
||||
expect(args[:relays]).to eq(["wss://write.example.com"])
|
||||
expect(args[:filter].kinds).to eq([NostrManagerService::PROFILE_KIND])
|
||||
nil
|
||||
end
|
||||
|
||||
described_class.call(
|
||||
pubkey: "abc123", kind: NostrManagerService::PROFILE_KIND,
|
||||
relays: ["wss://write.example.com"]
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user