1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #37330 from eugeneius/active_storage_key_length

Always use 28 characters for Active Storage keys
This commit is contained in:
John Hawthorn 2019-09-30 16:34:37 -07:00 committed by GitHub
commit 4f5c63296e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View file

@ -29,7 +29,7 @@ class ActiveStorage::Blob < ActiveRecord::Base
MINIMUM_TOKEN_LENGTH = 28 MINIMUM_TOKEN_LENGTH = 28
has_secure_token :key has_secure_token :key, length: MINIMUM_TOKEN_LENGTH
store :metadata, accessors: [ :analyzed, :identified ], coder: ActiveRecord::Coders::JSON store :metadata, accessors: [ :analyzed, :identified ], coder: ActiveRecord::Coders::JSON
class_attribute :service class_attribute :service
@ -115,7 +115,7 @@ class ActiveStorage::Blob < ActiveRecord::Base
# Always refer to blobs using the signed_id or a verified form of the key. # Always refer to blobs using the signed_id or a verified form of the key.
def key def key
# We can't wait until the record is first saved to have a key for it # We can't wait until the record is first saved to have a key for it
self[:key] ||= self.class.generate_unique_secure_token self[:key] ||= self.class.generate_unique_secure_token(length: MINIMUM_TOKEN_LENGTH)
end end
# Returns an ActiveStorage::Filename instance of the filename that can be # Returns an ActiveStorage::Filename instance of the filename that can be

View file

@ -99,6 +99,10 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
end end
end end
test "build_after_unfurling generates a 28-character base36 key" do
assert_match(/^[a-z0-9]{28}$/, build_blob_after_unfurling.key)
end
test "image?" do test "image?" do
blob = create_file_blob filename: "racecar.jpg" blob = create_file_blob filename: "racecar.jpg"
assert_predicate blob, :image? assert_predicate blob, :image?

View file

@ -63,6 +63,10 @@ class ActiveSupport::TestCase
ActiveStorage::Blob.create_before_direct_upload! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, record: record ActiveStorage::Blob.create_before_direct_upload! key: key, filename: filename, byte_size: byte_size, checksum: checksum, content_type: content_type, record: record
end end
def build_blob_after_unfurling(key: nil, data: "Hello world!", filename: "hello.txt", content_type: "text/plain", identify: true, record: nil)
ActiveStorage::Blob.build_after_unfurling key: key, io: StringIO.new(data), filename: filename, content_type: content_type, identify: identify, record: record
end
def directly_upload_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", record: nil) def directly_upload_file_blob(filename: "racecar.jpg", content_type: "image/jpeg", record: nil)
file = file_fixture(filename) file = file_fixture(filename)
byte_size = file.size byte_size = file.size