简体   繁体   English

GoLang untaint URL 变量修复 gosec 警告 G107

[英]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) 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)

I figured I should use the 'url' package but it doesn't seem to offer more than ParseQuery() to detect this, but although it gives an error, gosec still reports as a potential vulnerability.我想我应该使用'url' package 但它似乎并没有提供比 ParseQuery() 更多的功能来检测这个,但尽管它给出了一个错误,gosec 仍然报告为一个潜在的漏洞。

How to I write remove the warning, ideally using just the standard library?如何编写删除警告,理想情况下仅使用标准库?

func Run() {
    MakeGetRequest("https://www.google.com/hi?a=7; rm /* -rf")
}

func MakeGetRequest(uri string) {
    res, _ := http.Get(uri)
    fmt.Println(res)
}

As per guidelines mentioned for G107 you should mentioned the url in const .根据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)
}

For better understanding you can refer here: https://securego.io/docs/rules/g107.html为了更好地理解你可以参考这里: https://securego.io/docs/rules/g107.html

OR或者

If you want to remove G107 warning then you should explicitly exclude it.如果要删除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.

For more understanding please refer gosec docs: https://github.com/securego/gosec更多了解请参考 gosec 文档: https://github.com/securego/gosec

If you are using golangci-lint, and want it to simply ignore this warning since you cannot set the url as a constant, you can use //nolint directive like this:如果您正在使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM