|
| 1 | +/** |
| 2 | + * @name Unclosed stream |
| 3 | + * @description A stream that is not closed may leak system resources. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @precision high |
| 7 | + * @id js/unclosed-stream |
| 8 | + * @tags security |
| 9 | + * external/cwe/cwe-730 |
| 10 | + * external/cwe/cwe-404 |
| 11 | + */ |
| 12 | + |
| 13 | +import javascript |
| 14 | + |
| 15 | +/** |
| 16 | + * Gets a function that `caller` invokes directly or indirectly as a callback to a library function. |
| 17 | + */ |
| 18 | +Function getACallee(Function caller) { |
| 19 | + exists(DataFlow::InvokeNode invk | invk.getEnclosingFunction() = caller | |
| 20 | + result = invk.getACallee() |
| 21 | + or |
| 22 | + not exists(invk.getACallee()) and |
| 23 | + result = invk.getCallback(invk.getNumArgument()).getFunction() |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * A function that can be terminated meaningfully in an asynchronous context. |
| 29 | + */ |
| 30 | +abstract class AsyncTerminatableFunction extends DataFlow::FunctionNode { |
| 31 | + /** |
| 32 | + * Gets a node where this function terminates. |
| 33 | + * |
| 34 | + * It can be expected that no further expressions in this function will be evaluated after the evaluation of this node. |
| 35 | + */ |
| 36 | + abstract DataFlow::Node getTermination(); |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets a brief description of this function. |
| 40 | + */ |
| 41 | + abstract string getKind(); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * A promise executor as a function that can be terminated in an asynchronous context. |
| 46 | + */ |
| 47 | +class PromiseExecutor extends AsyncTerminatableFunction { |
| 48 | + PromiseDefinition def; |
| 49 | + |
| 50 | + PromiseExecutor() { this = def.getExecutor() } |
| 51 | + |
| 52 | + override DataFlow::CallNode getTermination() { |
| 53 | + result = [def.getRejectParameter(), def.getResolveParameter()].getACall() |
| 54 | + } |
| 55 | + |
| 56 | + override string getKind() { result = "promise" } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * A callback-invoking function heuristic as a function that can be terminated in an asynchronous context. |
| 61 | + */ |
| 62 | +class FunctionWithCallback extends AsyncTerminatableFunction { |
| 63 | + DataFlow::ParameterNode callbackParameter; |
| 64 | + |
| 65 | + FunctionWithCallback() { |
| 66 | + // the last parameter is the callback |
| 67 | + callbackParameter = this.getLastParameter() and |
| 68 | + // simple escape analysis |
| 69 | + not exists(DataFlow::Node escape | callbackParameter.flowsTo(escape) | |
| 70 | + escape = any(DataFlow::PropWrite w).getRhs() or |
| 71 | + escape = any(DataFlow::CallNode c).getAnArgument() |
| 72 | + ) and |
| 73 | + // no return value |
| 74 | + (this.getFunction() instanceof ArrowFunctionExpr or not exists(this.getAReturn())) and |
| 75 | + // all callback invocations are terminal (note that this permits calls in closures) |
| 76 | + forex(DataFlow::CallNode termination | termination = callbackParameter.getACall() | |
| 77 | + termination.asExpr() = any(Function f).getExit().getAPredecessor() |
| 78 | + ) and |
| 79 | + // avoid confusion with promises |
| 80 | + not this instanceof PromiseExecutor and |
| 81 | + not exists(PromiseCandidate c | this.flowsTo(c.getAnArgument())) |
| 82 | + } |
| 83 | + |
| 84 | + override DataFlow::CallNode getTermination() { result = callbackParameter.getACall() } |
| 85 | + |
| 86 | + override string getKind() { result = "asynchronous function" } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * A data stream. |
| 91 | + */ |
| 92 | +class Stream extends DataFlow::SourceNode { |
| 93 | + DataFlow::FunctionNode processor; |
| 94 | + |
| 95 | + Stream() { |
| 96 | + exists(DataFlow::CallNode onData | |
| 97 | + this instanceof EventEmitter and |
| 98 | + onData = this.getAMethodCall(EventEmitter::on()) and |
| 99 | + onData.getArgument(0).mayHaveStringValue("data") and |
| 100 | + processor = onData.getCallback(1) |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Gets a call that closes thes stream. |
| 106 | + */ |
| 107 | + DataFlow::Node getClose() { result = this.getAMethodCall("destroy") } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets a function that processes the content of the stream. |
| 111 | + */ |
| 112 | + DataFlow::FunctionNode getProcessor() { result = processor } |
| 113 | +} |
| 114 | + |
| 115 | +from |
| 116 | + AsyncTerminatableFunction terminatable, DataFlow::CallNode termination, Stream stream, |
| 117 | + Function processor |
| 118 | +where |
| 119 | + stream.getAstNode().getContainer() = getACallee*(terminatable.getFunction()) and |
| 120 | + termination = terminatable.getTermination() and |
| 121 | + processor = stream.getProcessor().getFunction() and |
| 122 | + termination.asExpr().getEnclosingFunction() = getACallee*(processor) and |
| 123 | + not exists(Function destroyFunction | |
| 124 | + destroyFunction = getACallee*(processor) and |
| 125 | + stream.getClose().asExpr().getEnclosingFunction() = destroyFunction |
| 126 | + ) |
| 127 | +select processor, "This stream processor $@ the enclosing $@ without closing the stream.", |
| 128 | + termination, "terminates", terminatable, terminatable.getKind() |
0 commit comments