-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #751 from DFE-Digital/112-malware-scanning-of-file…
…-uploads 112 malware scanning of file uploads
- Loading branch information
Showing
17 changed files
with
319 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class FetchMalwareScanResultJob < ApplicationJob | ||
discard_on ActiveRecord::RecordNotFound | ||
|
||
def perform(change_request:) | ||
Malware::FetchScanResult.new(change_request:).call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RemoveMalwareFileJob < ApplicationJob | ||
def perform(change_request:) | ||
Malware::RemoveFile.new(change_request:).call | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module Malware | ||
class FetchScanResult | ||
# Only later versions of the Azure Storage REST API support tags operations. | ||
Kernel.silence_warnings { Azure::Storage::Blob::Default::STG_VERSION = "2022-11-02" } | ||
|
||
SCAN_RESULT_TAG_KEY = /Malware Scanning scan result/ | ||
SCAN_RESULT_TAG_VALUE_CLEAN = /No threats found/ | ||
|
||
def initialize(change_request:) | ||
@change_request = change_request | ||
end | ||
|
||
def call | ||
return unless change_request.evidence.attached? | ||
|
||
response = fetch_scan_result | ||
if response.success? | ||
if response.body =~ SCAN_RESULT_TAG_KEY | ||
change_request.update!(malware_scan_result: malware_scan_result_from_response(response)) | ||
RemoveMalwareFileJob.perform_later(change_request:) if change_request.scan_result_suspect? | ||
end | ||
else | ||
change_request.scan_result_error! | ||
end | ||
rescue Azure::Core::Http::HTTPError | ||
change_request.scan_result_error! | ||
end | ||
|
||
private | ||
|
||
attr_reader :change_request | ||
|
||
def blob_service | ||
@blob_service ||= | ||
Azure::Storage::Blob::BlobService.new( | ||
storage_account_name: ENV["AZURE_STORAGE_ACCOUNT_NAME"], | ||
storage_access_key: ENV["AZURE_STORAGE_ACCESS_KEY"] | ||
) | ||
end | ||
|
||
def fetch_scan_result | ||
blob_service.call(:get, get_tags_for_blob_url) | ||
end | ||
|
||
def get_tags_for_blob_url | ||
@get_tags_for_blob_url ||= | ||
blob_service.generate_uri(File.join("evidence", change_request.evidence.key), { comp: "tags" }) | ||
end | ||
|
||
def malware_scan_result_from_response(response) | ||
response.body =~ SCAN_RESULT_TAG_VALUE_CLEAN ? "clean" : "suspect" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Malware | ||
class RemoveFile | ||
attr_reader :change_request | ||
|
||
def initialize(change_request:) | ||
@change_request = change_request | ||
end | ||
|
||
def call | ||
return unless change_request.scan_result_suspect? | ||
|
||
change_request.evidence.purge | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
db/migrate/20240717113527_add_malware_scan_result_to_change_requests.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddMalwareScanResultToChangeRequests < ActiveRecord::Migration[7.1] | ||
def change | ||
add_column :date_of_birth_changes, :malware_scan_result, :string, null: false, default: "pending" | ||
add_column :name_changes, :malware_scan_result, :string, null: false, default: "pending" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module MalwareScanHelpers | ||
def given_malware_scanning_is_enabled(scan_result: "No threats found") | ||
FeatureFlags::FeatureFlag.activate(:malware_scan) | ||
tags_url = "https://example.com/uploads/abc987xyz123?comp=tags" | ||
response_body = <<-XML.squish | ||
<Tags> | ||
<Tag> | ||
<Key>Malware Scanning scan result</Key> | ||
<Value>#{scan_result}</Value> | ||
</Tag> | ||
</Tags> | ||
XML | ||
stubbed_service = instance_double(Azure::Storage::Blob::BlobService, generate_uri: tags_url) | ||
stubbed_response = | ||
instance_double(Azure::Core::Http::HttpResponse, success?: true, body: response_body) | ||
allow(Azure::Storage::Blob::BlobService).to receive(:new).and_return(stubbed_service) | ||
allow(stubbed_service).to receive(:call).and_return(stubbed_response) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.