From 422afea8811cae7c7baea37d72b0c1cda03bd366 Mon Sep 17 00:00:00 2001 From: Santhi Prakash <38608178+santhiprakash@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:46:55 +0530 Subject: [PATCH] fix(server): answer file-serving errors as JSON, not the media type (#6516) --- internal/httphandler/httphandler.go | 9 +++ internal/httphandler/httphandler_test.go | 10 ++- internal/testutil/fakes3/server.go | 8 +++ server/fileserver/fileserver_test.go | 84 ++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 2 deletions(-) diff --git a/internal/httphandler/httphandler.go b/internal/httphandler/httphandler.go index 71f9f5f57..62e2526b3 100644 --- a/internal/httphandler/httphandler.go +++ b/internal/httphandler/httphandler.go @@ -41,6 +41,15 @@ func WriteError(w http.ResponseWriter, r *http.Request, err error) { if errors.As(err, &publicError) { code, message = publicError.Code, publicError.Message } + // Handlers may have prepared representation headers for the body they + // meant to serve before failing (e.g. the file server sets the attachment's + // Content-Type and Content-Disposition up front). The error body is JSON, + // so the stale values must not leak onto the response: a media-typed error + // gets handed to the browser's document viewer instead of being read. + h := w.Header() + h.Del("Content-Type") + h.Del("Content-Disposition") + h.Del("Content-Length") if r.Method == http.MethodHead { w.WriteHeader(code) return diff --git a/internal/httphandler/httphandler_test.go b/internal/httphandler/httphandler_test.go index 2b1fdb7f1..2b8222c9c 100644 --- a/internal/httphandler/httphandler_test.go +++ b/internal/httphandler/httphandler_test.go @@ -116,14 +116,20 @@ func TestJSONFailureBeforeCommit(t *testing.T) { require.JSONEq(t, `{"message":"Internal Server Error"}`, rec.Body.String()) } -func TestErrorPreservesExistingContentType(t *testing.T) { +// The error body is always JSON, so representation headers a handler prepared +// for the response it meant to serve must not survive onto the error: a +// media-typed error gets handed to the browser's document viewer (Chrome's +// "Failed to load PDF document" on usememos/memos#6515) instead of read. +func TestErrorUsesJSONContentType(t *testing.T) { rec := httptest.NewRecorder() Func(func(w http.ResponseWriter, _ *http.Request) error { w.Header().Set("Content-Type", "video/mp4") + w.Header().Set("Content-Disposition", "attachment") return &Error{Code: http.StatusRequestedRangeNotSatisfiable, Message: "requested range not satisfiable"} }).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil)) require.Equal(t, http.StatusRequestedRangeNotSatisfiable, rec.Code) - require.Equal(t, "video/mp4", rec.Header().Get("Content-Type")) + require.Equal(t, "application/json", rec.Header().Get("Content-Type")) + require.Empty(t, rec.Header().Get("Content-Disposition")) require.JSONEq(t, `{"message":"requested range not satisfiable"}`, rec.Body.String()) } diff --git a/internal/testutil/fakes3/server.go b/internal/testutil/fakes3/server.go index 97118be73..9501245fb 100644 --- a/internal/testutil/fakes3/server.go +++ b/internal/testutil/fakes3/server.go @@ -67,3 +67,11 @@ func (s *Server) GetObject(bucket, key string) ([]byte, error) { defer object.Contents.Close() return io.ReadAll(object.Contents) } + +// DeleteObject removes an object directly from the in-memory store, bypassing +// the HTTP API, for arranging state where the backend no longer holds an +// attachment's bytes. +func (s *Server) DeleteObject(bucket, key string) error { + _, err := s.memory.DeleteObject(bucket, key) + return err +} diff --git a/server/fileserver/fileserver_test.go b/server/fileserver/fileserver_test.go index b4e121888..2bb652a7c 100644 --- a/server/fileserver/fileserver_test.go +++ b/server/fileserver/fileserver_test.go @@ -101,6 +101,90 @@ func TestServeAttachmentFile_S3(t *testing.T) { require.Empty(t, multiRangeRecorder.Header().Get("Content-Range")) } +// A failed media fetch must answer with the JSON error it actually is, not the +// attachment's media type: browsers route a Content-Type like application/pdf +// to their document viewer, which then reports a misleading "Failed to load +// PDF document" for the JSON body (usememos/memos#6515). +func TestServeAttachmentFile_StorageFailureIsAJSONError(t *testing.T) { + ctx := context.Background() + fake := fakes3.New(t, "file-server-attachments") + svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t) + defer cleanup() + + configuredStorage := &storepb.Storage{ + Id: "s3-files", + Name: "File server S3", + Type: storepb.StorageType_STORAGE_TYPE_S3, + Config: &storepb.Storage_S3Config{S3Config: fake.Config("file-server-attachments")}, + } + _, err := stores.UpsertInstanceSetting(ctx, &storepb.InstanceSetting{ + Key: storepb.InstanceSettingKey_STORAGE, + Value: &storepb.InstanceSetting_StorageSetting{StorageSetting: &storepb.InstanceStorageSetting{ + FilepathTemplate: "files/{uuid}_{filename}", + Storages: []*storepb.Storage{configuredStorage}, + DefaultStorageId: configuredStorage.Id, + }}, + }) + require.NoError(t, err) + + creator, err := stores.CreateUser(ctx, &store.User{ + Username: "s3-failure-owner", + Role: store.RoleUser, + Email: "s3-failure-owner@example.com", + }) + require.NoError(t, err) + creatorCtx := context.WithValue(ctx, auth.UserIDContextKey, creator.ID) + + var attachments []*apipb.Attachment + for _, spec := range []struct{ filename, contentType string }{ + {"document.pdf", "application/pdf"}, + {"archive.zip", "application/zip"}, + } { + attachment, err := svc.CreateAttachment(creatorCtx, &apipb.CreateAttachmentRequest{Attachment: &apipb.Attachment{ + Filename: spec.filename, + Type: spec.contentType, + Content: []byte("s3 bytes"), + }}) + require.NoError(t, err) + attachments = append(attachments, attachment) + } + _, err = svc.CreateMemo(creatorCtx, &apipb.CreateMemoRequest{Memo: &apipb.Memo{ + Content: "public S3 attachments", + Visibility: apipb.Visibility_PUBLIC, + Attachments: []*apipb.Attachment{{Name: attachments[0].Name}, {Name: attachments[1].Name}}, + }}) + require.NoError(t, err) + + e := http.NewServeMux() + fs.RegisterRoutes(e, http.HandlerFunc(httphandler.NotFound)) + + // Healthy PDFs keep their media type and are served inline so a new tab + // renders them in the browser's document viewer. + recorder := httptest.NewRecorder() + e.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/file/%s/%s", attachments[0].Name, attachments[0].Filename), nil)) + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, "application/pdf", recorder.Header().Get("Content-Type")) + require.Empty(t, recorder.Header().Get("Content-Disposition")) + + // Drop the backing objects so the S3 reads fail after the handler has + // already prepared the attachments' media headers. + for _, attachment := range attachments { + uid := strings.TrimPrefix(attachment.Name, "attachments/") + stored, err := svc.Store.GetAttachment(ctx, &store.FindAttachment{UID: &uid}) + require.NoError(t, err) + require.NoError(t, fake.DeleteObject("file-server-attachments", stored.Payload.GetS3Object().Key)) + } + + for _, attachment := range attachments { + recorder := httptest.NewRecorder() + e.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/file/%s/%s", attachment.Name, attachment.Filename), nil)) + require.Equal(t, http.StatusInternalServerError, recorder.Code) + require.Equal(t, "application/json", recorder.Header().Get("Content-Type")) + require.Empty(t, recorder.Header().Get("Content-Disposition")) + require.Contains(t, recorder.Body.String(), "failed to stream from S3") + } +} + func TestServeAttachmentFile_ShareTokenAllowsDirectMemoAttachment(t *testing.T) { ctx := context.Background() svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t)