Skip to content

Commit

Permalink
Merge pull request #16220 from erik-krogh/domainAnc
Browse files Browse the repository at this point in the history
Go: Add an example specific to domain names in missing-regexp-anchor
  • Loading branch information
erik-krogh committed May 11, 2024
2 parents e7092b4 + 462e564 commit 0d814e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions go/ql/src/Security/CWE-020/MissingRegexpAnchor.qhelp
Expand Up @@ -45,6 +45,12 @@ one of the alternatives. As an example, the regular expression
<code>(^www\.example\.com)|(beta\.example\.com)/</code>, so the second alternative
<code>beta\.example\.com</code> is not anchored at the beginning of the string.
</p>

<p>
When checking for a domain name that may have subdomains, it is important to anchor the regular expression
or ensure that the domain name is prefixed with a dot.
</p>
<sample src="MissingRegexpAnchorGoodDomain.go"/>
</example>

<references>
Expand Down
20 changes: 20 additions & 0 deletions go/ql/src/Security/CWE-020/MissingRegexpAnchorGoodDomain.go
@@ -0,0 +1,20 @@
package main

import (
"regexp"
)

func checkSubdomain(domain String) {
// Checking strictly that the domain is `example.com`.
re := "^example\\.com$"
if matched, _ := regexp.MatchString(re, domain); matched {
// domain is good.
}

// GOOD: Alternatively, check the domain is `example.com` or a subdomain of `example.com`.
re2 := "(^|\\.)example\\.com$"

if matched, _ := regexp.MatchString(re2, domain); matched {
// domain is good.
}
}

0 comments on commit 0d814e0

Please sign in to comment.