Skip to content

Commit fdf259f

Browse files
committed
feat: Upload: add configurable max file size guard
1 parent 5214540 commit fdf259f

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

lib/active_storage/service/db_service.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ class Service::DBService < Service
2222

2323
def initialize(public: false, **)
2424
@chunk_size = [ENV.fetch("ASDB_CHUNK_SIZE") { 1.megabytes }.to_i, MINIMUM_CHUNK_SIZE].max
25+
@max_size = ENV.fetch("ASDB_MAX_FILE_SIZE", nil)&.to_i
2526
@public = public
2627
end
2728

2829
def upload(key, io, checksum: nil, **)
2930
instrument :upload, key: key, checksum: checksum do
3031
data = io.read
32+
if @max_size && data.bytesize > @max_size
33+
raise ArgumentError, "File size exceeds the maximum allowed size of #{@max_size} bytes"
34+
end
35+
3136
if checksum
3237
digest = Digest::MD5.base64digest(data)
3338
raise ActiveStorage::IntegrityError unless digest == checksum

0 commit comments

Comments
 (0)