kobaltz / clamby

ClamAV interface to your Ruby on Rails project.
MIT License
132 stars 29 forks source link

Scanning a Tempfile which exists in memory #26

Closed aidandamerell closed 4 years ago

aidandamerell commented 5 years ago

It would be handy if you could scan a Tempfile (child of File) object as it exists in memory. irb> require 'tempfile' file = Tempfile.open("/path/to/file/file.txt") file.unlink Clamby.safe?(file)

I realise this may be a limitation as clamscan but thought I would make the suggestion anyway.

janko commented 5 years ago

It appears that clamscan supports taking data from standard input, so with Open3 this should work:

require "open3"

stdout, stderr, status = Open3.popen3("clamscan -") do |stdin, stdout, stderr, thread|
  IO.copy_stream(file, stdin)
  stdin.close

  [stdout.read, stderr.read, thread.value]
end
aidandamerell commented 5 years ago

After a bit of digging around into clamscan, it appears that there is no way to force it to solely read a file from memory. If passing input from STDIN, clamscan will write the input to a temporary file then scan that. The use-case for this request was to avoid files touching disk so it may be a little redundant.