-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Java: CWE-489 Query to detect main() method in servlets #5102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a183b00
Query to detect main method in servlets
luchua-bc 5e36eed
Add check for test packages
luchua-bc e916ce8
Exclude test directories of typical build tools
luchua-bc 3d9ac0d
Add query for enterprise beans
luchua-bc dc79901
Add query for Struts and Spring actions
luchua-bc 40df01d
Update qldoc and method name
luchua-bc 9eb8ec7
Create a separate file for EJB check
luchua-bc 45f9125
Update test program
luchua-bc e34a203
Refactor the check of a main method in a test program to improve main…
luchua-bc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
java/ql/src/experimental/Security/CWE/CWE-489/EJBMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class EJBMain implements SessionBean { | ||
/** | ||
* Create the session bean (empty implementation) | ||
*/ | ||
public void ejbCreate() throws javax.ejb.CreateException { | ||
System.out.println("EJBMain:ejbCreate()"); | ||
} | ||
|
||
public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public void setSessionContext(SessionContext parm1) throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public String doService() { | ||
return null; | ||
} | ||
|
||
// BAD - Implement a main method in session bean. | ||
public static void main(String[] args) throws Exception { | ||
EJBMain b = new EJBMain(); | ||
b.doService(); | ||
} | ||
|
||
// GOOD - Not to have a main method in session bean. | ||
} |
27 changes: 27 additions & 0 deletions
27
java/ql/src/experimental/Security/CWE/CWE-489/EJBMain.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
<qhelp> | ||
|
||
<overview> | ||
<p>Debug code can create unintended entry points in a deployed Java EE web application therefore should never make into production. There is no reason to have a main method in a Java EE web application. Having a main method in the Java EE application increases the attack surface that an attacker can exploit to attack the application logic.</p> | ||
</overview> | ||
|
||
<recommendation> | ||
<p>Remove the main method from enterprise beans.</p> | ||
</recommendation> | ||
|
||
<example> | ||
<p>The following example shows two ways of implementing enterprise beans. In the 'BAD' case, a main method is implemented. In the 'GOOD' case, no main method is implemented.</p> | ||
<sample src="EJBMain.java" /> | ||
</example> | ||
|
||
<references> | ||
<li> | ||
SonarSource: | ||
<a href="https://rules.sonarsource.com/java/tag/owasp/RSPEC-2653">Web applications should not have a "main" method</a> | ||
</li> | ||
<li> | ||
Carnegie Mellon University: | ||
<a href="https://wiki.sei.cmu.edu/confluence/display/java/ENV06-J.+Production+code+must+not+contain+debugging+entry+points">ENV06-J. Production code must not contain debugging entry points</a> | ||
</li> | ||
</references> | ||
</qhelp> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @name Main Method in Enterprise Java Bean | ||
* @description Java EE applications with a main method. | ||
* @kind problem | ||
* @id java/main-method-in-enterprise-bean | ||
* @tags security | ||
* external/cwe-489 | ||
*/ | ||
|
||
import java | ||
import semmle.code.java.J2EE | ||
import MainLib | ||
|
||
/** The `main` method in an Enterprise Java Bean. */ | ||
class EnterpriseBeanMainMethod extends Method { | ||
EnterpriseBeanMainMethod() { | ||
this.getDeclaringType() instanceof EnterpriseBean and | ||
isMainMethod(this) and | ||
not isTestMethod(this) | ||
} | ||
} | ||
|
||
from EnterpriseBeanMainMethod sm | ||
select sm, "Java EE application has a main method." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** Definitions related to the main method in a test program. */ | ||
|
||
import java | ||
|
||
/** Holds if `m` is the main method of a Java class with the signature `public static void main(String[] args)`. */ | ||
predicate isMainMethod(Method m) { | ||
m.hasName("main") and | ||
m.isStatic() and | ||
m.getReturnType() instanceof VoidType and | ||
m.isPublic() and | ||
m.getNumberOfParameters() = 1 and | ||
m.getParameter(0).getType() instanceof Array | ||
} | ||
|
||
/** | ||
* Holds if `m` is a test method indicated by: | ||
* a) in a test directory such as `src/test/java` | ||
* b) in a test package whose name has the word `test` | ||
* c) in a test class whose name has the word `test` | ||
* d) in a test class implementing a test framework such as JUnit or TestNG | ||
*/ | ||
predicate isTestMethod(Method m) { | ||
m.getDeclaringType().getName().toLowerCase().matches("%test%") or // Simple check to exclude test classes to reduce FPs | ||
m.getDeclaringType().getPackage().getName().toLowerCase().matches("%test%") or // Simple check to exclude classes in test packages to reduce FPs | ||
exists(m.getLocation().getFile().getAbsolutePath().indexOf("/src/test/java")) or // Match test directory structure of build tools like maven | ||
m instanceof TestMethod // Test method of a test case implementing a test framework such as JUnit or TestNG | ||
} |
10 changes: 10 additions & 0 deletions
10
java/ql/src/experimental/Security/CWE/CWE-489/WebComponentMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class WebComponentMain implements Servlet { | ||
// BAD - Implement a main method in servlet. | ||
public static void main(String[] args) throws Exception { | ||
// Connect to my server | ||
URL url = new URL("https://www.example.com"); | ||
url.openConnection(); | ||
} | ||
|
||
// GOOD - Not to have a main method in servlet. | ||
} |
31 changes: 31 additions & 0 deletions
31
java/ql/src/experimental/Security/CWE/CWE-489/WebComponentMain.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
<qhelp> | ||
|
||
<overview> | ||
<p>Debug code can create unintended entry points in a deployed Java EE web application therefore should never make into production. There is no reason to have a main method in a Java EE web application. Having a main method in the Java EE application increases the attack surface that an attacker can exploit to attack the application logic.</p> | ||
</overview> | ||
|
||
<recommendation> | ||
<p>Remove the main method from web components including servlets, filters, and listeners.</p> | ||
</recommendation> | ||
|
||
<example> | ||
<p>The following example shows two ways of implementing web components. In the 'BAD' case, a main method is implemented. In the 'GOOD' case, no main method is implemented.</p> | ||
<sample src="WebComponentMain.java" /> | ||
</example> | ||
|
||
<references> | ||
<li> | ||
Fortify: | ||
<a href="https://vulncat.fortify.com/en/detail?id=desc.structural.java.j2ee_badpractices_leftover_debug_code">J2EE Bad Practices: Leftover Debug Code</a> | ||
</li> | ||
<li> | ||
SonarSource: | ||
<a href="https://rules.sonarsource.com/java/tag/owasp/RSPEC-2653">Web applications should not have a "main" method</a> | ||
</li> | ||
<li> | ||
Carnegie Mellon University: | ||
<a href="https://wiki.sei.cmu.edu/confluence/display/java/ENV06-J.+Production+code+must+not+contain+debugging+entry+points">ENV06-J. Production code must not contain debugging entry points</a> | ||
</li> | ||
</references> | ||
</qhelp> |
57 changes: 57 additions & 0 deletions
57
java/ql/src/experimental/Security/CWE/CWE-489/WebComponentMain.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @name Main Method in Java EE Web Components | ||
* @description Java EE web applications with a main method. | ||
* @kind problem | ||
* @id java/main-method-in-web-components | ||
* @tags security | ||
* external/cwe-489 | ||
*/ | ||
|
||
import java | ||
import semmle.code.java.frameworks.Servlets | ||
import MainLib | ||
|
||
/** The java type `javax.servlet.Filter`. */ | ||
class ServletFilterClass extends Class { | ||
ServletFilterClass() { this.getASupertype*().hasQualifiedName("javax.servlet", "Filter") } | ||
} | ||
|
||
/** Listener class in the package `javax.servlet` and `javax.servlet.http` */ | ||
class ServletListenerClass extends Class { | ||
// Various listener classes of Java EE such as ServletContextListener. They all have a name ending with the word "Listener". | ||
ServletListenerClass() { | ||
this.getASupertype*() | ||
.getQualifiedName() | ||
.regexpMatch([ | ||
"javax\\.servlet\\.[a-zA-Z]+Listener", "javax\\.servlet\\.http\\.[a-zA-Z]+Listener" | ||
]) | ||
} | ||
} | ||
|
||
/** The `main` method in `Servlet` and `Action` of the Spring and Struts framework. */ | ||
class WebComponentMainMethod extends Method { | ||
WebComponentMainMethod() { | ||
( | ||
this.getDeclaringType() instanceof ServletClass or | ||
this.getDeclaringType() instanceof ServletFilterClass or | ||
this.getDeclaringType() instanceof ServletListenerClass or | ||
this.getDeclaringType() | ||
.getASupertype*() | ||
.hasQualifiedName("org.apache.struts.action", "Action") or // Struts actions | ||
this.getDeclaringType() | ||
.getASupertype+() | ||
.hasQualifiedName("com.opensymphony.xwork2", "ActionSupport") or // Struts 2 actions | ||
this.getDeclaringType() | ||
.getASupertype+() | ||
.hasQualifiedName("org.springframework.web.struts", "ActionSupport") or // Spring/Struts 2 actions | ||
this.getDeclaringType() | ||
.getASupertype+() | ||
.hasQualifiedName("org.springframework.webflow.execution", "Action") // Spring actions | ||
) and | ||
isMainMethod(this) and | ||
not isTestMethod(this) | ||
} | ||
} | ||
|
||
from WebComponentMainMethod sm | ||
select sm, "Web application has a main method." |
1 change: 1 addition & 0 deletions
1
java/ql/test/experimental/query-tests/security/CWE-489/ServiceBean.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
| ServiceBean.java:55:24:55:27 | main | Java EE application has a main method. | |
59 changes: 59 additions & 0 deletions
59
java/ql/test/experimental/query-tests/security/CWE-489/ServiceBean.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import javax.ejb.SessionBean; | ||
import javax.ejb.EJBException; | ||
import java.rmi.RemoteException; | ||
import javax.ejb.SessionContext; | ||
import javax.naming.Context; | ||
import javax.naming.InitialContext; | ||
|
||
public class ServiceBean implements SessionBean { | ||
|
||
protected SessionContext ctx; | ||
|
||
private String _serviceName; | ||
|
||
/** | ||
* Create the session bean (empty implementation) | ||
*/ | ||
public void ejbCreate() throws javax.ejb.CreateException { | ||
System.out.println("ServiceBean:ejbCreate()"); | ||
} | ||
|
||
public void ejbActivate() throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public void ejbPassivate() throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public void ejbRemove() throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
public void setSessionContext(SessionContext parm1) throws javax.ejb.EJBException, java.rmi.RemoteException { | ||
} | ||
|
||
/** | ||
* Get service name | ||
* @return service name | ||
*/ | ||
public String getServiceName() { | ||
return _serviceName; | ||
} | ||
|
||
/** | ||
* Set service name | ||
* @param serviceName the service name | ||
*/ | ||
public void setServiceName(String serviceName) { | ||
_serviceName = serviceName; | ||
} | ||
|
||
/** Do service (no implementation) */ | ||
public String doService() { | ||
return null; | ||
} | ||
|
||
/** Local unit testing code */ | ||
public static void main(String[] args) throws Exception { | ||
ServiceBean b = new ServiceBean(); | ||
b.doService(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
java/ql/test/experimental/query-tests/security/CWE-489/ServiceBean.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
experimental/Security/CWE/CWE-489/EJBMain.ql |
25 changes: 25 additions & 0 deletions
25
java/ql/test/experimental/query-tests/security/CWE-489/ServletContextListenerMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
import java.net.URL; | ||
|
||
public class ServletContextListenerMain implements ServletContextListener { | ||
@Override | ||
public void contextInitialized(ServletContextEvent sce) { | ||
System.out.println("listener starts to work!"); | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent sce) { | ||
System.out.println("listener stopped!"); | ||
} | ||
|
||
// BAD - Implement a main method in servlet listener. | ||
public static void main(String[] args) { | ||
try { | ||
URL url = new URL("https://www.example.com"); | ||
url.openConnection(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
java/ql/test/experimental/query-tests/security/CWE-489/ServletMain.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| ServletContextListenerMain.java:17:21:17:24 | main | Web application has a main method. | | ||
| ServletMain.java:28:21:28:24 | main | Web application has a main method. | |
33 changes: 33 additions & 0 deletions
33
java/ql/test/experimental/query-tests/security/CWE-489/ServletMain.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import javax.servlet.Servlet; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.ServletResponse; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.ServletConfig; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
|
||
public class ServletMain implements Servlet { | ||
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { | ||
} | ||
|
||
public void init(ServletConfig servletConfig) throws ServletException { | ||
} | ||
|
||
public ServletConfig getServletConfig() { | ||
return null; | ||
} | ||
|
||
public String getServletInfo() { | ||
return null; | ||
} | ||
|
||
public void destroy() { | ||
} | ||
|
||
// BAD - Implement a main method in servlet. | ||
public static void main(String[] args) throws Exception { | ||
// Connect to my server | ||
URL url = new URL("https://www.example.com"); | ||
url.openConnection(); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
java/ql/test/experimental/query-tests/security/CWE-489/ServletMain.qlref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
experimental/Security/CWE/CWE-489/WebComponentMain.ql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// semmle-extractor-options: --javac-args -cp ${testdir}/../../../../stubs/servlet-api-2.4:${testdir}/../../../../stubs/ejb-3.2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.