Skip to content

Commit e247694

Browse files
committed
add model for the core Digest module
1 parent 7c912ea commit e247694

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

ruby/ql/lib/codeql/ruby/frameworks/Core.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import core.Hash
1414
import core.String
1515
import core.Regexp
1616
import core.IO
17+
import core.Digest
1718

1819
/**
1920
* A system command executed via subshell literal syntax.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Provides modeling for the `Digest` module.
3+
*/
4+
5+
private import codeql.ruby.ApiGraphs
6+
private import codeql.ruby.Concepts
7+
private import codeql.ruby.DataFlow
8+
9+
/** Gets an API node for a Digest class that hashes using `algo`. */
10+
private API::Node digest(Cryptography::HashingAlgorithm algo) {
11+
exists(string name | result = API::getTopLevelMember("Digest").getMember(name) |
12+
name = ["MD5", "SHA1", "SHA2", "RMD160"] and
13+
algo.matchesName(name)
14+
)
15+
}
16+
17+
/** A call that hashes some input using a hashing algorithm from the `Digest` module. */
18+
private class DigestCall extends Cryptography::CryptographicOperation::Range instanceof DataFlow::CallNode {
19+
Cryptography::HashingAlgorithm algo;
20+
21+
DigestCall() {
22+
this = digest(algo).getAMethodCall(["hexdigest", "base64digest", "bubblebabble"])
23+
or
24+
this = digest(algo).getAMethodCall("file") // it's directly hashing the contents of a file, but that's close enough for us.
25+
or
26+
this = digest(algo).getMethod("new").getReturn().getAMethodCall(["digest", "update", "<<"])
27+
}
28+
29+
override Cryptography::HashingAlgorithm getAlgorithm() { result = algo }
30+
31+
override DataFlow::Node getAnInput() { result = super.getArgument(0) }
32+
33+
override Cryptography::BlockMode getBlockMode() { none() }
34+
}

ruby/ql/test/query-tests/security/cwe-327/BrokenCryptoAlgorithm.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@
1717
| broken_crypto.rb:75:1:75:24 | call to new | The cryptographic algorithm RC4 is broken or weak, and should not be used. |
1818
| broken_crypto.rb:77:1:77:29 | call to new | The cryptographic algorithm RC4 is broken or weak, and should not be used. |
1919
| broken_crypto.rb:79:1:79:35 | call to new | The cryptographic algorithm RC4 is broken or weak, and should not be used. |
20+
| broken_crypto.rb:81:1:81:28 | call to hexdigest | The cryptographic algorithm MD5 is broken or weak, and should not be used. |
21+
| broken_crypto.rb:84:1:84:31 | call to base64digest | The cryptographic algorithm MD5 is broken or weak, and should not be used. |
22+
| broken_crypto.rb:87:1:87:20 | call to digest | The cryptographic algorithm MD5 is broken or weak, and should not be used. |
23+
| broken_crypto.rb:89:1:89:21 | call to update | The cryptographic algorithm MD5 is broken or weak, and should not be used. |
24+
| broken_crypto.rb:90:1:90:17 | ... << ... | The cryptographic algorithm MD5 is broken or weak, and should not be used. |
25+
| broken_crypto.rb:95:1:95:34 | call to bubblebabble | The cryptographic algorithm MD5 is broken or weak, and should not be used. |
26+
| broken_crypto.rb:97:11:97:37 | call to file | The cryptographic algorithm MD5 is broken or weak, and should not be used. |

ruby/ql/test/query-tests/security/cwe-327/broken_crypto.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,24 @@
7777
OpenSSL::Cipher::RC4.new '40'
7878
# BAD: weak encryption algorithm
7979
OpenSSL::Cipher::RC4.new 'hmac-md5'
80+
81+
Digest::MD5.hexdigest('foo') # BAD: weak hash algorithm
82+
Digest::SHA256.hexdigest('foo') # GOOD: strong hash algorithm
83+
84+
Digest::MD5.base64digest('foo') # BAD: weak hash algorithm
85+
86+
md5 = Digest::MD5.new
87+
md5.digest 'message' # BAD: weak hash algorithm
88+
89+
md5.update 'message1' # BAD: weak hash algorithm
90+
md5 << 'message2' # << is an alias for update
91+
92+
sha256 = Digest::SHA256.new
93+
sha256.digest 'message' # GOOD: strong hash algorithm
94+
95+
Digest::MD5.bubblebabble 'message' # BAD: weak hash algorithm
96+
97+
filemd5 = Digest::MD5.file 'testfile'
98+
filemd5.hexdigest
99+
100+
Digest("MD5").hexdigest('foo') # BAD: weak hash algorithm

0 commit comments

Comments
 (0)