Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions go/ql/lib/semmle/go/security/TaintedPathCustomizations.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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("path/filepath", "IsLocal") and
this = f.getACall()
)
}

override predicate checks(Expr e, boolean branch) {
e = this.getArgument(0).asExpr() and branch = true
}
}
}
4 changes: 4 additions & 0 deletions go/ql/src/change-notes/2025-07-15-islocal-sanitizer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* `filepath.IsLocal` is now recognised as a sanitizer against path-traversal and related vulnerabilities.
6 changes: 6 additions & 0 deletions go/ql/test/query-tests/Security/CWE-022/TaintedPath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +98 to +99
Copy link
Preview

Copilot AI Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filepath.IsLocal() check alone may not provide complete path traversal protection. Consider combining it with additional validation such as filepath.Clean() or checking against an allowlist of permitted directories, as IsLocal() only validates that the path doesn't escape the current directory but doesn't prevent access to sensitive files within it.

Suggested change
if filepath.IsLocal(tainted_path) {
data, _ = ioutil.ReadFile(tainted_path)
cleanedPath := filepath.Clean(tainted_path)
allowedDir := "/allowed/directory" // Replace with the actual allowed directory
if filepath.IsLocal(cleanedPath) && strings.HasPrefix(cleanedPath, allowedDir) {
data, _ = ioutil.ReadFile(cleanedPath)

Copilot uses AI. Check for mistakes.

w.Write(data)
}
}