Skip to content

Commit ecafce8

Browse files
committed
improve the CryptoJS model by using API::Node
1 parent 7768026 commit ecafce8

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

javascript/ql/lib/semmle/javascript/frameworks/CryptoLibraries.qll

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -279,23 +279,18 @@ private module CryptoJS {
279279
/**
280280
* Matches `CryptoJS.<algorithmName>` and `require("crypto-js/<algorithmName>")`
281281
*/
282-
private DataFlow::SourceNode getAlgorithmNode(CryptographicAlgorithm algorithm) {
282+
private API::Node getAlgorithmNode(CryptographicAlgorithm algorithm) {
283283
exists(string algorithmName | algorithm.matchesName(algorithmName) |
284-
exists(DataFlow::SourceNode mod | mod = DataFlow::moduleImport("crypto-js") |
285-
result = mod.getAPropertyRead(algorithmName) or
286-
result = mod.getAPropertyRead("Hmac" + algorithmName) // they prefix Hmac
284+
exists(API::Node mod | mod = API::moduleImport("crypto-js") |
285+
result = mod.getMember(algorithmName) or
286+
result = mod.getMember("Hmac" + algorithmName) // they prefix Hmac
287287
)
288288
or
289-
exists(DataFlow::SourceNode mod |
290-
mod = DataFlow::moduleImport("crypto-js/" + algorithmName) and
291-
result = mod
292-
)
289+
result = API::moduleImport("crypto-js/" + algorithmName)
293290
)
294291
}
295292

296-
private DataFlow::CallNode getEncryptionApplication(
297-
DataFlow::Node input, CryptographicAlgorithm algorithm
298-
) {
293+
private API::CallNode getEncryptionApplication(API::Node input, CryptographicAlgorithm algorithm) {
299294
/*
300295
* ```
301296
* var CryptoJS = require("crypto-js");
@@ -309,13 +304,11 @@ private module CryptoJS {
309304
* Also matches where `CryptoJS.<algorithmName>` has been replaced by `require("crypto-js/<algorithmName>")`
310305
*/
311306

312-
result = getAlgorithmNode(algorithm).getAMemberCall("encrypt") and
313-
input = result.getArgument(0)
307+
result = getAlgorithmNode(algorithm).getMember("encrypt").getACall() and
308+
input = result.getParameter(0)
314309
}
315310

316-
private DataFlow::CallNode getDirectApplication(
317-
DataFlow::Node input, CryptographicAlgorithm algorithm
318-
) {
311+
private API::CallNode getDirectApplication(API::Node input, CryptographicAlgorithm algorithm) {
319312
/*
320313
* ```
321314
* var CryptoJS = require("crypto-js");
@@ -331,28 +324,27 @@ private module CryptoJS {
331324
*/
332325

333326
result = getAlgorithmNode(algorithm).getACall() and
334-
input = result.getArgument(0)
327+
input = result.getParameter(0)
335328
}
336329

337-
private class Apply extends CryptographicOperation::Range, DataFlow::CallNode {
338-
DataFlow::Node input;
330+
private class Apply extends CryptographicOperation::Range instanceof API::CallNode {
331+
API::Node input;
339332
CryptographicAlgorithm algorithm; // non-functional
340333

341334
Apply() {
342335
this = getEncryptionApplication(input, algorithm) or
343336
this = getDirectApplication(input, algorithm)
344337
}
345338

346-
override DataFlow::Node getAnInput() { result = input }
339+
override DataFlow::Node getAnInput() { result = input.asSink() }
347340

348341
override CryptographicAlgorithm getAlgorithm() { result = algorithm }
349342

350343
// e.g. CryptoJS.AES.encrypt("msg", "key", { mode: CryptoJS.mode.<modeString> })
351344
private BlockMode getExplicitBlockMode() {
352-
exists(DataFlow::ObjectLiteralNode o, DataFlow::SourceNode modeNode, string modeString |
353-
modeNode = API::moduleImport("crypto-js").getMember("mode").getMember(modeString).asSource() and
354-
o.flowsTo(this.getArgument(2)) and
355-
modeNode = o.getAPropertySource("mode")
345+
exists(string modeString |
346+
API::moduleImport("crypto-js").getMember("mode").getMember(modeString).asSource() =
347+
super.getParameter(2).getMember("mode").asSink()
356348
|
357349
result.matchesString(modeString)
358350
)
@@ -372,15 +364,13 @@ private module CryptoJS {
372364

373365
private class Key extends CryptographicKey {
374366
Key() {
375-
exists(DataFlow::SourceNode e, CryptographicAlgorithm algorithm |
376-
e = getAlgorithmNode(algorithm)
377-
|
367+
exists(API::Node e, CryptographicAlgorithm algorithm | e = getAlgorithmNode(algorithm) |
378368
exists(string name |
379369
name = "encrypt" or
380370
name = "decrypt"
381371
|
382372
algorithm instanceof EncryptionAlgorithm and
383-
this = e.getAMemberCall(name).getArgument(1)
373+
this = e.getMember(name).getACall().getArgument(1)
384374
)
385375
or
386376
algorithm instanceof HashingAlgorithm and

0 commit comments

Comments
 (0)