|
12 | 12 | */ |
13 | 13 |
|
14 | 14 | import java |
15 | | - |
16 | | -/** |
17 | | - * The method to set Java properties |
18 | | - */ |
19 | | -class SetPropertyMethod extends Method { |
20 | | - SetPropertyMethod() { |
21 | | - this.hasName("setProperty") and |
22 | | - this.getDeclaringType().hasQualifiedName("java.util", "Properties") |
23 | | - or |
24 | | - this.hasName("put") and |
25 | | - this.getDeclaringType().getASourceSupertype*().hasQualifiedName("java.util", "Dictionary") |
26 | | - } |
27 | | -} |
28 | | - |
29 | | -/** |
30 | | - * The insecure way to set Java properties in mail sessions. |
31 | | - * 1. Set the mail.smtp.auth property to provide the SMTP Transport with a username and password when connecting to the SMTP server or |
32 | | - * set the mail.smtp.ssl.socketFactory/mail.smtp.ssl.socketFactory.class property to create an SMTP SSL socket. |
33 | | - * 2. No mail.smtp.ssl.checkserveridentity property is enabled. |
34 | | - */ |
35 | | -predicate isInsecureMailPropertyConfig(VarAccess propertiesVarAccess) { |
36 | | - exists(MethodAccess ma | |
37 | | - ma.getMethod() instanceof SetPropertyMethod and |
38 | | - ma.getQualifier() = propertiesVarAccess.getVariable().getAnAccess() and |
39 | | - ( |
40 | | - getStringValue(ma.getArgument(0)).matches("%.auth%") and //mail.smtp.auth |
41 | | - getStringValue(ma.getArgument(1)) = "true" |
42 | | - or |
43 | | - getStringValue(ma.getArgument(0)).matches("%.socketFactory%") //mail.smtp.socketFactory or mail.smtp.socketFactory.class |
44 | | - ) |
45 | | - ) and |
46 | | - not exists(MethodAccess ma | |
47 | | - ma.getMethod() instanceof SetPropertyMethod and |
48 | | - ma.getQualifier() = propertiesVarAccess.getVariable().getAnAccess() and |
49 | | - ( |
50 | | - getStringValue(ma.getArgument(0)).matches("%.ssl.checkserveridentity%") and //mail.smtp.ssl.checkserveridentity |
51 | | - getStringValue(ma.getArgument(1)) = "true" |
52 | | - ) |
53 | | - ) |
54 | | -} |
55 | | - |
56 | | -/** |
57 | | - * Helper method to get string value of an argument |
58 | | - */ |
59 | | -string getStringValue(Expr expr) { |
60 | | - result = expr.(CompileTimeConstantExpr).getStringValue() |
61 | | - or |
62 | | - result = getStringValue(expr.(AddExpr).getLeftOperand()) |
63 | | - or |
64 | | - result = getStringValue(expr.(AddExpr).getRightOperand()) |
65 | | -} |
66 | | - |
67 | | -/** |
68 | | - * The JavaMail session class `javax.mail.Session` |
69 | | - */ |
70 | | -class MailSession extends RefType { |
71 | | - MailSession() { this.hasQualifiedName("javax.mail", "Session") } |
72 | | -} |
73 | | - |
74 | | -/** |
75 | | - * The class of Apache SimpleMail |
76 | | - */ |
77 | | -class SimpleMail extends RefType { |
78 | | - SimpleMail() { this.hasQualifiedName("org.apache.commons.mail", "SimpleEmail") } |
79 | | -} |
80 | | - |
81 | | -/** |
82 | | - * Has TLS/SSL enabled with SimpleMail |
83 | | - */ |
84 | | -predicate enableTLSWithSimpleMail(MethodAccess ma) { |
85 | | - ma.getMethod().hasName("setSSLOnConnect") and |
86 | | - ma.getArgument(0).(BooleanLiteral).getBooleanValue() = true |
87 | | -} |
88 | | - |
89 | | -/** |
90 | | - * Has no certificate check |
91 | | - */ |
92 | | -predicate hasNoCertCheckWithSimpleMail(VarAccess va) { |
93 | | - not exists(MethodAccess ma | |
94 | | - ma.getQualifier() = va.getVariable().getAnAccess() and |
95 | | - ma.getMethod().hasName("setSSLCheckServerIdentity") and |
96 | | - ma.getArgument(0).(BooleanLiteral).getBooleanValue() = true |
97 | | - ) |
98 | | -} |
| 15 | +import semmle.code.java.security.Mail |
99 | 16 |
|
100 | 17 | from MethodAccess ma |
101 | 18 | where |
102 | | - ma.getMethod().getDeclaringType() instanceof MailSession and |
103 | | - ma.getMethod().getName() = "getInstance" and |
| 19 | + ma.getMethod() instanceof MailSessionGetInstanceMethod and |
104 | 20 | isInsecureMailPropertyConfig(ma.getArgument(0)) |
105 | 21 | or |
106 | | - enableTLSWithSimpleMail(ma) and hasNoCertCheckWithSimpleMail(ma.getQualifier()) |
| 22 | + enablesEmailSsl(ma) and not hasSslCertificateCheck(ma.getQualifier()) |
107 | 23 | select ma, "Java mailing has insecure SSL configuration" |
0 commit comments