|
| 1 | +import java |
| 2 | +import DataFlow |
| 3 | +import semmle.code.java.dataflow.FlowSources |
| 4 | +import semmle.code.java.frameworks.Servlets |
| 5 | +import semmle.code.java.frameworks.spring.SpringWeb |
| 6 | +import semmle.code.java.security.RequestForgery |
| 7 | +private import semmle.code.java.dataflow.StringPrefixes |
| 8 | + |
| 9 | +/** A sanitizer for unsafe url forward vulnerabilities. */ |
| 10 | +abstract class UnsafeUrlForwardSanitizer extends DataFlow::Node { } |
| 11 | + |
| 12 | +private class PrimitiveSanitizer extends UnsafeUrlForwardSanitizer { |
| 13 | + PrimitiveSanitizer() { |
| 14 | + this.getType() instanceof PrimitiveType or |
| 15 | + this.getType() instanceof BoxedType or |
| 16 | + this.getType() instanceof NumberType |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +private class SanitizingPrefix extends InterestingPrefix { |
| 21 | + SanitizingPrefix() { |
| 22 | + not this.getStringValue().matches("/WEB-INF/%") and |
| 23 | + not this.getStringValue() = "forward:" |
| 24 | + } |
| 25 | + |
| 26 | + override int getOffset() { result = 0 } |
| 27 | +} |
| 28 | + |
| 29 | +private class FollowsSanitizingPrefix extends UnsafeUrlForwardSanitizer { |
| 30 | + FollowsSanitizingPrefix() { this.asExpr() = any(SanitizingPrefix fp).getAnAppendedExpression() } |
| 31 | +} |
| 32 | + |
| 33 | +abstract class UnsafeUrlForwardSink extends DataFlow::Node { } |
| 34 | + |
| 35 | +/** An argument to `ServletRequest.getRequestDispatcher`. */ |
| 36 | +private class RequestDispatcherSink extends UnsafeUrlForwardSink { |
| 37 | + RequestDispatcherSink() { |
| 38 | + exists(MethodAccess ma | |
| 39 | + ma.getMethod() instanceof ServletRequestGetRequestDispatcherMethod and |
| 40 | + ma.getArgument(0) = this.asExpr() |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** An argument to `new ModelAndView` or `ModelAndView.setViewName`. */ |
| 46 | +private class SpringModelAndViewSink extends UnsafeUrlForwardSink { |
| 47 | + SpringModelAndViewSink() { |
| 48 | + exists(ClassInstanceExpr cie | |
| 49 | + cie.getConstructedType() instanceof ModelAndView and |
| 50 | + cie.getArgument(0) = this.asExpr() |
| 51 | + ) |
| 52 | + or |
| 53 | + exists(SpringModelAndViewSetViewNameCall smavsvnc | smavsvnc.getArgument(0) = this.asExpr()) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +private class ForwardPrefix extends InterestingPrefix { |
| 58 | + ForwardPrefix() { this.getStringValue() = "forward:" } |
| 59 | + |
| 60 | + override int getOffset() { result = 0 } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * An expression appended (perhaps indirectly) to `"forward:"`, and which |
| 65 | + * is reachable from a Spring entry point. |
| 66 | + */ |
| 67 | +private class SpringUrlForwardSink extends UnsafeUrlForwardSink { |
| 68 | + SpringUrlForwardSink() { |
| 69 | + any(SpringRequestMappingMethod sqmm).polyCalls*(this.getEnclosingCallable()) and |
| 70 | + this.asExpr() = any(ForwardPrefix fp).getAnAppendedExpression() |
| 71 | + } |
| 72 | +} |
0 commit comments