File tree Expand file tree Collapse file tree 3 files changed +33
-18
lines changed
java/ql/src/experimental/Security/CWE/CWE-489 Expand file tree Collapse file tree 3 files changed +33
-18
lines changed Original file line number Diff line number Diff line change 99
1010import java
1111import semmle.code.java.J2EE
12+ import MainLib
1213
1314/** The `main` method in an Enterprise Java Bean. */
1415class 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 99
1010import java
1111import semmle.code.java.frameworks.Servlets
12+ import MainLib
1213
1314/** The java type `javax.servlet.Filter`. */
1415class 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
You can’t perform that action at this time.
0 commit comments