|
| 1 | +/** |
| 2 | + * @id java/trust-boundary-violation |
| 3 | + * @name Trust boundary violation |
| 4 | + * @description A user-provided value is used to set a session attribute. |
| 5 | + * @kind path-problem |
| 6 | + * @problem.severity error |
| 7 | + * @precision medium |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-501 |
| 10 | + */ |
| 11 | + |
| 12 | +import java |
| 13 | +import semmle.code.java.dataflow.DataFlow |
| 14 | +import semmle.code.java.dataflow.FlowSources |
| 15 | +import semmle.code.java.dataflow.TaintTracking |
| 16 | +import semmle.code.java.security.TrustBoundaryViolationQuery |
| 17 | + |
| 18 | +/** |
| 19 | + * The `setAttribute` method of the `HttpSession` interface. |
| 20 | + */ |
| 21 | +abstract class SessionSetAttributeMethod extends Method { |
| 22 | + abstract int getArgumentIndex(); |
| 23 | +} |
| 24 | + |
| 25 | +private class PlayMvcResultAddingToSessionMethod extends SessionSetAttributeMethod { |
| 26 | + PlayMvcResultAddingToSessionMethod() { |
| 27 | + this.getDeclaringType().hasQualifiedName("play.mvc", "Result") and |
| 28 | + this.hasName("addingToSession") |
| 29 | + } |
| 30 | + |
| 31 | + override int getArgumentIndex() { result = [1, 2] } |
| 32 | +} |
| 33 | + |
| 34 | +private class Struts2SessionMapPutMethod extends SessionSetAttributeMethod { |
| 35 | + Struts2SessionMapPutMethod() { |
| 36 | + this.getDeclaringType().hasQualifiedName("org.apache.struts2.dispatcher", "SessionMap") and |
| 37 | + this.hasName("put") |
| 38 | + } |
| 39 | + |
| 40 | + override int getArgumentIndex() { result = 1 } |
| 41 | +} |
| 42 | + |
| 43 | +private class Struts2SessionSetMethod extends SessionSetAttributeMethod { |
| 44 | + Struts2SessionSetMethod() { |
| 45 | + this.getDeclaringType().hasQualifiedName("org.apache.struts2.interceptor", "SessionAware") and |
| 46 | + this.hasName(["setSession", "withSession"]) |
| 47 | + } |
| 48 | + |
| 49 | + override int getArgumentIndex() { result = 0 } |
| 50 | +} |
| 51 | + |
| 52 | +module TrustBoundaryConfig implements DataFlow::ConfigSig { |
| 53 | + predicate isSource(DataFlow::Node source) { |
| 54 | + source instanceof RemoteFlowSource and |
| 55 | + source.asExpr().(MethodAccess).getQualifier().getType() instanceof HttpServletRequest |
| 56 | + } |
| 57 | + |
| 58 | + predicate isSink(DataFlow::Node sink) { |
| 59 | + exists(MethodAccess ma, SessionSetAttributeMethod m | m = ma.getMethod() | |
| 60 | + sink.asExpr() = ma.getArgument(m.getArgumentIndex()) |
| 61 | + ) |
| 62 | + or |
| 63 | + sink instanceof TrustBoundaryViolationSink |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +module TrustBoundaryFlow = TaintTracking::Global<TrustBoundaryConfig>; |
| 68 | + |
| 69 | +import TrustBoundaryFlow::PathGraph |
| 70 | + |
| 71 | +from TrustBoundaryFlow::PathNode source, TrustBoundaryFlow::PathNode sink |
| 72 | +where TrustBoundaryFlow::flowPath(source, sink) |
| 73 | +select sink.getNode(), sink, source, |
| 74 | + "This servlet reads data from a remote source and writes it to a $@.", sink.getNode(), |
| 75 | + "session variable" |
0 commit comments