Golang: Mark filepath.IsLocal as a tainted-path sanitizer guard

This commit is contained in:
Chris Smowton
2025-07-15 14:47:17 +01:00
committed by GitHub
parent b13f11883c
commit c8eefb7c5c
2 changed files with 22 additions and 0 deletions

View File

@@ -243,4 +243,20 @@ module TaintedPath {
override predicate checks(Expr e, boolean branch) { regexpFunctionChecksExpr(this, e, branch) }
}
/**
* A call of the form `filepath.IsLocal(path)` considered as a sanitizer guard for `path`.
*/
class IsLocalCheck extends SanitizerGuard, DataFlow::CallNode {
IsLocalCheck() {
exists(Function f |
f.hasQualifiedName("filepath", "IsLocal") and
this = f.getACall()
)
}
override predicate checks(Expr e, boolean branch) {
e = this.getArgument(0).asExpr() and branch = true
}
}
}

View File

@@ -93,4 +93,10 @@ func handler(w http.ResponseWriter, r *http.Request) {
}
data, _ = ioutil.ReadFile(part.FileName())
// GOOD: An attempt has been made to prevent path traversal
if filepath.IsLocal(tainted_path) {
data, _ = ioutil.ReadFile(tainted_path)
w.Write(data)
}
}