carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.66k forks source link

Unexpected file upload updating unrelated column #2757

Open xanderman opened 4 weeks ago

xanderman commented 4 weeks ago

I have a model with an uploader mounted on remote_document, and code that downloads the file to do a virus scan on it. The code looks roughly like this:

# I can't figure out a better way to download the file locally and check that it worked. Open to suggestions.
cache = document.remote_document.cache!
if cache.nil?
  Rails.logger.warn("download failed")
  document.update(virus_scan_result: :error)
  return
end

path = document.remote_document.file.file
result = ClamScan::Client.scan(location: path)
document.update(virus_scan_result: result.status)

In our S3 logs we noticed unexpected PUT requests, apparently uploading new versions of files. I dug into spans in datadog and was shocked to discover that there is a PUT happening as part of the transaction in this code (I think it's the "happy path" based on logging). The only thing I can guess is that carrierwave's callback decided that the file was modified. Why would there be an upload here? I'm not even updating the remote_document column.