Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Change MD5 Fingerprints to SHA1 Fingerprints #1708

Closed
wants to merge 3 commits into from

Conversation

teliosdev
Copy link

MD5 can collide. SHA1 is better.

@teliosdev
Copy link
Author

I'm not sure why the paperclip spec for fog is failing...

@clemensg
Copy link
Contributor

clemensg commented Dec 4, 2014

👍

@jyurek Please merge this change. On modern hardware SHA1 is even faster than MD5 and at the same time it is less prone to collision attacks.

The travis error has nothing to do with this change. According to the Travis build history the fog test failure did also occur with other PRs.

@jyurek
Copy link

jyurek commented Dec 4, 2014

This seems fine. But this is a pretty big change, given that people currently have fingerprints in MD5 format.

But, anyway, collisions are problems with hashing functions in general. Are collisions actually something you've had problems with in Paperclip?

@clemensg
Copy link
Contributor

clemensg commented Dec 4, 2014

It's known for a long time that MD5 is vulnerable to collision attacks. For SHA1 on the other hand, there are still no known collisions, although that might just be a matter of time.

I did not have problems yet, but I am using it in a critical environment, so I'd rather be safe than sorry. I don't fear the probability of accidentally colliding filenames, but a deliberate collision attack by a malicious user. (Maybe I am a little paranoid :D)

Regarding backwards-compatibility:
It is likely that some paperclip users relied on the fact that it uses MD5 for the fingerprint.

What do you think about letting the user choose the hash algorithm?
I'd use MD5 as a default, so existing apps won't be affected. The rails generator could then add the SHA1 setting for new apps.

@teliosdev
Copy link
Author

@jyurek With a carefully crafted file, an attacker cam create two distinct files that make the same MD5, and given the nature of paperclip, I think it would be best to move to SHA1.

@clemensg I don't like the idea of making MD5 default. The Rails Way™ is convention over configuration, so they should have to choose to use MD5; not to say that the compatibility thing isn't an issue, though.

Maybe have a converter that updates the hash to SHA1 whenever it can?

@clemensg
Copy link
Contributor

clemensg commented Dec 4, 2014

Maybe a rake task to regenerate all fingerprints and a warning in the README and NEWS file?

@jyurek Do you expect any issues with backwards-compatibility?
What is your opinion on the configurability of the hash function and how do you want to proceed?

@jyurek
Copy link

jyurek commented Dec 5, 2014

Yes, I expect issues with backwards compatibility! If we're going to add this, then an option for which Digest class to use should be in place, but I'm not 100% sure how/where to add it yet. The fingerprint method is in lib/paperclip/io_adapters/abstract_adaptor.rb, which doesn't have access to the Paperclip options.

@maclover7
Copy link
Contributor

Hi everybody! Is this still an issue? If so, can you please rebase your PR and make sure everything is passing? Thanks!

@teliosdev
Copy link
Author

Yeah, I'll rebase this, give me a little while.

@teliosdev
Copy link
Author

Updated, but I'm not sure what's going on with travis...

@maclover7
Copy link
Contributor

This looks set to go.

paging @jferris @jyurek

@teliosdev
Copy link
Author

Any progress on the status of this?

@maclover7
Copy link
Contributor

Hi @medcat! Currently just waiting on @jyurek or @jferris to say if this is ready to be merged into Paperclip. I think it's ready, but we need to wait for one of them (I don't have write permissions to the repo).

@clemensg
Copy link
Contributor

Hi! What about existing projects who update to this version.. shouldn't there be a way to automatically migrate their MD5 fingerprints to SHA1? A rake task to force the regeneration of all fingerprints would be nice.

@maclover7 maclover7 mentioned this pull request Mar 23, 2015
@maclover7
Copy link
Contributor

paging @jferris @jyurek @mike-burns

@jyurek
Copy link

jyurek commented Apr 24, 2015

This isn't going to get merged without a way to either automatically update the hashes or a way to switch between which class is doing the digesting. As I said before, there are a LOT of people already using MD5 hashes and this would break every single one. Please address this or it won't get merged. Thanks.

