1+ /**
2+ * @name Insecure Cryptographic Implementation
3+ * @description Usage of weak cryptographic algorithms or improper implementations can lead to security vulnerabilities.
4+ * @kind problem
5+ * @problem.severity error
6+ * @precision high
7+ * @id js/insecure-crypto
8+ * @tags security
9+ * external/cwe/cwe-327
10+ */
11+
12+ import javascript
13+
14+ /**
15+ * Identifies calls to crypto functions with insecure algorithms
16+ */
17+ predicate isInsecureCryptoCall ( CallExpr call ) {
18+ // Node.js crypto module uses
19+ exists ( string methodName |
20+ methodName = call .getCalleeName ( ) and
21+ (
22+ // Detect MD5 usage
23+ methodName .regexpMatch ( "(?i).*md5.*" ) or
24+ methodName .regexpMatch ( "(?i).*sha1.*" ) or
25+
26+ // Insecure crypto constructors
27+ (
28+ methodName = "createHash" or
29+ methodName = "createCipheriv" or
30+ methodName = "createDecipher"
31+ ) and
32+ (
33+ exists ( StringLiteral algo |
34+ algo = call .getArgument ( 0 ) and
35+ (
36+ algo .getValue ( ) .regexpMatch ( "(?i).*(md5|md4|md2|sha1|des|rc4|blowfish).*" ) or
37+ algo .getValue ( ) .regexpMatch ( "(?i).*(ecb).*" ) // ECB mode
38+ )
39+ )
40+ )
41+ )
42+ )
43+ or
44+ // Browser crypto API uses
45+ exists ( MethodCallExpr mce , string propertyName |
46+ propertyName = mce .getMethodName ( ) and
47+ (
48+ propertyName = "subtle" and
49+ exists ( MethodCallExpr subtleCall |
50+ subtleCall .getReceiver ( ) = mce and
51+ subtleCall .getMethodName ( ) = "encrypt" and
52+ exists ( ObjectExpr obj |
53+ obj = subtleCall .getArgument ( 0 ) and
54+ exists ( Property p |
55+ p = obj .getAProperty ( ) and
56+ p .getName ( ) = "name" and
57+ exists ( StringLiteral algo |
58+ algo = p .getInit ( ) and
59+ algo .getValue ( ) .regexpMatch ( "(?i).*(rc4|des|aes-cbc).*" )
60+ )
61+ )
62+ )
63+ )
64+ )
65+ )
66+ }
67+
68+ /**
69+ * Identifies usage of Math.random() for security-sensitive operations
70+ */
71+ predicate isInsecureRandomCall ( CallExpr call ) {
72+ exists ( PropertyAccess prop |
73+ prop .getPropertyName ( ) = "random" and
74+ prop .getBase ( ) .toString ( ) = "Math" and
75+ call .getCallee ( ) = prop
76+ )
77+ }
78+
79+ from Expr insecureExpr , string message
80+ where
81+ (
82+ insecureExpr instanceof CallExpr and
83+ isInsecureCryptoCall ( insecureExpr ) and
84+ message = "Using potentially insecure cryptographic algorithm or mode."
85+ ) or (
86+ insecureExpr instanceof CallExpr and
87+ isInsecureRandomCall ( insecureExpr ) and
88+ message = "Using Math.random() for security-sensitive operation. Consider using crypto.getRandomValues() instead."
89+ )
90+ select insecureExpr , message
0 commit comments