Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(docs): drop NUL bytes from doc and folder text (#10936)

pseudoyu38c5cabeb1ac
diff --git a/server/internal/handler/doc.go b/server/internal/handler/doc.go
index 18cf5f3896..30499a60a2 100644
--- a/server/internal/handler/doc.go
+++ b/server/internal/handler/doc.go
@@ -315,7 +315,10 @@ func (h *Handler) CreateDoc(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
- req.Title = strings.TrimSpace(req.Title)
+ // PostgreSQL TEXT rejects NUL, which survives a JSON round trip.
+ req.Title = strings.TrimSpace(sanitizeNullBytes(req.Title))
+ req.Content = sanitizeNullBytes(req.Content)
+ req.Message = sanitizeNullBytes(req.Message)
if !validateDocTitle(w, req.Title) || !validateDocContent(w, req.Content) || !validateDocMessage(w, req.Message) {
return
}
@@ -417,15 +420,20 @@ func (h *Handler) UpdateDoc(w http.ResponseWriter, r *http.Request) {
return
}
if req.Title != nil {
- trimmed := strings.TrimSpace(*req.Title)
+ trimmed := strings.TrimSpace(sanitizeNullBytes(*req.Title))
req.Title = &trimmed
if !validateDocTitle(w, trimmed) {
return
}
}
- if req.Content != nil && !validateDocContent(w, *req.Content) {
- return
+ if req.Content != nil {
+ content := sanitizeNullBytes(*req.Content)
+ req.Content = &content
+ if !validateDocContent(w, content) {
+ return
+ }
}
+ req.Message = sanitizeNullBytes(req.Message)
if !validateDocMessage(w, req.Message) {
return
}
@@ -704,7 +712,7 @@ func (h *Handler) CreateDocFolder(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
- req.Title = strings.TrimSpace(req.Title)
+ req.Title = strings.TrimSpace(sanitizeNullBytes(req.Title))
if !validateDocFolderTitle(w, req.Title) {
return
}
@@ -765,7 +773,7 @@ func (h *Handler) UpdateDocFolder(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, "invalid request body")
return
}
- req.Title = strings.TrimSpace(req.Title)
+ req.Title = strings.TrimSpace(sanitizeNullBytes(req.Title))
if !validateDocFolderTitle(w, req.Title) {
return
}
diff --git a/server/internal/handler/doc_test.go b/server/internal/handler/doc_test.go
index ebaab768d0..d48cb5af1a 100644
--- a/server/internal/handler/doc_test.go
+++ b/server/internal/handler/doc_test.go
@@ -146,3 +146,46 @@ func TestDocEditsMergeIntoOneVersionPerEditingStretch(t *testing.T) {
t.Fatalf("edit after a restore must append: %+v", got)
}
}
+
+// A NUL survives the JSON round trip but PostgreSQL TEXT rejects it, so an
+// agent saving tool output that carries one got a 500 and wrote nothing.
+func TestDocTextDropsNULBytes(t *testing.T) {
+ var doc DocResponse
+ testutil.Call(t, testHandler.CreateDoc, newRequest("POST", "/api/docs", map[string]any{
+ "title": "Run\x00book", "content": "Step\x00 one", "message": "Im\x00port",
+ })).Want(http.StatusCreated).JSON(&doc)
+ t.Cleanup(func() {
+ testPool.Exec(context.Background(), "DELETE FROM doc_version WHERE doc_id = $1", doc.ID)
+ testPool.Exec(context.Background(), "DELETE FROM doc WHERE id = $1", doc.ID)
+ })
+ if doc.Title != "Runbook" || doc.Content != "Step one" {
+ t.Fatalf("create must drop NUL bytes: %+v", doc)
+ }
+
+ var updated DocResponse
+ req := withURLParam(newRequest("PATCH", "/api/docs/"+doc.ID, map[string]any{
+ "base_revision": 1, "title": "Run\x00book 2", "content": "Step\x00 two", "message": "Re\x00vise",
+ }), "id", doc.ID)
+ testutil.Call(t, testHandler.UpdateDoc, req).Want(http.StatusOK).JSON(&updated)
+ if updated.Title != "Runbook 2" || updated.Content != "Step two" {
+ t.Fatalf("update must drop NUL bytes: %+v", updated)
+ }
+ var history struct {
+ Versions []DocVersionResponse `json:"versions"`
+ }
+ testutil.Call(t, testHandler.ListDocVersions, withURLParam(newRequest("GET", "/api/docs/versions", nil), "id", doc.ID)).Want(http.StatusOK).JSON(&history)
+ if len(history.Versions) != 2 || history.Versions[0].Message != "Revise" || history.Versions[1].Message != "Import" {
+ t.Fatalf("history messages must drop NUL bytes: %+v", history.Versions)
+ }
+
+ var folder DocFolderResponse
+ testutil.Call(t, testHandler.CreateDocFolder, newRequest("POST", "/api/doc-folders", map[string]any{"title": "Gui\x00des"})).Want(http.StatusCreated).JSON(&folder)
+ t.Cleanup(func() { testPool.Exec(context.Background(), "DELETE FROM doc_folder WHERE id = $1", folder.ID) })
+ if folder.Title != "Guides" {
+ t.Fatalf("folder create must drop NUL bytes: %+v", folder)
+ }
+ testutil.Call(t, testHandler.UpdateDocFolder, withURLParam(newRequest("PATCH", "/api/doc-folders/"+folder.ID, map[string]any{"title": "Ref\x00s"}), "id", folder.ID)).Want(http.StatusOK).JSON(&folder)
+ if folder.Title != "Refs" {
+ t.Fatalf("folder rename must drop NUL bytes: %+v", folder)
+ }
+}