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 16, 2024
1 parent a99849d commit dba8a0e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions go/ql/src/Security/CWE-020/MissingRegexpAnchor.qhelp
Original file line number Diff line number Diff line change
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
17 changes: 17 additions & 0 deletions go/ql/src/Security/CWE-020/MissingRegexpAnchorGoodDomain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"regexp"
)

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

// Alternatively, 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 dba8a0e

Please sign in to comment.