- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: kotlin-security/avoid-runtime-injection
Language: Kotlin
Severity: Error
Category: Security
CWE: 73
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.
class CommandExecutor {
fun executeCommand(userInput: String) {
val runtime = Runtime.getRuntime()
// Dangerous: Command injection possible
runtime.exec("ls " + userInput)
runtime.exec("/bin/sh -c ${userInput}")
runtime.exec(String.format("cat %s", userInput))
}
fun loadDynamicLibrary(libName: String) {
val runtime = Runtime.getRuntime()
// Dangerous: Path injection possible
runtime.loadLibrary("lib" + libName)
runtime.loadLibrary("lib ${libName}")
runtime.loadLibrary(String.format("%s.dll", libName))
}
}
class CommandExecutor {
fun executeCommand(userInput: String) {
val runtime = 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()
}
fun loadDynamicLibrary() {
val runtime = Runtime.getRuntime()
// Safe: Use fixed, known library names
runtime.loadLibrary("mylib")
// Alternative: Use allowlist for library names
val allowedLibs = setOf("lib1", "lib2")
if (libName in allowedLibs) {
runtime.loadLibrary(libName)
}
}
}