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
Ruby: port py/weak-sensitive-data-hashing
#12782
base: main
Are you sure you want to change the base?
Conversation
ruby/ql/lib/codeql/ruby/security/WeakSensitiveDataHashingCustomizations.qll
Fixed
Show fixed
Hide fixed
|
QHelp previews: ruby/ql/src/queries/security/cwe-327/WeakSensitiveDataHashing.qhelpUse of a broken or weak cryptographic hashing algorithm on sensitive dataUsing a broken or weak cryptographic hash function can leave data vulnerable, and should not be used in security related code. A strong cryptographic hash function should be resistant to:
As an example, both MD5 and SHA-1 are known to be vulnerable to collision attacks. Since it's OK to use a weak cryptographic hash function in a non-security context, this query only alerts when these are used to hash sensitive data (such as passwords, certificates, usernames). Use of broken or weak cryptographic algorithms that are not hashing algorithms, is handled by the RecommendationEnsure that you use a strong, modern cryptographic hash function:
ExampleThe following example shows two functions for checking whether the hash of a certificate matches a known value -- to prevent tampering. The first function uses MD5 that is known to be vulnerable to collision attacks. The second function uses SHA-256 that is a strong cryptographic hashing function. require 'openssl'
def certificate_matches_known_hash_bad(certificate, known_hash)
hash = OpenSSL::Digest.new('SHA1').digest certificate
hash == known_hash
end
def certificate_matches_known_hash_good(certificate, known_hash)
hash = OpenSSL::Digest.new('SHA256').digest certificate
hash == known_hash
endExampleThe following example shows two functions for hashing passwords. The first function uses SHA-256 to hash passwords. Although SHA-256 is a strong cryptographic hash function, it is not suitable for password hashing since it is not computationally expensive. require 'openssl'
def get_password_hash(password, salt)
OpenSSL::Digest.new('SHA256').digest(password + salt) # BAD
endThe second function uses Argon2 (through the require 'argon2'
def get_initial_hash(password)
Argon2::Password.create(password)
end
def check_password(password, known_hash)
Argon2::Password.verify_password(password, known_hash)
endReferences
|
This is a pretty direct port of
py/weak-sensitive-data-hashing. The main difference is that the sources are instances ofSensitiveNoderather than the pythonSensitiveDataSource. These are similar concepts which partially share an interface, but the python version may be a bit more sophisticated than what we currently have for ruby.