fix(api): validate list name, domain and title on create
createList accepted a title with CR/LF (every later post to the list then failed to rewrite and got 451) and names with '/', '<', '>' or tabs. - name: lowercased, 1 to 30 characters of a-z 0-9 . _ -, no leading or trailing dot, no ".." - domain: lowercased ASCII host name, labels of 1 to 63 characters - title: non-empty single line, at most 200 characters, no control characters - updateList uses the same title rule (it also refuses tabs and long titles now) - tests: refused names, domains and titles, the accepted limits, nothing created for a bad title
This commit is contained in:
parent
28b73fa24a
commit
b8fcbf9acf
2 changed files with 139 additions and 7 deletions
|
|
@ -12,6 +12,8 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"codeberg.org/squidd/parsimony/internal/archive"
|
||||
"codeberg.org/squidd/parsimony/internal/store"
|
||||
|
|
@ -184,6 +186,53 @@ func (s *server) listLists(w http.ResponseWriter, _ *http.Request) {
|
|||
writeJSON(w, http.StatusOK, out)
|
||||
}
|
||||
|
||||
const (
|
||||
maxNameLen = 30 // keeps list+confirm-<token> and list+bounce-<id>-<mac> within 64 characters
|
||||
maxTitleLen = 200
|
||||
titleHint = "title must be a non-empty single line of at most 200 characters, without control characters"
|
||||
)
|
||||
|
||||
// validName reports whether name (already lowercased) works as the local part of a list address.
|
||||
func validName(name string) bool {
|
||||
if name == "" || len(name) > maxNameLen || strings.HasPrefix(name, ".") || strings.HasSuffix(name, ".") || strings.Contains(name, "..") {
|
||||
return false
|
||||
}
|
||||
for _, c := range name {
|
||||
if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '.' && c != '_' && c != '-' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// validDomain reports whether d (already lowercased) is an ASCII host name: dot-separated labels of
|
||||
// 1 to 63 letters, digits and hyphens, none starting or ending with a hyphen.
|
||||
func validDomain(d string) bool {
|
||||
if d == "" || len(d) > 253 {
|
||||
return false
|
||||
}
|
||||
for _, label := range strings.Split(d, ".") {
|
||||
if label == "" || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
|
||||
return false
|
||||
}
|
||||
for _, c := range label {
|
||||
if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// validTitle reports whether t is a usable list title: not empty, at most maxTitleLen characters,
|
||||
// no line breaks or other control characters (it goes into mail headers).
|
||||
func validTitle(t string) bool {
|
||||
if t == "" || utf8.RuneCountInString(t) > maxTitleLen {
|
||||
return false
|
||||
}
|
||||
return !strings.ContainsFunc(t, unicode.IsControl)
|
||||
}
|
||||
|
||||
func (s *server) createList(w http.ResponseWriter, r *http.Request) {
|
||||
var in struct {
|
||||
Name string `json:"name"`
|
||||
|
|
@ -193,13 +242,22 @@ func (s *server) createList(w http.ResponseWriter, r *http.Request) {
|
|||
if !decodeBody(w, r, &in) {
|
||||
return
|
||||
}
|
||||
in.Name, in.Domain = strings.TrimSpace(in.Name), strings.TrimSpace(in.Domain)
|
||||
if in.Name == "" || in.Domain == "" || strings.ContainsAny(in.Name+in.Domain, "@+ \r\n") {
|
||||
writeError(w, http.StatusBadRequest, "name and domain are required and must not contain '@', '+' or spaces")
|
||||
typedName := strings.TrimSpace(in.Name)
|
||||
in.Name, in.Domain = strings.ToLower(typedName), strings.ToLower(strings.TrimSpace(in.Domain))
|
||||
if !validName(in.Name) {
|
||||
writeError(w, http.StatusBadRequest, "name must be 1 to 30 characters of a-z, 0-9, '.', '_' or '-', without a dot at either end or two dots in a row")
|
||||
return
|
||||
}
|
||||
if in.Title == "" {
|
||||
in.Title = in.Name
|
||||
if !validDomain(in.Domain) {
|
||||
writeError(w, http.StatusBadRequest, "domain must be a host name such as lists.example.org")
|
||||
return
|
||||
}
|
||||
if in.Title = strings.TrimSpace(in.Title); in.Title == "" {
|
||||
in.Title = typedName
|
||||
}
|
||||
if !validTitle(in.Title) {
|
||||
writeError(w, http.StatusBadRequest, titleHint)
|
||||
return
|
||||
}
|
||||
l, err := s.store.CreateList(in.Name, in.Domain, in.Title)
|
||||
if err != nil {
|
||||
|
|
@ -248,8 +306,8 @@ func (s *server) updateList(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
if in.Title != nil {
|
||||
title := strings.TrimSpace(*in.Title)
|
||||
if title == "" || strings.ContainsAny(title, "\r\n") {
|
||||
writeError(w, http.StatusBadRequest, "title must be a non-empty single line")
|
||||
if !validTitle(title) {
|
||||
writeError(w, http.StatusBadRequest, titleHint)
|
||||
return
|
||||
}
|
||||
l.Title = title
|
||||
|
|
|
|||
|
|
@ -152,6 +152,34 @@ func TestCreateListValidation(t *testing.T) {
|
|||
`{"name":"a+b","domain":"example.org"}`,
|
||||
`{"name":"dev","domain":""}`,
|
||||
`{"name":"dev","domain":"example.org","bogus":1}`,
|
||||
// Names that break an address: separators, brackets, whitespace, control characters, dots at the ends.
|
||||
`{"name":"a/b","domain":"example.org"}`,
|
||||
`{"name":"a<b","domain":"example.org"}`,
|
||||
`{"name":"a>b","domain":"example.org"}`,
|
||||
`{"name":"a\tb","domain":"example.org"}`,
|
||||
`{"name":"a b","domain":"example.org"}`,
|
||||
`{"name":"a\"b","domain":"example.org"}`,
|
||||
`{"name":"caffè","domain":"example.org"}`,
|
||||
`{"name":".dev","domain":"example.org"}`,
|
||||
`{"name":"dev.","domain":"example.org"}`,
|
||||
`{"name":"a..b","domain":"example.org"}`,
|
||||
`{"name":"` + strings.Repeat("a", 31) + `","domain":"example.org"}`,
|
||||
// Domains that are not host names.
|
||||
`{"name":"dev","domain":"exa mple.org"}`,
|
||||
`{"name":"dev","domain":"example..org"}`,
|
||||
`{"name":"dev","domain":"-example.org"}`,
|
||||
`{"name":"dev","domain":"example-.org"}`,
|
||||
`{"name":"dev","domain":"example.org."}`,
|
||||
`{"name":"dev","domain":"ex_ample.org"}`,
|
||||
`{"name":"dev","domain":"exämple.org"}`,
|
||||
`{"name":"dev","domain":"a/b.org"}`,
|
||||
`{"name":"dev","domain":"` + strings.Repeat("a", 64) + `.org"}`,
|
||||
// Titles that are not one clean line.
|
||||
`{"name":"dev","domain":"example.org","title":"Dev\r\nBcc: x@y.com"}`,
|
||||
`{"name":"dev","domain":"example.org","title":"Dev\nlist"}`,
|
||||
`{"name":"dev","domain":"example.org","title":"Dev\tlist"}`,
|
||||
`{"name":"dev","domain":"example.org","title":"Dev\u0000list"}`,
|
||||
`{"name":"dev","domain":"example.org","title":"` + strings.Repeat("x", 201) + `"}`,
|
||||
} {
|
||||
rec := do(t, h, "POST", "/api/v1/lists", body, true)
|
||||
if rec.Code != 400 {
|
||||
|
|
@ -732,3 +760,49 @@ func TestEveryRouteNeedsTheToken(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateListAcceptsTheLimits(t *testing.T) {
|
||||
h := newServer(t)
|
||||
for _, body := range []string{
|
||||
`{"name":"` + strings.Repeat("a", 30) + `","domain":"example.org","title":"` + strings.Repeat("è", 200) + `"}`,
|
||||
`{"name":"dev-team_1.eu","domain":"lists.sub-domain.example.org"}`,
|
||||
`{"name":"Dev2","domain":"localhost","title":"Développeurs ☕"}`,
|
||||
} {
|
||||
if rec := do(t, h, "POST", "/api/v1/lists", body, true); rec.Code != 201 {
|
||||
t.Errorf("%.80q: status %d, body %s", body, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A title with a line break would make every later post to the list fail to rewrite, so the list
|
||||
// must not come into existence at all.
|
||||
func TestCreateListWithABadTitleLeavesNothingBehind(t *testing.T) {
|
||||
h := newServer(t)
|
||||
expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev\r\nX: y"}`, true), 400)
|
||||
rec := do(t, h, "GET", "/api/v1/lists", "", true)
|
||||
if strings.TrimSpace(rec.Body.String()) != "[]" {
|
||||
t.Fatalf("the refused list was created: %s", rec.Body.String())
|
||||
}
|
||||
// The name is free for a good request.
|
||||
expect(t, do(t, h, "POST", "/api/v1/lists", `{"name":"dev","domain":"example.org","title":"Dev"}`, true), 201)
|
||||
}
|
||||
|
||||
func TestCreateListLowercasesNameAndDomain(t *testing.T) {
|
||||
h := newServer(t)
|
||||
rec := do(t, h, "POST", "/api/v1/lists", `{"name":" Dev ","domain":"Example.ORG"}`, true)
|
||||
expect(t, rec, 201)
|
||||
var l listJSON
|
||||
decode(t, rec, &l)
|
||||
if l.Address != "dev@example.org" || l.Title != "Dev" {
|
||||
t.Fatalf("%+v", l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateListTitleUsesTheSameRules(t *testing.T) {
|
||||
h := newServer(t)
|
||||
createList(t, h)
|
||||
for _, title := range []string{`"a\tb"`, `"` + strings.Repeat("x", 201) + `"`, `" "`, `"a\r\nb"`} {
|
||||
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"title":`+title+`}`, true), 400)
|
||||
}
|
||||
expect(t, do(t, h, "PATCH", "/api/v1/lists/1", `{"title":"`+strings.Repeat("x", 200)+`"}`, true), 200)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue