Skip to content

Commit d5739b2

Browse files
committed
Convert 3 barriers for path injection to MaD
1 parent 1dd68fb commit d5739b2

File tree

3 files changed

+26
-51
lines changed

3 files changed

+26
-51
lines changed

go/ql/lib/ext/mime.multipart.model.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: barrierModel
5+
data:
6+
# The only way to create a `mime/multipart.FileHeader` is to create a
7+
# `mime/multipart.Form`, which creates the `Filename` field of each
8+
# `mime/multipart.FileHeader` by calling `Part.FileName`, which calls
9+
# `path/filepath.Base` on its return value. In general `path/filepath.Base`
10+
# is not a sanitizer for path traversal, but in this specific case where the
11+
# output is going to be used as a filename rather than a directory name, it
12+
# is adequate.
13+
- ["mime/multipart", "FileHeader", False, "Filename", "", "", "", "path-injection", "manual"]
14+
# `Part.FileName` calls `path/filepath.Base` on its return value. In
15+
# general `path/filepath.Base` is not a sanitizer for path traversal, but in
16+
# this specific case where the output is going to be used as a filename
17+
# rather than a directory name, it is adequate.
18+
- ["mime/multipart", "Part", False, "FileName", "", "", "ReturnValue", "path-injection", "manual"]
219
- addsTo:
320
pack: codeql/go-all
421
extensible: summaryModel

go/ql/lib/ext/path.filepath.model.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
extensions:
2+
- addsTo:
3+
pack: codeql/go-all
4+
extensible: barrierModel
5+
data:
6+
- ["path/filepath", "", False, "Rel", "", "", "ReturnValue", "path-injection", "manual"]
27
- addsTo:
38
pack: codeql/go-all
49
extensible: summaryModel

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

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ module TaintedPath {
5757
PathAsSink() { this = any(FileSystemAccess fsa).getAPathArgument() }
5858
}
5959

60+
private class ExternalSanitizer extends Sanitizer {
61+
ExternalSanitizer() { barrierNode(this, "path-injection") }
62+
}
63+
6064
/**
6165
* A numeric- or boolean-typed node, considered a sanitizer for path traversal.
6266
*/
@@ -66,19 +70,6 @@ module TaintedPath {
6670
}
6771
}
6872

69-
/**
70-
* A call to `filepath.Rel`, considered as a sanitizer for path traversal.
71-
*/
72-
class FilepathRelSanitizer extends Sanitizer {
73-
FilepathRelSanitizer() {
74-
exists(Function f, FunctionOutput outp |
75-
f.hasQualifiedName("path/filepath", "Rel") and
76-
outp.isResult(0) and
77-
this = outp.getNode(f.getACall())
78-
)
79-
}
80-
}
81-
8273
/**
8374
* A call to `filepath.Clean("/" + e)`, considered to sanitize `e` against path traversal.
8475
*/
@@ -112,44 +103,6 @@ module TaintedPath {
112103
}
113104
}
114105

115-
/**
116-
* A read from the field `Filename` of the type `mime/multipart.FileHeader`,
117-
* considered as a sanitizer for path traversal.
118-
*
119-
* The only way to create a `mime/multipart.FileHeader` is to create a
120-
* `mime/multipart.Form`, which creates the `Filename` field of each
121-
* `mime/multipart.FileHeader` by calling `Part.FileName`, which calls
122-
* `path/filepath.Base` on its return value. In general `path/filepath.Base`
123-
* is not a sanitizer for path traversal, but in this specific case where the
124-
* output is going to be used as a filename rather than a directory name, it
125-
* is adequate.
126-
*/
127-
class MimeMultipartFileHeaderFilenameSanitizer extends Sanitizer {
128-
MimeMultipartFileHeaderFilenameSanitizer() {
129-
this.(DataFlow::FieldReadNode)
130-
.getField()
131-
.hasQualifiedName("mime/multipart", "FileHeader", "Filename")
132-
}
133-
}
134-
135-
/**
136-
* A call to `mime/multipart.Part.FileName`, considered as a sanitizer
137-
* against path traversal.
138-
*
139-
* `Part.FileName` calls `path/filepath.Base` on its return value. In
140-
* general `path/filepath.Base` is not a sanitizer for path traversal, but in
141-
* this specific case where the output is going to be used as a filename
142-
* rather than a directory name, it is adequate.
143-
*/
144-
class MimeMultipartPartFileNameSanitizer extends Sanitizer {
145-
MimeMultipartPartFileNameSanitizer() {
146-
this =
147-
any(Method m | m.hasQualifiedName("mime/multipart", "Part", "FileName"))
148-
.getACall()
149-
.getResult()
150-
}
151-
}
152-
153106
/**
154107
* A check of the form `!strings.Contains(nd, "..")`, considered as a sanitizer guard for
155108
* path traversal.

0 commit comments

Comments
 (0)