@@ -149,7 +149,7 @@ private module BrowserIdCrypto {
149
149
* A model of the Node.js builtin crypto library.
150
150
*/
151
151
private module NodeJSCrypto {
152
- private class InstantiatedAlgorithm extends DataFlow :: CallNode {
152
+ private class InstantiatedAlgorithm extends API :: CallNode {
153
153
private string algorithmName ;
154
154
155
155
InstantiatedAlgorithm ( ) {
@@ -166,11 +166,11 @@ private module NodeJSCrypto {
166
166
* Also matches `createHash`, `createHmac`, `createSign` instead of `createCipher`.
167
167
*/
168
168
169
- exists ( DataFlow :: SourceNode mod |
170
- mod = DataFlow :: moduleImport ( "crypto" ) and
171
- this = mod . getAMemberCall ( "create" + [ "Hash" , "Hmac" , "Sign" , "Cipher" ] ) and
172
- algorithmName = this . getArgument ( 0 ) . getStringValue ( )
173
- )
169
+ this =
170
+ API :: moduleImport ( "crypto" )
171
+ . getMember ( "create" + [ "Hash" , "Hmac" , "Sign" , "Cipher" ] )
172
+ . getACall ( ) and
173
+ algorithmName = this . getArgument ( 0 ) . getStringValue ( )
174
174
}
175
175
176
176
CryptographicAlgorithm getAlgorithm ( ) { result .matchesName ( algorithmName ) }
@@ -197,13 +197,12 @@ private module NodeJSCrypto {
197
197
// crypto.generateKeyPair(type, options, callback)
198
198
// crypto.generateKeyPairSync(type, options)
199
199
// crypto.generateKeySync(type, options)
200
- exists ( DataFlow :: SourceNode mod , string keyType |
200
+ exists ( string keyType |
201
201
keyType = "Key" and symmetric = true
202
202
or
203
203
keyType = "KeyPair" and symmetric = false
204
204
|
205
- mod = DataFlow:: moduleImport ( "crypto" ) and
206
- this = mod .getAMemberCall ( "generate" + keyType + [ "" , "Sync" ] )
205
+ this = API:: moduleImport ( "crypto" ) .getMember ( "generate" + keyType + [ "" , "Sync" ] ) .getACall ( )
207
206
)
208
207
}
209
208
@@ -249,17 +248,15 @@ private module NodeJSCrypto {
249
248
250
249
private class Key extends CryptographicKey {
251
250
Key ( ) {
252
- exists ( InstantiatedAlgorithm instantiation , string name |
253
- name = "setPrivateKey" or
254
- name = "sign"
255
- |
256
- this = instantiation . getAMethodCall ( name ) . getArgument ( 0 )
257
- )
251
+ this =
252
+ any ( InstantiatedAlgorithm i )
253
+ . getReturn ( )
254
+ . getMember ( [ "setPrivateKey" , "sign" ] )
255
+ . getParameter ( 0 )
256
+ . asSink ( )
258
257
or
259
- exists ( DataFlow:: SourceNode mod , string name , DataFlow:: InvokeNode call , int index |
260
- mod = DataFlow:: moduleImport ( "crypto" ) and
261
- call = mod .getAMemberCall ( name ) and
262
- this = call .getArgument ( index )
258
+ exists ( string name , int index |
259
+ this = API:: moduleImport ( "crypto" ) .getMember ( name ) .getACall ( ) .getArgument ( index )
263
260
|
264
261
index = 0 and
265
262
( name = "privateDecrypt" or name = "privateEncrypt" )
@@ -497,13 +494,12 @@ private module TweetNaCl {
497
494
* Also matches the "hash" method name, and the "nacl-fast" module.
498
495
*/
499
496
500
- exists ( DataFlow :: SourceNode mod , string name |
497
+ exists ( string name |
501
498
name = "hash" and algorithm .matchesName ( "SHA512" )
502
499
or
503
500
name = "sign" and algorithm .matchesName ( "ed25519" )
504
501
|
505
- ( mod = DataFlow:: moduleImport ( "nacl" ) or mod = DataFlow:: moduleImport ( "nacl-fast" ) ) and
506
- this = mod .getAMemberCall ( name ) and
502
+ this = API:: moduleImport ( [ "nacl" , "nacl-fast" ] ) .getMember ( name ) .getACall ( ) and
507
503
super .getArgument ( 0 ) = input
508
504
)
509
505
}
@@ -529,17 +525,13 @@ private module HashJs {
529
525
*/
530
526
private DataFlow:: CallNode getAlgorithmNode ( CryptographicAlgorithm algorithm ) {
531
527
exists ( string algorithmName | algorithm .matchesName ( algorithmName ) |
532
- result = DataFlow :: moduleMember ( "hash.js" , algorithmName ) .getACall ( )
528
+ result = API :: moduleImport ( "hash.js" ) . getMember ( algorithmName ) .getACall ( )
533
529
or
534
- exists ( DataFlow:: SourceNode mod |
535
- mod = DataFlow:: moduleImport ( "hash.js/lib/hash/" + algorithmName )
536
- or
537
- exists ( string size |
538
- mod = DataFlow:: moduleImport ( "hash.js/lib/hash/sha/" + size ) and
539
- algorithmName = "SHA" + size
540
- )
541
- |
542
- result = mod .getACall ( )
530
+ result = API:: moduleImport ( "hash.js/lib/hash/" + algorithmName ) .getACall ( )
531
+ or
532
+ exists ( string size |
533
+ result = API:: moduleImport ( "hash.js/lib/hash/sha/" + size ) .getACall ( ) and
534
+ algorithmName = "SHA" + size
543
535
)
544
536
)
545
537
}
@@ -579,10 +571,7 @@ private module HashJs {
579
571
* A model of the forge library.
580
572
*/
581
573
private module Forge {
582
- private DataFlow:: SourceNode getAnImportNode ( ) {
583
- result = DataFlow:: moduleImport ( "forge" ) or
584
- result = DataFlow:: moduleImport ( "node-forge" )
585
- }
574
+ private API:: Node getAnImportNode ( ) { result = API:: moduleImport ( [ "forge" , "node-forge" ] ) }
586
575
587
576
abstract private class Cipher extends DataFlow:: CallNode {
588
577
abstract CryptographicAlgorithm getAlgorithm ( ) ;
@@ -594,14 +583,14 @@ private module Forge {
594
583
private string blockModeString ;
595
584
596
585
KeyCipher ( ) {
597
- exists ( DataFlow:: SourceNode mod , string algorithmName |
598
- mod = getAnImportNode ( ) and
599
- algorithm .matchesName ( algorithmName )
600
- |
601
- exists ( string createName , string cipherName , string cipherPrefix |
586
+ exists ( string algorithmName | algorithm .matchesName ( algorithmName ) |
587
+ exists ( string cipherName , string cipherPrefix |
602
588
// `require('forge').cipher.createCipher("3DES-CBC").update("secret", "key");`
603
- ( createName = "createCipher" or createName = "createDecipher" ) and
604
- this = mod .getAPropertyRead ( "cipher" ) .getAMemberCall ( createName ) and
589
+ this =
590
+ getAnImportNode ( )
591
+ .getMember ( "cipher" )
592
+ .getMember ( [ "createCipher" , "createDecipher" ] )
593
+ .getACall ( ) and
605
594
this .getArgument ( 0 ) .mayHaveStringValue ( cipherName ) and
606
595
cipherName = cipherPrefix + "-" + blockModeString and
607
596
blockModeString = [ "CBC" , "CFB" , "CTR" , "ECB" , "GCM" , "OFB" ] and
@@ -610,13 +599,13 @@ private module Forge {
610
599
)
611
600
or
612
601
// `require("forge").rc2.createEncryptionCipher("key").update("secret");`
613
- exists ( string createName |
614
- createName = "createEncryptionCipher" or createName = "createDecryptionCipher"
615
- |
616
- this = mod . getAPropertyRead ( algorithmName ) . getAMemberCall ( createName ) and
617
- key = this . getArgument ( 0 ) and
618
- blockModeString = algorithmName
619
- )
602
+ this =
603
+ getAnImportNode ( )
604
+ . getMember ( algorithmName )
605
+ . getMember ( [ "createEncryptionCipher" , "createDecryptionCipher" ] )
606
+ . getACall ( ) and
607
+ key = this . getArgument ( 0 ) and
608
+ blockModeString = algorithmName
620
609
)
621
610
}
622
611
@@ -637,10 +626,7 @@ private module Forge {
637
626
exists ( string algorithmName | algorithm .matchesName ( algorithmName ) |
638
627
// require("forge").md.md5.create().update('The quick brown fox jumps over the lazy dog');
639
628
this =
640
- getAnImportNode ( )
641
- .getAPropertyRead ( "md" )
642
- .getAPropertyRead ( algorithmName )
643
- .getAMemberCall ( "create" )
629
+ getAnImportNode ( ) .getMember ( "md" ) .getMember ( algorithmName ) .getMember ( "create" ) .getACall ( )
644
630
)
645
631
}
646
632
@@ -676,15 +662,17 @@ private module Forge {
676
662
// var cipher = forge.rc2.createEncryptionCipher(key, 128);
677
663
this =
678
664
getAnImportNode ( )
679
- .getAPropertyRead ( any ( string s | algorithm .matchesName ( s ) ) )
680
- .getAMemberCall ( "createEncryptionCipher" )
665
+ .getMember ( any ( string s | algorithm .matchesName ( s ) ) )
666
+ .getMember ( "createEncryptionCipher" )
667
+ .getACall ( )
681
668
or
682
669
// var key = forge.random.getBytesSync(16);
683
670
// var cipher = forge.cipher.createCipher('AES-CBC', key);
684
671
this =
685
672
getAnImportNode ( )
686
- .getAPropertyRead ( "cipher" )
687
- .getAMemberCall ( [ "createCipher" , "createDecipher" ] ) and
673
+ .getMember ( "cipher" )
674
+ .getMember ( [ "createCipher" , "createDecipher" ] )
675
+ .getACall ( ) and
688
676
algorithm .matchesName ( this .getArgument ( 0 ) .getStringValue ( ) )
689
677
}
690
678
@@ -713,12 +701,9 @@ private module Md5 {
713
701
714
702
Apply ( ) {
715
703
// `require("md5")("message");`
716
- exists ( DataFlow:: SourceNode mod |
717
- mod = DataFlow:: moduleImport ( "md5" ) and
718
- algorithm .matchesName ( "MD5" ) and
719
- this = mod .getACall ( ) and
720
- super .getArgument ( 0 ) = input
721
- )
704
+ algorithm .matchesName ( "MD5" ) and
705
+ this = API:: moduleImport ( "md5" ) .getACall ( ) and
706
+ super .getArgument ( 0 ) = input
722
707
}
723
708
724
709
override DataFlow:: Node getAnInput ( ) { result = input }
@@ -740,21 +725,12 @@ private module Bcrypt {
740
725
741
726
Apply ( ) {
742
727
// `require("bcrypt").hash(password);` with minor naming variations
743
- exists ( DataFlow:: SourceNode mod , string moduleName , string methodName |
744
- algorithm .matchesName ( "BCRYPT" ) and
745
- (
746
- moduleName = "bcrypt" or
747
- moduleName = "bcryptjs" or
748
- moduleName = "bcrypt-nodejs"
749
- ) and
750
- (
751
- methodName = "hash" or
752
- methodName = "hashSync"
753
- ) and
754
- mod = DataFlow:: moduleImport ( moduleName ) and
755
- this = mod .getAMemberCall ( methodName ) and
756
- super .getArgument ( 0 ) = input
757
- )
728
+ algorithm .matchesName ( "BCRYPT" ) and
729
+ this =
730
+ API:: moduleImport ( [ "bcrypt" , "bcryptjs" , "bcrypt-nodejs" ] )
731
+ .getMember ( [ "hash" , "hashSync" ] )
732
+ .getACall ( ) and
733
+ super .getArgument ( 0 ) = input
758
734
}
759
735
760
736
override DataFlow:: Node getAnInput ( ) { result = input }
@@ -776,13 +752,11 @@ private module Hasha {
776
752
777
753
Apply ( ) {
778
754
// `require('hasha')('unicorn', { algorithm: "md5" });`
779
- exists ( DataFlow:: SourceNode mod , string algorithmName , DataFlow:: Node algorithmNameNode |
780
- mod = DataFlow:: moduleImport ( "hasha" ) and
781
- this = mod .getACall ( ) and
755
+ exists ( string algorithmName |
756
+ this = API:: moduleImport ( "hasha" ) .getACall ( ) and
782
757
super .getArgument ( 0 ) = input and
783
758
algorithm .matchesName ( algorithmName ) and
784
- super .getOptionArgument ( 1 , "algorithm" ) = algorithmNameNode and
785
- algorithmNameNode .mayHaveStringValue ( algorithmName )
759
+ super .getOptionArgument ( 1 , "algorithm" ) .mayHaveStringValue ( algorithmName )
786
760
)
787
761
}
788
762
@@ -800,7 +774,7 @@ private module Hasha {
800
774
*/
801
775
private module ExpressJwt {
802
776
private class Key extends CryptographicKey {
803
- Key ( ) { this = DataFlow :: moduleMember ( "express-jwt" , "sign" ) .getACall ( ) .getArgument ( 1 ) }
777
+ Key ( ) { this = API :: moduleImport ( "express-jwt" ) . getMember ( "sign" ) .getACall ( ) .getArgument ( 1 ) }
804
778
}
805
779
}
806
780
0 commit comments