Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Modify the model
  • Loading branch information
haby0 authored and smowton committed Oct 19, 2021
commit 283376eb199cda013a6023a9edaae8490d0846ae
10 changes: 4 additions & 6 deletions java/ql/lib/semmle/code/java/frameworks/Servlets.qll
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ library class HttpServletRequestGetQueryStringMethod extends Method {
/**
* The method `getPathInfo()` declared in `javax.servlet.http.HttpServletRequest`.
*/
library class HttpServletRequestGetPathMethod extends Method {
class HttpServletRequestGetPathMethod extends Method {
HttpServletRequestGetPathMethod() {
getDeclaringType() instanceof HttpServletRequest and
hasName("getPathInfo") and
Expand Down Expand Up @@ -120,7 +120,7 @@ library class HttpServletRequestGetHeaderNamesMethod extends Method {
/**
* The method `getRequestURL()` declared in `javax.servlet.http.HttpServletRequest`.
*/
library class HttpServletRequestGetRequestURLMethod extends Method {
class HttpServletRequestGetRequestURLMethod extends Method {
HttpServletRequestGetRequestURLMethod() {
getDeclaringType() instanceof HttpServletRequest and
hasName("getRequestURL") and
Expand All @@ -131,7 +131,7 @@ library class HttpServletRequestGetRequestURLMethod extends Method {
/**
* The method `getRequestURI()` declared in `javax.servlet.http.HttpServletRequest`.
*/
library class HttpServletRequestGetRequestURIMethod extends Method {
class HttpServletRequestGetRequestURIMethod extends Method {
HttpServletRequestGetRequestURIMethod() {
getDeclaringType() instanceof HttpServletRequest and
hasName("getRequestURI") and
Expand Down Expand Up @@ -197,9 +197,7 @@ class HttpServletResponseSendErrorMethod extends Method {
class ServletRequestGetRequestDispatcherMethod extends Method {
ServletRequestGetRequestDispatcherMethod() {
getDeclaringType() instanceof ServletRequest and
hasName("getRequestDispatcher") and
getNumberOfParameters() = 1 and
getParameter(0).getType() instanceof TypeString
hasName("getRequestDispatcher")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java
import UnsafeUrlForward
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.frameworks.Servlets
import DataFlow::PathGraph

private class StartsWithSanitizer extends DataFlow::BarrierGuard {
Expand All @@ -32,12 +33,12 @@ class UnsafeUrlForwardFlowConfig extends TaintTracking::Configuration {

override predicate isSource(DataFlow::Node source) {
source instanceof RemoteFlowSource and
not exists(MethodAccess ma |
ma.getMethod().getName() in ["getRequestURI", "getRequestURL", "getPathInfo"] and
ma.getMethod()
.getDeclaringType()
.getASupertype*()
.hasQualifiedName("javax.servlet.http", "HttpServletRequest") and
not exists(MethodAccess ma, Method m | ma.getMethod() = m |
(
m instanceof HttpServletRequestGetRequestURIMethod or
m instanceof HttpServletRequestGetRequestURLMethod or
m instanceof HttpServletRequestGetPathMethod
) and
ma = source.asExpr()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import java
import DataFlow
import semmle.code.java.dataflow.FlowSources
import semmle.code.java.frameworks.Servlets
import semmle.code.java.frameworks.spring.SpringWeb

/** A sanitizer for unsafe url forward vulnerabilities. */
abstract class UnsafeUrlForwardSanitizer extends DataFlow::Node { }
Expand Down Expand Up @@ -144,7 +145,7 @@ private class UnsafeUrlForwardSanitizedExpr extends Expr {
/**
* A concatenate expression using the string `forward:` on the left.
*
* E.g: `"forward:" + url`
* For example, `"forward:" + url`.
*/
private class ForwardBuilderExpr extends AddExpr {
ForwardBuilderExpr() {
Expand All @@ -155,7 +156,7 @@ private class ForwardBuilderExpr extends AddExpr {
/**
* A call to `StringBuilder.append` or `StringBuffer.append` method, and the parameter value is `"forward:"`.
*
* E.g: `StringBuilder.append("forward:")`
* For example, `StringBuilder.append("forward:")`.
*/
private class ForwardAppendCall extends StringBuilderAppend {
ForwardAppendCall() {
Expand Down Expand Up @@ -191,7 +192,7 @@ private class SpringUrlForwardSink extends UnsafeUrlForwardSink {
)
or
exists(ClassInstanceExpr cie |
cie.getConstructedType().hasQualifiedName("org.springframework.web.servlet", "ModelAndView") and
cie.getConstructedType() instanceof ModelAndView and
(
exists(ForwardBuilderExpr rbe |
rbe = cie.getArgument(0) and rbe.getRightOperand() = this.asExpr()
Expand All @@ -201,12 +202,6 @@ private class SpringUrlForwardSink extends UnsafeUrlForwardSink {
)
)
or
exists(MethodAccess ma |
ma.getMethod().hasName("setViewName") and
ma.getMethod()
.getDeclaringType()
.hasQualifiedName("org.springframework.web.servlet", "ModelAndView") and
ma.getArgument(0) = this.asExpr()
)
exists(SpringModelAndViewSetViewNameCall smavsvnc | smavsvnc.getArgument(0) = this.asExpr())
}
}