Skip to content

Commit 3febbec

Browse files
committed
fix qldoc and review suggestions
1 parent fd0d194 commit 3febbec

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

go/ql/lib/semmle/go/security/FileSystemAccess.qll

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ import go
44
* The File system access sinks of `net/http` package
55
*/
66
class HttpServeFile extends FileSystemAccess::Range, DataFlow::CallNode {
7-
int pathArg;
8-
97
HttpServeFile() {
108
exists(Function f |
119
f.hasQualifiedName("net/http", "ServeFile") and
12-
this = f.getACall() and
13-
pathArg = 2
10+
this = f.getACall()
1411
)
1512
}
1613

17-
override DataFlow::Node getAPathArgument() { result = this.getArgument(pathArg) }
14+
override DataFlow::Node getAPathArgument() { result = this.getArgument(2) }
1815
}
1916

2017
/**
@@ -42,7 +39,7 @@ class BeegoFileSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode
4239
}
4340

4441
/**
45-
* Provide File system access sinks of [beego](https://github.com/beego/beego) web framework
42+
* The File system access sinks of [beego](https://github.com/beego/beego) web framework
4643
*/
4744
class EchoFileSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode {
4845
int pathArg;
@@ -128,75 +125,65 @@ class FiberSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode {
128125

129126
/**
130127
* Provide File system access sinks of [afero](https://github.com/spf13/afero) framework
131-
* The Types that are not vulnerable: `afero.BasePathFs` and `afero.IOFS`
132128
*/
133129
module Afero {
134-
string aferoPackage() { result = "github.com/spf13/afero" }
130+
string aferoPackage() { result = package("github.com/spf13/afero", "") }
135131

136132
/**
137-
* Provide File system access sinks of [afero](https://github.com/spf13/afero) framework methods
133+
* The File system access sinks of [afero](https://github.com/spf13/afero) framework methods
138134
*/
139135
class AferoSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode {
140-
int pathArg;
141-
142136
AferoSystemAccess() {
143137
exists(Method f |
144-
f.hasQualifiedName(package(aferoPackage(), ""), "HttpFs",
138+
f.hasQualifiedName(aferoPackage(), "HttpFs",
145139
["Create", "Open", "OpenFile", "Remove", "RemoveAll"]) and
146-
this = f.getACall() and
147-
pathArg = 0
140+
this = f.getACall()
148141
or
149-
f.hasQualifiedName(package(aferoPackage(), ""), "RegexpFs",
142+
f.hasQualifiedName(aferoPackage(), "RegexpFs",
150143
["Create", "Open", "OpenFile", "Remove", "RemoveAll", "Mkdir", "MkdirAll"]) and
151-
this = f.getACall() and
152-
pathArg = 0
144+
this = f.getACall()
153145
or
154-
f.hasQualifiedName(package(aferoPackage(), ""), "ReadOnlyFs",
146+
f.hasQualifiedName(aferoPackage(), "ReadOnlyFs",
155147
["Create", "Open", "OpenFile", "ReadDir", "ReadlinkIfPossible", "Mkdir", "MkdirAll"]) and
156-
this = f.getACall() and
157-
pathArg = 0
148+
this = f.getACall()
158149
or
159-
f.hasQualifiedName(package(aferoPackage(), ""), "OsFs",
150+
f.hasQualifiedName(aferoPackage(), "OsFs",
160151
[
161152
"Create", "Open", "OpenFile", "ReadlinkIfPossible", "Remove", "RemoveAll", "Mkdir",
162153
"MkdirAll"
163154
]) and
164-
this = f.getACall() and
165-
pathArg = 0
155+
this = f.getACall()
166156
or
167-
f.hasQualifiedName(package(aferoPackage(), ""), "MemMapFs",
157+
f.hasQualifiedName(aferoPackage(), "MemMapFs",
168158
["Create", "Open", "OpenFile", "Remove", "RemoveAll", "Mkdir", "MkdirAll"]) and
169-
this = f.getACall() and
170-
pathArg = 0
159+
this = f.getACall()
171160
)
172161
}
173162

174-
override DataFlow::Node getAPathArgument() { result = this.getArgument(pathArg) }
163+
override DataFlow::Node getAPathArgument() { result = this.getArgument(0) }
175164
}
176165

177166
/**
178-
* Provide File system access sinks of [afero](https://github.com/spf13/afero) framework utility functions
167+
* The File system access sinks of [afero](https://github.com/spf13/afero) framework utility functions
168+
*
179169
* The Types that are not vulnerable: `afero.BasePathFs` and `afero.IOFS`
180170
*/
181171
class AferoUtilityFunctionSystemAccess extends FileSystemAccess::Range, DataFlow::CallNode {
182-
int pathArg;
183-
184172
AferoUtilityFunctionSystemAccess() {
185173
// utility functions
186174
exists(Function f |
187-
f.hasQualifiedName(package(aferoPackage(), ""),
175+
f.hasQualifiedName(aferoPackage(),
188176
["WriteReader", "SafeWriteReader", "WriteFile", "ReadFile", "ReadDir"]) and
189177
this = f.getACall() and
190-
pathArg = 1 and
191178
not aferoSanitizer(this.getArgument(0))
192179
)
193180
}
194181

195-
override DataFlow::Node getAPathArgument() { result = this.getArgument(pathArg) }
182+
override DataFlow::Node getAPathArgument() { result = this.getArgument(1) }
196183
}
197184

198185
/**
199-
* A sanitizer for when the Afero utility functions has a first argument of a safe type like NewBasePathFs
186+
* Holds if the Afero utility function has a first argument of a safe type like `NewBasePathFs`.
200187
*
201188
* e.g.
202189
* ```
@@ -206,19 +193,21 @@ module Afero {
206193
*/
207194
predicate aferoSanitizer(DataFlow::Node n) {
208195
exists(Function f |
209-
f.hasQualifiedName(package(aferoPackage(), ""), "NewBasePathFs") and
210-
TaintTracking::localTaint(f.getACall(), n)
196+
f.hasQualifiedName(aferoPackage(), "NewBasePathFs") and
197+
DataFlow::localFlow(f.getACall(), n)
211198
)
212199
}
213200

214201
/**
202+
* Holds if there is a dataflow node from n1 to n2 when initializing the Afero instance
203+
*
215204
* A helper for `aferoSanitizer` for when the Afero instance is initialized with one of the safe FS types like IOFS
216205
*
217206
* e.g.`n2 := &afero.Afero{Fs: afero.NewBasePathFs(osFS, "./")}` n1 is `afero.NewBasePathFs(osFS, "./")`
218207
*/
219208
predicate additionalTaintStep(DataFlow::Node n1, DataFlow::Node n2) {
220-
exists(StructLit st | st.getType().hasQualifiedName(package(aferoPackage(), ""), "Afero") |
221-
n1.asExpr() = st.getAChildExpr*() and
209+
exists(StructLit st | st.getType().hasQualifiedName(aferoPackage(), "Afero") |
210+
n1.asExpr() = st.getAChildExpr().(KeyValueExpr).getAChildExpr() and
222211
n2.asExpr() = st
223212
)
224213
}

0 commit comments

Comments
 (0)