Skip to content

Commit 84f6864

Browse files
authored
Merge pull request #1523 from alphagov/base64-encode-files
Base64 encode file attachments
2 parents 38acea9 + be2cb4d commit 84f6864

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

app/mailers/aws_ses_form_submission_mailer.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ def submission_email(answer_content_html:, answer_content_plain_text:, submissio
1111
@csv_filename = csv_filename
1212

1313
files.each do |name, file|
14-
attachments[name] = file
14+
attachments[name] = {
15+
encoding: "base64",
16+
content: Base64.encode64(file),
17+
}
1518
end
1619

1720
mail(to: submission_email_address, subject: @subject)

spec/features/fill_in_file_upload_question_spec.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
end
2525

2626
let(:test_file) { "tmp/a-file.txt" }
27+
let(:test_file_content) { "some content" }
28+
29+
let(:mock_s3_client) do
30+
Aws::S3::Client.new(stub_responses: {
31+
get_object: { body: test_file_content },
32+
})
33+
end
2734

2835
after do
2936
File.delete(test_file) if File.exist?(test_file)
@@ -36,12 +43,11 @@
3643

3744
allow(ReferenceNumberService).to receive(:generate).and_return(reference)
3845

39-
mock_s3_client = Aws::S3::Client.new(stub_responses: true)
4046
allow(Aws::S3::Client).to receive(:new).and_return(mock_s3_client)
4147
allow(mock_s3_client).to receive(:put_object)
4248
allow(mock_s3_client).to receive(:get_object_tagging).and_return({ tag_set: [{ key: "GuardDutyMalwareScanStatus", value: scan_status }] })
4349

44-
File.write(test_file, "some content")
50+
File.write(test_file, test_file_content)
4551
end
4652

4753
context "when the file is successfully uploaded" do
@@ -78,6 +84,29 @@
7884
end
7985
end
8086

87+
context "when the file unexpectedly contains binary" do
88+
# A PNG file which has been renamed to look like a TXT
89+
let(:test_file_content) { File.read("spec/fixtures/disguised_binary_file.txt") }
90+
let(:scan_status) { "NO_THREATS_FOUND" }
91+
92+
scenario "As a form filler" do
93+
when_i_visit_the_form_start_page
94+
then_i_should_see_the_first_question
95+
then_i_see_the_file_upload_component
96+
when_i_upload_a_file
97+
and_i_click_on_continue
98+
then_i_see_the_review_file_page
99+
and_i_click_on_continue
100+
then_i_should_see_the_check_your_answers_page
101+
102+
when_i_opt_out_of_email_confirmation
103+
and_i_submit_my_form
104+
then_my_form_should_be_submitted
105+
and_i_should_receive_a_reference_number
106+
and_an_email_submission_should_have_been_sent
107+
end
108+
end
109+
81110
def when_i_visit_the_form_start_page
82111
visit form_path(mode: "form", form_id: 1, form_slug: "fill-in-this-form")
83112
expect_page_to_have_no_axe_errors(page)
150 KB
Binary file not shown.

0 commit comments

Comments
 (0)