This rule helps prevent severe security vulnerabilities such as command injection and path injection. Command injection occurs when an attacker can influence the formation of a system command that your app executes, potentially allowing them to execute arbitrary commands on your system. Path injection is similar but involves influencing file or library paths, which can lead to unauthorized file access or loading malicious libraries.
To avoid this, sanitize and validate user input before using it in a system command or file path. For example, you can use an allowlist of permitted commands or library names. Alternatively, you can use the array form of runtime.exec or ProcessBuilder, which doesn’t involve string concatenation or interpolation that could lead to command injection.
It’s essential to be aware of the risks and to validate and sanitize user input rigorously. It’s always safer to avoid using user input directly in system commands or file paths.
Non-Compliant Code Examples
classCommandExecutor{funexecuteCommand(userInput:String){valruntime=Runtime.getRuntime()// Dangerous: Command injection possible
runtime.exec("ls "+userInput)runtime.exec("/bin/sh -c ${userInput}")runtime.exec(String.format("cat %s",userInput))}funloadDynamicLibrary(libName:String){valruntime=Runtime.getRuntime()// Dangerous: Path injection possible
runtime.loadLibrary("lib"+libName)runtime.loadLibrary("lib ${libName}")runtime.loadLibrary(String.format("%s.dll",libName))}}
Compliant Code Examples
classCommandExecutor{funexecuteCommand(userInput:String){valruntime=Runtime.getRuntime()// Safe: Use array form with fixed command and arguments
runtime.exec(arrayOf("ls",userInput))// Safe: Use ProcessBuilder with argument list
ProcessBuilder("cat",userInput).redirectError(ProcessBuilder.Redirect.INHERIT).start()}funloadDynamicLibrary(){valruntime=Runtime.getRuntime()// Safe: Use fixed, known library names
runtime.loadLibrary("mylib")// Alternative: Use allowlist for library names
valallowedLibs=setOf("lib1","lib2")if(libNameinallowedLibs){runtime.loadLibrary(libName)}}}
Seamless integrations. Try Datadog Code Security
Datadog Code Security
Try this rule and analyze your code with Datadog Code Security
How to use this rule
1
2
rulesets:- kotlin-security # Rules to enforce Kotlin security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Security scans to your CI pipelines