Skip to content

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

Go

fix(issues): file-card render for self-host with local storage (#2349)

Prateek Bhatnagar681d72067175
diff --git a/packages/ui/markdown/Markdown.tsx b/packages/ui/markdown/Markdown.tsx
index b3d8e9a9a7..4958f9cd53 100644
--- a/packages/ui/markdown/Markdown.tsx
+++ b/packages/ui/markdown/Markdown.tsx
@@ -9,7 +9,7 @@ import remarkMath from 'remark-math'
import { FileText, Download } from 'lucide-react'
import { cn } from '@multica/ui/lib/utils'
import { CodeBlock, InlineCode } from './CodeBlock'
-import { preprocessFileCards } from './file-cards'
+import { isAllowedFileCardHref, preprocessFileCards } from './file-cards'
import { preprocessLinks } from './linkify'
import { preprocessMentionShortcodes } from './mentions'
import 'katex/dist/katex.min.css'
@@ -120,8 +120,7 @@ function createComponents(
const dataType = node?.properties?.dataType as string | undefined
if (dataType === 'fileCard') {
const rawHref = (node?.properties?.dataHref as string) || ''
- // Only allow http(s) URLs to prevent javascript: and other dangerous schemes.
- const href = /^https?:\/\//i.test(rawHref) ? rawHref : ''
+ const href = isAllowedFileCardHref(rawHref) ? rawHref : ''
const filename = (node?.properties?.dataFilename as string) || ''
return (
<div className="my-1 flex items-center gap-2 rounded-md border border-border bg-muted/50 px-2.5 py-1 transition-colors hover:bg-muted">
diff --git a/packages/ui/markdown/file-cards.ts b/packages/ui/markdown/file-cards.ts
index 63bf101a6d..f33e05dc84 100644
--- a/packages/ui/markdown/file-cards.ts
+++ b/packages/ui/markdown/file-cards.ts
@@ -15,8 +15,28 @@
const IMAGE_EXTS = /\.(png|jpe?g|gif|webp|svg|ico|bmp|tiff?)$/i
+/**
+ * URL alternation accepted inside `!file[name](url)` markdown.
+ *
+ * Restricted to:
+ * - `/uploads/...` site-relative paths (LocalStorage backend with no LOCAL_UPLOAD_BASE_URL)
+ * - `http(s)://...` absolute URLs (S3 / CloudFront / hosted)
+ *
+ * Anything else — `javascript:`, `data:`, protocol-relative `//host/x`, other
+ * APIs `/api/…`, path-traversal `/../…` — is rejected so a stored file-card
+ * cannot be turned into an out-of-band navigation.
+ */
+export const FILE_CARD_URL_PATTERN = /\/uploads\/[^)]*|https?:\/\/[^)]+/
+
+/** Prefix test applied by renderers to validate `data-href` before opening it. */
+export function isAllowedFileCardHref(href: string): boolean {
+ return /^(https?:\/\/|\/uploads\/)/i.test(href)
+}
+
/** New syntax: !file[name](url) — unambiguous, no hostname matching needed. */
-const NEW_FILE_CARD_RE = /^!file\[([^\]]*)\]\((https?:\/\/[^)]+)\)$/
+const NEW_FILE_CARD_RE = new RegExp(
+ `^!file\\[([^\\]]*)\\]\\((${FILE_CARD_URL_PATTERN.source})\\)$`,
+)
/** Legacy syntax: [name](cdnUrl) on its own line — matched by CDN hostname. */
const FILE_LINK_LINE = /^\[([^\]]+)\]\((https?:\/\/[^)]+)\)$/
diff --git a/packages/ui/markdown/index.ts b/packages/ui/markdown/index.ts
index 7f4a068509..d11121fb27 100644
--- a/packages/ui/markdown/index.ts
+++ b/packages/ui/markdown/index.ts
@@ -3,4 +3,10 @@ export { CodeBlock, InlineCode, type CodeBlockProps } from './CodeBlock'
export { StreamingMarkdown, type StreamingMarkdownProps } from './StreamingMarkdown'
export { preprocessLinks, detectLinks, hasLinks } from './linkify'
export { preprocessMentionShortcodes } from './mentions'
-export { preprocessFileCards, isCdnUrl, isFileCardUrl } from './file-cards'
+export {
+ preprocessFileCards,
+ isCdnUrl,
+ isFileCardUrl,
+ isAllowedFileCardHref,
+ FILE_CARD_URL_PATTERN,
+} from './file-cards'
diff --git a/packages/views/editor/extensions/file-card.tsx b/packages/views/editor/extensions/file-card.tsx
index 3a94f6dfb3..ba7f2f65bb 100644
--- a/packages/views/editor/extensions/file-card.tsx
+++ b/packages/views/editor/extensions/file-card.tsx
@@ -18,11 +18,16 @@ import { Node, mergeAttributes } from "@tiptap/core";
import { ReactNodeViewRenderer, NodeViewWrapper } from "@tiptap/react";
import type { NodeViewProps } from "@tiptap/react";
import { Eye, FileText, Loader2, Download } from "lucide-react";
+import { FILE_CARD_URL_PATTERN } from "@multica/ui/markdown";
import { useT } from "../../i18n";
import { useAttachmentDownloadResolver } from "../attachment-download-context";
import { useAttachmentPreview } from "../attachment-preview-modal";
import { getPreviewKind } from "../utils/preview";
+const FILE_CARD_MARKDOWN_RE = new RegExp(
+ `^!file\\[([^\\]]*)\\]\\((${FILE_CARD_URL_PATTERN.source})\\)`,
+);
+
// ---------------------------------------------------------------------------
// Helpers
@@ -184,7 +189,7 @@ export const FileCardExtension = Node.create({
return src.search(/^!file\[/m);
},
tokenize(src: string) {
- const match = src.match(/^!file\[([^\]]*)\]\((https?:\/\/[^)]+)\)/);
+ const match = src.match(FILE_CARD_MARKDOWN_RE);
if (!match) return undefined;
return {
type: "fileCard",
diff --git a/packages/views/editor/file-card-href.test.ts b/packages/views/editor/file-card-href.test.ts
new file mode 100644
index 0000000000..8d86fcb9d4
--- /dev/null
+++ b/packages/views/editor/file-card-href.test.ts
@@ -0,0 +1,92 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ FILE_CARD_URL_PATTERN,
+ isAllowedFileCardHref,
+ preprocessFileCards,
+} from "@multica/ui/markdown";
+
+describe("isAllowedFileCardHref", () => {
+ it.each([
+ ["/uploads/ok", true],
+ ["/uploads/workspaces/abc/file.png", true],
+ ["https://cdn.example.com/x", true],
+ ["http://localhost:8080/uploads/x.png", true],
+ ["HTTPS://CDN.EXAMPLE.COM/x", true],
+ ])("accepts %s", (href, expected) => {
+ expect(isAllowedFileCardHref(href)).toBe(expected);
+ });
+
+ it.each([
+ ["javascript:alert(1)", false],
+ ["JavaScript:alert(1)", false],
+ ["data:text/html,xss", false],
+ ["//evil.com/x", false],
+ ["/../api/x", false],
+ ["/api/x", false],
+ ["/api/internal/x", false],
+ ["", false],
+ ["ftp://example.com/x", false],
+ ["uploads/x", false],
+ ])("rejects %s", (href, expected) => {
+ expect(isAllowedFileCardHref(href)).toBe(expected);
+ });
+});
+
+describe("FILE_CARD_URL_PATTERN", () => {
+ // Mirror the parser usage: a fresh anchored regex composed from the pattern.
+ const parser = new RegExp(
+ `^!file\\[([^\\]]*)\\]\\((${FILE_CARD_URL_PATTERN.source})\\)$`,
+ );
+
+ it.each([
+ "!file[doc.md](/uploads/x.md)",
+ "!file[name](/uploads/workspaces/abc/019e.md)",
+ "!file[doc.md](https://cdn.example.com/x.md)",
+ "!file[doc.md](http://localhost:8080/uploads/x.md)",
+ ])("parses %s", (input) => {
+ expect(parser.test(input)).toBe(true);
+ });
+
+ it.each([
+ "!file[evil.txt](javascript:alert(1))",
+ "!file[evil.txt](data:text/html,xss)",
+ "!file[evil.txt](//evil.com/x)",
+ "!file[evil.txt](/../api/x)",
+ "!file[evil.txt](/api/x)",
+ "!file[doc.md](uploads/x.md)",
+ "!file[doc.md](ftp://example.com/x)",
+ ])("does not parse %s", (input) => {
+ expect(parser.test(input)).toBe(false);
+ });
+});
+
+describe("preprocessFileCards (integration)", () => {
+ const cdn = "cdn.example.com";
+
+ it("converts !file[…](/uploads/…) into a file-card div", () => {
+ const out = preprocessFileCards("!file[doc.md](/uploads/x.md)", cdn);
+ expect(out).toContain('data-type="fileCard"');
+ expect(out).toContain('data-href="/uploads/x.md"');
+ expect(out).toContain('data-filename="doc.md"');
+ });
+
+ it("leaves a protocol-relative href untouched (not parsed as file-card)", () => {
+ const out = preprocessFileCards("!file[evil.txt](//evil.com/x)", cdn);
+ expect(out).not.toContain('data-type="fileCard"');
+ expect(out).toBe("!file[evil.txt](//evil.com/x)");
+ });
+
+ it("leaves javascript: untouched (not parsed as file-card)", () => {
+ const out = preprocessFileCards(
+ "!file[evil.txt](javascript:alert(1))",
+ cdn,
+ );
+ expect(out).not.toContain('data-type="fileCard"');
+ });
+
+ it("leaves a non-/uploads relative path untouched", () => {
+ const out = preprocessFileCards("!file[name](/api/internal/x)", cdn);
+ expect(out).not.toContain('data-type="fileCard"');
+ });
+});
diff --git a/packages/views/editor/readonly-content.tsx b/packages/views/editor/readonly-content.tsx
index b7a917e6e7..e7a7868844 100644
--- a/packages/views/editor/readonly-content.tsx
+++ b/packages/views/editor/readonly-content.tsx
@@ -42,6 +42,7 @@ import { IssueMentionCard } from "../issues/components/issue-mention-card";
import { ImageLightbox } from "./extensions/image-view";
import { useLinkHover, LinkHoverCard } from "./link-hover-card";
import { openLink, isMentionHref } from "./utils/link-handler";
+import { isAllowedFileCardHref } from "@multica/ui/markdown";
import { preprocessMarkdown } from "./utils/preprocess";
import { MermaidDiagram } from "./mermaid-diagram";
import { useDownloadAttachment } from "./use-download-attachment";
@@ -332,8 +333,7 @@ function buildComponents(
const dataType = node?.properties?.dataType as string | undefined;
if (dataType === "fileCard") {
const rawHref = (node?.properties?.dataHref as string) || "";
- // Only allow http(s) URLs to prevent javascript: and other dangerous schemes.
- const href = /^https?:\/\//i.test(rawHref) ? rawHref : "";
+ const href = isAllowedFileCardHref(rawHref) ? rawHref : "";
const filename = (node?.properties?.dataFilename as string) || "";
return (
<ReadonlyFileCard
diff --git a/packages/views/issues/components/board-card.tsx b/packages/views/issues/components/board-card.tsx
index 11548c2619..c5d67f5bb7 100644
--- a/packages/views/issues/components/board-card.tsx
+++ b/packages/views/issues/components/board-card.tsx
@@ -32,6 +32,17 @@ function formatDate(date: string): string {
});
}
+function descriptionPreview(markdown: string): string {
+ return markdown
+ .replace(/!file\[[^\]]*\]\([^)]*\)/g, "")
+ .replace(/!\[[^\]]*\]\([^)]*\)/g, "")
+ .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
+ .replace(/[*_`~]+/g, "")
+ .replace(/^[\s>#]+/gm, "")
+ .replace(/\s+/g, " ")
+ .trim();
+}
+
/** Stops event from bubbling to Link/drag handlers */
function PickerWrapper({ children }: { children: React.ReactNode }) {
const stop = (e: React.SyntheticEvent) => {
@@ -117,12 +128,15 @@ export const BoardCardContent = memo(function BoardCardContent({
</div>
)}
- {/* Description */}
- {showDescription && (
- <p className="mt-1 text-xs text-muted-foreground line-clamp-1">
- {issue.description}
- </p>
- )}
+ {showDescription && (() => {
+ const preview = descriptionPreview(issue.description!);
+ if (!preview) return null;
+ return (
+ <p className="mt-1 text-xs text-muted-foreground line-clamp-1">
+ {preview}
+ </p>
+ );
+ })()}
{/* Row 3: Assignee, priority badge, due date */}
{(showAssignee || showPriority || showDueDate) && (