Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(editor): re-sign download of CDN images quoted from another comment (#9920)

Shaikh Fardin27b987aba1a1
diff --git a/packages/core/types/attachment-url.test.ts b/packages/core/types/attachment-url.test.ts
index ad3f2cd2bb..b002cbfaa6 100644
--- a/packages/core/types/attachment-url.test.ts
+++ b/packages/core/types/attachment-url.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
attachmentDownloadPath,
attachmentIdFromDownloadURL,
+ attachmentIdFromStorageURL,
contentReferencesAttachment,
stripChannelMediaMarkers,
} from "./attachment-url";
@@ -155,3 +156,32 @@ describe("contentReferencesAttachment", () => {
expect(contentReferencesAttachment(md, att)).toBe(true);
});
});
+
+describe("attachmentIdFromStorageURL", () => {
+ const WS = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
+ const CDN = "static.multica.ai";
+
+ it("extracts the attachment id from a workspace object on the CDN", () => {
+ expect(attachmentIdFromStorageURL(`https://${CDN}/workspaces/${WS}/${ID}.jpg`, CDN)).toBe(ID);
+ });
+
+ it("accepts an object without an extension and ignores query strings", () => {
+ expect(attachmentIdFromStorageURL(`https://${CDN}/workspaces/${WS}/${ID}?x=1`, CDN)).toBe(ID);
+ });
+
+ it("ignores URLs on another host", () => {
+ expect(
+ attachmentIdFromStorageURL(`https://example.com/workspaces/${WS}/${ID}.jpg`, CDN),
+ ).toBeUndefined();
+ });
+
+ it("ignores non-workspace paths and an unset CDN domain", () => {
+ expect(
+ attachmentIdFromStorageURL(`https://${CDN}/users/${WS}/${ID}.jpg`, CDN),
+ ).toBeUndefined();
+ expect(
+ attachmentIdFromStorageURL(`https://${CDN}/workspaces/${WS}/${ID}.jpg`, ""),
+ ).toBeUndefined();
+ expect(attachmentIdFromStorageURL(`/workspaces/${WS}/${ID}.jpg`, CDN)).toBeUndefined();
+ });
+});
diff --git a/packages/core/types/attachment-url.ts b/packages/core/types/attachment-url.ts
index 9b9a980594..0d8ebfd74c 100644
--- a/packages/core/types/attachment-url.ts
+++ b/packages/core/types/attachment-url.ts
@@ -83,6 +83,45 @@ export function attachmentIdFromDownloadURL(rawURL: string): string | undefined
return id;
}
+// Storage object path the upload handler writes for workspace files:
+// `workspaces/<workspace-id>/<attachment-id><ext>` (server/internal/handler/file.go
+// uses the attachment id as the object name).
+const UUID_PATTERN = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
+const WORKSPACE_STORAGE_PATH_RE = new RegExp(
+ `^/workspaces/${UUID_PATTERN}/(${UUID_PATTERN})(?:\\.[A-Za-z0-9]+)?$`,
+ "i",
+);
+
+/**
+ * Extract the attachment id from a raw storage URL on the configured CDN
+ * domain, e.g. `https://static.multica.ai/workspaces/<ws>/<id>.jpg`.
+ *
+ * Markdown can embed a storage URL whose attachment record is not in the
+ * surrounding surface's attachment list (an agent quoting an image from
+ * another comment). On a signed CDN that bare URL only loads with the
+ * in-app CloudFront cookies, so download handlers use this id to re-sign
+ * through the API instead of opening the unsigned URL externally.
+ *
+ * Returns `undefined` when the URL is not on `cdnDomain` or the path is not
+ * a workspace storage object.
+ */
+export function attachmentIdFromStorageURL(
+ rawURL: string,
+ cdnDomain: string,
+): string | undefined {
+ const expectedHost = cdnDomain.trim().toLowerCase().replace(/\.$/, "");
+ if (!rawURL || !expectedHost || !/^https?:\/\//i.test(rawURL)) return undefined;
+ let u: URL;
+ try {
+ u = new URL(rawURL);
+ } catch {
+ return undefined;
+ }
+ if (u.hostname.toLowerCase().replace(/\.$/, "") !== expectedHost) return undefined;
+ const match = WORKSPACE_STORAGE_PATH_RE.exec(u.pathname);
+ return match?.[1];
+}
+
function stripQueryAndFragment(url: string): string {
return url.split(/[?#]/, 1)[0] ?? "";
}
diff --git a/packages/core/types/index.ts b/packages/core/types/index.ts
index 8fef55fb63..adadfe9ce5 100644
--- a/packages/core/types/index.ts
+++ b/packages/core/types/index.ts
@@ -169,6 +169,7 @@ export type { Attachment } from "./attachment";
export {
attachmentDownloadPath,
attachmentIdFromDownloadURL,
+ attachmentIdFromStorageURL,
contentReferencesAttachment,
stripChannelMediaMarkers,
} from "./attachment-url";
diff --git a/packages/views/editor/attachment-preview-modal.tsx b/packages/views/editor/attachment-preview-modal.tsx
index 1475061cb4..b9e1a5218c 100644
--- a/packages/views/editor/attachment-preview-modal.tsx
+++ b/packages/views/editor/attachment-preview-modal.tsx
@@ -55,6 +55,8 @@ import {
type IconComponent,
} from "@multica/ui/icons";
import type { Attachment } from "@multica/core/types";
+import { attachmentIdFromStorageURL } from "@multica/core/types/attachment-url";
+import { useConfigStore } from "@multica/core/config";
import { paths, useWorkspaceSlug } from "@multica/core/paths";
import { createShortcutChord } from "@multica/core/shortcuts";
import { cn } from "@multica/ui/lib/utils";
@@ -416,6 +418,7 @@ export function AttachmentPreviewModal({
onOpenOverview,
}: AttachmentPreviewModalProps & { onExitComplete?: () => void }) {
const download = useDownloadAttachment();
+ const cdnDomain = useConfigStore((s) => s.cdnDomain);
const shouldReduceMotion = useReducedMotion() ?? false;
const state = normalize(source);
// useWorkspaceSlug (not useWorkspacePaths) — returns null outside a
@@ -489,11 +492,15 @@ export function AttachmentPreviewModal({
const kind = state.kind;
// Download dispatcher: re-sign through `getAttachment` when an id is
- // available; otherwise fall back to opening the (possibly stale) URL
- // externally — same tradeoff as the file-card NodeView's download path.
+ // available (from the record, or parsed out of a CDN storage URL that
+ // didn't resolve to a record); otherwise fall back to opening the
+ // (possibly stale) URL externally — same tradeoff as the file-card
+ // NodeView's download path.
const handleDownload = () => {
- if (state.attachmentId) {
- download(state.attachmentId);
+ const attachmentId =
+ state.attachmentId ?? attachmentIdFromStorageURL(state.mediaUrl, cdnDomain);
+ if (attachmentId) {
+ download(attachmentId);
} else {
openExternal(state.mediaUrl);
}
diff --git a/packages/views/editor/attachment.tsx b/packages/views/editor/attachment.tsx
index f1b53c860c..787653890c 100644
--- a/packages/views/editor/attachment.tsx
+++ b/packages/views/editor/attachment.tsx
@@ -40,6 +40,7 @@ import { copyText } from "@multica/ui/lib/clipboard";
import { api } from "@multica/core/api";
import { useConfigStore } from "@multica/core/config";
import type { Attachment as AttachmentRecord } from "@multica/core/types";
+import { attachmentIdFromStorageURL } from "@multica/core/types/attachment-url";
import { useT } from "../i18n";
import { DeferredTooltip } from "../common/deferred-tooltip";
import { useAttachmentDownloadResolver } from "./attachment-download-context";
@@ -348,8 +349,14 @@ export function Attachment({
};
const handleDownload = () => {
- if (state.attachmentId) {
- download(state.attachmentId);
+ // A CDN storage URL that didn't resolve against this surface's
+ // attachments (e.g. an image quoted from another comment) still names
+ // its attachment id, so re-sign it instead of opening the bare URL,
+ // which a signed CDN rejects outside the app.
+ const attachmentId =
+ state.attachmentId ?? attachmentIdFromStorageURL(shareUrl, cdnDomain);
+ if (attachmentId) {
+ download(attachmentId);
return;
}
if (shareUrl) openByUrl(shareUrl);