Skip to content

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

Description

@deepracercve

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions