Rule Definition
Using 'Throwable.printStackTrace()' usually means that it is used to log the stack of errors, which has the risk of filling up the stdout or the log file. Moreover, it is accessible to end-user which is bad for security and user-experience.
It is recommended to use a Logger instead.
Remediation
1. Loggers should be used instead to print Throwables, as they have many advantages:Users are able to easily retrieve the logs.
The format of log messages is uniform and allows users to browse the logs easily.
2. Define a specific stream where the stack trace is to be printed.
Violation Code Sample
fun main(args: Array<String>){
try {
test()
} catch (e: UnsupportedOperationException) {
e.printStackTrace()
}
}
Fixed Code Sample
fun main(args: Array<String>){
try {
test()
} catch (e: UnsupportedOperationException) {
LOGGER.log("context",e) //Violation Fixed: Logger is used
}
}
________________________________________________
fun main(args: Array<String>){
try {
test()
} catch (e: UnsupportedOperationException) {
e.printStackTrace(stream: PrintStream) //Violation fixed: A specific stream is defined to print the stack trace
}
}
Reference
http://cwe.mitre.org/data/definitions/489.html
OWASP Top 10 2017 Category A3 - Sensitive Data Exposure
Related Technologies
Technical Criterion
PCI-DSS4-Requirement-6.2.4 - Software engineering techniques or other methods are defined and in use by software development personnel to prevent or mitigate common software attacks and related vulnerabilities
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.