通过堆栈跟踪暴露信息¶
ID: go/stack-trace-exposure
Kind: path-problem
Security severity: 5.4
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-209
- external/cwe/cwe-497
Query suites:
- go-code-scanning.qls
- go-security-extended.qls
- go-security-and-quality.qls
软件开发人员经常在错误消息中添加堆栈跟踪,作为调试辅助。每当最终用户遇到该错误消息时,开发人员可以使用堆栈跟踪来帮助确定如何解决问题。特别是,堆栈跟踪可以告诉开发人员更多关于导致故障的事件顺序,而不仅仅是发生错误时软件的最终状态。
不幸的是,相同的信息对攻击者也很有用。堆栈跟踪中的类名序列可以揭示应用程序的结构以及它所依赖的任何内部组件。
建议¶
向用户发送更通用的错误消息,以减少信息泄露。完全抑制堆栈跟踪,或者只在服务器上记录它。
示例¶
在以下示例中,以两种不同的方式处理了恐慌。在第一个版本(标记为 BAD)中,详细的堆栈跟踪被写入面向用户的 HTTP 响应对象,这可能会泄露敏感信息。在第二个版本中,错误消息仅记录在服务器上。这样,开发人员仍然可以访问和使用错误日志,但远程用户将看不到这些信息。
package example
import (
"log"
"net/http"
"runtime"
)
func handlePanic(w http.ResponseWriter, r *http.Request) {
buf := make([]byte, 2<<16)
buf = buf[:runtime.Stack(buf, true)]
// BAD: printing a stack trace back to the response
w.Write(buf)
// GOOD: logging the response to the server and sending
// a more generic message.
log.Printf("Panic: %s", buf)
w.Write([]byte("An unexpected runtime error occurred"))
}