Path traversal High

Creating file paths from untrusted input could allow a malicious actor to access arbitrary files on a disk by manipulating the file name in the path.

Detector ID
kotlin/path-traversal@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1// Noncompliant: Using untrusted inputs to access a file path
2fun noncompliant() {
3    print("Enter your filename:")
4    val filename = readLine()
5
6    val file = File(filename)
7    val lines = file.readLines()
8    for (line in lines) {
9        println(line)
10    }
11}

Compliant example

1// Compliant: Using safe input to access a file path
2fun compliant(filename: String) {
3    val file = File(filename)
4    val lines = file.readLines()
5    for (line in lines) {
6        println(line)
7    }
8}