Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File not found - despite it persisting in the database #20

Open
deepracercve opened this issue Jun 4, 2024 · 1 comment
Open

File not found - despite it persisting in the database #20

deepracercve opened this issue Jun 4, 2024 · 1 comment

Comments

@deepracercve
Copy link

deepracercve commented Jun 4, 2024

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

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

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant