Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #452: use SHA256 instead of MD5 for FIPS compatibility #541

Merged
merged 1 commit into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/listen/file.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'digest/md5'
require 'digest'

module Listen
class File
Expand Down Expand Up @@ -53,7 +53,7 @@ def self.change(record, rel_path)
# then at ???14.998, but the fstat time would be ???14.0 in
# both cases).
#
# If change happend at ???14.999997, the mtime is 14.0, so for
# If change happened at ???14.999997, the mtime is 14.0, so for
# an mtime=???14.0 we assume it could even be almost ???15.0
#
# So if Time.now.to_f is ???15.999998 and stat reports mtime
Expand All @@ -67,9 +67,11 @@ def self.change(record, rel_path)
#
return if data[:mtime].to_i + 2 <= Time.now.to_f

md5 = Digest::MD5.file(path).digest
record.update_file(rel_path, data.merge(md5: md5))
:modified if record_data[:md5] && md5 != record_data[:md5]
sha = Digest::SHA256.file(path).digest
record.update_file(rel_path, data.merge(sha: sha))
if record_data[:sha] && sha != record_data[:sha]
:modified
end
rescue SystemCallError
record.unset_path(rel_path)
:removed
Expand Down
30 changes: 15 additions & 15 deletions spec/lib/listen/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

context 'with file record' do
let(:record_mtime) { nil }
let(:record_md5) { nil }
let(:record_sha) { nil }
let(:record_mode) { nil }
let(:record_size) { nil }

let(:record_data) do
{ mtime: record_mtime, md5: record_md5, mode: record_mode, size: record_size }
{ mtime: record_mtime, sha: record_sha, mode: record_mode, size: record_size }
end

context 'with non-existing file' do
Expand All @@ -54,7 +54,7 @@
let(:record_size) { 42 }
let(:stat_size) { record_size }

let(:md5) { fail 'stub me (md5)' }
let(:sha) { fail 'stub me (sha)' }

let(:stat) do
instance_double(
Expand All @@ -69,7 +69,7 @@

before do
allow(::File).to receive(:lstat) { stat }
allow(Digest::MD5).to receive(:file) { double(:md5, digest: md5) }
allow(Digest::SHA256).to receive(:file) { double(:sha, digest: sha) }
end

context 'with different mode in record' do
Expand Down Expand Up @@ -148,8 +148,8 @@

before { allow(Time).to receive(:now) { now } }

context 'without available md5' do
let(:md5) { fail Errno::ENOENT }
context 'without available sha' do
let(:sha) { fail Errno::ENOENT }

# Treat it as a removed file, because chances are ...
# whatever is listening for changes won't be able to deal
Expand All @@ -161,25 +161,25 @@
end
end

context 'with available md5' do
let(:md5) { 'd41d8cd98f00b204e9800998ecf8427e' }
context 'with available sha' do
let(:sha) { 'd41d8cd98f00b204e9800998ecf8427e' }

context 'with same md5 in record' do
let(:record_md5) { md5 }
context 'with same sha in record' do
let(:record_sha) { sha }
it { should be_nil }
end

context 'with no md5 in record' do
let(:record_md5) { nil }
context 'with no sha in record' do
let(:record_sha) { nil }
it { should be_nil }
end

context 'with different md5 in record' do
let(:record_md5) { 'foo' }
context 'with different sha in record' do
let(:record_sha) { 'foo' }
it { should be :modified }

it 'sets path in record with expected data' do
expected = expected_data.merge(md5: md5)
expected = expected_data.merge(sha: sha)
expect(record).to receive(:update_file).
with('file.rb', expected)
subject
Expand Down
2 changes: 1 addition & 1 deletion spec/support/acceptance_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def change_fs(type, path)
# notification happens a little while later, e.g. at 1234568.111, now the file
# mtime and the current time in seconds are different (1234567 vs 1234568), and
# so the MD5 test won't kick in (see file.rb) - the file will not be considered
# for content checking (md5), so File.change will consider the file unmodified.
# for content checking (sha), so File.change will consider the file unmodified.
#
# This means, that if a file is added at 1234567.888 (and updated in Record),
# and then its content is modified at 1234567.999, and checking for changes
Expand Down