Merge branch 'feature/nostr-profile-fields' into live
This commit is contained in:
2 files changed
+52
-11
No files matched your search
@@ -20,14 +20,16 @@ module Settings
|
||||
return render_error("Too many publish attempts. Please try again later.", :too_many_requests) if rate_limited?
|
||||
|
||||
event = Nostr::Event.new(**event_params)
|
||||
return render_error("Invalid event", :unprocessable_entity) unless valid_event?(event)
|
||||
reason = invalid_event_reason(event)
|
||||
return render_error("Invalid event: #{reason}", :unprocessable_entity) if reason
|
||||
|
||||
NostrPublishUserEventJob.perform_later(event: event.to_h.transform_keys(&:to_s))
|
||||
clear_metadata_cache
|
||||
|
||||
render json: { ok: true }
|
||||
rescue ActionController::ParameterMissing, ArgumentError, TypeError
|
||||
render_error("Invalid event", :unprocessable_entity)
|
||||
rescue ActionController::ParameterMissing, ArgumentError, TypeError => e
|
||||
Rails.logger.warn("[nostr] Malformed publish request from user #{current_user&.id}: #{e.class}: #{e.message}")
|
||||
render_error("Invalid event: malformed request", :unprocessable_entity)
|
||||
end
|
||||
|
||||
private
|
||||
@@ -45,15 +47,30 @@ module Settings
|
||||
}
|
||||
end
|
||||
|
||||
def valid_event?(event)
|
||||
return false unless event.pubkey == current_user.nostr_pubkey
|
||||
return false unless event.created_at.is_a?(Integer) && event.kind.is_a?(Integer)
|
||||
return false unless NostrManager::VerifySignedEvent.call(
|
||||
event: event, allowed_kinds: ALLOWED_KINDS
|
||||
)
|
||||
return false unless fresh?(event)
|
||||
def invalid_event_reason(event)
|
||||
reason =
|
||||
if event.pubkey != current_user.nostr_pubkey
|
||||
"public key mismatch"
|
||||
elsif !event.created_at.is_a?(Integer) || !event.kind.is_a?(Integer)
|
||||
"created_at/kind are not integers"
|
||||
elsif !ALLOWED_KINDS.include?(event.kind)
|
||||
"kind #{event.kind} not allowed"
|
||||
elsif !NostrManager::VerifySignedEvent.call(event: event)
|
||||
"invalid id or signature"
|
||||
elsif !fresh?(event)
|
||||
"stale created_at"
|
||||
elsif !within_size_limits?(event)
|
||||
"content or tags too large"
|
||||
end
|
||||
|
||||
within_size_limits?(event)
|
||||
return nil if reason.nil?
|
||||
|
||||
Rails.logger.warn(
|
||||
"[nostr] Rejected kind #{event.kind} event from #{event.pubkey} for user #{current_user.id}: " \
|
||||
"#{reason} (created_at=#{event.created_at.inspect}, tags=#{Array(event.tags).size}, " \
|
||||
"content_bytes=#{event.content.to_s.bytesize})"
|
||||
)
|
||||
reason
|
||||
end
|
||||
|
||||
def fresh?(event)
|
||||
|
||||
@@ -34,6 +34,10 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
}
|
||||
end
|
||||
|
||||
def error_message
|
||||
JSON.parse(response.body)["error"]
|
||||
end
|
||||
|
||||
describe "POST /settings/nostr_publish" do
|
||||
it "enqueues a publish job and reports success" do
|
||||
expect(NostrPublishUserEventJob).to receive(:perform_later)
|
||||
@@ -117,6 +121,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(forged)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: public key mismatch")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -129,6 +134,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(forged)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: invalid id or signature")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -139,6 +145,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(signed_event(kind: 1))
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: kind 1 not allowed")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -149,6 +156,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(signed_event(created_at: 1.hour.ago.to_i))
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: stale created_at")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -159,6 +167,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(signed_event(created_at: 1.hour.from_now.to_i))
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: stale created_at")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -169,6 +178,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(signed_event(content: "x" * 10_001))
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: content or tags too large")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -182,6 +192,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: malformed request")
|
||||
end
|
||||
|
||||
it "returns 422 when signed_event is not an object" do
|
||||
@@ -193,6 +204,7 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
}
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: malformed request")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -203,6 +215,18 @@ RSpec.describe "Settings::NostrEvents", type: :request do
|
||||
post_signed_event(signed_event(tags: ["not-an-array"]))
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(error_message).to eq("Invalid event: content or tags too large")
|
||||
end
|
||||
end
|
||||
|
||||
context "when an event is rejected" do
|
||||
it "logs the specific reason" do
|
||||
allow(Rails.logger).to receive(:warn)
|
||||
|
||||
post_signed_event(Nostr::Event.new(**signed_event.to_h.merge(pubkey: "0" * 64)))
|
||||
|
||||
expect(Rails.logger).to have_received(:warn).with(a_string_including("public key mismatch"))
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user