Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow _ in domain code #210

Merged
merged 2 commits into from Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -25,5 +25,5 @@ require (
zgo.at/zpack v1.0.1
zgo.at/zstripe v1.0.0
zgo.at/ztest v0.0.0-20200316134318-cfad86d80b41
zgo.at/zvalidate v0.0.0-20200316145402-78e496f56ba0
zgo.at/zvalidate v0.0.0-20200319081247-6653bab59e5e
)
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -87,5 +87,5 @@ zgo.at/zstripe v1.0.0 h1:rfBrWEUpveYFZKNo4rdc3Fvhb9LEzbMIyL76EXAmXDk=
zgo.at/zstripe v1.0.0/go.mod h1:EqblFpMvXAhzZAUUt0EVom06EnN5+D5ESBTSOkwSwTY=
zgo.at/ztest v0.0.0-20200316134318-cfad86d80b41 h1:2vR+4hRs2YI2Lcm68AjHLSOHxQy/3XJ/b2nCV/dCrxs=
zgo.at/ztest v0.0.0-20200316134318-cfad86d80b41/go.mod h1:xGKONSdBgBNBcHRbi+yY9xFSre7ip8gRsQBS9QW/J2I=
zgo.at/zvalidate v0.0.0-20200316145402-78e496f56ba0 h1:doxAl28VsPfUUFs4pB/TNppFf7jLrtQ/cQvgxqiaMQ0=
zgo.at/zvalidate v0.0.0-20200316145402-78e496f56ba0/go.mod h1:SguuRnGmIa4boKWBN8G1l6Z1WWDGRDbkoY6KliwHXPg=
zgo.at/zvalidate v0.0.0-20200319081247-6653bab59e5e h1:6Rqe0hJ390YiocFdVckdlD/VklcMPW/65KBBuUWg9LA=
zgo.at/zvalidate v0.0.0-20200319081247-6653bab59e5e/go.mod h1:SguuRnGmIa4boKWBN8G1l6Z1WWDGRDbkoY6KliwHXPg=
14 changes: 4 additions & 10 deletions site.go
Expand Up @@ -155,6 +155,10 @@ func (s *Site) Validate(ctx context.Context) error {
v.Len("code", s.Code, 2, 50)
v.Len("name", s.Name, 4, 255)
v.Exclude("code", s.Code, reserved)
labels := v.Hostname("code", s.Code)
if len(labels) > 1 {
v.Append("code", "cannot contain '.'")
}
if s.Cname != nil {
v.Len("cname", *s.Cname, 4, 255)
v.Domain("cname", *s.Cname)
Expand All @@ -178,16 +182,6 @@ func (s *Site) Validate(ctx context.Context) error {
v.Append("stripe", "not a valid Stripe customer ID")
}

for _, c := range s.Code {
if !(c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
v.Append("code", fmt.Sprintf("%q not allowed; characters are limited to '_', '-', a to z, and numbers", c))
break
}
}
if len(s.Code) > 0 && (s.Code[0] == '_' || s.Code[0] == '-') { // Special domains, like _acme-challenge.
v.Append("code", "cannot start with underscore or dash (_, -)")
}

if !v.HasErrors() {
var code uint8
err := zdb.MustGet(ctx).GetContext(ctx, &code,
Expand Down
10 changes: 5 additions & 5 deletions site_test.go
Expand Up @@ -19,7 +19,7 @@ func TestSiteInsert(t *testing.T) {
ctx, clean := gctest.DB(t)
defer clean()

s := Site{Code: "the_code", Name: "the-code.com", Plan: PlanPersonal}
s := Site{Code: "the-code", Name: "the-code.com", Plan: PlanPersonal}
err := s.Insert(ctx)
if err != nil {
t.Fatal(err)
Expand All @@ -37,19 +37,19 @@ func TestSiteValidate(t *testing.T) {
want map[string][]string
}{
{
Site{Name: "Hello", Code: "hello-_0", State: StateActive, Plan: PlanPersonal},
Site{Name: "Hello", Code: "hello-0", State: StateActive, Plan: PlanPersonal},
nil,
nil,
},
{
Site{Name: "Hello", Code: "h€llo", State: StateActive, Plan: PlanPersonal},
nil,
map[string][]string{"code": {"'€' not allowed; characters are limited to '_', '-', a to z, and numbers"}},
map[string][]string{"code": {"must be a valid hostname: invalid character: '€'"}},
},
{
Site{Name: "Hello", Code: "-hello", State: StateActive, Plan: PlanPersonal},
Site{Name: "Hello", Code: "hel_lo", State: StateActive, Plan: PlanPersonal},
nil,
map[string][]string{"code": {"cannot start with underscore or dash (_, -)"}},
map[string][]string{"code": {"must be a valid hostname: invalid character: '_'"}},
},
{
Site{Name: "Hello", Code: "hello", State: StateActive, Plan: PlanPersonal},
Expand Down