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)}}}
원활한 통합. Datadog Code Security 사용해 보기
Datadog Code Security
이 규칙을 사용해 Datadog Code Security로 코드 분석해 보기
이 규칙의 사용법
1
2
rulesets:- kotlin-security # Rules to enforce Kotlin security.
리포지토리 루트에서 위의 콘텐츠를 사용해 static-analysis.datadog.yml 생성
Datadog의 무료 IDE Plugins를 사용하거나 Code Security 스캔을 CI 파이프라인에 추가