activestorage-clamav-analyzer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +24 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +4 -0
- data/.tool-versions +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +128 -0
- data/LICENSE +21 -0
- data/README.md +118 -0
- data/Rakefile +13 -0
- data/activestorage-clamav-analyzer.gemspec +46 -0
- data/bin/console +15 -0
- data/bin/setup +7 -0
- data/lib/active_storage/clamav/analyzer.rb +72 -0
- data/lib/active_storage/clamav/railtie.rb +12 -0
- metadata +163 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 64d36015b61b0f2da77d7ad174feabb940e0e4dfa2aa6e95c62a49ec61e9acfc
|
|
4
|
+
data.tar.gz: 0e92c75870d1733459ca3d4112e07f144dbdf2b5fa4ee75c76c717122ab99004
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4766a756e4abedaf3e4c0069eefc9a802dc5167f01cb87d8607244f13e39107dca6bb7ff230ac5325fecd38ff3b62d8abdb842b7d796fcfab033d9632400e052
|
|
7
|
+
data.tar.gz: 9748013cb1bbb086cd237703f58ecf99f3496061eb32966a426ffdef3e2c418dcf176e4e77a6d92282b2fa41455ae50f1bb330776441ec1c3798b6b8ea122a07
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: Ruby
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [ main ]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [ main ]
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
build:
|
|
11
|
+
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v2
|
|
16
|
+
- name: Set up Ruby
|
|
17
|
+
uses: ruby/setup-ruby@v1
|
|
18
|
+
- name: Install ClamAV
|
|
19
|
+
run: sudo apt install clamav clamav-daemon
|
|
20
|
+
- name: Build and test with Rake
|
|
21
|
+
run: |
|
|
22
|
+
gem install bundler
|
|
23
|
+
bundle install --jobs 4 --retry 3
|
|
24
|
+
WITH_CLAMD=true bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.tool-versions
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby 3.0.0
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
activestorage-clamav-analyzer (0.1.0)
|
|
5
|
+
activestorage
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
actionpack (7.0.3)
|
|
11
|
+
actionview (= 7.0.3)
|
|
12
|
+
activesupport (= 7.0.3)
|
|
13
|
+
rack (~> 2.0, >= 2.2.0)
|
|
14
|
+
rack-test (>= 0.6.3)
|
|
15
|
+
rails-dom-testing (~> 2.0)
|
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
17
|
+
actionview (7.0.3)
|
|
18
|
+
activesupport (= 7.0.3)
|
|
19
|
+
builder (~> 3.1)
|
|
20
|
+
erubi (~> 1.4)
|
|
21
|
+
rails-dom-testing (~> 2.0)
|
|
22
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
23
|
+
activejob (7.0.3)
|
|
24
|
+
activesupport (= 7.0.3)
|
|
25
|
+
globalid (>= 0.3.6)
|
|
26
|
+
activemodel (7.0.3)
|
|
27
|
+
activesupport (= 7.0.3)
|
|
28
|
+
activerecord (7.0.3)
|
|
29
|
+
activemodel (= 7.0.3)
|
|
30
|
+
activesupport (= 7.0.3)
|
|
31
|
+
activestorage (7.0.3)
|
|
32
|
+
actionpack (= 7.0.3)
|
|
33
|
+
activejob (= 7.0.3)
|
|
34
|
+
activerecord (= 7.0.3)
|
|
35
|
+
activesupport (= 7.0.3)
|
|
36
|
+
marcel (~> 1.0)
|
|
37
|
+
mini_mime (>= 1.1.0)
|
|
38
|
+
activesupport (7.0.3)
|
|
39
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
40
|
+
i18n (>= 1.6, < 2)
|
|
41
|
+
minitest (>= 5.1)
|
|
42
|
+
tzinfo (~> 2.0)
|
|
43
|
+
ast (2.4.2)
|
|
44
|
+
builder (3.2.4)
|
|
45
|
+
coderay (1.1.3)
|
|
46
|
+
concurrent-ruby (1.1.10)
|
|
47
|
+
crass (1.0.6)
|
|
48
|
+
diff-lcs (1.3)
|
|
49
|
+
erubi (1.10.0)
|
|
50
|
+
globalid (1.0.0)
|
|
51
|
+
activesupport (>= 5.0)
|
|
52
|
+
i18n (1.10.0)
|
|
53
|
+
concurrent-ruby (~> 1.0)
|
|
54
|
+
loofah (2.18.0)
|
|
55
|
+
crass (~> 1.0.2)
|
|
56
|
+
nokogiri (>= 1.5.9)
|
|
57
|
+
marcel (1.0.2)
|
|
58
|
+
method_source (1.0.0)
|
|
59
|
+
mini_mime (1.1.2)
|
|
60
|
+
mini_portile2 (2.8.0)
|
|
61
|
+
minitest (5.15.0)
|
|
62
|
+
nokogiri (1.13.6)
|
|
63
|
+
mini_portile2 (~> 2.8.0)
|
|
64
|
+
racc (~> 1.4)
|
|
65
|
+
parallel (1.22.1)
|
|
66
|
+
parser (3.1.2.0)
|
|
67
|
+
ast (~> 2.4.1)
|
|
68
|
+
pry (0.14.1)
|
|
69
|
+
coderay (~> 1.1)
|
|
70
|
+
method_source (~> 1.0)
|
|
71
|
+
racc (1.6.0)
|
|
72
|
+
rack (2.2.3)
|
|
73
|
+
rack-test (1.1.0)
|
|
74
|
+
rack (>= 1.0, < 3)
|
|
75
|
+
rails-dom-testing (2.0.3)
|
|
76
|
+
activesupport (>= 4.2.0)
|
|
77
|
+
nokogiri (>= 1.6)
|
|
78
|
+
rails-html-sanitizer (1.4.2)
|
|
79
|
+
loofah (~> 2.3)
|
|
80
|
+
rainbow (3.1.1)
|
|
81
|
+
rake (10.5.0)
|
|
82
|
+
regexp_parser (2.4.0)
|
|
83
|
+
rexml (3.2.5)
|
|
84
|
+
rspec (3.8.0)
|
|
85
|
+
rspec-core (~> 3.8.0)
|
|
86
|
+
rspec-expectations (~> 3.8.0)
|
|
87
|
+
rspec-mocks (~> 3.8.0)
|
|
88
|
+
rspec-core (3.8.2)
|
|
89
|
+
rspec-support (~> 3.8.0)
|
|
90
|
+
rspec-expectations (3.8.4)
|
|
91
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
92
|
+
rspec-support (~> 3.8.0)
|
|
93
|
+
rspec-mocks (3.8.1)
|
|
94
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
95
|
+
rspec-support (~> 3.8.0)
|
|
96
|
+
rspec-support (3.8.2)
|
|
97
|
+
rubocop (1.29.1)
|
|
98
|
+
parallel (~> 1.10)
|
|
99
|
+
parser (>= 3.1.0.0)
|
|
100
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
101
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
102
|
+
rexml (>= 3.2.5, < 4.0)
|
|
103
|
+
rubocop-ast (>= 1.17.0, < 2.0)
|
|
104
|
+
ruby-progressbar (~> 1.7)
|
|
105
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
106
|
+
rubocop-ast (1.18.0)
|
|
107
|
+
parser (>= 3.1.1.0)
|
|
108
|
+
rubocop-rspec (2.6.0)
|
|
109
|
+
rubocop (~> 1.19)
|
|
110
|
+
ruby-progressbar (1.11.0)
|
|
111
|
+
tzinfo (2.0.4)
|
|
112
|
+
concurrent-ruby (~> 1.0)
|
|
113
|
+
unicode-display_width (2.1.0)
|
|
114
|
+
|
|
115
|
+
PLATFORMS
|
|
116
|
+
ruby
|
|
117
|
+
|
|
118
|
+
DEPENDENCIES
|
|
119
|
+
activestorage-clamav-analyzer!
|
|
120
|
+
bundler (~> 2.0)
|
|
121
|
+
pry
|
|
122
|
+
rake (~> 10.0)
|
|
123
|
+
rspec (~> 3.0)
|
|
124
|
+
rubocop
|
|
125
|
+
rubocop-rspec
|
|
126
|
+
|
|
127
|
+
BUNDLED WITH
|
|
128
|
+
2.3.11
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Ackama
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# ActiveStorage::ClamAV::Analyzer
|
|
2
|
+
|
|
3
|
+
This gem packages an analyzer to perform ClamAV virus scans on uploaded ActiveStorage::Blob objects, adding the results of the scan to the blob metadata.
|
|
4
|
+
|
|
5
|
+
The actual analyzer is very simple, and can be found in `lib/active_storage/clamav/analyzer` if you would prefer to just drop this in `app/analyzers` in your codebase and prepend it to the analyzers list yourself.
|
|
6
|
+
|
|
7
|
+
## Installing ClamAV
|
|
8
|
+
|
|
9
|
+
Ensure you have ClamAV installed. This gem uses these commands, but does not
|
|
10
|
+
set them up if they are missing. On your path you should have:
|
|
11
|
+
|
|
12
|
+
- `clamav`
|
|
13
|
+
- `clamscan`
|
|
14
|
+
|
|
15
|
+
On most platforms, you can install ClamAV with the package name:
|
|
16
|
+
|
|
17
|
+
- Mac OS: `brew install clamav` (Further setup steps are necessary with Homebrewed ClamAV, see https://gist.github.com/mendozao/3ea393b91f23a813650baab9964425b9)
|
|
18
|
+
- Debian/Ubuntu: `apt install clamav`
|
|
19
|
+
|
|
20
|
+
There are plenty of other installation methods and platforms available. More information about these is available on [ClamAV's website](https://docs.clamav.net/manual/Installing.html)
|
|
21
|
+
|
|
22
|
+
You can also run ClamAV scans in a Docker container. The [ClamAV documentation] has [an installation page](https://docs.clamav.net/manual/Installing/Docker.html) dedicated to this. While you will have to tune `ActiveStorage::ClamAV::Analyzer.command` for this, it should work with this gem.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
Add this line to your application's Gemfile:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
gem 'activestorage-clamav-analyzer', require: "active_storage/clamav/analyzer"
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
And then execute:
|
|
33
|
+
|
|
34
|
+
$ bundle
|
|
35
|
+
|
|
36
|
+
Or install it yourself as:
|
|
37
|
+
|
|
38
|
+
$ gem install activestorage-clamav-analyzer
|
|
39
|
+
|
|
40
|
+
This gem will automatically add itself to the analyzer pipeline and run across any
|
|
41
|
+
supported image files. If you wish to control the precise analyzer order, you can
|
|
42
|
+
manipulate the `ActiveStorage.analyzers` array.
|
|
43
|
+
|
|
44
|
+
## Usage
|
|
45
|
+
|
|
46
|
+
This gem automatically adds itself to the analysis pipeline, simply ensure that analysis is run on your uploaded files.
|
|
47
|
+
|
|
48
|
+
To manually analyze a particular blob, simply grab an attachment and pass the
|
|
49
|
+
blob directly to the analyzer:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
ActiveStorage::ClamAV::Analyzer.new(ActiveStorage::Attachment.first.blob).metadata
|
|
53
|
+
=> {
|
|
54
|
+
"analyzed"=>true,
|
|
55
|
+
"clamav": {
|
|
56
|
+
"detection": true,
|
|
57
|
+
"output": "test.txt: Eicar-Signature FOUND\n\n-------" #...
|
|
58
|
+
}}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Recipes
|
|
62
|
+
|
|
63
|
+
#### Scan with ClamD
|
|
64
|
+
|
|
65
|
+
`clamscan` is the default command, but starts up ClamAV from scratch each time it is run, which takes several seconds.
|
|
66
|
+
Using `clamd` is much faster, but requires you to have started `clamd` ahead of time.
|
|
67
|
+
|
|
68
|
+
An example of the speedups that are possible:
|
|
69
|
+
|
|
70
|
+
- `clamscan README.md 9.68s user 0.36s system 96% cpu 10.400 total`
|
|
71
|
+
- `clamdscan README.md 0.01s user 0.00s system 36% cpu 0.026 total`
|
|
72
|
+
|
|
73
|
+
If your infrastructure set up allows you to run `clamd`, you can adjust the command to use `clamdscan`, which will
|
|
74
|
+
scan files in a fraction of the time:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
# config/initializers/active_storage.rb
|
|
78
|
+
ActiveStorage::ClamAV::Analyzer.command = "clamdscan"
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Scan with a Docker container
|
|
82
|
+
|
|
83
|
+
ClamAV has comprehensive documentation on [how to scan files in a Docker container](https://docs.clamav.net/manual/Installing/Docker.html).
|
|
84
|
+
If you'd like to do this yourself using the ClamAV analyzer, that's no problem! You'll need to build a custom
|
|
85
|
+
command to mount the blob's tempfile into your container to get the result. `ActiveStorage::ClamAV::Analyzer.command` accepts anything
|
|
86
|
+
that responds to `#call`, so you can customise your command:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
# config/initializers/active_storage.rb
|
|
90
|
+
|
|
91
|
+
clamav_command = (tempfile) -> { "docker run --rm -v #{tempfile.path}:#{tempfile.path} clamav/clamav clamscan" }
|
|
92
|
+
ActiveStorage::ClamAV::Analyzer.command = clamav_command
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Remove blobs that have a detection
|
|
96
|
+
|
|
97
|
+
This analyzer records detections, but by default takes no action. Destroying blobs from a library could surprise some library
|
|
98
|
+
users, and also would prevent any investigation of the blob, who uploaded it, and what the exact detection is.
|
|
99
|
+
|
|
100
|
+
The analyzer does however call a callable (Proc, lambda, etc) when a detection occurs, passing the blob - so it's very simple
|
|
101
|
+
to remove the blob automatically when a detection occurs.
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
ActiveStorage::ClamAV::Analyzer.on_detection = (blob) -> { blob.destroy }
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
You can use the same technique to take some other action - perhaps quarantine the blob in some way (make it inactive), or
|
|
108
|
+
add it to a moderation or alert queue.
|
|
109
|
+
|
|
110
|
+
## Development
|
|
111
|
+
|
|
112
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the lint checks and tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
113
|
+
|
|
114
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
115
|
+
|
|
116
|
+
## Contributing
|
|
117
|
+
|
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ackama/activestorage-clamav-analyzer.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
require 'rubocop/rake_task'
|
|
6
|
+
|
|
7
|
+
RuboCop::RakeTask.new(:rubocop) do |t|
|
|
8
|
+
t.options = ['--parallel']
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
12
|
+
|
|
13
|
+
task default: %i[rubocop spec]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'activestorage-clamav-analyzer'
|
|
8
|
+
spec.version = '0.1.0'
|
|
9
|
+
spec.authors = ['Josh McArthur']
|
|
10
|
+
spec.email = ['joshua.mcarthur@gmail.com']
|
|
11
|
+
spec.required_ruby_version = '>= 2.5.0'
|
|
12
|
+
|
|
13
|
+
spec.summary = '
|
|
14
|
+
Performs anti-virus scans on ActiveStorage::Blob objects using ClamAV'
|
|
15
|
+
spec.description = '
|
|
16
|
+
Add ActiveStorage Analyzer class to perform ClamAV virus scans on
|
|
17
|
+
ActiveStorage::Blob objects. Supports ClamAV daemon mode, custom arguments
|
|
18
|
+
and callbacks on detection.'
|
|
19
|
+
spec.homepage = 'https://github.com/ackama/activestorage-clamav-analyzer'
|
|
20
|
+
|
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
22
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
|
23
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
|
24
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
25
|
+
|
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been
|
|
28
|
+
# added into git.
|
|
29
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
30
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
31
|
+
f.match(%r{^(test|spec|features)/})
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
spec.bindir = 'exe'
|
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
36
|
+
spec.require_paths = ['lib']
|
|
37
|
+
|
|
38
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
|
39
|
+
spec.add_development_dependency 'pry'
|
|
40
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
41
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
42
|
+
spec.add_development_dependency 'rubocop'
|
|
43
|
+
spec.add_development_dependency 'rubocop-rspec'
|
|
44
|
+
spec.add_dependency 'activestorage'
|
|
45
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
46
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'activestorage/clamav/analyzer'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require 'irb'
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open3'
|
|
4
|
+
require 'active_storage'
|
|
5
|
+
require 'active_storage/analyzer/image_analyzer'
|
|
6
|
+
|
|
7
|
+
module ActiveStorage
|
|
8
|
+
module ClamAV
|
|
9
|
+
##
|
|
10
|
+
# Uses ClamAV to perform an antivirus scan on the ActiveStorage::Blob,
|
|
11
|
+
# taking action if a detection occurs and otherwise recording the scan
|
|
12
|
+
# results as metadata.
|
|
13
|
+
#
|
|
14
|
+
# This analyzer requires that ClamAV is installed, but otherwise makes the
|
|
15
|
+
# command and flags available via accessors on this module.
|
|
16
|
+
class Analyzer < ActiveStorage::Analyzer
|
|
17
|
+
##
|
|
18
|
+
# Configure the command to run when analyzing blobs. This can either be a
|
|
19
|
+
# string of command or command + args, or can be a callable object.
|
|
20
|
+
# If callable, it will be called before analysing, and will pass an
|
|
21
|
+
# instance of a tempfile. This is useful for using ClamAV via things
|
|
22
|
+
# like Docker, when you may want to mount the tempfile into a container
|
|
23
|
+
# for scanning.
|
|
24
|
+
mattr_accessor :command, default: 'clamscan'
|
|
25
|
+
|
|
26
|
+
##
|
|
27
|
+
# Configure a callable to run when ClamAV returns a non-zero status
|
|
28
|
+
# indicating a virus was detected. The callable receives the blob and
|
|
29
|
+
# can take action to quarantine or remove the blob record, send an alert,
|
|
30
|
+
# or some other action.
|
|
31
|
+
mattr_accessor :on_detection, default: ->(blob) {}
|
|
32
|
+
|
|
33
|
+
##
|
|
34
|
+
# All blobs can be virus scanned
|
|
35
|
+
def self.accept?
|
|
36
|
+
true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def metadata
|
|
40
|
+
{ clamav: download_blob_to_tempfile(&method(:perform_virus_scan)) }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def run_command(*args)
|
|
46
|
+
command = if self.command.respond_to?(:call)
|
|
47
|
+
self.command.call(*args)
|
|
48
|
+
else
|
|
49
|
+
self.command
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
stdout, _stderr, status = Open3.capture3(command, *args)
|
|
53
|
+
status == 127 && \
|
|
54
|
+
raise("#{command} not found. Is it installed and available on PATH?")
|
|
55
|
+
|
|
56
|
+
[stdout, status]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def perform_virus_scan(tempfile)
|
|
60
|
+
output, status = run_command(tempfile.path)
|
|
61
|
+
on_detection.call(blob) rescue nil unless status.success? # rubocop:disable Style/RescueModifier
|
|
62
|
+
|
|
63
|
+
{
|
|
64
|
+
detection: !status.success?,
|
|
65
|
+
output: output
|
|
66
|
+
}
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
require 'active_storage/clamav/railtie' if defined?(Rails)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ActiveStorage
|
|
4
|
+
module ClamAV
|
|
5
|
+
##
|
|
6
|
+
# Adds the analyser to the list of ActiveStorage analyzers
|
|
7
|
+
# when the Rails app is configured
|
|
8
|
+
class Railtie < Rails::Railtie
|
|
9
|
+
config.active_storage.analyzers.prepend ActiveStorage::ClamAV::Analyzer
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: activestorage-clamav-analyzer
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Josh McArthur
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-05-20 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: pry
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '10.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '10.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rubocop
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rubocop-rspec
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: activestorage
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :runtime
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
description: |2-
|
|
112
|
+
|
|
113
|
+
Add ActiveStorage Analyzer class to perform ClamAV virus scans on
|
|
114
|
+
ActiveStorage::Blob objects. Supports ClamAV daemon mode, custom arguments
|
|
115
|
+
and callbacks on detection.
|
|
116
|
+
email:
|
|
117
|
+
- joshua.mcarthur@gmail.com
|
|
118
|
+
executables: []
|
|
119
|
+
extensions: []
|
|
120
|
+
extra_rdoc_files: []
|
|
121
|
+
files:
|
|
122
|
+
- ".github/workflows/ruby.yml"
|
|
123
|
+
- ".gitignore"
|
|
124
|
+
- ".rspec"
|
|
125
|
+
- ".rubocop.yml"
|
|
126
|
+
- ".tool-versions"
|
|
127
|
+
- Gemfile
|
|
128
|
+
- Gemfile.lock
|
|
129
|
+
- LICENSE
|
|
130
|
+
- README.md
|
|
131
|
+
- Rakefile
|
|
132
|
+
- activestorage-clamav-analyzer.gemspec
|
|
133
|
+
- bin/console
|
|
134
|
+
- bin/setup
|
|
135
|
+
- lib/active_storage/clamav/analyzer.rb
|
|
136
|
+
- lib/active_storage/clamav/railtie.rb
|
|
137
|
+
homepage: https://github.com/ackama/activestorage-clamav-analyzer
|
|
138
|
+
licenses: []
|
|
139
|
+
metadata:
|
|
140
|
+
homepage_uri: https://github.com/ackama/activestorage-clamav-analyzer
|
|
141
|
+
source_code_uri: https://github.com/ackama/activestorage-clamav-analyzer
|
|
142
|
+
changelog_uri: https://github.com/ackama/activestorage-clamav-analyzer
|
|
143
|
+
rubygems_mfa_required: 'true'
|
|
144
|
+
post_install_message:
|
|
145
|
+
rdoc_options: []
|
|
146
|
+
require_paths:
|
|
147
|
+
- lib
|
|
148
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: 2.5.0
|
|
153
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
|
+
requirements:
|
|
155
|
+
- - ">="
|
|
156
|
+
- !ruby/object:Gem::Version
|
|
157
|
+
version: '0'
|
|
158
|
+
requirements: []
|
|
159
|
+
rubygems_version: 3.2.3
|
|
160
|
+
signing_key:
|
|
161
|
+
specification_version: 4
|
|
162
|
+
summary: Performs anti-virus scans on ActiveStorage::Blob objects using ClamAV
|
|
163
|
+
test_files: []
|