From 10907c8b04a3d6d391ad0b4f4076532c8868adfe Mon Sep 17 00:00:00 2001 From: Sauyon Lee Date: Fri, 6 Dec 2019 17:33:57 -0800 Subject: [PATCH] IncompleteHostnameRegexp: disallow unescaped dot before TLD --- change-notes/1.24/analysis-go.md | 1 + .../CWE-020/IncompleteHostnameRegexp.go | 18 +++++++++--------- .../CWE-020/IncompleteHostnameRegexp.ql | 2 +- .../CWE-020/IncompleteHostnameRegexpGood.go | 18 +++++++++--------- .../IncompleteHostnameRegexp.expected | 10 +++++----- .../IncompleteHostnameRegexp.go | 18 +++++++++--------- .../IncompleteHostnameRegexpGood.go | 18 +++++++++--------- 7 files changed, 43 insertions(+), 42 deletions(-) diff --git a/change-notes/1.24/analysis-go.md b/change-notes/1.24/analysis-go.md index 9c0963a4374..47b3505b67f 100644 --- a/change-notes/1.24/analysis-go.md +++ b/change-notes/1.24/analysis-go.md @@ -13,3 +13,4 @@ |-----------------------------------------------------|------------------------------|-----------------------------------------------------------| | Reflected cross-site scripting (`go/reflected-xss`) | Fewer results | Untrusted input flowing into an HTTP header definition is no longer flagged, since this is often harmless. | | Useless assignment to field (`go/useless-assignment-to-field`) | Fewer false positives | The query now conservatively handles fields promoted through embedded pointer types. | +| Incomplete regular expression for hostnames (`go/incomplete-hostname-regexp`) | More results | The query now flags unescaped dots before the TLD in a hostname regex. | diff --git a/ql/src/Security/CWE-020/IncompleteHostnameRegexp.go b/ql/src/Security/CWE-020/IncompleteHostnameRegexp.go index cccf148c55a..073c8555efc 100644 --- a/ql/src/Security/CWE-020/IncompleteHostnameRegexp.go +++ b/ql/src/Security/CWE-020/IncompleteHostnameRegexp.go @@ -1,16 +1,16 @@ package main import ( - "errors" - "regexp" - "net/http" + "errors" + "net/http" + "regexp" ) func checkRedirect(req *http.Request, via []*http.Request) error { - // BAD: the host of `url` may be controlled by an attacker - re := "^((www|beta).)?example.com/" - if matched, _ := regexp.MatchString(re, req.URL.Host); matched { - return nil - } - return errors.New("Invalid redirect") + // BAD: the host of `req.URL` may be controlled by an attacker + re := "^((www|beta).)?example.com/" + if matched, _ := regexp.MatchString(re, req.URL.Host); matched { + return nil + } + return errors.New("Invalid redirect") } diff --git a/ql/src/Security/CWE-020/IncompleteHostnameRegexp.ql b/ql/src/Security/CWE-020/IncompleteHostnameRegexp.ql index 7a8559f041a..a9109e6a5ca 100644 --- a/ql/src/Security/CWE-020/IncompleteHostnameRegexp.ql +++ b/ql/src/Security/CWE-020/IncompleteHostnameRegexp.ql @@ -26,7 +26,7 @@ predicate isIncompleteHostNameRegexpPattern(string pattern, string hostPart) { "(?