Files
Agent-System/internal/httpx/envelope_test.go
T
weichenandCursor f6813cc272 feat(api): add additive HTTP/SSE error envelopes
Public errors now emit code, safe message, and request_id while keeping the legacy error field. Raw SQL, paths, tokens, and XSS payloads are stripped; the frontend branches on code instead of substring matching.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-15 14:40:56 +00:00

58 lines
1.6 KiB
Go

package httpx
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestWriteSanitizesInternalAndKeepsLegacyCode(t *testing.T) {
rec := httptest.NewRecorder()
Write(rec, http.StatusInternalServerError, CodeINTERNAL, "pq: relation foo leaked /home/weichen/secret", "req_test", nil)
var env Envelope
if err := json.Unmarshal(rec.Body.Bytes(), &env); err != nil {
t.Fatal(err)
}
if env.Code != CodeINTERNAL || env.RequestID != "req_test" {
t.Fatalf("%+v", env)
}
if strings.Contains(rec.Body.String(), "pq:") || strings.Contains(rec.Body.String(), "/home/") {
t.Fatalf("leaked: %s", rec.Body.String())
}
if env.Error != CodeINTERNAL {
t.Fatalf("legacy error=%s", env.Error)
}
}
func TestWriteKeepsStablePublicErrorCode(t *testing.T) {
rec := httptest.NewRecorder()
Write(rec, http.StatusForbidden, CodeAUTHZ, "ERR_FORBIDDEN", "req_2", nil)
var env Envelope
_ = json.Unmarshal(rec.Body.Bytes(), &env)
if env.Error != "ERR_FORBIDDEN" || env.Code != CodeAUTHZ {
t.Fatalf("%+v", env)
}
if strings.Contains(rec.Body.String(), "<script") {
t.Fatal("xss")
}
}
func TestLooksInternalXSS(t *testing.T) {
rec := httptest.NewRecorder()
Write(rec, http.StatusBadRequest, CodeVALIDATION, "<script>alert(1)</script>", "req_3", nil)
if strings.Contains(rec.Body.String(), "<script") {
t.Fatalf("%s", rec.Body.String())
}
}
func TestCodeFromStatus(t *testing.T) {
if CodeFromStatus(http.StatusUnauthorized) != CodeAUTHZ {
t.Fatal("401")
}
if CodeFromStatus(http.StatusConflict) != CodeVERSION_CONFLICT {
t.Fatal("409")
}
}