active_storage-postgresql icon indicating copy to clipboard operation
active_storage-postgresql copied to clipboard

File not found - despite it persisting in the database

Open deepracercve opened this issue 1 year ago • 1 comments

I'm still wrapping my head around the gem and ActiveStorage.

When using the gem - the uploaded files were persisted in the databases - however I wasn't able to serve them using either url_for or image_tag.

It looks like that the ActiveSupport::MessageVerifier does not work well with ruby hashes. If the message that needs to be generated is a hash with symbolized keys, the verify method returns the message with stringified keys (rather than a hash with indifferent access)

Steps to repeat:

verifier = ActiveSupport::MessageVerifier.new("secret")
signed_message = verifier.generate({key: '123'}) # "eyJrZXkiOiIxMjMifQ==--99a8291c065a28dff7471cadea5fe8151b2e45c0"
verifier.verify(signed_message) # {"key"=>"123"}

The above is used when serving the file from the URL. Example:

Started GET "/rails/active_storage/postgresql/eyJfcmFpbHMiOnsiZGF0YSI6eyJrZXkiOiIxbmQzbHoyanptNDE3dmtpMjh4dWl4bTBpdWM4IiwiZGlzcG9zaXRpb24iOiJpbmxpbmU7IGZpbGVuYW1lPVwiaXdwLmpwZ1wiOyBmaWxlbmFtZSo9VVRGLTgnJ2l3cC5qcGciLCJjb250ZW50X3R5cGUiOiJpbWFnZS9wbmcifSwiZXhwIjoiMjAyNC0wNi0wNFQwMDo0NToyMS45ODFaIiwicHVyIjoiYmxvYl9rZXkifX0=--29d1f4d86f0c7b323e8dac08629e1c624cb88ea4/iwp.jpg?content_type=image%2Fpng&disposition=inline%3B+filename%3D%22iwp.jpg%22%3B+filename%2A%3DUTF-8%27%27iwp.jpg" for 127.0.0.1 at 2024-06-03 18:42:24 -0600
Processing by ActiveStorage::PostgresqlController#show as JPEG
  Parameters: {"content_type"=>"image/png", "disposition"=>"inline; filename=\"iwp.jpg\"; filename*=UTF-8''iwp.jpg", "encoded_key"=>"[FILTERED]", "filename"=>"iwp"}

All example I've found online for generating a message assumed a string message. So, I'm not entirely sure if the message can be a key.

However for this to work the following line might need to be changed to symbolize the verified keys

https://github.com/lsylvester/active_storage-postgresql/blob/fa5bc8fc1882e1877d88fe557962b1126ea9c60d/app/controllers/active_storage/postgresql_controller.rb#L64

ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key)&.symbolize_key

Edit

versions:

  • ruby 3.1.3
  • rails 7.1.3
  • active_storage-postgresql 0.3.1

deepracercve avatar Jun 04 '24 01:06 deepracercve

For the time being - I've resorted to monkey-patching

added the following to an initializer (initializer/active_storage_postgresql.rb)

# MONEKY PATHCING
Rails.configuration.to_prepare do
    ActiveStorage::PostgresqlController.class_eval do

        private
     
          def decode_verified_key
            key = ActiveStorage.verifier.verified(params[:encoded_key], purpose: :blob_key)
            if key.is_a?(Hash)
                key = key.with_indifferent_access
            end
            key
          end
      
      
          def decode_verified_token
            token = ActiveStorage.verifier.verified(params[:encoded_token], purpose: :blob_token)
            if token.is_a?(Hash)
                token = token.with_indifferent_access
            end
            token
          end
      
    end
end

deepracercve avatar Jun 04 '24 01:06 deepracercve