[英]How to handle gosec linter warning: Potential file inclusion via variable
[英]GoLang untaint URL variable to fix gosec warning G107
If I run gosec on the below fragment I get a tainted URL warning: G107 (CWE-88): Potential HTTP request made with variable url (Confidence: MEDIUM, Severity: MEDIUM)
我想我應該使用'url' package 但它似乎並沒有提供比 ParseQuery() 更多的功能來檢測這個,但盡管它給出了一個錯誤,gosec 仍然報告為一個潛在的漏洞。
如何編寫刪除警告,理想情況下僅使用標准庫?
func Run() {
MakeGetRequest("https://www.google.com/hi?a=7; rm /* -rf")
}
func MakeGetRequest(uri string) {
res, _ := http.Get(uri)
fmt.Println(res)
}
根據G107
提到的指南,您應該在const
中提到url
。
package main
import (
"fmt"
"net/http"
)
const url = "url"
func main() {
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
fmt.Println(resp.Status)
}
為了更好地理解你可以參考這里: https://securego.io/docs/rules/g107.html
或者
如果要刪除G107
警告,則應明確排除它。
# Run a specific set of rules
$ gosec -include=G101,G203,G401 ./...
# Run everything except for rule G303
$ gosec -exclude=G303 ./...
# folders and files also can be excluded.
更多了解請參考 gosec 文檔: https://github.com/securego/gosec
如果您正在使用 golangci-lint,並希望它簡單地忽略此警告,因為您無法將 url 設置為常量,您可以使用//nolint
指令,如下所示:
func Run() {
MakeGetRequest("https://www.google.com/hi?a=7; rm /* -rf")
}
func MakeGetRequest(uri string) {
res, _ := http.Get(uri) //nolint
fmt.Println(res)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.