Skip to content

Commit d5209d4

Browse files
committed
add example for domain names with sub-domains to missing-regexp-anchor
1 parent a99849d commit d5209d4

File tree

2 files changed

+22
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)