Skip to content

Commit 0d814e0

Browse files
authored
Merge pull request github#16220 from erik-krogh/domainAnc
Go: Add an example specific to domain names in missing-regexp-anchor
2 parents e7092b4 + 462e564 commit 0d814e0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

go/ql/src/Security/CWE-020/MissingRegexpAnchor.qhelp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ one of the alternatives. As an example, the regular expression
4545
<code>(^www\.example\.com)|(beta\.example\.com)/</code>, so the second alternative
4646
<code>beta\.example\.com</code> is not anchored at the beginning of the string.
4747
</p>
48+
49+
<p>
50+
When checking for a domain name that may have subdomains, it is important to anchor the regular expression
51+
or ensure that the domain name is prefixed with a dot.
52+
</p>
53+
<sample src="MissingRegexpAnchorGoodDomain.go"/>
4854
</example>
4955

5056
<references>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
func checkSubdomain(domain String) {
8+
// Checking strictly that the domain is `example.com`.
9+
re := "^example\\.com$"
10+
if matched, _ := regexp.MatchString(re, domain); matched {
11+
// domain is good.
12+
}
13+
14+
// GOOD: Alternatively, check the domain is `example.com` or a subdomain of `example.com`.
15+
re2 := "(^|\\.)example\\.com$"
16+
17+
if matched, _ := regexp.MatchString(re2, domain); matched {
18+
// domain is good.
19+
}
20+
}

0 commit comments

Comments
 (0)