Woot4Moo
Woot4Moo

Reputation: 24316

Application.rb override base Ruby class

I have a RoR application that is doing the following in its application.rb

Digest::MD5 = Digest::SHA256

This in turn ensures that everytime anyone invokes Digest::MD5 that it will instead replace the result with a Digest::SHA256. I believe this will have some unintended consequences, such as runtime issues that are hard to debug. Is there any alternative to this approach or is this sound?

Upvotes: 2

Views: 66

Answers (1)

Anthony E
Anthony E

Reputation: 11235

Looks pretty dangerous to me. I understand wanting to use SHA256 over MD5, but they're definitely not interoperable, and there are certainly benign uses for MD5 which may be used by some of your dependencies.

Instead, why not use an around alias to issue a warning if MD5 is used?

class Digest::MD5
   alias :orig_hexencode :hexencode

  def hexencode(str)
    Rails.logger.warn("Hexencode called #{}")
    puts caller

    orig_hexencode(str)
  end
end

Upvotes: 2

Related Questions