Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
fix(web): encode filenames in attachment URLs (#6506)
Vo Nguyen Thai Hoc334c758bac75
diff --git a/web/src/hooks/useAttachmentLibrary.ts b/web/src/hooks/useAttachmentLibrary.tsindex 674675437..e132689b8 100644--- a/web/src/hooks/useAttachmentLibrary.ts+++ b/web/src/hooks/useAttachmentLibrary.ts@@ -10,7 +10,7 @@ import {import { useInfiniteAttachments } from "@/hooks/useAttachmentQueries";import { combineCELFilters } from "@/lib/cel-filter";import type { Attachment, ListAttachmentsRequest } from "@/types/proto/api/attachment_service_pb";-import { isMotionAttachment } from "@/utils/attachment";+import { getAttachmentUrl, isMotionAttachment } from "@/utils/attachment";import { useTranslate } from "@/utils/i18n";import { type AttachmentVisualItem, buildAttachmentVisualItems, countLogicalAttachmentItems } from "@/utils/media-item";@@ -123,7 +123,7 @@ const toLibraryListItem = (attachment: Attachment, locale: string): AttachmentLifileTypeLabel,fileSizeLabel,memoName: attachment.memo,- sourceUrl: attachment.externalLink || `${window.location.origin}/file/${attachment.name}/${attachment.filename}`,+ sourceUrl: getAttachmentUrl(attachment),};};diff --git a/web/src/hooks/useMemoShareQueries.ts b/web/src/hooks/useMemoShareQueries.tsindex 947a28db4..3a3c164ff 100644--- a/web/src/hooks/useMemoShareQueries.ts+++ b/web/src/hooks/useMemoShareQueries.ts@@ -11,6 +11,7 @@ import {ListMemoSharesRequestSchema,MemoShareSchema,} from "@/types/proto/api/memo_service_pb";+import { getAttachmentUrl } from "@/utils/attachment";// Query keys factory for share-related cache management.export const memoShareKeys = {@@ -96,6 +97,6 @@ export function getShareToken(share: MemoShare): string {export function withShareAttachmentLinks(attachments: Attachment[], token: string): Attachment[] {return attachments.map((a) => {if (a.externalLink) return a;- return { ...a, externalLink: `${window.location.origin}/file/${a.name}/${a.filename}?share_token=${encodeURIComponent(token)}` };+ return { ...a, externalLink: `${getAttachmentUrl(a)}?share_token=${encodeURIComponent(token)}` };});}diff --git a/web/src/utils/attachment.ts b/web/src/utils/attachment.tsindex b85542d0c..a485826d5 100644--- a/web/src/utils/attachment.ts+++ b/web/src/utils/attachment.ts@@ -5,7 +5,7 @@ export const getAttachmentUrl = (attachment: Attachment) => {return attachment.externalLink;}- return `${window.location.origin}/file/${attachment.name}/${attachment.filename}`;+ return `${window.location.origin}/file/${attachment.name}/${encodeURIComponent(attachment.filename)}`;};// Appends a thumbnail or motion selector only to share-mode links; other external links fall back to the server-managed attachment URL.@@ -27,7 +27,7 @@ export const getAttachmentThumbnailUrl = (attachment: Attachment) => {return shareUrl;}- return `${window.location.origin}/file/${attachment.name}/${attachment.filename}?thumbnail=true`;+ return `${window.location.origin}/file/${attachment.name}/${encodeURIComponent(attachment.filename)}?thumbnail=true`;};export const getAttachmentMotionClipUrl = (attachment: Attachment) => {@@ -36,7 +36,7 @@ export const getAttachmentMotionClipUrl = (attachment: Attachment) => {return shareUrl;}- return `${window.location.origin}/file/${attachment.name}/${attachment.filename}?motion=true`;+ return `${window.location.origin}/file/${attachment.name}/${encodeURIComponent(attachment.filename)}?motion=true`;};export const getAttachmentType = (attachment: Attachment) => {diff --git a/web/tests/attachment-library-stats.test.ts b/web/tests/attachment-library-stats.test.tsindex 908667afe..6a923d522 100644--- a/web/tests/attachment-library-stats.test.ts+++ b/web/tests/attachment-library-stats.test.ts@@ -24,6 +24,21 @@ const attachment = (name: string, type: string, memo?: string): Attachment =>describe("buildAttachmentLibraryStats", () => {beforeEach(() => clients.listAttachments.mockReset());+ it.each(["100% complete.txt", "ảnh#1?.png", "literal%20name.txt"])("keeps %s in attachment library URLs", async (filename) => {+ const file = attachment("test-uid", "text/plain");+ file.filename = filename;+ clients.listAttachments.mockResolvedValue({ attachments: [file], nextPageToken: "" });+ const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });+ const wrapper = ({ children }: PropsWithChildren) => createElement(QueryClientProvider, { client: queryClient }, children);+ const { result } = renderHook(() => useUnusedAttachmentLibrary("en"), { wrapper });++ await waitFor(() => expect(result.current.isComplete).toBe(true));+ const url = new URL(result.current.unusedItems[0].sourceUrl);+ expect(decodeURIComponent(url.pathname.split("/").at(-1)!)).toBe(filename);+ expect(url.hash).toBe("");+ expect(url.search).toBe("");+ });+it("counts linked attachment types without expanding live-photo pairs", () => {const still = attachment("live.jpg", "image/jpeg", "memos/one");still.motionMedia = create(MotionMediaSchema, {diff --git a/web/tests/attachment-urls.test.ts b/web/tests/attachment-urls.test.tsindex 7c749ea7e..617617005 100644--- a/web/tests/attachment-urls.test.ts+++ b/web/tests/attachment-urls.test.ts@@ -10,6 +10,24 @@ const baseAttachment = {type: "image/png",} as Attachment;+describe("attachment filename URL paths", () => {+ const builders = [+ { name: "download", build: getAttachmentUrl, query: [] },+ { name: "thumbnail", build: getAttachmentThumbnailUrl, query: [["thumbnail", "true"]] },+ { name: "motion clip", build: getAttachmentMotionClipUrl, query: [["motion", "true"]] },+ ];++ for (const { name, build, query } of builders) {+ it.each(["100% complete.txt", "ảnh#1?.png", "literal%20name.txt"])(`keeps %s as a filename in the ${name} URL`, (filename) => {+ const url = new URL(build({ ...baseAttachment, filename } as Attachment));++ expect(decodeURIComponent(url.pathname.split("/").at(-1)!)).toBe(filename);+ expect(url.hash).toBe("");+ expect([...url.searchParams.entries()]).toEqual(query);+ });+ }+});+// Regression tests for #6128: share-mode thumbnails/motion clips must keep the share token on externalLink.describe("attachment URL builders in share mode", () => {it("appends thumbnail=true to an externalLink that carries a share token", () => {diff --git a/web/tests/memo-share-attachment-links.test.ts b/web/tests/memo-share-attachment-links.test.tsnew file mode 100644index 000000000..5234878d0--- /dev/null+++ b/web/tests/memo-share-attachment-links.test.ts@@ -0,0 +1,23 @@+import { describe, expect, it, vi } from "vitest";+import { withShareAttachmentLinks } from "@/hooks/useMemoShareQueries";+import type { Attachment } from "@/types/proto/api/attachment_service_pb";++vi.mock("@/connect", () => ({ memoServiceClient: {} }));++describe("shared attachment filename URLs", () => {+ it.each(["100% complete.txt", "ảnh#1?.png", "literal%20name.txt"])("keeps the filename and share token for %s", (filename) => {+ const attachment = { name: "attachments/test-uid", filename } as Attachment;+ const [shared] = withShareAttachmentLinks([attachment], "test-token");+ const url = new URL(shared.externalLink);++ expect(decodeURIComponent(url.pathname.split("/").at(-1)!)).toBe(filename);+ expect(url.hash).toBe("");+ expect([...url.searchParams.entries()]).toEqual([["share_token", "test-token"]]);+ });++ it("preserves an existing external URL", () => {+ const attachment = { externalLink: "https://cdn.example.com/photo.png?version=123" } as Attachment;++ expect(withShareAttachmentLinks([attachment], "test-token")[0]).toBe(attachment);+ });+});