@@ -94,7 +95,7 @@ def initialize(name, instance, options = {})
# attachment:
# new_user.avatar = old_user.avatar
def assign(uploaded_file)
@file = Paperclip.io_adapters.for(uploaded_file)
@file = Paperclip.io_adapters.for(uploaded_file, @options[:adapter_options])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

end

context "SHA1" do
let(:options) { { :hash_digest => Digest::SHA1 } }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the new Ruby 1.9 hash syntax.

@teliosdev
Copy link
Author

I believe I am content with it.

@@ -32,6 +32,7 @@ def self.default_options
:use_timestamp => true,
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails],
:validate_media_type => true,
:adapter_options => { :hash_digest => Digest::SHA1 },
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry to have to repeat this, but SHA1 cannot be the default. At least, not until we have a major version bump. This should remain MD5, and changing it to SHA1 (or anything else provided by Digest) should be well documented. There can (and should) still be tests for SHA1, but it should be specified as an option in those tests, not assumed that it's the default.

@tute tute removed this from the v4.4.0 milestone Sep 10, 2015
Adapters now accept an options parameter, that currently specifies
the type of hash digest to use.  The default value is SHA1, but
can be specified to be any OpenSSL-supported digest.  The specs are
modified to reflect that.

The task just reassigns all of the attachments, thereby regenerating
their fingerprints.
@@ -2,7 +2,8 @@

describe Paperclip::AttachmentAdapter do
before do
rebuild_model path: "tmp/:class/:attachment/:style/:filename", styles: {thumb: '50x50'}
rebuild_model path: "tmp/:class/:attachment/:style/:filename",
styles: {thumb: '50x50'}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Align the elements of a hash literal if they span more than one line.
Space inside { missing.
Space inside } missing.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

@@ -65,7 +65,7 @@

@attachment.assign(@file)
@attachment.save
@subject = Paperclip.io_adapters.for(@attachment)
@subject = Paperclip.io_adapters.for(@attachment, hash_digest: Digest::SHA1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

@teliosdev
Copy link
Author

It defaults to MD5 now.

@bdewater
Copy link
Contributor

bdewater commented Mar 7, 2016

I believe defaulting to MD5 is a mistake with this change possibly getting released with Paperclip 5.0 (#1991, #2049), where you can 'get away' with breaking changes. Keep it around as an option for existing users to add so they can upgrade the hashes at their own pace, but keep it secure by default. Go with SHA2, based on https://gist.github.com/tqbf/be58d2d39690c3b366ad

@clemensg
Copy link
Contributor

clemensg commented Mar 7, 2016

I second that. SHA-256 seems to be a safe default for Paperclip 5.0.
The upgrade rake task makes the update process easy.

I think it is a mistake to switch to SHA1 which has several known flaws. If we update then let's go straight to SHA2 (with 256 bits). SHA2-512 and SHA3 are probably overkill.

This PR is sitting around since the end of 2014. Any remaining objections from the maintainers?

@jyurek @tute @mike-burns @maclover7

@tute
Copy link
Contributor

tute commented Mar 12, 2016

Deferring to @jyurek.

@tute
Copy link
Contributor

tute commented May 9, 2016

I agree that if we change the hashing algorithm, it should be SHA2 by default, and it should be configurable so users upgrading can continue to use MD5 until needed. We can make this decision until we release the next major version, which is soon upcoming.

We want to comply with the coding standards and Hound comments for changed code, but leave alone the preexisting code. That way the changeset is smaller and easier to review. If there are only stylistic changes and you can undo them, please do. Thanks!

Can you please rebase on top of latest master? Thank you all for your input and work.

@bdewater
Copy link
Contributor

Unless somebody beats me to it, I'm willing to work the next week on rebasing this PR with SHA256 as default, incorporating the feedback from Hound and the above comment from @tute, so this can ship in Paperclip 5.0.

@tute
Copy link
Contributor

tute commented Aug 16, 2016

Closing in favor of @bdewater's #2229. Thank you.

@tute tute closed this Aug 16, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants