📤 fix(langfuse): support documented media upload types (#16079)

* fix(langfuse): allow text and JSON media uploads

* fix(langfuse): support documented attachment MIME types
This commit is contained in:
Ravi Kumar L 2026-09-19 15:03:32 +02:00 • committed by GitHub
parent c0affede00
commit 7ceafeba14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 125 additions and 80 deletions

View file

@ -1284,9 +1284,20 @@ func isAzureUploadURL(value string) bool {
func allowedUploadContentType(value string) bool {
mediaType := strings.ToLower(strings.TrimSpace(strings.Split(value, ";")[0]))
return mediaType == "application/octet-stream" ||
mediaType == "application/pdf" ||
strings.HasPrefix(mediaType, "image/") ||
switch mediaType {
case "text/plain", "text/html", "text/css", "text/csv", "text/markdown",
"text/x-python", "text/x-typescript", "application/javascript", "application/x-yaml",
"application/pdf", "application/msword", "application/rtf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/json", "application/x-ndjson", "application/xml",
"application/vnd.apache.parquet", "application/zip", "application/gzip",
"application/x-tar", "application/x-7z-compressed", "application/octet-stream":
return true
}
return strings.HasPrefix(mediaType, "image/") ||
strings.HasPrefix(mediaType, "audio/") ||
strings.HasPrefix(mediaType, "video/")
}

View file

@ -349,88 +349,111 @@ func TestJSONTraceProxyAddsRoutingAttributesFromPath(t *testing.T) {
func TestMediaUploadFansOutToCentralAndTenant(t *testing.T) {
t.Parallel()
var mu sync.Mutex
uploads := map[string]string{}
upstream := func(name string) *httptest.Server {
return httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodPost && r.URL.Path == mediaPath:
uploadURL := "https://" + r.Host + "/upload/" + name
writeJSON(w, http.StatusCreated, mediaUploadResponse{
MediaID: "same-media-id",
UploadURL: &uploadURL,
})
case r.Method == http.MethodPut && r.URL.Path == "/upload/"+name:
body, _ := io.ReadAll(r.Body)
mu.Lock()
uploads[name] = string(body)
mu.Unlock()
w.WriteHeader(http.StatusOK)
case r.Method == http.MethodPatch && r.URL.Path == mediaPath+"/same-media-id":
w.WriteHeader(http.StatusNoContent)
default:
http.NotFound(w, r)
for _, contentType := range []string{
"image/png", "audio/wav", "video/mp4",
"text/plain", "text/plain; charset=utf-8", "text/html", "text/css", "text/csv",
"text/markdown", "text/x-python", "text/x-typescript",
"application/javascript", "application/x-yaml", "application/pdf", "application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.ms-excel",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
"application/rtf", "application/json", "Application/JSON; charset=utf-8",
"application/x-ndjson", "application/xml", "application/vnd.apache.parquet",
"application/zip", "application/gzip", "application/x-tar",
"application/x-7z-compressed", "application/octet-stream",
} {
t.Run(contentType, func(t *testing.T) {
t.Parallel()
var mu sync.Mutex
uploads := map[string]string{}
uploadContentTypes := map[string]string{}
upstream := func(name string) *httptest.Server {
return httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodPost && r.URL.Path == mediaPath:
uploadURL := "https://" + r.Host + "/upload/" + name
writeJSON(w, http.StatusCreated, mediaUploadResponse{
MediaID: "same-media-id",
UploadURL: &uploadURL,
})
case r.Method == http.MethodPut && r.URL.Path == "/upload/"+name:
body, _ := io.ReadAll(r.Body)
mu.Lock()
uploads[name] = string(body)
uploadContentTypes[name] = r.Header.Get("Content-Type")
mu.Unlock()
w.WriteHeader(http.StatusOK)
case r.Method == http.MethodPatch && r.URL.Path == mediaPath+"/same-media-id":
w.WriteHeader(http.StatusNoContent)
default:
http.NotFound(w, r)
}
}))
}
}))
}
central := upstream("central")
defer central.Close()
tenant := upstream("tenant")
defer tenant.Close()
central := upstream("central")
defer central.Close()
tenant := upstream("tenant")
defer tenant.Close()
store := newFakeUploadPlanStore()
createGateway := newTestGatewayWithStore(central.URL, map[string]string{"eu": tenant.URL}, store)
uploadGateway := newTestGatewayWithStore(central.URL, map[string]string{"eu": tenant.URL}, store)
createGateway.cfg.client = central.Client()
uploadGateway.cfg.client = central.Client()
createBody := `{"traceId":"trace","contentType":"image/png","contentLength":5,"sha256Hash":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","field":"input"}`
req := httptest.NewRequest(http.MethodPost, tenantPrefix+"eu"+mediaPath, strings.NewReader(createBody))
req.Header.Set("Authorization", "Basic tenant")
resp := httptest.NewRecorder()
store := newFakeUploadPlanStore()
createGateway := newTestGatewayWithStore(central.URL, map[string]string{"eu": tenant.URL}, store)
uploadGateway := newTestGatewayWithStore(central.URL, map[string]string{"eu": tenant.URL}, store)
createGateway.cfg.client = central.Client()
uploadGateway.cfg.client = central.Client()
createBody := fmt.Sprintf(`{"traceId":"trace","contentType":%q,"contentLength":5,"sha256Hash":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","field":"input"}`, contentType)
req := httptest.NewRequest(http.MethodPost, tenantPrefix+"eu"+mediaPath, strings.NewReader(createBody))
req.Header.Set("Authorization", "Basic tenant")
resp := httptest.NewRecorder()
createGateway.handle(resp, req)
if resp.Code != http.StatusCreated {
t.Fatalf("create status = %d, body = %s", resp.Code, resp.Body.String())
}
var create mediaUploadResponse
if err := json.NewDecoder(resp.Body).Decode(&create); err != nil {
t.Fatal(err)
}
if create.MediaID != "same-media-id" || create.UploadURL == nil || !strings.Contains(*create.UploadURL, mediaUploadProxyPath) {
t.Fatalf("unexpected create response: %#v", create)
}
uploadID := strings.TrimPrefix(newUploadURLPath(t, *create.UploadURL), mediaUploadProxyPath)
store.mu.Lock()
storedPlan := store.plans[uploadID]
store.mu.Unlock()
storedPlanJSON, err := json.Marshal(storedPlan)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(storedPlanJSON), "Basic ") {
t.Fatalf("stored upload plan leaked authorization: %s", storedPlanJSON)
}
createGateway.handle(resp, req)
if resp.Code != http.StatusCreated {
t.Fatalf("create status = %d, body = %s", resp.Code, resp.Body.String())
}
var create mediaUploadResponse
if err := json.NewDecoder(resp.Body).Decode(&create); err != nil {
t.Fatal(err)
}
if create.MediaID != "same-media-id" || create.UploadURL == nil || !strings.Contains(*create.UploadURL, mediaUploadProxyPath) {
t.Fatalf("unexpected create response: %#v", create)
}
uploadID := strings.TrimPrefix(newUploadURLPath(t, *create.UploadURL), mediaUploadProxyPath)
store.mu.Lock()
storedPlan := store.plans[uploadID]
store.mu.Unlock()
storedPlanJSON, err := json.Marshal(storedPlan)
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(storedPlanJSON), "Basic ") {
t.Fatalf("stored upload plan leaked authorization: %s", storedPlanJSON)
}
uploadReq := httptest.NewRequest(http.MethodPut, *create.UploadURL, strings.NewReader("hello"))
uploadReq.Header.Set("Content-Type", "image/png")
uploadResp := httptest.NewRecorder()
uploadGateway.handle(uploadResp, uploadReq)
if uploadResp.Code != http.StatusOK {
t.Fatalf("upload status = %d, body = %s", uploadResp.Code, uploadResp.Body.String())
}
uploadReq := httptest.NewRequest(http.MethodPut, *create.UploadURL, strings.NewReader("hello"))
uploadReq.Header.Set("Content-Type", contentType)
uploadResp := httptest.NewRecorder()
uploadGateway.handle(uploadResp, uploadReq)
if uploadResp.Code != http.StatusOK {
t.Fatalf("upload status = %d, body = %s", uploadResp.Code, uploadResp.Body.String())
}
patchReq := httptest.NewRequest(http.MethodPatch, tenantPrefix+"eu"+mediaPath+"/same-media-id", strings.NewReader(`{"uploadHttpStatus":200}`))
patchReq.Header.Set("Authorization", "Basic tenant")
patchResp := httptest.NewRecorder()
uploadGateway.handle(patchResp, patchReq)
if patchResp.Code != http.StatusNoContent {
t.Fatalf("patch status = %d, body = %s", patchResp.Code, patchResp.Body.String())
}
patchReq := httptest.NewRequest(http.MethodPatch, tenantPrefix+"eu"+mediaPath+"/same-media-id", strings.NewReader(`{"uploadHttpStatus":200}`))
patchReq.Header.Set("Authorization", "Basic tenant")
patchResp := httptest.NewRecorder()
uploadGateway.handle(patchResp, patchReq)
if patchResp.Code != http.StatusNoContent {
t.Fatalf("patch status = %d, body = %s", patchResp.Code, patchResp.Body.String())
}
mu.Lock()
defer mu.Unlock()
if uploads["central"] != "hello" || uploads["tenant"] != "hello" {
t.Fatalf("uploads = %#v", uploads)
mu.Lock()
defer mu.Unlock()
if uploads["central"] != "hello" || uploads["tenant"] != "hello" {
t.Fatalf("uploads = %#v", uploads)
}
if uploadContentTypes["central"] != contentType || uploadContentTypes["tenant"] != contentType {
t.Fatalf("upload content types = %#v", uploadContentTypes)
}
})
}
}
@ -742,6 +765,17 @@ func TestMediaUploadOversizeRestoresPlanForRetry(t *testing.T) {
}
}
func TestUploadContentTypeRejectsUnsupportedTypes(t *testing.T) {
t.Parallel()
for _, contentType := range []string{"", "text/x-unsupported", "application/x-unsupported", "application/jsonp", "text/plain-invalid", "multipart/form-data"} {
t.Run(contentType, func(t *testing.T) {
if allowedUploadContentType(contentType) {
t.Fatalf("unsupported content type accepted: %q", contentType)
}
})
}
}
func TestMediaUploadUnsupportedContentTypeRestoresPlanForRetry(t *testing.T) {
t.Parallel()
@ -770,7 +804,7 @@ func TestMediaUploadUnsupportedContentTypeRestoresPlanForRetry(t *testing.T) {
gw.cfg.client = upstream.Client()
badReq := httptest.NewRequest(http.MethodPut, mediaUploadProxyPath+uploadID, strings.NewReader("hello"))
badReq.Header.Set("Content-Type", "text/html")
badReq.Header.Set("Content-Type", "application/x-unsupported")
badResp := httptest.NewRecorder()
gw.handle(badResp, badReq)
if badResp.Code != http.StatusBadGateway {