Merge pull request #15933 from github/max-schaefer/go-incomplete-hostname-regex

Go: Mention raw string iterals in QHelp for `go/incomplete-hostname-regexp`.
This commit is contained in:
Max Schaefer
2024-03-15 15:07:10 +00:00
committed by GitHub
2 changed files with 20 additions and 0 deletions

View File

@@ -41,6 +41,10 @@ domain such as <code>wwwXexample.com</code>.
Address this vulnerability by escaping <code>.</code> appropriately:
</p>
<sample src="IncompleteHostnameRegexpGood.go"/>
<p>
You may also want to consider using raw string literals to avoid having to escape backslashes:
</p>
<sample src="IncompleteHostnameRegexpGood2.go"/>
</example>
<references>

View File

@@ -0,0 +1,16 @@
package main
import (
"errors"
"net/http"
"regexp"
)
func checkRedirectGood(req *http.Request, via []*http.Request) error {
// GOOD: the host of `req.URL` must be `example.com`, `www.example.com` or `beta.example.com`
re := `^((www|beta)\.)?example\.com/`
if matched, _ := regexp.MatchString(re, req.URL.Host); matched {
return nil
}
return errors.New("Invalid redirect")
}