mirror of
https://github.com/basecamp/once-campfire.git
synced 2026-10-28 08:26:48 +09:00
Look the endpoint up past the query cache, and keep a subscription a race refused
The lookup under the write lock repeated the one made before
validation, so the query cache could answer it with that earlier miss
even after another connection had registered the endpoint. It runs
uncached now.
Enabling notifications unsubscribed the browser on any refusal, so a
session signed out between authenticating and saving lost the browser's
subscription too, and the next session had nothing to post. Only a
refused endpoint unsubscribes it now.
From #371.
(cherry picked from commit aa93269ceb)
This commit is contained in:
1 parent
fadf6f1859
commit
bf2b9cc61b
3 files changed
+42
-9
No files matched your search
@@ -28,15 +28,17 @@ class Users::PushSubscriptionsController < ApplicationController
|
|||||||
|
|
||||||
# A password change ends every session and subscription. A request that authenticated just before it mustn't
|
# A password change ends every session and subscription. A request that authenticated just before it mustn't
|
||||||
# put one back, so the session is checked under the same write lock as the save, and the endpoint is looked up
|
# put one back, so the session is checked under the same write lock as the save, and the endpoint is looked up
|
||||||
# again there so two tabs posting at once register it once. Validation, which resolves the endpoint's address,
|
# again there, past the query cache, so two tabs posting at once register it once. Validation, which resolves
|
||||||
# runs before taking the lock.
|
# the endpoint's address, runs before taking the lock.
|
||||||
def register_while_signed_in(subscription)
|
def register_while_signed_in(subscription)
|
||||||
Push::Subscription.transaction do
|
Push::Subscription.uncached do
|
||||||
if Session.exists?(Current.session.id)
|
Push::Subscription.transaction do
|
||||||
if existing = @push_subscriptions.find_by(push_subscription_params)
|
if Session.exists?(Current.session.id)
|
||||||
existing.touch
|
if existing = @push_subscriptions.find_by(push_subscription_params)
|
||||||
else
|
existing.touch
|
||||||
subscription.save!(validate: false)
|
else
|
||||||
|
subscription.save!(validate: false)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -106,7 +106,8 @@ export default class extends Controller {
|
|||||||
|
|
||||||
async #syncPushSubscription(subscription) {
|
async #syncPushSubscription(subscription) {
|
||||||
const response = await post(this.subscriptionsUrlValue, { body: this.#extractJsonPayloadAsString(subscription), responseKind: "turbo-stream" })
|
const response = await post(this.subscriptionsUrlValue, { body: this.#extractJsonPayloadAsString(subscription), responseKind: "turbo-stream" })
|
||||||
if (!response.ok) subscription.unsubscribe()
|
// Only a refused endpoint is for good. A session signed out mid-request posts it again after signing in.
|
||||||
|
if (response.unprocessableEntity) subscription.unsubscribe()
|
||||||
}
|
}
|
||||||
|
|
||||||
async #requestPermissionAndSubscribe(registration) {
|
async #requestPermissionAndSubscribe(registration) {
|
||||||
|
|||||||
@@ -139,3 +139,33 @@ class Users::PushSubscriptionsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
{ "endpoint" => "https://fcm.googleapis.com/fcm/send/abc123", "p256dh_key" => "123", "auth_key" => "456" }
|
{ "endpoint" => "https://fcm.googleapis.com/fcm/send/abc123", "p256dh_key" => "123", "auth_key" => "456" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Users::PushSubscriptionsControllerForeignWriteTest < ActionDispatch::IntegrationTest
|
||||||
|
self.use_transactional_tests = false
|
||||||
|
|
||||||
|
setup do
|
||||||
|
sign_in :david
|
||||||
|
stub_web_push_dns_resolution
|
||||||
|
end
|
||||||
|
|
||||||
|
test "a subscription another connection registers while this one validates isn't registered twice" do
|
||||||
|
params = { "endpoint" => "https://fcm.googleapis.com/fcm/send/foreign", "p256dh_key" => "123", "auth_key" => "456" }
|
||||||
|
Push::Subscription.any_instance.stubs(:valid?).with { foreign_insert(params); true }.returns(true)
|
||||||
|
|
||||||
|
post user_push_subscriptions_url, params: { push_subscription: params }
|
||||||
|
|
||||||
|
assert_response :ok
|
||||||
|
assert_equal 1, Push::Subscription.where(endpoint: params["endpoint"]).count
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def foreign_insert(params)
|
||||||
|
unless @inserted
|
||||||
|
SQLite3::Database.new(ActiveRecord::Base.connection_db_config.database) do |database|
|
||||||
|
database.execute "INSERT INTO push_subscriptions (user_id, endpoint, p256dh_key, auth_key, created_at, updated_at) " \
|
||||||
|
"VALUES (?, ?, ?, ?, datetime('now'), datetime('now'))", [ users(:david).id, *params.values ]
|
||||||
|
end
|
||||||
|
@inserted = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in new issue
Block a user