|
| 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 | +class Configuration extends TaintTracking::Configuration { |
| 40 | + Configuration() { this = "Configuration" } |
| 41 | + |
| 42 | + override predicate isSource(DataFlow::Node node) { node instanceof RemoteFlowSource } |
| 43 | + |
| 44 | + override predicate isSink(DataFlow::Node node) { |
| 45 | + // using control characters in a header value will cause an exception |
| 46 | + isHeaderValue(_, node) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +predicate isLikelyToThrow(DataFlow::Node crash) { |
| 51 | + exists(Configuration cfg, DataFlow::Node sink | cfg.hasFlow(_, sink) | isHeaderValue(crash, sink)) |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * A call that looks like it is asynchronous. |
| 56 | + */ |
| 57 | +class AsyncCall extends DataFlow::CallNode { |
| 58 | + DataFlow::FunctionNode callback; |
| 59 | + |
| 60 | + AsyncCall() { |
| 61 | + callback.flowsTo(getLastArgument()) and |
| 62 | + callback.getParameter(0).getName() = ["e", "err", "error"] and |
| 63 | + callback.getNumParameter() = 2 and |
| 64 | + not exists(callback.getAReturn()) |
| 65 | + } |
| 66 | + |
| 67 | + DataFlow::FunctionNode getCallback() { result = callback } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Gets a function that is invoked by `asyncCallback` without any try-block wrapping, `asyncCallback` is in turn is called indirectly by `routeHandler`. |
| 72 | + * |
| 73 | + * If the result throws an excection, the server of `routeHandler` will crash. |
| 74 | + */ |
| 75 | +Function getAPotentialServerCrasher( |
| 76 | + HTTP::RouteHandler routeHandler, DataFlow::FunctionNode asyncCallback |
| 77 | +) { |
| 78 | + exists(AsyncCall asyncCall | |
| 79 | + // the route handler transitively calls an async function |
| 80 | + asyncCall.getEnclosingFunction() = |
| 81 | + getACallee*(routeHandler.(DataFlow::FunctionNode).getFunction()) and |
| 82 | + asyncCallback = asyncCall.getCallback() and |
| 83 | + // the async function transitively calls a function that may throw an exception out of the the async function |
| 84 | + result = getAnUnguardedCallee*(asyncCallback.getFunction()) |
| 85 | + ) |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Gets an AST node that is likely to throw an uncaught exception in `fun`. |
| 90 | + */ |
| 91 | +ExprOrStmt getALikelyExceptionThrower(Function fun) { |
| 92 | + result.getContainer() = fun and |
| 93 | + not exists([result.(Expr).getEnclosingStmt(), result.(Stmt)].getEnclosingTryCatchStmt()) and |
| 94 | + (isLikelyToThrow(result.(Expr).flow()) or result instanceof ThrowStmt) |
| 95 | +} |
| 96 | + |
| 97 | +from HTTP::RouteHandler routeHandler, DataFlow::FunctionNode asyncCallback, ExprOrStmt crasher |
| 98 | +where crasher = getALikelyExceptionThrower(getAPotentialServerCrasher(routeHandler, asyncCallback)) |
| 99 | +select crasher, "When an exception is thrown here and later exits $@, the server of $@ will crash.", |
| 100 | + asyncCallback, "this asynchronous callback", routeHandler, "this route handler" |
0 commit comments