mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
fix(server): redirect external attachments to their reference URL (#6574)
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"io/fs"
|
"io/fs"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
@@ -174,6 +175,11 @@ func (s *FileServerService) serveAttachmentFile(w http.ResponseWriter, r *http.R
|
|||||||
if readClass == access.MemoReadClassPublic {
|
if readClass == access.MemoReadClassPublic {
|
||||||
w.Header().Set("Cache-Control", publicAttachmentCacheControl)
|
w.Header().Set("Cache-Control", publicAttachmentCacheControl)
|
||||||
}
|
}
|
||||||
|
// External attachments carry no blob; their reference is the upstream URL
|
||||||
|
// the content lives at, so they are answered with a redirect.
|
||||||
|
if attachment.StorageType == storepb.AttachmentStorageType_EXTERNAL {
|
||||||
|
return serveExternalAttachment(w, r, attachment)
|
||||||
|
}
|
||||||
if attachment.StorageType != storepb.AttachmentStorageType_LOCAL && attachment.StorageType != storepb.AttachmentStorageType_S3 {
|
if attachment.StorageType != storepb.AttachmentStorageType_LOCAL && attachment.StorageType != storepb.AttachmentStorageType_S3 {
|
||||||
attachment, err = s.Store.GetAttachment(ctx, &store.FindAttachment{ID: &attachment.ID, GetBlob: true})
|
attachment, err = s.Store.GetAttachment(ctx, &store.FindAttachment{ID: &attachment.ID, GetBlob: true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -252,6 +258,21 @@ func (s *FileServerService) serveUserAvatar(w http.ResponseWriter, r *http.Reque
|
|||||||
// File Serving Methods
|
// File Serving Methods
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
|
// serveExternalAttachment redirects the client to an external attachment's
|
||||||
|
// recorded URL. External rows carry no blob, so there is nothing to stream or
|
||||||
|
// thumbnail locally; only http(s) targets are redirected so a malformed or
|
||||||
|
// hostile reference cannot turn the file route into an open redirector for
|
||||||
|
// other schemes.
|
||||||
|
func serveExternalAttachment(w http.ResponseWriter, r *http.Request, attachment *store.Attachment) error {
|
||||||
|
target, err := url.Parse(attachment.Reference)
|
||||||
|
if err != nil || target.Hostname() == "" || (target.Scheme != "http" && target.Scheme != "https") {
|
||||||
|
return &httphandler.Error{Code: http.StatusInternalServerError, Message: "attachment has no servable external URL"}
|
||||||
|
}
|
||||||
|
setSecurityHeaders(w)
|
||||||
|
http.Redirect(w, r, attachment.Reference, http.StatusFound)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// serveMediaStream serves video/audio files using streaming to avoid memory exhaustion.
|
// serveMediaStream serves video/audio files using streaming to avoid memory exhaustion.
|
||||||
func (s *FileServerService) serveMediaStream(w http.ResponseWriter, r *http.Request, attachment *store.Attachment, contentType string) error {
|
func (s *FileServerService) serveMediaStream(w http.ResponseWriter, r *http.Request, attachment *store.Attachment, contentType string) error {
|
||||||
setSecurityHeaders(w)
|
setSecurityHeaders(w)
|
||||||
|
|||||||
@@ -192,6 +192,78 @@ func TestServeAttachmentFile_StorageFailureIsAJSONError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// External attachments store no blob; the file route answers with a redirect
|
||||||
|
// to the recorded external URL, for thumbnail requests as well as full reads,
|
||||||
|
// instead of an empty 200 body (usememos/memos#6573).
|
||||||
|
func TestServeAttachmentFile_ExternalAttachmentRedirectsToReference(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
creator, err := svc.Store.CreateUser(ctx, &store.User{
|
||||||
|
Username: "external-owner",
|
||||||
|
Role: store.RoleUser,
|
||||||
|
Email: "external-owner@example.com",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
creatorCtx := context.WithValue(ctx, auth.UserIDContextKey, creator.ID)
|
||||||
|
|
||||||
|
memo, err := svc.CreateMemo(creatorCtx, &apipb.CreateMemoRequest{Memo: &apipb.Memo{
|
||||||
|
Content: "external attachment",
|
||||||
|
Visibility: apipb.Visibility_PUBLIC,
|
||||||
|
}})
|
||||||
|
require.NoError(t, err)
|
||||||
|
memoUID := strings.TrimPrefix(memo.Name, "memos/")
|
||||||
|
storedMemo, err := stores.GetMemo(ctx, &store.FindMemo{UID: &memoUID})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, storedMemo)
|
||||||
|
|
||||||
|
// Memo import is the path that records EXTERNAL rows: the reference is
|
||||||
|
// the external URL and the row carries no blob.
|
||||||
|
attachment, err := stores.CreateAttachment(ctx, &store.Attachment{
|
||||||
|
UID: random.UUID(),
|
||||||
|
CreatorID: creator.ID,
|
||||||
|
MemoID: &storedMemo.ID,
|
||||||
|
Filename: "linked.png",
|
||||||
|
Type: "image/png",
|
||||||
|
Size: 1234,
|
||||||
|
StorageType: storepb.AttachmentStorageType_EXTERNAL,
|
||||||
|
Reference: "https://cdn.example.com/linked.png",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
e := http.NewServeMux()
|
||||||
|
fs.RegisterRoutes(e, http.HandlerFunc(httphandler.NotFound))
|
||||||
|
|
||||||
|
url := fmt.Sprintf("/file/attachments/%s/%s", attachment.UID, attachment.Filename)
|
||||||
|
for _, suffix := range []string{"", "?thumbnail=true"} {
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, url+suffix, nil))
|
||||||
|
require.Equal(t, http.StatusFound, rec.Code, "request %q", suffix)
|
||||||
|
require.Equal(t, attachment.Reference, rec.Header().Get("Location"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A stored reference that is not a servable http(s) URL cannot be
|
||||||
|
// redirected to; the request fails instead of serving another scheme,
|
||||||
|
// a hostless URL, or an empty body.
|
||||||
|
for _, reference := range []string{"javascript:alert(1)", "https://:443/file"} {
|
||||||
|
badAttachment, err := stores.CreateAttachment(ctx, &store.Attachment{
|
||||||
|
UID: random.UUID(),
|
||||||
|
CreatorID: creator.ID,
|
||||||
|
MemoID: &storedMemo.ID,
|
||||||
|
Filename: "not-a-url.png",
|
||||||
|
Type: "image/png",
|
||||||
|
StorageType: storepb.AttachmentStorageType_EXTERNAL,
|
||||||
|
Reference: reference,
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, fmt.Sprintf("/file/attachments/%s/%s", badAttachment.UID, badAttachment.Filename), nil))
|
||||||
|
require.Equal(t, http.StatusInternalServerError, rec.Code, "reference %q", reference)
|
||||||
|
require.Empty(t, rec.Header().Get("Location"), "reference %q", reference)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestServeAttachmentFile_ShareTokenAllowsDirectMemoAttachment(t *testing.T) {
|
func TestServeAttachmentFile_ShareTokenAllowsDirectMemoAttachment(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t)
|
svc, fs, stores, cleanup := newShareAttachmentTestServices(ctx, t)
|
||||||
|
|||||||
Reference in New Issue
Block a user