Skip to content

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

Go

feat(upload): add file type whitelist aligned with Agent readability

Naiyuan Qing102b19d948ea
diff --git a/apps/web/components/common/file-upload-button.tsx b/apps/web/components/common/file-upload-button.tsx
index a52bd1dcdf..0f993851fe 100644
--- a/apps/web/components/common/file-upload-button.tsx
+++ b/apps/web/components/common/file-upload-button.tsx
@@ -3,6 +3,7 @@
import { useRef } from "react";
import { Paperclip } from "lucide-react";
import { cn } from "@/lib/utils";
+import { FILE_INPUT_ACCEPT } from "@/shared/constants/upload";
interface FileUploadButtonProps {
/** Called with the selected File — caller handles upload. */
@@ -48,6 +49,7 @@ function FileUploadButton({
ref={inputRef}
type="file"
className="hidden"
+ accept={FILE_INPUT_ACCEPT}
onChange={handleChange}
/>
</>
diff --git a/apps/web/shared/constants/__tests__/upload.test.ts b/apps/web/shared/constants/__tests__/upload.test.ts
new file mode 100644
index 0000000000..389ccbb1b7
--- /dev/null
+++ b/apps/web/shared/constants/__tests__/upload.test.ts
@@ -0,0 +1,63 @@
+import { describe, it, expect } from "vitest";
+import {
+ isAllowedFileType,
+ ALLOWED_MIME_PATTERNS,
+ MAX_FILE_SIZE,
+} from "../upload";
+
+describe("upload constants", () => {
+ it("allows standard image types", () => {
+ expect(isAllowedFileType("image/png")).toBe(true);
+ expect(isAllowedFileType("image/jpeg")).toBe(true);
+ expect(isAllowedFileType("image/gif")).toBe(true);
+ expect(isAllowedFileType("image/webp")).toBe(true);
+ expect(isAllowedFileType("image/svg+xml")).toBe(true);
+ });
+
+ it("allows PDF", () => {
+ expect(isAllowedFileType("application/pdf")).toBe(true);
+ });
+
+ it("allows text types via wildcard", () => {
+ expect(isAllowedFileType("text/plain")).toBe(true);
+ expect(isAllowedFileType("text/markdown")).toBe(true);
+ expect(isAllowedFileType("text/csv")).toBe(true);
+ expect(isAllowedFileType("text/html")).toBe(true);
+ expect(isAllowedFileType("text/x-python")).toBe(true);
+ });
+
+ it("allows JSON", () => {
+ expect(isAllowedFileType("application/json")).toBe(true);
+ });
+
+ it("rejects video/audio", () => {
+ expect(isAllowedFileType("video/mp4")).toBe(false);
+ expect(isAllowedFileType("audio/mpeg")).toBe(false);
+ });
+
+ it("rejects Office binary formats", () => {
+ expect(
+ isAllowedFileType(
+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
+ ),
+ ).toBe(false);
+ });
+
+ it("rejects unknown types", () => {
+ expect(isAllowedFileType("application/octet-stream")).toBe(false);
+ });
+
+ it("handles case-insensitivity", () => {
+ expect(isAllowedFileType("IMAGE/PNG")).toBe(true);
+ expect(isAllowedFileType("Text/Plain")).toBe(true);
+ });
+
+ it("exports MAX_FILE_SIZE as 100MB", () => {
+ expect(MAX_FILE_SIZE).toBe(100 * 1024 * 1024);
+ });
+
+ it("exports ALLOWED_MIME_PATTERNS as non-empty array", () => {
+ expect(ALLOWED_MIME_PATTERNS).toBeInstanceOf(Array);
+ expect(ALLOWED_MIME_PATTERNS.length).toBeGreaterThan(0);
+ });
+});
diff --git a/apps/web/shared/constants/upload.ts b/apps/web/shared/constants/upload.ts
new file mode 100644
index 0000000000..cf27f73147
--- /dev/null
+++ b/apps/web/shared/constants/upload.ts
@@ -0,0 +1,81 @@
+/**
+ * Upload file type configuration.
+ *
+ * Aligned with what Claude Code agents can natively read:
+ * - Images: multimodal vision (Read tool)
+ * - PDF: Read tool (max 20 pages at a time)
+ * - Text/code: Read tool (plain text)
+ * - JSON: Read tool (plain text)
+ *
+ * NOT supported (Agent can't read):
+ * - Video/audio: Claude has no AV processing
+ * - Office binary (.docx/.xlsx): needs conversion tools
+ * - Arbitrary binaries: no use case
+ */
+
+export const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100 MB
+
+/** MIME patterns — exact match or wildcard (e.g. "text/*") */
+export const ALLOWED_MIME_PATTERNS: readonly string[] = [
+ // Images (Agent: multimodal vision)
+ "image/png",
+ "image/jpeg",
+ "image/gif",
+ "image/webp",
+ "image/svg+xml",
+ // Documents (Agent: Read tool)
+ "application/pdf",
+ // Text & code (Agent: Read tool, plain text)
+ "text/*",
+ // Structured data (Agent: Read tool)
+ "application/json",
+];
+
+/**
+ * HTML accept attribute value for <input type="file">.
+ * Must mirror ALLOWED_MIME_PATTERNS for browser-level filtering.
+ */
+export const FILE_INPUT_ACCEPT = [
+ "image/png",
+ "image/jpeg",
+ "image/gif",
+ "image/webp",
+ "image/svg+xml",
+ "application/pdf",
+ "text/*",
+ "application/json",
+ ".md",
+ ".markdown",
+ ".json",
+ ".csv",
+ ".ts",
+ ".tsx",
+ ".js",
+ ".jsx",
+ ".py",
+ ".go",
+ ".rs",
+ ".rb",
+ ".java",
+ ".c",
+ ".cpp",
+ ".h",
+ ".sql",
+ ".yaml",
+ ".yml",
+ ".toml",
+ ".xml",
+ ".sh",
+ ".ipynb",
+].join(",");
+
+/** Check if a MIME type matches our allowed patterns. */
+export function isAllowedFileType(mimeType: string): boolean {
+ const ct = mimeType.toLowerCase();
+ return ALLOWED_MIME_PATTERNS.some((pattern) => {
+ if (pattern.endsWith("/*")) {
+ return ct.startsWith(pattern.slice(0, -1));
+ }
+ return ct === pattern;
+ });
+}
diff --git a/apps/web/shared/hooks/use-file-upload.ts b/apps/web/shared/hooks/use-file-upload.ts
index ddfe02f9ae..0134cc2d9b 100644
--- a/apps/web/shared/hooks/use-file-upload.ts
+++ b/apps/web/shared/hooks/use-file-upload.ts
@@ -4,8 +4,7 @@ import { useState, useCallback } from "react";
import { toast } from "sonner";
import { api } from "@/shared/api";
import type { Attachment } from "@/shared/types";
-
-const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100 MB
+import { MAX_FILE_SIZE, isAllowedFileType } from "@/shared/constants/upload";
export interface UploadResult {
id: string;
@@ -23,6 +22,12 @@ export function useFileUpload() {
const upload = useCallback(
async (file: File, ctx?: UploadContext): Promise<UploadResult | null> => {
+ if (!isAllowedFileType(file.type)) {
+ throw new Error(
+ `Unsupported file type: ${file.type || file.name.split(".").pop()}`,
+ );
+ }
+
if (file.size > MAX_FILE_SIZE) {
throw new Error("File exceeds 100 MB limit");
}