Merge pull request #678 from smowton/smowton/feature/note-filepath-clean-sanitizer

Note that `filepath.Clean("/" + e)` is a sanitizer against path traversal attacks
This commit is contained in:
Chris Smowton 2022-01-31 10:55:48 +00:00 коммит произвёл GitHub
Родитель 28461f57ef de2ed83b55
Коммит d064b17d7b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 19 добавлений и 0 удалений

Просмотреть файл

@ -76,6 +76,20 @@ module TaintedPath {
}
}
/**
* A call to `filepath.Clean("/" + e)`, considered to sanitize `e` against path traversal.
*/
class FilepathCleanSanitizer extends Sanitizer {
FilepathCleanSanitizer() {
exists(DataFlow::CallNode cleanCall, StringOps::Concatenation concatNode |
cleanCall = any(Function f | f.hasQualifiedName("path/filepath", "Clean")).getACall() and
concatNode = cleanCall.getArgument(0) and
concatNode.getOperand(0).asExpr().(StringLit).getValue() = "/" and
this = cleanCall.getResult()
)
}
}
/**
* A check of the form `!strings.Contains(nd, "..")`, considered as a sanitizer guard for
* path traversal.

Просмотреть файл

@ -51,4 +51,9 @@ func handler(w http.ResponseWriter, r *http.Request) {
data, _ = ioutil.ReadFile(filepath.Join("/home/user/", path))
w.Write(data)
}
// GOOD: Sanitized by filepath.Clean with a prepended '/' forcing interpretation
// as an absolute path, so that Clean will throw away any leading `..` components.
data, _ = ioutil.ReadFile(filepath.Clean("/" + path))
w.Write(data)
}