A file is created from the name of an archive entry without checking that it stays in the target directory

tx0/kotlin:zip-entry-path-traversalrecommendedwarningsecurity/path-traversaldepth fastsecurity, cwe-22

What it finds

The names in a zip or jar archive can contain ../, so File(target, entry.name) may point outside target and overwrite any file the program may write. The usual check compares the canonical path of the result with the target directory. Reported when the function creates such a file and calls none of canonicalPath, normalize, startsWith or contains("..").

From the rule's source, zip_entry_traversal.rs.

Example from the rule's tests
fun unpack(zip: ZipInputStream, target: File) {    var entry = zip.nextEntry    while (entry != null) {        val file = File(target, entry.name)tx0 reports: the entry name can contain ../        file.outputStream().use { zip.copyTo(it) }        entry = zip.nextEntry    }} fun unpackSafely(zip: ZipInputStream, target: File) {    val entry = zip.nextEntry    val file = File(target, entry.name)    require(file.canonicalPath.startsWith(target.canonicalPath))}
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:zip-entry-path-traversal" = "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:zip-entry-path-traversal] reason="..."

tx0 explain tx0/kotlin:zip-entry-path-traversal shows the same in the terminal. Found a false positive? Report it.