Skip to content

Commit 71ffeee

Browse files
authored
Merge pull request #147 from basecamp/fix-sgid-decoding
Try to decodde SGIDs in multiple ways
2 parents 238f73c + 77bcad6 commit 71ffeee

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/rails_ext/action_text_attachables.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ def from_node(node, attachable = nil)
1212

1313
def attachable_from_possibly_expired_sgid(sgid)
1414
if message = sgid&.split("--")&.first
15-
encoded_message = JSON.parse Base64.strict_decode64(message)
15+
encoded_message = JSON.parse(decode_base64(message))
1616

1717
decoded_gid = if data = encoded_message.dig("_rails", "data")
1818
data
1919
elsif data = encoded_message.dig("_rails", "message")
2020
# Rails 7 used an older format of GID that serialized the payload using Marshall
2121
# Since we intentionally skip signature verification, we can't safely unmarshal the data
2222
# To work around this, we manually extract the GID from the marshaled data
23-
Base64.urlsafe_decode64(data).match(%r{(gid://campfire/[^/]+/\d+)})&.to_s
23+
decode_base64(data).match(%r{(gid://campfire/[^/]+/\d+)})&.to_s
2424
else
2525
nil
2626
end
@@ -32,6 +32,12 @@ def attachable_from_possibly_expired_sgid(sgid)
3232
rescue ActiveRecord::RecordNotFound
3333
nil
3434
end
35+
36+
def decode_base64(message)
37+
Base64.strict_decode64(message)
38+
rescue => _e
39+
Base64.urlsafe_decode64(message)
40+
end
3541
end
3642
end
3743
end

0 commit comments

Comments
 (0)