|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * zip slip vulnerabilities, as well as extension points for |
| 4 | + * adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import codeql.ruby.AST |
| 8 | +private import codeql.ruby.ApiGraphs |
| 9 | +private import codeql.ruby.CFG |
| 10 | +private import codeql.ruby.Concepts |
| 11 | +private import codeql.ruby.DataFlow |
| 12 | +private import codeql.ruby.dataflow.BarrierGuards |
| 13 | +private import codeql.ruby.dataflow.RemoteFlowSources |
| 14 | + |
| 15 | +/** |
| 16 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 17 | + * zip slip vulnerabilities, as well as extension points for |
| 18 | + * adding your own. |
| 19 | + */ |
| 20 | +module ZipSlip { |
| 21 | + /** |
| 22 | + * A data flow source for zip slip vulnerabilities. |
| 23 | + */ |
| 24 | + abstract class Source extends DataFlow::Node { } |
| 25 | + |
| 26 | + /** |
| 27 | + * A data flow sink for zip slip vulnerabilities. |
| 28 | + */ |
| 29 | + abstract class Sink extends DataFlow::Node { } |
| 30 | + |
| 31 | + /** |
| 32 | + * A sanitizer for zip slip vulnerabilities. |
| 33 | + */ |
| 34 | + abstract class Sanitizer extends DataFlow::Node { } |
| 35 | + |
| 36 | + /** |
| 37 | + * A file system access, considered as a flow sink. |
| 38 | + */ |
| 39 | + class FileSystemAccessAsSink extends Sink { |
| 40 | + FileSystemAccessAsSink() { this = any(FileSystemAccess e).getAPathArgument() } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * A call to `Zlib::GzipReader.open(path)`, considered a flow source. |
| 45 | + */ |
| 46 | + private class GzipReaderOpen extends Source { |
| 47 | + GzipReaderOpen() { |
| 48 | + ( |
| 49 | + this = API::getTopLevelMember("Zlib").getMember("GzipReader").getReturn("open").asSource() |
| 50 | + or |
| 51 | + this = API::getTopLevelMember("Zlib").getMember("GzipReader").getInstance().asSource() |
| 52 | + ) and |
| 53 | + // If argument refers to a string object, then it's a hardcoded path and |
| 54 | + // this file is safe. |
| 55 | + not this.(DataFlow::CallNode) |
| 56 | + .getArgument(0) |
| 57 | + .getALocalSource() |
| 58 | + .getConstantValue() |
| 59 | + .isStringlikeValue(_) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * A call to `Gem::Package::TarReader.new(file_stream)`, considered a flow source. |
| 65 | + */ |
| 66 | + private class TarReaderInstance extends Source { |
| 67 | + TarReaderInstance() { |
| 68 | + exists(API::MethodAccessNode newTarReader | |
| 69 | + newTarReader = |
| 70 | + API::getTopLevelMember("Gem").getMember("Package").getMember("TarReader").getMethod("new") |
| 71 | + | |
| 72 | + // Unlike in two other modules, there's no check for the constant path because TarReader class is called with an `io` object and not filepath. |
| 73 | + // This, of course, can be modeled but probably in the internal IO.qll file |
| 74 | + // For now, I'm leaving this prone to false-positives |
| 75 | + not exists(newTarReader.getBlock()) and this = newTarReader.getReturn().asSource() |
| 76 | + or |
| 77 | + this = newTarReader.getBlock().getParameter(0).asSource() |
| 78 | + ) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * A call to `Zip::File.open(path)`, considered a flow source. |
| 84 | + */ |
| 85 | + private class ZipFileOpen extends Source { |
| 86 | + ZipFileOpen() { |
| 87 | + exists(API::MethodAccessNode zipOpen | |
| 88 | + zipOpen = API::getTopLevelMember("Zip").getMember("File").getMethod("open") and |
| 89 | + // If argument refers to a string object, then it's a hardcoded path and |
| 90 | + // this file is safe. |
| 91 | + not zipOpen |
| 92 | + .getCallNode() |
| 93 | + .getArgument(0) |
| 94 | + .getALocalSource() |
| 95 | + .getConstantValue() |
| 96 | + .isStringlikeValue(_) |
| 97 | + | |
| 98 | + // the case with variable assignment `zip_file = Zip::File.open(path)` |
| 99 | + not exists(zipOpen.getBlock()) and this = zipOpen.getReturn().asSource() |
| 100 | + or |
| 101 | + // the case with direct block`Zip::File.open(path) do |zip_file|` case |
| 102 | + this = zipOpen.getBlock().getParameter(0).asSource() |
| 103 | + ) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * A comparison with a constant string, considered as a sanitizer-guard. |
| 109 | + */ |
| 110 | + private class StringConstCompareAsSanitizer extends Sanitizer, StringConstCompareBarrier { } |
| 111 | + |
| 112 | + /** |
| 113 | + * An inclusion check against an array of constant strings, considered as a |
| 114 | + * sanitizer-guard. |
| 115 | + */ |
| 116 | + private class StringConstArrayInclusionCallAsSanitizer extends Sanitizer, |
| 117 | + StringConstArrayInclusionCallBarrier { } |
| 118 | + |
| 119 | + /** |
| 120 | + * A sanitizer like `File.expand_path(path).start_with?` where `path` is a path of a single entry inside the archive. |
| 121 | + * It is assumed that if `File.expand_path` is called, it is to verify the path is safe so there's no modeling of `start_with?` or other comparisons to avoid false-negatives. |
| 122 | + */ |
| 123 | + private class ExpandedPathStartsWithAsSanitizer extends Sanitizer { |
| 124 | + ExpandedPathStartsWithAsSanitizer() { |
| 125 | + exists(DataFlow::CallNode cn | |
| 126 | + cn.getMethodName() = "expand_path" and |
| 127 | + this = cn.getArgument(0) |
| 128 | + ) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Existing PathSanitization model created for regular path traversals |
| 134 | + */ |
| 135 | + private class PathSanitizationAsSanitizer extends Sanitizer instanceof Path::PathSanitization { } |
| 136 | +} |
0 commit comments