禁用的 TLS 证书检查¶
ID: go/disabled-certificate-check
Kind: problem
Security severity: 7.5
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-295
Query suites:
- go-code-scanning.qls
- go-security-extended.qls
- go-security-and-quality.qls
字段 InsecureSkipVerify
控制 TLS 客户端是否验证服务器的证书链和主机名。如果设置为 true
,则客户端将接受该证书中的任何证书和任何主机名,使其容易受到中间人攻击。
建议¶
除非在测试中,否则不要将 InsecureSkipVerify
设置为 true
。
示例¶
以下代码片段显示了一个函数,该函数在禁用了证书验证的情况下通过 TLS 执行 HTTP 请求
package main
import (
"crypto/tls"
"net/http"
)
func doAuthReq(authReq *http.Request) *http.Response {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
res, _ := client.Do(authReq)
return res
}
虽然这在测试中是可以接受的,但不应在生产代码中使用。相反,应配置证书以执行验证。