Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

server: stop HTML-escaping cloud proxy request bodies (#19008)

Devon Rifkin957c8cd5ce85
diff --git a/server/cloud_proxy.go b/server/cloud_proxy.go
index 51db28d0d..99954adf8 100644
--- a/server/cloud_proxy.go
+++ b/server/cloud_proxy.go
@@ -164,7 +164,7 @@ func proxyCloudJSONRequest(c *gin.Context, payload any, disabledOperation string
}
func proxyCloudJSONRequestWithPath(c *gin.Context, payload any, path string, disabledOperation string) {
- body, err := json.Marshal(payload)
+ body, err := marshalNoHTMLEscape(payload)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -284,7 +284,20 @@ func replaceJSONModelField(body []byte, model string) ([]byte, error) {
}
payload["model"] = modelJSON
- return json.Marshal(payload)
+ return marshalNoHTMLEscape(payload)
+}
+
+// marshalNoHTMLEscape is json.Marshal without HTML escaping, which would turn
+// each <, >, and & in a client's values into six bytes that count against
+// upstream byte limits.
+func marshalNoHTMLEscape(v any) ([]byte, error) {
+ var buf bytes.Buffer
+ enc := json.NewEncoder(&buf)
+ enc.SetEscapeHTML(false)
+ if err := enc.Encode(v); err != nil {
+ return nil, err
+ }
+ return bytes.TrimSuffix(buf.Bytes(), []byte("\n")), nil
}
func readRequestBody(r *http.Request) ([]byte, error) {
diff --git a/server/cloud_proxy_test.go b/server/cloud_proxy_test.go
index 05788bad4..c68a66021 100644
--- a/server/cloud_proxy_test.go
+++ b/server/cloud_proxy_test.go
@@ -233,6 +233,56 @@ func TestCloudPassthroughMiddleware_ZstdBodyTooLarge(t *testing.T) {
}
}
+func TestCloudProxyPreservesHTMLCharacters(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+ setTestHome(t, t.TempDir())
+
+ var forwarded []byte
+ upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ forwarded, _ = io.ReadAll(r.Body)
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{}`))
+ }))
+ defer upstream.Close()
+
+ original := cloudProxyBaseURL
+ cloudProxyBaseURL = upstream.URL
+ t.Cleanup(func() { cloudProxyBaseURL = original })
+
+ s := &Server{}
+ router, err := s.GenerateRoutes()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ content := `<b>Tom & Jerry</b> if (a < b && c > d)`
+ cases := []struct {
+ name string
+ path string
+ body string
+ }{
+ {"v1 passthrough", "/v1/chat/completions", `{"model":"test-model:cloud","messages":[{"role":"user","content":"` + content + `"}]}`},
+ {"api re-encode", "/api/generate", `{"model":"test-model:cloud","prompt":"` + content + `","stream":false}`},
+ }
+ for _, tt := range cases {
+ t.Run(tt.name, func(t *testing.T) {
+ forwarded = nil
+ rec := httptest.NewRecorder()
+ router.ServeHTTP(rec, httptest.NewRequest(http.MethodPost, tt.path, bytes.NewBufferString(tt.body)))
+
+ if rec.Code != http.StatusOK {
+ t.Fatalf("expected status 200, got %d (%s)", rec.Code, rec.Body.String())
+ }
+ if model, _ := extractModelField(forwarded); model != "test-model" {
+ t.Fatalf("expected normalized model %q, got %q", "test-model", model)
+ }
+ if !bytes.Contains(forwarded, []byte(content)) {
+ t.Fatalf("expected content to be forwarded unescaped, got %s", forwarded)
+ }
+ })
+ }
+}
+
func TestBuildCloudSignatureChallengeOverwritesExistingTimestamp(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, "https://ollama.com/v1/messages?beta=true&ts=999", nil)
if err != nil {