Insecure cookie query: accept ServletRequest.isSecure(), and allow more than one possible input to a setSecure(...) call.

This commit is contained in:
Chris Smowton
2022-05-11 11:59:37 +01:00
parent 1af0e9b619
commit c17ef42cc7
3 changed files with 87 additions and 7 deletions

View File

@@ -13,6 +13,18 @@
import java import java
import semmle.code.java.frameworks.Servlets import semmle.code.java.frameworks.Servlets
import semmle.code.java.dataflow.DataFlow
predicate isSafeSecureCookieSetting(Expr e) {
e.(CompileTimeConstantExpr).getBooleanValue() = true
or
exists(Method isSecure |
isSecure.getName() = "isSecure" and
isSecure.getDeclaringType().getASourceSupertype*() instanceof ServletRequest
|
e.(MethodAccess).getMethod() = isSecure
)
}
from MethodAccess add from MethodAccess add
where where
@@ -20,7 +32,12 @@ where
not exists(Variable cookie, MethodAccess m | not exists(Variable cookie, MethodAccess m |
add.getArgument(0) = cookie.getAnAccess() and add.getArgument(0) = cookie.getAnAccess() and
m.getMethod().getName() = "setSecure" and m.getMethod().getName() = "setSecure" and
m.getArgument(0).(CompileTimeConstantExpr).getBooleanValue() = true and forex(DataFlow::Node argSource |
DataFlow::localFlow(argSource, DataFlow::exprNode(m.getArgument(0))) and
not DataFlow::localFlowStep(_, argSource)
|
isSafeSecureCookieSetting(argSource.asExpr())
) and
m.getQualifier() = cookie.getAnAccess() m.getQualifier() = cookie.getAnAccess()
) )
select add, "Cookie is added to response without the 'secure' flag being set." select add, "Cookie is added to response without the 'secure' flag being set."

View File

@@ -1 +1,4 @@
| Test.java:16:4:16:29 | addCookie(...) | Cookie is added to response without the 'secure' flag being set. | | Test.java:19:4:19:29 | addCookie(...) | Cookie is added to response without the 'secure' flag being set. |
| Test.java:28:4:28:29 | addCookie(...) | Cookie is added to response without the 'secure' flag being set. |
| Test.java:37:4:37:29 | addCookie(...) | Cookie is added to response without the 'secure' flag being set. |
| Test.java:51:4:51:29 | addCookie(...) | Cookie is added to response without the 'secure' flag being set. |

View File

@@ -8,21 +8,81 @@ package test.cwe614.semmle.tests;
import javax.servlet.http.*; import javax.servlet.http.*;
class Test { class Test {
public static void test(HttpServletRequest request, HttpServletResponse response) {
public static final boolean constTrue = true;
public static void test(HttpServletRequest request, HttpServletResponse response, boolean alwaysSecure, boolean otherInput) {
{ {
Cookie cookie = new Cookie("secret" ,"fakesecret"); Cookie cookie = new Cookie("secret" ,"fakesecret");
// BAD: secure flag not set // BAD: secure flag not set
response.addCookie(cookie); response.addCookie(cookie);
} }
{ {
Cookie cookie = new Cookie("secret" ,"fakesecret"); Cookie cookie = new Cookie("secret" ,"fakesecret");
// BAD: secure flag set to false
cookie.setSecure(false);
response.addCookie(cookie);
}
{
Cookie cookie = new Cookie("secret" ,"fakesecret");
// BAD: secure flag set to something not clearly true or request.isSecure()
cookie.setSecure(otherInput);
response.addCookie(cookie);
}
{
Cookie cookie = new Cookie("secret" ,"fakesecret");
// BAD: secure flag sometimes set to something clearly true or request.isSecure()
boolean secureVal;
if(alwaysSecure)
secureVal = true;
else
secureVal = otherInput;
cookie.setSecure(secureVal);
response.addCookie(cookie);
}
{
Cookie cookie = new Cookie("secret" ,"fakesecret");
// GOOD: set secure flag // GOOD: set secure flag
cookie.setSecure(true); cookie.setSecure(true);
response.addCookie(cookie); response.addCookie(cookie);
} }
{
Cookie cookie = new Cookie("secret" ,"fakesecret");
// GOOD: set secure flag
cookie.setSecure(true);
response.addCookie(cookie);
}
{
Cookie cookie = new Cookie("secret" ,"fakesecret");
// GOOD: set secure flag
cookie.setSecure(constTrue);
response.addCookie(cookie);
}
{
Cookie cookie = new Cookie("secret" ,"fakesecret");
// GOOD: set secure flag if contacted over HTTPS
cookie.setSecure(alwaysSecure ? true : request.isSecure());
response.addCookie(cookie);
}
} }
} }