Files
tenant-registry/internal/server/feedback.go
Sharang ParnerkarandClaude Opus 5.5 dfc9f72a24
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 6m49s
ci / image (pull_request) Skipped
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q9inRayW7ztyk1uAtwMJPR
2026-11-23 13:53:03 +01:00

154 lines
5.5 KiB
Go

package server
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"
"git.breakpilot.com/platform/tenant-registry/internal/authn"
"git.breakpilot.com/platform/tenant-registry/internal/store"
)
// ─── feedback (werkpilot#86) ──────────────────────────────────────────────
//
// `werkpilot` sends crash reports and /bug reports with the user's own
// token. Each becomes an issue in a private Gitea repo, headed by where it
// came from; the tenant is the token's, never the caller's say. The caller
// gets the issue number only: the repo is internal.
const (
feedbackMaxBody = 200000
feedbackMaxTitle = 200
feedbackMaxShort = 100
feedbackPerHour = 10
feedbackMaxRequest = 2 << 20 // the body JSON-escaped, with room to spare
)
type feedbackReq struct {
Product string `json:"product"`
Kind string `json:"kind"`
Title string `json:"title"`
Body string `json:"body"`
Version string `json:"version"`
Device string `json:"device"`
}
func (s *Server) postFeedback(w http.ResponseWriter, r *http.Request) {
cfg := s.cfg()
if cfg.FeedbackGiteaToken == "" {
writeError(w, http.StatusServiceUnavailable, "feedback_disabled", "feedback is not configured")
return
}
p, has := authn.PrincipalFrom(r.Context())
if !has || !p.IsUser() {
writeError(w, http.StatusForbidden, "user_token_required", "feedback is sent with the token of the person sending it")
return
}
var in feedbackReq
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, feedbackMaxRequest)).Decode(&in); err != nil {
var tooBig *http.MaxBytesError
if errors.As(err, &tooBig) {
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "the report is larger than "+strconv.Itoa(feedbackMaxBody)+" bytes")
return
}
writeError(w, http.StatusBadRequest, "invalid_input", "request body is not valid JSON")
return
}
if len(in.Body) > feedbackMaxBody {
writeError(w, http.StatusRequestEntityTooLarge, "too_large", "the report is larger than "+strconv.Itoa(feedbackMaxBody)+" bytes")
return
}
if msg := in.validate(); msg != "" {
writeError(w, http.StatusBadRequest, "invalid_input", msg)
return
}
now := s.now()
if !s.feedbackLimiter.allow(p.Subject, now, feedbackPerHour) {
writeError(w, http.StatusTooManyRequests, "rate_limited", "at most "+strconv.Itoa(feedbackPerHour)+" reports per hour")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
slug := p.TenantSlug
if t, err := s.Store.GetTenant(ctx, p.TenantID); err == nil {
slug = t.Slug
}
title := "[" + slug + "] " + in.Kind + ": " + in.Title
number, err := s.Gitea.CreateIssue(ctx, cfg.FeedbackGiteaURL, cfg.FeedbackGiteaToken, cfg.FeedbackGiteaRepo, title, feedbackIssueBody(in, p, slug, now))
if err != nil {
s.Log.Error("feedback issue not created", "err", err, "tenant_id", p.TenantID, "kind", in.Kind)
writeError(w, http.StatusBadGateway, "feedback_failed", "the report could not be filed")
return
}
s.emitAudit(ctx, r, store.AuditEvent{TenantID: p.TenantID, Action: "feedback.submitted", TargetID: strconv.FormatInt(number, 10), TargetType: "feedback",
ActorID: p.Subject, ActorName: p.Email, ActorType: "user", Product: in.Product,
Metadata: map[string]interface{}{"kind": in.Kind, "version": in.Version, "device": in.Device}})
writeJSON(w, http.StatusCreated, map[string]int64{"id": number})
}
// 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
// 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"
}
if in.Product, ok = singleLine(in.Product, feedbackMaxShort, true); !ok {
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"
}
if in.Device, ok = singleLine(in.Device, feedbackMaxShort, false); !ok {
return "device: at most " + strconv.Itoa(feedbackMaxShort) + " characters"
}
if strings.TrimSpace(in.Body) == "" || !utf8.ValidString(in.Body) {
return "body is required"
}
return ""
}
func singleLine(v string, max int, required bool) (string, bool) {
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.
func feedbackIssueBody(in feedbackReq, p *authn.Principal, slug string, received time.Time) string {
user := p.Subject
if p.Email != "" {
user += " (" + p.Email + ")"
}
var b strings.Builder
for _, line := range [][2]string{
{"Tenant", slug + " (" + p.TenantID + ")"},
{"User", user},
{"Product", in.Product},
{"Version", in.Version},
{"Device", in.Device},
{"Kind", in.Kind},
{"Received", received.UTC().Format(time.RFC3339)},
} {
b.WriteString("- **" + line[0] + ":** " + line[1] + "\n")
}
b.WriteString("\n---\n\n")
b.WriteString(in.Body)
return b.String()
}