diff options
author | Julik Tarkhanov <me@julik.nl> | 2018-12-30 17:56:22 +0100 |
---|---|---|
committer | George Claghorn <george.claghorn@gmail.com> | 2018-12-30 11:56:22 -0500 |
commit | e5f4162b6178489181e3d7e7163ac12b7e0efc9d (patch) | |
tree | 75dea44e5e9bb7d87eeeeb2733666f5831a2344c /activestorage/app/models | |
parent | a796b993ad3717f9a0b3d5be070311ab7a0c95ba (diff) | |
download | rails-e5f4162b6178489181e3d7e7163ac12b7e0efc9d.tar.gz rails-e5f4162b6178489181e3d7e7163ac12b7e0efc9d.tar.bz2 rails-e5f4162b6178489181e3d7e7163ac12b7e0efc9d.zip |
Make Active Storage blob keys lowercase
Accommodate case-insensitive filesystems and database collations.
Diffstat (limited to 'activestorage/app/models')
-rw-r--r-- | activestorage/app/models/active_storage/blob.rb | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/activestorage/app/models/active_storage/blob.rb b/activestorage/app/models/active_storage/blob.rb index 04f9dbff9f..6ca7d49bc1 100644 --- a/activestorage/app/models/active_storage/blob.rb +++ b/activestorage/app/models/active_storage/blob.rb @@ -79,6 +79,15 @@ class ActiveStorage::Blob < ActiveRecord::Base def create_before_direct_upload!(filename:, byte_size:, checksum:, content_type: nil, metadata: nil) create! filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, metadata: metadata end + + # To prevent problems with case-insensitive filesystems, especially in combination + # with databases which treat indices as case-sensitive, all blob keys generated are going + # to only contain the base-36 character alphabet and will therefore be lowercase. To maintain + # the same or higher amount of entropy as in the base-58 encoding used by `has_secure_token` + # the number of bytes used is increased to 28 from the standard 24 + def generate_unique_secure_token + SecureRandom.base36(28) + end end # Returns a signed ID for this blob that's suitable for reference on the client-side without fear of tampering. @@ -87,9 +96,10 @@ class ActiveStorage::Blob < ActiveRecord::Base ActiveStorage.verifier.generate(id, purpose: :blob_id) end - # Returns the key pointing to the file on the service that's associated with this blob. The key is in the - # standard secure-token format from Rails. So it'll look like: XTAPjJCJiuDrLk3TmwyJGpUo. This key is not intended - # to be revealed directly to the user. Always refer to blobs using the signed_id or a verified form of the key. + # Returns the key pointing to the file on the service that's associated with this blob. The key is the + # secure-token format from Rails in lower case. So it'll look like: xtapjjcjiudrlk3tmwyjgpuobabd. + # This key is not intended to be revealed directly to the user. + # Always refer to blobs using the signed_id or a verified form of the key. def key # We can't wait until the record is first saved to have a key for it self[:key] ||= self.class.generate_unique_secure_token |