|
| 1 | +import semmle.javascript.dataflow.TaintTracking |
| 2 | + |
| 3 | +import github.CommandLine |
| 4 | + |
| 5 | +class RandomTaintsSourceConfiguration extends TaintTracking::Configuration { |
| 6 | + RandomTaintsSourceConfiguration() { this = "RandomTaintsSourceConfiguration" } |
| 7 | + |
| 8 | + override predicate isSource(DataFlow::Node source) { |
| 9 | + isSecureRandom(source) |
| 10 | + } |
| 11 | + |
| 12 | + override predicate isSink(DataFlow::Node sink) { |
| 13 | + not isSecureRandom(sink) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +class InsecureIVConfiguration extends TaintTracking::Configuration { |
| 18 | + InsecureIVConfiguration() { this = "InsecureIVConfiguration" } |
| 19 | + |
| 20 | + override predicate isSource(DataFlow::Node source) { |
| 21 | + exists(Literal literal|literal.flow() = source) |
| 22 | + or |
| 23 | + source instanceof DataFlow::ArrayLiteralNode |
| 24 | + or |
| 25 | + source instanceof RemoteFlowSource |
| 26 | + or |
| 27 | + source instanceof FileSystemReadAccess |
| 28 | + or |
| 29 | + source instanceof DatabaseAccess |
| 30 | + or |
| 31 | + source instanceof CommandLineArgument |
| 32 | + or |
| 33 | + // an external function that is not a known source of randomness |
| 34 | + ( |
| 35 | + source instanceof ExternalCallWithOutput |
| 36 | + and not source instanceof CreateIVArgument |
| 37 | + and not source instanceof SecureRandomSource |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + override predicate isSink(DataFlow::Node sink) { |
| 42 | + sink instanceof CreateIVArgument |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class ExternalCallWithOutput extends DataFlow::Node { |
| 47 | + CallExpr call; |
| 48 | + |
| 49 | + ExternalCallWithOutput() { |
| 50 | + not exists(MethodCallExpr method_call, ThisExpr this_expr| method_call = call and method_call.getReceiver() = this_expr ) |
| 51 | + and |
| 52 | + this = call.flow() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +class SecureRandomSource extends DataFlow::Node { |
| 57 | + SecureRandomSource() { |
| 58 | + isSecureRandom(this) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +predicate isSecureRandom(DataFlow::Node node) { |
| 63 | + exists(string name| |
| 64 | + name in ["randomBytes", "getRandomValues"] and |
| 65 | + DataFlow::moduleMember("crypto", name).getACall() = node |
| 66 | + ) |
| 67 | + or |
| 68 | + exists(string name| |
| 69 | + name in ["randomFill", "randomFillSync"] and |
| 70 | + DataFlow::moduleMember("crypto", name).getACall().getArgument(0) = node |
| 71 | + ) |
| 72 | + or |
| 73 | + exists(string name| |
| 74 | + name in ["randomKey", "randomString"] and |
| 75 | + DataFlow::moduleMember("crypto-extra", name).getACall() = node |
| 76 | + ) |
| 77 | + or |
| 78 | + exists(string name| |
| 79 | + name in ["cryptoRandomString", "cryptoRandomStringAsync"] and |
| 80 | + DataFlow::moduleMember("crypto-random-string", name).getACall() = node |
| 81 | + ) |
| 82 | + or |
| 83 | + exists(string name| |
| 84 | + name in ["secureRandom", "randomArray", "randomUint8Array", "randomBuffer"] and |
| 85 | + DataFlow::moduleMember("secure-random", name).getACall() = node |
| 86 | + ) |
| 87 | +} |
| 88 | + |
| 89 | +class CreateIVArgument extends DataFlow::Node { |
| 90 | + CreateIVArgument() { |
| 91 | + isCreateIV(this) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +predicate isCreateIV(DataFlow::Node node) { |
| 96 | + exists(string name| |
| 97 | + name = "createCipheriv" and |
| 98 | + DataFlow::moduleMember("crypto", name).getACall().getArgument(2) = node |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +predicate knownCryptTest(DataFlow::Node sink) { |
| 103 | + sink.getFile().getRelativePath().matches( |
| 104 | + [ |
| 105 | + "%/des.js/test/%", |
| 106 | + "test/common/tls.js", |
| 107 | + "test/%/test-crypto-%.js", |
| 108 | + "%/browserify-aes/populateFixtures.js", |
| 109 | + "%/evp_bytestokey%/test.js", |
| 110 | + "%/sshpk/lib/formats/ssh-private.js" |
| 111 | + ] |
| 112 | + ) |
| 113 | +} |
0 commit comments