Skip to content

Commit cba81ee

Browse files
committed
Fix string/type match and add a test case
1 parent 6c24f36 commit cba81ee

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

java/ql/src/experimental/Security/CWE/CWE-297/InsecureJavaMail.ql

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ predicate isInsecureMailPropertyConfig(VarAccess propertiesVarAccess) {
3333
ma.getMethod() instanceof SetPropertyMethod and
3434
ma.getQualifier() = propertiesVarAccess.getVariable().getAnAccess() and
3535
(
36-
getStringValue(ma.getArgument(0)).indexOf(".auth") != -1 and //mail.smtp.auth
36+
getStringValue(ma.getArgument(0)).matches("%.auth%") and //mail.smtp.auth
3737
getStringValue(ma.getArgument(1)) = "true"
3838
or
39-
getStringValue(ma.getArgument(0)).indexOf(".socketFactory") != -1 //mail.smtp.socketFactory or mail.smtp.socketFactory.class
39+
getStringValue(ma.getArgument(0)).matches("%.socketFactory%") //mail.smtp.socketFactory or mail.smtp.socketFactory.class
4040
)
4141
) and
4242
not exists(MethodAccess ma |
4343
ma.getMethod() instanceof SetPropertyMethod and
4444
ma.getQualifier() = propertiesVarAccess.getVariable().getAnAccess() and
4545
(
46-
getStringValue(ma.getArgument(0)).indexOf(".ssl.checkserveridentity") != -1 and //mail.smtp.ssl.checkserveridentity
46+
getStringValue(ma.getArgument(0)).matches("%.ssl.checkserveridentity%") and //mail.smtp.ssl.checkserveridentity
4747
getStringValue(ma.getArgument(1)) = "true"
4848
)
4949
)
@@ -53,11 +53,7 @@ predicate isInsecureMailPropertyConfig(VarAccess propertiesVarAccess) {
5353
* Helper method to get string value of an argument
5454
*/
5555
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-
)
56+
result = expr.(CompileTimeConstantExpr).getStringValue()
6157
or
6258
result = getStringValue(expr.(AddExpr).getLeftOperand())
6359
or
@@ -68,14 +64,14 @@ string getStringValue(Expr expr) {
6864
* The JavaMail session class `javax.mail.Session`
6965
*/
7066
class MailSession extends RefType {
71-
MailSession() { this.getQualifiedName() = "javax.mail.Session" }
67+
MailSession() { this.hasQualifiedName("javax.mail", "Session") }
7268
}
7369

7470
/**
7571
* The class of Apache SimpleMail
7672
*/
7773
class SimpleMail extends RefType {
78-
SimpleMail() { this.getQualifiedName() = "org.apache.commons.mail.SimpleEmail" }
74+
SimpleMail() { this.hasQualifiedName("org.apache.commons.mail", "SimpleEmail") }
7975
}
8076

8177
/**
@@ -101,7 +97,7 @@ from MethodAccess ma
10197
where
10298
ma.getMethod().getDeclaringType() instanceof MailSession and
10399
ma.getMethod().getName() = "getInstance" and
104-
isInsecureMailPropertyConfig(ma.getArgument(0).(VarAccess))
100+
isInsecureMailPropertyConfig(ma.getArgument(0))
105101
or
106-
enableTLSWithSimpleMail(ma) and hasNoCertCheckWithSimpleMail(ma.getQualifier().(VarAccess))
107-
select ma, "Java mailing has insecure SSL configuration"
102+
enableTLSWithSimpleMail(ma) and hasNoCertCheckWithSimpleMail(ma.getQualifier())
103+
select ma, "Java mailing has insecure SSL configuration"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| InsecureJavaMail.java:33:28:33:73 | getInstance(...) | Java mailing has insecure SSL configuration |
2+
| InsecureJavaMail.java:41:3:41:29 | setSSLOnConnect(...) | Java mailing has insecure SSL configuration |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.Properties;
2+
3+
import javax.activation.DataSource;
4+
import javax.mail.Authenticator;
5+
import javax.mail.Message;
6+
import javax.mail.MessagingException;
7+
import javax.mail.PasswordAuthentication;
8+
import javax.mail.Session;
9+
10+
import org.apache.commons.mail.DefaultAuthenticator;
11+
import org.apache.commons.mail.Email;
12+
import org.apache.commons.mail.EmailException;
13+
import org.apache.commons.mail.SimpleEmail;
14+
15+
import java.util.Properties;
16+
17+
class InsecureJavaMail {
18+
public void testJavaMail() {
19+
final Properties properties = new Properties();
20+
properties.put("mail.transport.protocol", "protocol");
21+
properties.put("mail.smtp.host", "hostname");
22+
properties.put("mail.smtp.socketFactory.class", "classname");
23+
24+
final javax.mail.Authenticator authenticator = new javax.mail.Authenticator() {
25+
protected PasswordAuthentication getPasswordAuthentication() {
26+
return new PasswordAuthentication("username", "password");
27+
}
28+
};
29+
if (null != authenticator) {
30+
properties.put("mail.smtp.auth", "true");
31+
// properties.put("mail.smtp.ssl.checkserveridentity", "true");
32+
}
33+
final Session session = Session.getInstance(properties, authenticator);
34+
}
35+
36+
public void testSimpleMail() {
37+
Email email = new SimpleEmail();
38+
email.setHostName("config.hostName");
39+
email.setSmtpPort(25);
40+
email.setAuthenticator(new DefaultAuthenticator("config.username", "config.password"));
41+
email.setSSLOnConnect(true);
42+
// email.setSSLCheckServerIdentity(true);
43+
email.setFrom("fromAddress");
44+
email.setSubject("subject");
45+
email.setMsg("body");
46+
email.addTo("toAddress");
47+
email.send();
48+
}
49+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Security/CWE/CWE-297/InsecureJavaMail.ql

0 commit comments

Comments
 (0)