A file is opened or resolved at a path taken from an HTTP request without a check

tx0/kotlin:request-path-in-file-accessrecommendedwarningsecurity/path-traversaldepth fastsecurity, cwe-22

What it finds

File(uploads, call.parameters["name"]!!) opens whatever path the client names: a name such as ../../etc/passwd walks out of the directory, and the server reads, writes or sends a file the client should never reach (CWE-22). The path has to be checked, for example by resolving it with normalize() and testing that it still starts with the directory, or replaced by an identifier looked up on the server.

Reported for a value read from the request in the same function (Ktor's call.parameters and call.request, a parameter bound with @RequestParam or @PathVariable, a servlet request's getParameter), directly or through local variables, given to File, FileInputStream, FileOutputStream, FileReader, FileWriter, Paths.get, Path.of or resolve, when the function does not test it in a condition first.

From the rule's source, request_flows.rs.

Example from the rule's tests
import io.ktor.server.application.ApplicationCallimport org.springframework.web.bind.annotation.RequestParam class Files(private val root: File) {    suspend fun download(call: ApplicationCall) {        val name = call.parameters["name"]!!        call.respondFile(File(root, name))tx0 reports: the file path comes from the request (`call.parameters        val other = call.request.queryParameters["path"] ?: "index.html"        val stream = FileInputStream("$root/$other")tx0 reports: the file path comes from the request        val checked = call.parameters["file"]!!        if (!checked.matches(Regex("[a-z]+\\.txt"))) return        call.respondFile(File(root, checked))        val missing = call.parameters["n"]        if (missing == null) return        call.respondFile(File(root, missing))tx0 reports: the file path comes from the request        val up = File(call.request.path()).parent    }     fun read(@RequestParam path: String, @RequestParam size: Int, plain: String): String {        val a = Paths.get("/data", path)tx0 reports: the file path comes from the request (path)        val b = File(root, size.toString())        val c = File(root, plain)        return root.toPath().resolve(path).toString()tx0 reports: the file path comes from the request    }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:request-path-in-file-access" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/kotlin:request-path-in-file-access] reason="..."

tx0 explain tx0/kotlin:request-path-in-file-access shows the same in the terminal. Found a false positive? Report it.