Skip to content

Commit

Permalink
add more validation for domain/s
Browse files Browse the repository at this point in the history
  • Loading branch information
komuw committed Jul 4, 2022
1 parent 81f31f7 commit 198af5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
14 changes: 9 additions & 5 deletions server/tls_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (
// getTlsConfig returns a proper tls configuration given the options passed in.
// The tls config may either procure certifiates from LetsEncrypt, from disk or be nil(for non-tls traffic)
func getTlsConfig(o opts, logger log.Logger) (*tls.Config, error) {
if o.tls.email != "" {
// 1. use letsencrypt.
//

if o.tls.enabled {
if err := validateDomain(o.tls.domain); err != nil {
return nil, err
}
}

if o.tls.email != "" {
// 1. use letsencrypt.
//
const letsEncryptProductionUrl = "https://acme-v02.api.letsencrypt.org/directory"
_ = letsEncryptProductionUrl
const letsEncryptStagingUrl = "https://acme-staging-v02.api.letsencrypt.org/directory"
Expand Down Expand Up @@ -90,14 +91,17 @@ func getTlsConfig(o opts, logger log.Logger) (*tls.Config, error) {

func validateDomain(domain string) error {
if len(domain) < 1 {
return ongErrors.New("domain cannot be empty if email is also specified")
return ongErrors.New("domain cannot be empty if email/certFile is also specified")
}
if strings.Count(domain, "*") > 1 {
return ongErrors.New("domain can only contain one wildcard character")
}
if strings.Contains(domain, "*") && !strings.HasPrefix(domain, "*") {
return ongErrors.New("wildcard character should be a prefix")
}
if strings.Contains(domain, "*") && domain[1] != '.' {
return ongErrors.New("wildcard character should be followed by a `.` character")
}

if !strings.Contains(domain, "*") {
// not wildcard
Expand Down
1 change: 1 addition & 0 deletions server/tls_conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func TestValidateDomain(t *testing.T) {
{"one.example.com", true},
//
{"*.example.org", true},
{"*example.org", false}, // wildcard character should be followed by a `.` character
{"*.example.*", false},
{"example.*org", false},
//
Expand Down

0 comments on commit 198af5d

Please sign in to comment.