|
| 1 | +/** |
| 2 | + * @id java/insecure-smtp-ssl |
| 3 | + * @name Insecure JavaMail SSL Configuration |
| 4 | + * @description Java application configured to use authenticated mail session over SSL does not validate the SSL certificate to properly ensure that it is actually associated with that host. |
| 5 | + * @kind problem |
| 6 | + * @tags security |
| 7 | + * external/cwe-297 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | + |
| 12 | +/** |
| 13 | + * The method to set Java properties |
| 14 | + */ |
| 15 | +class SetPropertyMethod extends Method { |
| 16 | + SetPropertyMethod() { |
| 17 | + this.hasName("setProperty") and |
| 18 | + this.getDeclaringType().hasQualifiedName("java.util", "Properties") |
| 19 | + or |
| 20 | + this.hasName("put") and |
| 21 | + this.getDeclaringType().getASourceSupertype*().hasQualifiedName("java.util", "Dictionary") |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * The insecure way to set Java properties in mail sessions. |
| 27 | + * 1. Set the mail.smtp.auth property to provide the SMTP Transport with a username and password when connecting to the SMTP server or |
| 28 | + * set the mail.smtp.ssl.socketFactory/mail.smtp.ssl.socketFactory.class property to create an SMTP SSL socket. |
| 29 | + * 2. No mail.smtp.ssl.checkserveridentity property is enabled. |
| 30 | + */ |
| 31 | +predicate isInsecureMailPropertyConfig(VarAccess propertiesVarAccess) { |
| 32 | + exists(MethodAccess ma | |
| 33 | + ma.getMethod() instanceof SetPropertyMethod and |
| 34 | + ma.getQualifier() = propertiesVarAccess.getVariable().getAnAccess() and |
| 35 | + ( |
| 36 | + getStringValue(ma.getArgument(0)).indexOf(".auth") != -1 and //mail.smtp.auth |
| 37 | + getStringValue(ma.getArgument(1)) = "true" |
| 38 | + or |
| 39 | + getStringValue(ma.getArgument(0)).indexOf(".socketFactory") != -1 //mail.smtp.socketFactory or mail.smtp.socketFactory.class |
| 40 | + ) |
| 41 | + ) and |
| 42 | + not exists(MethodAccess ma | |
| 43 | + ma.getMethod() instanceof SetPropertyMethod and |
| 44 | + ma.getQualifier() = propertiesVarAccess.getVariable().getAnAccess() and |
| 45 | + ( |
| 46 | + getStringValue(ma.getArgument(0)).indexOf(".ssl.checkserveridentity") != -1 and //mail.smtp.ssl.checkserveridentity |
| 47 | + getStringValue(ma.getArgument(1)) = "true" |
| 48 | + ) |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Helper method to get string value of an argument |
| 54 | + */ |
| 55 | +string getStringValue(Expr expr) { |
| 56 | + result = expr.(StringLiteral).getRepresentedString() |
| 57 | + or |
| 58 | + exists(Variable v | expr = v.getAnAccess() | |
| 59 | + result = getStringValue(v.getInitializer().(CompileTimeConstantExpr)) |
| 60 | + ) |
| 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.getQualifiedName() = "javax.mail.Session" } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * The class of Apache SimpleMail |
| 76 | + */ |
| 77 | +class SimpleMail extends RefType { |
| 78 | + SimpleMail() { this.getQualifiedName() = "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 | +} |
| 99 | + |
| 100 | +from MethodAccess ma |
| 101 | +where |
| 102 | + ma.getMethod().getDeclaringType() instanceof MailSession and |
| 103 | + ma.getMethod().getName() = "getInstance" and |
| 104 | + isInsecureMailPropertyConfig(ma.getArgument(0).(VarAccess)) |
| 105 | + or |
| 106 | + enableTLSWithSimpleMail(ma) and hasNoCertCheckWithSimpleMail(ma.getQualifier().(VarAccess)) |
| 107 | +select ma, "Java mailing has insecure SSL configuration" |
0 commit comments