From 77bcad65b5daecb8c2cb969cfae5490691d51613 Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Mon, 15 Dec 2025 16:54:12 +0100 Subject: [PATCH] Try to decode SGIDs in multiple ways This should avoid message decoding failures between different versions of sgids --- lib/rails_ext/action_text_attachables.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/rails_ext/action_text_attachables.rb b/lib/rails_ext/action_text_attachables.rb index e0d41f5..491f2ea 100644 --- a/lib/rails_ext/action_text_attachables.rb +++ b/lib/rails_ext/action_text_attachables.rb @@ -12,7 +12,7 @@ ActiveSupport.on_load(:action_text_content) do def attachable_from_possibly_expired_sgid(sgid) if message = sgid&.split("--")&.first - encoded_message = JSON.parse Base64.strict_decode64(message) + encoded_message = JSON.parse(decode_base64(message)) decoded_gid = if data = encoded_message.dig("_rails", "data") data @@ -20,7 +20,7 @@ ActiveSupport.on_load(:action_text_content) do # Rails 7 used an older format of GID that serialized the payload using Marshall # Since we intentionally skip signature verification, we can't safely unmarshal the data # To work around this, we manually extract the GID from the marshaled data - Base64.urlsafe_decode64(data).match(%r{(gid://campfire/[^/]+/\d+)})&.to_s + decode_base64(data).match(%r{(gid://campfire/[^/]+/\d+)})&.to_s else nil end @@ -32,6 +32,12 @@ ActiveSupport.on_load(:action_text_content) do rescue ActiveRecord::RecordNotFound nil end + + def decode_base64(message) + Base64.strict_decode64(message) + rescue => _e + Base64.urlsafe_decode64(message) + end end end end