|
| 1 | +/** |
| 2 | + * Provides a taint tracking configuration for reasoning about unsafe zip extraction. |
| 3 | + */ |
| 4 | + |
| 5 | +import javascript |
| 6 | + |
| 7 | +module ZipSlip { |
| 8 | + /** |
| 9 | + * A data flow source for unsafe zip extraction. |
| 10 | + */ |
| 11 | + abstract class Source extends DataFlow::Node { } |
| 12 | + |
| 13 | + /** |
| 14 | + * A data flow sink for unsafe zip extraction. |
| 15 | + */ |
| 16 | + abstract class Sink extends DataFlow::Node { } |
| 17 | + |
| 18 | + /** |
| 19 | + * A sanitizer guard for unsafe zip extraction. |
| 20 | + */ |
| 21 | + abstract class SanitizerGuard extends TaintTracking::SanitizerGuardNode, DataFlow::ValueNode { } |
| 22 | + |
| 23 | + /** A taint tracking configuration for unsafe zip extraction. */ |
| 24 | + class Configuration extends TaintTracking::Configuration { |
| 25 | + Configuration() { this = "ZipSlip" } |
| 26 | + |
| 27 | + override predicate isSource(DataFlow::Node source) { source instanceof Source } |
| 28 | + |
| 29 | + override predicate isSink(DataFlow::Node sink) { sink instanceof Sink } |
| 30 | + |
| 31 | + override predicate isSanitizerGuard(TaintTracking::SanitizerGuardNode nd) { |
| 32 | + nd instanceof SanitizerGuard |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Gets a node that can be a parsed zip archive. |
| 38 | + */ |
| 39 | + private DataFlow::SourceNode parsedArchive() { |
| 40 | + result = DataFlow::moduleImport("unzip").getAMemberCall("Parse") |
| 41 | + or |
| 42 | + // `streamProducer.pipe(unzip.Parse())` is a typical (but not |
| 43 | + // universal) pattern when using nodejs streams, whose return |
| 44 | + // value is the parsed stream. |
| 45 | + exists(DataFlow::MethodCallNode pipe | |
| 46 | + pipe = result and |
| 47 | + pipe.getMethodName() = "pipe" and |
| 48 | + parsedArchive().flowsTo(pipe.getArgument(0)) |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + /** A zip archive entry path access, as a source for unsafe zip extraction. */ |
| 53 | + class UnzipEntrySource extends Source { |
| 54 | + // For example, in |
| 55 | + // ```javascript |
| 56 | + // const unzip = require('unzip'); |
| 57 | + // |
| 58 | + // fs.createReadStream('archive.zip') |
| 59 | + // .pipe(unzip.Parse()) |
| 60 | + // .on('entry', entry => { |
| 61 | + // const path = entry.path; |
| 62 | + // }); |
| 63 | + // ``` |
| 64 | + // there is an `UnzipEntrySource` node corresponding to |
| 65 | + // the expression `entry.path`. |
| 66 | + UnzipEntrySource() { |
| 67 | + exists(DataFlow::CallNode cn | |
| 68 | + cn = parsedArchive().getAMemberCall("on") and |
| 69 | + cn.getArgument(0).mayHaveStringValue("entry") and |
| 70 | + this = cn.getCallback(1) |
| 71 | + .getParameter(0) |
| 72 | + .getAPropertyRead("path")) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** A call to `fs.createWriteStream`, as a sink for unsafe zip extraction. */ |
| 77 | + class CreateWriteStreamSink extends Sink { |
| 78 | + CreateWriteStreamSink() { |
| 79 | + // This is not covered by `FileSystemWriteSink`, because it is |
| 80 | + // required that a write actually takes place to the stream. |
| 81 | + // However, we want to consider even the bare `createWriteStream` |
| 82 | + // to be a zipslip vulnerability since it may truncate an |
| 83 | + // existing file. |
| 84 | + this = DataFlow::moduleImport("fs").getAMemberCall("createWriteStream").getArgument(0) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** A file path of a file write, as a sink for unsafe zip extraction. */ |
| 89 | + class FileSystemWriteSink extends Sink { |
| 90 | + FileSystemWriteSink() { exists(FileSystemWriteAccess fsw | fsw.getAPathArgument() = this) } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Gets a string which is sufficient to exclude to make |
| 95 | + * a filepath definitely not refer to parent directories. |
| 96 | + */ |
| 97 | + private string getAParentDirName() { result = ".." or result = "../" } |
| 98 | + |
| 99 | + /** A check that a path string does not include '..' */ |
| 100 | + class NoParentDirSanitizerGuard extends SanitizerGuard { |
| 101 | + StringOps::Includes incl; |
| 102 | + |
| 103 | + NoParentDirSanitizerGuard() { this = incl } |
| 104 | + |
| 105 | + override predicate sanitizes(boolean outcome, Expr e) { |
| 106 | + incl.getPolarity().booleanNot() = outcome and |
| 107 | + incl.getBaseString().asExpr() = e and |
| 108 | + incl.getSubstring().mayHaveStringValue(getAParentDirName()) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments