|
| 1 | +/** |
| 2 | + * @name Server crash |
| 3 | + * @description A server that can be forced to crash may be vulnerable to denial-of-service |
| 4 | + * attacks. |
| 5 | + * @kind problem |
| 6 | + * @problem.severity error |
| 7 | + * @precision high |
| 8 | + * @id js/server-crash |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-730 |
| 11 | + */ |
| 12 | + |
| 13 | +import javascript |
| 14 | + |
| 15 | +/** |
| 16 | + * Gets a function that `caller` invokes. |
| 17 | + */ |
| 18 | +Function getACallee(Function caller) { |
| 19 | + exists(DataFlow::InvokeNode invk | |
| 20 | + invk.getEnclosingFunction() = caller and result = invk.getACallee() |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Gets a function that `caller` invokes, excluding calls guarded in `try`-blocks. |
| 26 | + */ |
| 27 | +Function getAnUnguardedCallee(Function caller) { |
| 28 | + exists(DataFlow::InvokeNode invk | |
| 29 | + invk.getEnclosingFunction() = caller and |
| 30 | + result = invk.getACallee() and |
| 31 | + not exists(invk.asExpr().getEnclosingStmt().getEnclosingTryCatchStmt()) |
| 32 | + ) |
| 33 | +} |
| 34 | + |
| 35 | +predicate isHeaderValue(HTTP::ExplicitHeaderDefinition def, DataFlow::Node node) { |
| 36 | + def.definesExplicitly(_, node.asExpr()) |
| 37 | +} |
| 38 | + |
| 39 | +predicate isDangerousPropertyRead(DataFlow::PropRead read) { |
| 40 | + // TODO use flow labels |
| 41 | + exists(DataFlow::PropRead base | |
| 42 | + base = read.getBase() and |
| 43 | + not read instanceof RemoteFlowSource and |
| 44 | + not exists(read.getACall()) |
| 45 | + ) |
| 46 | +} |
| 47 | + |
| 48 | +class Configuration extends TaintTracking::Configuration { |
| 49 | + Configuration() { this = "Configuration" } |
| 50 | + |
| 51 | + override predicate isSource(DataFlow::Node node) { node instanceof RemoteFlowSource } |
| 52 | + |
| 53 | + override predicate isSink(DataFlow::Node node) { |
| 54 | + // using control characters in a header value will cause an exception |
| 55 | + isHeaderValue(_, node) |
| 56 | + or |
| 57 | + // accessing a property of undefined will cause an exception |
| 58 | + isDangerousPropertyRead(node) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +predicate isLikelyToThrow(DataFlow::Node crash) { |
| 63 | + exists(Configuration cfg, DataFlow::Node sink | cfg.hasFlow(_, sink) | |
| 64 | + isHeaderValue(crash, sink) |
| 65 | + or |
| 66 | + isDangerousPropertyRead(sink) and sink = crash |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * A call that looks like it is asynchronous. |
| 72 | + */ |
| 73 | +class AsyncCall extends DataFlow::CallNode { |
| 74 | + DataFlow::FunctionNode callback; |
| 75 | + |
| 76 | + AsyncCall() { |
| 77 | + callback.flowsTo(getLastArgument()) and |
| 78 | + callback.getParameter(0).getName() = ["e", "err", "error"] and |
| 79 | + callback.getNumParameter() = 2 and |
| 80 | + not exists(callback.getAReturn()) |
| 81 | + } |
| 82 | + |
| 83 | + DataFlow::FunctionNode getCallback() { result = callback } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * A node that will crash a server if it throws an exception. |
| 88 | + * |
| 89 | + * The crash happens because a route handler invokes an asynchronous function that in turn throws an exception produced by this node. |
| 90 | + */ |
| 91 | +class ServerCrasher extends ExprOrStmt { |
| 92 | + HTTP::RouteHandler routeHandler; |
| 93 | + DataFlow::FunctionNode asyncFunction; |
| 94 | + |
| 95 | + ServerCrasher() { |
| 96 | + exists(AsyncCall asyncCall, Function throwingFunction | |
| 97 | + // the route handler transitively calls an async function |
| 98 | + asyncCall.getEnclosingFunction() = |
| 99 | + getACallee*(routeHandler.(DataFlow::FunctionNode).getFunction()) and |
| 100 | + asyncFunction = asyncCall.getCallback() and |
| 101 | + // the async function transitively calls a function that may throw an exception out of the the async function |
| 102 | + throwingFunction = getAnUnguardedCallee*(asyncFunction.getFunction()) and |
| 103 | + this.getContainer() = throwingFunction and |
| 104 | + not exists([this.(Expr).getEnclosingStmt(), this.(Stmt)].getEnclosingTryCatchStmt()) |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Gets the asynchronous function from which a server-crashing exception escapes. |
| 110 | + */ |
| 111 | + DataFlow::FunctionNode getAsyncFunction() { result = asyncFunction } |
| 112 | + |
| 113 | + /** |
| 114 | + * Gets the route handler that ultimately is responsible for the server crash. |
| 115 | + */ |
| 116 | + HTTP::RouteHandler getRouteHandler() { result = routeHandler } |
| 117 | +} |
| 118 | + |
| 119 | +from ServerCrasher crasher |
| 120 | +where isLikelyToThrow(crasher.(Expr).flow()) or crasher instanceof ThrowStmt |
| 121 | +select crasher, "When an exception is thrown here and later exits $@, the server of $@ will crash.", |
| 122 | + crasher.getAsyncFunction(), "an asynchronous function", crasher.getRouteHandler(), |
| 123 | + "this route handler" |
0 commit comments