|
| 1 | +/** |
| 2 | + * Provides classes for working with sinks and taint propagators |
| 3 | + * from the `github.com/spf13/afero` package. |
| 4 | + */ |
| 5 | + |
| 6 | +import go |
| 7 | + |
| 8 | +/** |
| 9 | + * Provide File system access sinks of [afero](https://github.com/spf13/afero) framework |
| 10 | + */ |
| 11 | +module Afero { |
| 12 | + /** |
| 13 | + * Gets all versions of `github.com/spf13/afero` |
| 14 | + */ |
| 15 | + string aferoPackage() { result = package("github.com/spf13/afero", "") } |
| 16 | + |
| 17 | + /** |
| 18 | + * The File system access sinks of [afero](https://github.com/spf13/afero) framework methods |
| 19 | + */ |
| 20 | + class AferoSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode { |
| 21 | + AferoSystemAccess() { |
| 22 | + exists(Method m | |
| 23 | + m.hasQualifiedName(aferoPackage(), "HttpFs", |
| 24 | + ["Create", "Open", "OpenFile", "Remove", "RemoveAll"]) and |
| 25 | + this = m.getACall() |
| 26 | + or |
| 27 | + m.hasQualifiedName(aferoPackage(), "RegexpFs", |
| 28 | + ["Create", "Open", "OpenFile", "Remove", "RemoveAll", "Mkdir", "MkdirAll"]) and |
| 29 | + this = m.getACall() |
| 30 | + or |
| 31 | + m.hasQualifiedName(aferoPackage(), "ReadOnlyFs", |
| 32 | + ["Create", "Open", "OpenFile", "ReadDir", "ReadlinkIfPossible", "Mkdir", "MkdirAll"]) and |
| 33 | + this = m.getACall() |
| 34 | + or |
| 35 | + m.hasQualifiedName(aferoPackage(), "OsFs", |
| 36 | + [ |
| 37 | + "Create", "Open", "OpenFile", "ReadlinkIfPossible", "Remove", "RemoveAll", "Mkdir", |
| 38 | + "MkdirAll" |
| 39 | + ]) and |
| 40 | + this = m.getACall() |
| 41 | + or |
| 42 | + m.hasQualifiedName(aferoPackage(), "MemMapFs", |
| 43 | + ["Create", "Open", "OpenFile", "Remove", "RemoveAll", "Mkdir", "MkdirAll"]) and |
| 44 | + this = m.getACall() |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + override DataFlow::Node getAPathArgument() { result = this.getArgument(0) } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * The File system access sinks of [afero](https://github.com/spf13/afero) framework utility functions |
| 53 | + * |
| 54 | + * Afero Type is basically is an wrapper around utility functions which make them like a method, look at [here](https://github.com/spf13/afero/blob/cf95922e71986c0116204b6eeb3b345a01ffd842/ioutil.go#L61) |
| 55 | + * |
| 56 | + * The Types that are not vulnerable: `afero.BasePathFs` and `afero.IOFS` |
| 57 | + */ |
| 58 | + class AferoUtilityFunctionSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode { |
| 59 | + int pathArg; |
| 60 | + |
| 61 | + AferoUtilityFunctionSystemAccess() { |
| 62 | + // utility functions |
| 63 | + exists(Function f | |
| 64 | + f.hasQualifiedName(aferoPackage(), |
| 65 | + ["WriteReader", "SafeWriteReader", "WriteFile", "ReadFile", "ReadDir"]) and |
| 66 | + this = f.getACall() and |
| 67 | + pathArg = 1 and |
| 68 | + not aferoSanitizer(this.getArgument(0)) |
| 69 | + ) |
| 70 | + or |
| 71 | + exists(Method m | |
| 72 | + m.hasQualifiedName(aferoPackage(), "Afero", |
| 73 | + ["WriteReader", "SafeWriteReader", "WriteFile", "ReadFile", "ReadDir"]) and |
| 74 | + this = m.getACall() and |
| 75 | + pathArg = 0 and |
| 76 | + not aferoSanitizer(this.getReceiver()) |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + override DataFlow::Node getAPathArgument() { result = this.getArgument(pathArg) } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Holds if the Afero utility function has a first argument of a safe type like `NewBasePathFs`. |
| 85 | + * |
| 86 | + * e.g. |
| 87 | + * ``` |
| 88 | + * basePathFs := afero.NewBasePathFs(osFS, "tmp") |
| 89 | + * afero.ReadFile(basePathFs, filepath) |
| 90 | + * ``` |
| 91 | + */ |
| 92 | + predicate aferoSanitizer(DataFlow::Node n) { |
| 93 | + exists(Function f | |
| 94 | + f.hasQualifiedName(aferoPackage(), ["NewBasePathFs", "NewIOFS"]) and |
| 95 | + TaintTracking::localTaint(f.getACall(), n) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Holds if there is a dataflow node from n1 to n2 when initializing the Afero instance |
| 101 | + * |
| 102 | + * A helper for `aferoSanitizer` for when the Afero instance is initialized with one of the safe FS types like IOFS |
| 103 | + * |
| 104 | + * e.g.`n2 := &afero.Afero{Fs: afero.NewBasePathFs(osFS, "./")}` n1 is `afero.NewBasePathFs(osFS, "./")` |
| 105 | + */ |
| 106 | + class AdditionalTaintStep extends TaintTracking::AdditionalTaintStep { |
| 107 | + override predicate step(DataFlow::Node n1, DataFlow::Node n2) { |
| 108 | + exists(StructLit st | st.getType().hasQualifiedName(aferoPackage(), "Afero") | |
| 109 | + n1.asExpr() = st.getAnElement().(KeyValueExpr).getAChildExpr() and |
| 110 | + n2.asExpr() = st |
| 111 | + ) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments