Skip to content

Commit

Permalink
add example for domain names with sub-domains to missing-regexp-anchor
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-krogh committed Apr 15, 2024
1 parent a99849d commit d5209d4
Show file tree
Hide file tree
Showing 2 changed files with 22 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 with 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
16 changes: 16 additions & 0 deletions go/ql/src/Security/CWE-020/MissingRegexpAnchorGoodDomain.go
@@ -0,0 +1,16 @@
package main

import (
"regexp"
)

func checkSubdomain(domain String) {
// GOOD: Checking the domain is `example.com` or a subdomain of `example.com`.
re := "(^|\\.)example\\.com$"
// GOOD: Checking strictly that the domain is `example.com`.
re2 := "^example\\.com$"

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

0 comments on commit d5209d4

Please sign in to comment.