Titles are one line: clean them server-side; the large title's field has a maxlength
Fable on #179 (minor): a title arrived with whatever its sender put in it, newlines included, and the rename handler only trimmed the ends. cleanTitle (handlers.go) turns newlines, tabs, every other control character and the Unicode line and paragraph separators into spaces, collapses runs, and trims, for a rename and a new document alike. The large title's field takes at most 256 characters, which are at most the 1024 bytes the handler accepts. TestATitleIsOneLineOfText fails without it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+41
−3
internal/docs/handlers.go+15 −2internal/docs/phone_test.go+25 −0internal/docs/views/show.html+1 −1
diff --git a/internal/docs/handlers.go b/internal/docs/handlers.go| index e0e327e..018aae5 100644 |
| --- a/internal/docs/handlers.go |
| +++ b/internal/docs/handlers.go |
| @@ -13,6 +13,7 @@ import ( |
| "strconv" |
| "strings" |
| "time" |
| + "unicode" |
| "github.com/google/uuid" |
| "gorm.io/gorm" |
| @@ -426,7 +427,7 @@ func (h *handlers) create(w http.ResponseWriter, r *http.Request) { |
| } |
| token = parsed.String() |
| } |
| - title := strings.TrimSpace(r.PostFormValue("title")) |
| + title := cleanTitle(r.PostFormValue("title")) |
| if len(title) > 1024 { |
| h.fail(w, r, http.StatusBadRequest, failStatus) |
| return |
| @@ -1316,6 +1317,18 @@ func formatWhen(now, t time.Time) string { |
| return t.Format("Jan 2, 2006") |
| } |
| +// cleanTitle makes a title one line of text, whatever sent it: newlines, |
| +// tabs and every other control character (and the Unicode line and |
| +// paragraph separators) become spaces, a run of spaces becomes one, and |
| +// the ends are trimmed. A title is a heading, a list row and a file name; |
| +// a newline in it is never something the person meant to keep (Fable on |
| +// #179). |
| +func cleanTitle(s string) string { |
| + return strings.Join(strings.FieldsFunc(s, func(r rune) bool { |
| + return unicode.IsSpace(r) || unicode.IsControl(r) || r == '\u2028' || r == '\u2029' |
| + }), " ") |
| +} |
| + |
| func (h *handlers) rename(w http.ResponseWriter, r *http.Request) { |
| doc, ok := h.getDocumentForUpdates(w, r) |
| if !ok { |
| @@ -1331,7 +1344,7 @@ func (h *handlers) rename(w http.ResponseWriter, r *http.Request) { |
| h.fail(w, r, http.StatusBadRequest, failStatus) |
| return |
| } |
| - title := strings.TrimSpace(r.PostFormValue("title")) |
| + title := cleanTitle(r.PostFormValue("title")) |
| if title == "" { |
| title = rastrillo.T(r, "docs.document.untitled") |
| } |
diff --git a/internal/docs/phone_test.go b/internal/docs/phone_test.go| index 50c7a25..51a8fc6 100644 |
| --- a/internal/docs/phone_test.go |
| +++ b/internal/docs/phone_test.go |
| @@ -2,6 +2,7 @@ package docs_test |
| import ( |
| "net/http" |
| + "net/url" |
| "regexp" |
| "strings" |
| "testing" |
| @@ -218,3 +219,27 @@ func TestTheLargeTitle(t *testing.T) { |
| t.Error("the floating save status is gone") |
| } |
| } |
| + |
| +// A title is one line of text: a rename or a new document's title loses |
| +// its newlines, tabs and other control characters to spaces, server-side, |
| +// whatever sent it (Fable on #179). The large title's field holds at most |
| +// what the handler takes: 256 characters are at most 1024 bytes. |
| +func TestATitleIsOneLineOfText(t *testing.T) { |
| + app, _ := docs.NewForTest(t) |
| + alice := signInCookies(t, app, "alice@example.com") |
| + sqid := createDoc(t, app, alice, "Minutes") |
| + if rec := post(t, app, alice, "/d/"+sqid+"/title", "title="+url.QueryEscape("Board\nminutes\r\n\tdraft\x07 two")); rec.Code != http.StatusSeeOther { |
| + t.Fatalf("rename: %d", rec.Code) |
| + } |
| + body := get(t, app, alice, "/d/"+sqid).Body.String() |
| + if !strings.Contains(body, `value="Board minutes draft two"`) { |
| + t.Errorf("the renamed title kept a control character or a run of spaces:\n%s", section(t, body, "<input docs-title-input", ">")) |
| + } |
| + made := createDoc(t, app, alice, "New\nline") |
| + if !strings.Contains(get(t, app, alice, "/d/"+made).Body.String(), `value="New line"`) { |
| + t.Error("a new document's title kept its newline") |
| + } |
| + if !strings.Contains(section(t, body, "<input docs-title-input", ">"), ` maxlength="256"`) { |
| + t.Error("the large title's field has no maxlength") |
| + } |
| +} |
diff --git a/internal/docs/views/show.html b/internal/docs/views/show.html| index 131deca..a858cc1 100644 |
| --- a/internal/docs/views/show.html |
| +++ b/internal/docs/views/show.html |
| @@ -1859,7 +1859,7 @@ |
| {{- if .CanWrite}} |
| <form docs-title-form method="post" action="/d/{{.Sqid}}/title"> |
| <h1 docs-title dir="auto"><button type="button" docs-title-edit><span docs-title-text>{{.Title}}</span></button></h1> |
| -<input docs-title-input id="docs-title-input" name="title" value="{{.Title}}" aria-label="{{.Large.TitleLabel}}" aria-describedby="docs-title-hint" autocomplete="off" spellcheck="false" dir="auto" enterkeyhint="done"> |
| +<input docs-title-input id="docs-title-input" name="title" value="{{.Title}}" maxlength="256" aria-label="{{.Large.TitleLabel}}" aria-describedby="docs-title-hint" autocomplete="off" spellcheck="false" dir="auto" enterkeyhint="done"> |
| <p docs-title-hint id="docs-title-hint">{{.Large.TitleHint}}</p> |
| <button rst-btn type="submit" docs-title-submit>{{.Large.Rename}}</button> |
| </form> |