Skip to content

Commit 16f3fc6

Browse files
authored
Merge pull request #20056 from github/smowton/fix/tainted-path-is-local
Golang: Mark filepath.IsLocal as a tainted-path sanitizer guard
2 parents 327c4b3 + b71f9ae commit 16f3fc6

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,20 @@ module TaintedPath {
243243

244244
override predicate checks(Expr e, boolean branch) { regexpFunctionChecksExpr(this, e, branch) }
245245
}
246+
247+
/**
248+
* A call of the form `filepath.IsLocal(path)` considered as a sanitizer guard for `path`.
249+
*/
250+
class IsLocalCheck extends SanitizerGuard, DataFlow::CallNode {
251+
IsLocalCheck() {
252+
exists(Function f |
253+
f.hasQualifiedName("path/filepath", "IsLocal") and
254+
this = f.getACall()
255+
)
256+
}
257+
258+
override predicate checks(Expr e, boolean branch) {
259+
e = this.getArgument(0).asExpr() and branch = true
260+
}
261+
}
246262
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* `filepath.IsLocal` is now recognised as a sanitizer against path-traversal and related vulnerabilities.

go/ql/test/query-tests/Security/CWE-022/TaintedPath.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
9393
}
9494

9595
data, _ = ioutil.ReadFile(part.FileName())
96+
97+
// GOOD: An attempt has been made to prevent path traversal
98+
if filepath.IsLocal(tainted_path) {
99+
data, _ = ioutil.ReadFile(tainted_path)
100+
w.Write(data)
101+
}
96102
}

0 commit comments

Comments
 (0)