Skip to content

Commit e34a203

Browse files
committed
Refactor the check of a main method in a test program to improve maintainability
1 parent 45f9125 commit e34a203

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

java/ql/src/experimental/Security/CWE/CWE-489/EJBMain.ql

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@
99

1010
import java
1111
import semmle.code.java.J2EE
12+
import MainLib
1213

1314
/** The `main` method in an Enterprise Java Bean. */
1415
class EnterpriseBeanMainMethod extends Method {
1516
EnterpriseBeanMainMethod() {
1617
this.getDeclaringType() instanceof EnterpriseBean and
17-
this.hasName("main") and
18-
this.isStatic() and
19-
this.getReturnType() instanceof VoidType and
20-
this.isPublic() and
21-
this.getNumberOfParameters() = 1 and
22-
this.getParameter(0).getType() instanceof Array and
23-
not this.getDeclaringType().getName().toLowerCase().matches("%test%") and // Simple check to exclude test classes to reduce FPs
24-
not this.getDeclaringType().getPackage().getName().toLowerCase().matches("%test%") and // Simple check to exclude classes in test packages to reduce FPs
25-
not exists(this.getLocation().getFile().getAbsolutePath().indexOf("/src/test/java")) // Match test directory structure of build tools like maven
18+
isMainMethod(this) and
19+
not isTestMethod(this)
2620
}
2721
}
2822

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** Definitions related to the main method in a test program. */
2+
3+
import java
4+
5+
/** Holds if `m` is the main method of a Java class with the signature `public static void main(String[] args)`. */
6+
predicate isMainMethod(Method m) {
7+
m.hasName("main") and
8+
m.isStatic() and
9+
m.getReturnType() instanceof VoidType and
10+
m.isPublic() and
11+
m.getNumberOfParameters() = 1 and
12+
m.getParameter(0).getType() instanceof Array
13+
}
14+
15+
/**
16+
* Holds if `m` is a test method indicated by:
17+
* a) in a test directory such as `src/test/java`
18+
* b) in a test package whose name has the word `test`
19+
* c) in a test class whose name has the word `test`
20+
* d) in a test class implementing a test framework such as JUnit or TestNG
21+
*/
22+
predicate isTestMethod(Method m) {
23+
m.getDeclaringType().getName().toLowerCase().matches("%test%") or // Simple check to exclude test classes to reduce FPs
24+
m.getDeclaringType().getPackage().getName().toLowerCase().matches("%test%") or // Simple check to exclude classes in test packages to reduce FPs
25+
exists(m.getLocation().getFile().getAbsolutePath().indexOf("/src/test/java")) or // Match test directory structure of build tools like maven
26+
m instanceof TestMethod // Test method of a test case implementing a test framework such as JUnit or TestNG
27+
}

java/ql/src/experimental/Security/CWE/CWE-489/WebComponentMain.ql

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java
1111
import semmle.code.java.frameworks.Servlets
12+
import MainLib
1213

1314
/** The java type `javax.servlet.Filter`. */
1415
class ServletFilterClass extends Class {
@@ -47,15 +48,8 @@ class WebComponentMainMethod extends Method {
4748
.getASupertype+()
4849
.hasQualifiedName("org.springframework.webflow.execution", "Action") // Spring actions
4950
) and
50-
this.hasName("main") and
51-
this.isStatic() and
52-
this.getReturnType() instanceof VoidType and
53-
this.isPublic() and
54-
this.getNumberOfParameters() = 1 and
55-
this.getParameter(0).getType() instanceof Array and
56-
not this.getDeclaringType().getName().toLowerCase().matches("%test%") and // Simple check to exclude test classes to reduce FPs
57-
not this.getDeclaringType().getPackage().getName().toLowerCase().matches("%test%") and // Simple check to exclude classes in test packages to reduce FPs
58-
not exists(this.getLocation().getFile().getAbsolutePath().indexOf("/src/test/java")) // Match test directory structure of build tools like maven
51+
isMainMethod(this) and
52+
not isTestMethod(this)
5953
}
6054
}
6155

0 commit comments

Comments
 (0)