|
| 1 | +/** |
| 2 | + * @name Main Method in Java EE Web Components |
| 3 | + * @description Java EE web applications with a main method. |
| 4 | + * @kind problem |
| 5 | + * @id java/main-method-in-web-components |
| 6 | + * @tags security |
| 7 | + * external/cwe-489 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | +import semmle.code.java.frameworks.Servlets |
| 12 | +import MainLib |
| 13 | + |
| 14 | +/** The java type `javax.servlet.Filter`. */ |
| 15 | +class ServletFilterClass extends Class { |
| 16 | + ServletFilterClass() { this.getASupertype*().hasQualifiedName("javax.servlet", "Filter") } |
| 17 | +} |
| 18 | + |
| 19 | +/** Listener class in the package `javax.servlet` and `javax.servlet.http` */ |
| 20 | +class ServletListenerClass extends Class { |
| 21 | + // Various listener classes of Java EE such as ServletContextListener. They all have a name ending with the word "Listener". |
| 22 | + ServletListenerClass() { |
| 23 | + this.getASupertype*() |
| 24 | + .getQualifiedName() |
| 25 | + .regexpMatch([ |
| 26 | + "javax\\.servlet\\.[a-zA-Z]+Listener", "javax\\.servlet\\.http\\.[a-zA-Z]+Listener" |
| 27 | + ]) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** The `main` method in `Servlet` and `Action` of the Spring and Struts framework. */ |
| 32 | +class WebComponentMainMethod extends Method { |
| 33 | + WebComponentMainMethod() { |
| 34 | + ( |
| 35 | + this.getDeclaringType() instanceof ServletClass or |
| 36 | + this.getDeclaringType() instanceof ServletFilterClass or |
| 37 | + this.getDeclaringType() instanceof ServletListenerClass or |
| 38 | + this.getDeclaringType() |
| 39 | + .getASupertype*() |
| 40 | + .hasQualifiedName("org.apache.struts.action", "Action") or // Struts actions |
| 41 | + this.getDeclaringType() |
| 42 | + .getASupertype+() |
| 43 | + .hasQualifiedName("com.opensymphony.xwork2", "ActionSupport") or // Struts 2 actions |
| 44 | + this.getDeclaringType() |
| 45 | + .getASupertype+() |
| 46 | + .hasQualifiedName("org.springframework.web.struts", "ActionSupport") or // Spring/Struts 2 actions |
| 47 | + this.getDeclaringType() |
| 48 | + .getASupertype+() |
| 49 | + .hasQualifiedName("org.springframework.webflow.execution", "Action") // Spring actions |
| 50 | + ) and |
| 51 | + isMainMethod(this) and |
| 52 | + not isTestMethod(this) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +from WebComponentMainMethod sm |
| 57 | +select sm, "Web application has a main method." |
0 commit comments