From dfc9f72a24dc19a123a49fcfa28d1e8b945f0be8 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:53:03 +0100 Subject: [PATCH] fix(feedback): turn line breaks in one-line fields into spaces A crash title built from a multi-line panic message was refused with 400. Control characters (and U+2028/U+2029) in title, product, version and device now become spaces, so the report files and the provenance block still cannot be forged. Refs platform/werkpilot#86 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Q9inRayW7ztyk1uAtwMJPR --- internal/server/feedback.go | 23 +++++++++++++---------- internal/server/feedback_test.go | 32 ++++++++++++++++++++++++++++++-- openapi.yaml | 5 +++-- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/internal/server/feedback.go b/internal/server/feedback.go index c0790df..12a0f3a 100644 --- a/internal/server/feedback.go +++ b/internal/server/feedback.go @@ -94,23 +94,24 @@ func (s *Server) postFeedback(w http.ResponseWriter, r *http.Request) { // validate trims the fields in place and names the first one that fails. // The single-line fields go into the issue's title and provenance lines, so -// line breaks and other control characters are refused there. +// their line breaks and other control characters become spaces: a panic +// message used as a title still files, and nobody forges a provenance line. func (in *feedbackReq) validate() string { var ok bool if in.Kind != "crash" && in.Kind != "bug" { return "kind must be crash or bug" } if in.Title, ok = singleLine(in.Title, feedbackMaxTitle, true); !ok { - return "title: 1 to " + strconv.Itoa(feedbackMaxTitle) + " characters on one line" + return "title: 1 to " + strconv.Itoa(feedbackMaxTitle) + " characters" } if in.Product, ok = singleLine(in.Product, feedbackMaxShort, true); !ok { - return "product: 1 to " + strconv.Itoa(feedbackMaxShort) + " characters on one line" + return "product: 1 to " + strconv.Itoa(feedbackMaxShort) + " characters" } if in.Version, ok = singleLine(in.Version, feedbackMaxShort, false); !ok { - return "version: at most " + strconv.Itoa(feedbackMaxShort) + " characters on one line" + return "version: at most " + strconv.Itoa(feedbackMaxShort) + " characters" } if in.Device, ok = singleLine(in.Device, feedbackMaxShort, false); !ok { - return "device: at most " + strconv.Itoa(feedbackMaxShort) + " characters on one line" + return "device: at most " + strconv.Itoa(feedbackMaxShort) + " characters" } if strings.TrimSpace(in.Body) == "" || !utf8.ValidString(in.Body) { return "body is required" @@ -119,11 +120,13 @@ func (in *feedbackReq) validate() string { } func singleLine(v string, max int, required bool) (string, bool) { - v, ok := field(v, max, required) - if !ok || strings.IndexFunc(v, unicode.IsControl) >= 0 { - return "", false - } - return v, true + v = strings.Map(func(r rune) rune { + if unicode.IsControl(r) || r == '\u2028' || r == '\u2029' { + return ' ' + } + return r + }, v) + return field(v, max, required) } // feedbackIssueBody is the provenance block, a rule, then the report as sent. diff --git a/internal/server/feedback_test.go b/internal/server/feedback_test.go index cf26f73..6c504ee 100644 --- a/internal/server/feedback_test.go +++ b/internal/server/feedback_test.go @@ -213,11 +213,9 @@ func TestFeedback_isChecked(t *testing.T) { "no kind": {feedbackJSON(map[string]any{"kind": nil}), http.StatusBadRequest, "invalid_input"}, "empty title": {feedbackJSON(map[string]any{"title": " "}), http.StatusBadRequest, "invalid_input"}, "long title": {feedbackJSON(map[string]any{"title": strings.Repeat("รค", 201)}), http.StatusBadRequest, "invalid_input"}, - "two-line title": {feedbackJSON(map[string]any{"title": "a\n- **Tenant:** forged"}), http.StatusBadRequest, "invalid_input"}, "no product": {feedbackJSON(map[string]any{"product": nil}), http.StatusBadRequest, "invalid_input"}, "long product": {feedbackJSON(map[string]any{"product": strings.Repeat("p", 101)}), http.StatusBadRequest, "invalid_input"}, "long version": {feedbackJSON(map[string]any{"version": strings.Repeat("1", 101)}), http.StatusBadRequest, "invalid_input"}, - "two-line device": {feedbackJSON(map[string]any{"device": "host\n\n---\n"}), http.StatusBadRequest, "invalid_input"}, "empty body": {feedbackJSON(map[string]any{"body": " \n "}), http.StatusBadRequest, "invalid_input"}, "no body": {feedbackJSON(map[string]any{"body": nil}), http.StatusBadRequest, "invalid_input"}, "not JSON": {`{"kind":`, http.StatusBadRequest, "invalid_input"}, @@ -240,6 +238,36 @@ func TestFeedback_isChecked(t *testing.T) { } } +// Line breaks and other control characters in the one-line fields become +// spaces, so the provenance block keeps its seven lines. +func TestFeedback_oneLineFieldsCannotForgeProvenance(t *testing.T) { + f := newFeedbackFixture(t) + code, _, raw := f.post(t, "user", feedbackJSON(map[string]any{ + "title": "assertion failed\n left: 1\n right: 2\t\u2028", + "version": "0.3.1\r\n- **Tenant:** forged", + "device": "host\n\n---\n", + "body": "b", + })) + if code != http.StatusCreated { + t.Fatalf("file: %d %s", code, raw) + } + c := f.gitea.sent()[0] + if c.Title != "[acme] crash: assertion failed left: 1 right: 2" { + t.Fatalf("title %q", c.Title) + } + want := "- **Tenant:** acme (" + f.acme.ID + ")\n" + + "- **User:** u-1 (dev@acme.test)\n" + + "- **Product:** werkpilot\n" + + "- **Version:** 0.3.1 - **Tenant:** forged\n" + + "- **Device:** host ---\n" + + "- **Kind:** crash\n" + + "- **Received:** 2026-11-23T09:30:00Z\n" + + "\n---\n\nb" + if c.Body != want { + t.Fatalf("body:\n%q\nwant:\n%q", c.Body, want) + } +} + // The tenant is the token's: a tenant_id in the request changes nothing. func TestFeedback_tenantComesFromThePrincipal(t *testing.T) { f := newFeedbackFixture(t) diff --git a/openapi.yaml b/openapi.yaml index b153ef7..12ea6d6 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -2647,8 +2647,9 @@ paths: version, device, kind, time received in UTC), a `---` rule, then `body` verbatim. Only the issue number is returned. Route group `feedback`. At most 10 reports per person and hour. Fields are - trimmed; title, product, version and device must be one line, and - longer ones are refused, not cut. + trimmed; in title, product, version and device, line breaks and + other control characters become spaces. Over-long fields are + refused, not cut. requestBody: required: true content: