Skip to content

Commit

Permalink
Merge pull request #931 from traPtitech/feat/domainRegexp
Browse files Browse the repository at this point in the history
ドメイン名に半角英数字を弾く正規表現を追加した
  • Loading branch information
Pugma authored Jul 3, 2024
2 parents 7b76a19 + 05c0d47 commit e3471ac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/domain/app_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"fmt"
"net/url"
"regexp"
"strings"

"github.com/friendsofgo/errors"
Expand All @@ -12,11 +13,19 @@ import (
"github.com/traPtitech/neoshowcase/pkg/util/ds"
)

var domainRegexp = regexp.MustCompile(`^[a-z0-9-_]+(\.[a-z0-9-_]+)*$`)

func ValidateDomain(domain string) error {
// ドメインが大文字を含むときはエラー
if domain != strings.ToLower(domain) {
return errors.Errorf("domain %v must be lower case", domain)
}

// 半角数字と英小文字以外を含むときはエラー
if !domainRegexp.MatchString(domain) {
return errors.Errorf("domain %v must consist of lower alpha-numeric letters, hyphen (-), or underscore (_)", domain)
}

// 面倒なのでtrailing dotは無しで統一
if strings.HasSuffix(domain, ".") {
return errors.Errorf("trailing dot not allowed in domain %v", domain)
Expand Down
4 changes: 2 additions & 2 deletions pkg/domain/app_website_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func TestValidateDomain(t *testing.T) {
}{
{"ok 1", "google.com", false},
{"ok 2", "hyphens-are-allowed.example.com", false},
{"ok 3", "日本語.jp", false},
{"ok 4", "underscore_allowed.example.com", false},
{"ok 3", "underscore_allowed.example.com", false},
{"invalid characters 1", "[email protected]", true},
{"invalid characters 2", "space not allowed.example.com", true},
{"invalid characters 3", "UPPERCASE.example.com", true},
{"invalid characters 4", "日本語.jp", true},
{"wildcard ng", "*.trap.show", true},
{"multi wildcard ng", "*.*.trap.show", true},
{"wildcard in middle", "trap.*.show", true},
Expand Down

0 comments on commit e3471ac

Please sign in to comment.