Skip to content

Commit a9b9410

Browse files
authored
Merge pull request github#17759 from Kwstubbs/path-sanitizers
Go: Add Tainted Path sanitizers
2 parents d8fe4d2 + 347e5ed commit a9b9410

File tree

16 files changed

+662
-0
lines changed

16 files changed

+662
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ module TaintedPath {
9393
}
9494
}
9595

96+
/**
97+
* A call to `mux.Vars(path)`, considered to sanitize `path` against path traversal.
98+
* Only enabled when `SkipClean` is not set true.
99+
*/
100+
class MuxVarsSanitizer extends Sanitizer {
101+
MuxVarsSanitizer() {
102+
exists(Function m |
103+
m.hasQualifiedName(package("github.com/gorilla/mux", ""), "Vars") and
104+
this = m.getACall().getResult()
105+
) and
106+
not exists(CallExpr f |
107+
f.getTarget()
108+
.(Method)
109+
.hasQualifiedName(package("github.com/gorilla/mux", ""), "Router", "SkipClean") and
110+
f.getArgument(0).getBoolValue() = true
111+
)
112+
}
113+
}
114+
96115
/**
97116
* A read from the field `Filename` of the type `mime/multipart.FileHeader`,
98117
* considered as a sanitizer for path traversal.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Added [github.com/gorilla/mux.Vars](https://pkg.go.dev/github.com/gorilla/mux#Vars) to path sanitizers (disabled if [github.com/gorilla/mix.Router.SkipClean](https://pkg.go.dev/github.com/gorilla/mux#Router.SkipClean) has been called).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"net/http"
6+
"path/filepath"
7+
8+
"github.com/gorilla/mux"
9+
)
10+
11+
// GOOD: Sanitized by Gorilla's cleaner
12+
func GorillaHandler(w http.ResponseWriter, r *http.Request) {
13+
not_tainted_path := mux.Vars(r)["id"]
14+
data, _ := ioutil.ReadFile(filepath.Join("/home/user/", not_tainted_path))
15+
w.Write(data)
16+
}
17+
18+
func main() {
19+
var router = mux.NewRouter()
20+
router.SkipClean(false)
21+
router.HandleFunc("/{category}", GorillaHandler)
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
edges
2+
nodes
3+
subpaths
4+
#select
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: Security/CWE-022/TaintedPath.ql
2+
postprocess: utils/test//PrettyPrintModels.ql
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module codeql-go-tests/frameworks/Mux
2+
3+
go 1.14
4+
5+
require github.com/gorilla/mux v1.7.4

go/ql/test/query-tests/Security/CWE-022/GorillaMuxDefault/vendor/github.com/gorilla/mux/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/ql/test/query-tests/Security/CWE-022/GorillaMuxDefault/vendor/github.com/gorilla/mux/stub.go

Lines changed: 252 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# github.com/gorilla/mux v1.7.4
2+
## explicit
3+
github.com/gorilla/mux
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"io/ioutil"
5+
"net/http"
6+
"path/filepath"
7+
8+
"github.com/gorilla/mux"
9+
)
10+
11+
// BAD: Gorilla's `Vars` is not a sanitizer as `Router.SkipClean` has been called
12+
func GorillaHandler(w http.ResponseWriter, r *http.Request) {
13+
not_tainted_path := mux.Vars(r)["id"]
14+
data, _ := ioutil.ReadFile(filepath.Join("/home/user/", not_tainted_path))
15+
w.Write(data)
16+
}
17+
18+
func main() {
19+
var router = mux.NewRouter()
20+
router.SkipClean(true)
21+
router.HandleFunc("/{category}", GorillaHandler)
22+
}

0 commit comments

Comments
 (0)