Skip to content
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

Java: Add query to detect Apache Struts enabled Devmode #3945

Merged
merged 3 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions java/ql/src/experimental/Security/CWE/CWE-489/StrutsBad.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="utf-8" />
<include file="login.xml" />
</struts>
11 changes: 11 additions & 0 deletions java/ql/src/experimental/Security/CWE/CWE-489/StrutsGood.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="false" />
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<include file="login.xml" />
</struts>
32 changes: 32 additions & 0 deletions java/ql/src/experimental/Security/CWE/CWE-489/devMode.qhelp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
<qhelp>

<overview>
<p>Turning Apache Struts' development mode configuration on while deploying applications to production environments can lead to remote code execution.</p>

</overview>
<recommendation>

<p>An application should disable the development mode at the time of deployment.</p>

</recommendation>
<example>

<p>The following example shows a `struts.xml` file with `struts.devmode` enabled.</p>

<sample src="StrutsBad.xml" />

<p>This can be easily corrected by setting the value of the `struts.devmode` parameter to false.</p>

<sample src="StrutsGood.xml" />

</example>
<references>

<li>
Apache Struts:
<a href="https://struts.apache.org/core-developers/development-mode.html">Struts development mode configuration</a>
</li>

</references>
</qhelp>
24 changes: 24 additions & 0 deletions java/ql/src/experimental/Security/CWE/CWE-489/devMode.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @name Apache Struts development mode enabled
* @description Enabling struts development mode in production environment
* can lead to remote code execution.
* @kind problem
* @problem.severity error
* @precision high
* @id java/struts-development-mode
* @tags security
* external/cwe/cwe-489
*/

import java
porcupineyhairs marked this conversation as resolved.
Show resolved Hide resolved
import experimental.semmle.code.xml.StrutsXML

bindingset[path]
predicate isLikelyDemoProject(string path) { path.regexpMatch("(?i).*(demo|test|example).*") }

from ConstantParameter c
where
c.getNameValue() = "struts.devMode" and
c.getValueValue() = "true" and
not isLikelyDemoProject(c.getFile().getRelativePath())
select c, "Enabling development mode in production environments is dangerous"
40 changes: 40 additions & 0 deletions java/ql/src/experimental/semmle/code/xml/StrutsXML.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java

/**
* A deployment descriptor file, typically called `struts.xml`.
*/
class StrutsXMLFile extends XMLFile {
StrutsXMLFile() {
count(XMLElement e | e = this.getAChild()) = 1 and
this.getAChild().getName() = "struts"
}
}

/**
* An XML element in a `StrutsXMLFile`.
*/
class StrutsXMLElement extends XMLElement {
StrutsXMLElement() { this.getFile() instanceof StrutsXMLFile }

/**
* Gets the value for this element, with leading and trailing whitespace trimmed.
*/
string getValue() { result = allCharactersString().trim() }
porcupineyhairs marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* A `<constant>` element in a `StrutsXMLFile`.
*/
class ConstantParameter extends StrutsXMLElement {
ConstantParameter() { this.getName() = "constant" }

/**
* Gets the value of the `name` attribute of this `<constant>`.
*/
string getNameValue() { result = getAttributeValue("name") }

/**
* Gets the value of the `value` attribute of this `<constant>`.
*/
string getValueValue() { result = getAttributeValue("value") }
}