From c4b74a4bf6f386d9c3ea63dd315a3320fb89f245 Mon Sep 17 00:00:00 2001 From: Brian Borge Date: Fri, 8 Dec 2023 16:14:04 -0500 Subject: [PATCH 1/2] Add spec for webhook signature validation with files --- spec/resources/webhook_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/resources/webhook_spec.rb b/spec/resources/webhook_spec.rb index 80a71a2..dabf75e 100644 --- a/spec/resources/webhook_spec.rb +++ b/spec/resources/webhook_spec.rb @@ -30,5 +30,22 @@ expect(result).to eq(false) end end + + context 'with files' do + let(:signature) { '1d2426c242a8c5de7eb1d9b662b7fda1d0b6edab' } + let(:params) { { test: true } } + + let(:files) do + [ + { name: 'file', tempfile: Tempfile.new('foo') }, + { name: 'file', tempfile: Tempfile.new('bar') } + ] + end + + it 'returns true' do + result = action + expect(result).to eq(true) + end + end end end From 2f8ebe1d15826cc816fc518c266b810dd74a6360 Mon Sep 17 00:00:00 2001 From: Brian Borge Date: Mon, 8 Jan 2024 15:53:58 -0500 Subject: [PATCH 2/2] Rewind tempfiles after webhook signature validation --- lib/phaxio/resources/webhook.rb | 4 +++- spec/resources/webhook_spec.rb | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/phaxio/resources/webhook.rb b/lib/phaxio/resources/webhook.rb index a05c0ae..8762811 100644 --- a/lib/phaxio/resources/webhook.rb +++ b/lib/phaxio/resources/webhook.rb @@ -57,7 +57,9 @@ def files_to_array(files) end def generate_file_string(file) - file[:name] + DIGEST.hexdigest(file[:tempfile].read) + file_string = file[:name] + DIGEST.hexdigest(file[:tempfile].read) + file[:tempfile].rewind + file_string end end end diff --git a/spec/resources/webhook_spec.rb b/spec/resources/webhook_spec.rb index dabf75e..5c18637 100644 --- a/spec/resources/webhook_spec.rb +++ b/spec/resources/webhook_spec.rb @@ -35,10 +35,22 @@ let(:signature) { '1d2426c242a8c5de7eb1d9b662b7fda1d0b6edab' } let(:params) { { test: true } } + let(:file1) do + tempfile = Tempfile.new + tempfile.write("foo") + tempfile + end + + let(:file2) do + tempfile = Tempfile.new + tempfile.write("bar") + tempfile + end + let(:files) do [ - { name: 'file', tempfile: Tempfile.new('foo') }, - { name: 'file', tempfile: Tempfile.new('bar') } + { name: 'file', tempfile: file1 }, + { name: 'file', tempfile: file2 } ] end @@ -46,6 +58,11 @@ result = action expect(result).to eq(true) end + + it 'rewinds the files' do + action + expect(files.all? { |f| f[:tempfile].pos.zero? }).to eq(true) + end end end end