Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

Merge commit from fork

Croff42208900be7d
diff --git a/apps/api/src/modules/attachments/__tests__/integration/attachments.test.ts b/apps/api/src/modules/attachments/__tests__/integration/attachments.test.ts
index 56b27639..a63eb5cc 100644
--- a/apps/api/src/modules/attachments/__tests__/integration/attachments.test.ts
+++ b/apps/api/src/modules/attachments/__tests__/integration/attachments.test.ts
@@ -354,6 +354,16 @@ describe('attachments', () => {
expect(String(raw.data)).toBe('hello');
});
+ it('rejects a content type that carries a second type', async () => {
+ const { asOwner, issueId } = await setupIssue();
+ const res = await asOwner.issues({ issueId }).attachments.import.post({
+ filename: 'x.html',
+ contentBase64: Buffer.from('<script>alert(1)</script>').toString('base64'),
+ contentType: 'video/mp4,text/html',
+ });
+ expect(res.status).toBe(400);
+ });
+
it('rejects a request with neither url nor contentBase64', async () => {
const { asOwner, issueId } = await setupIssue();
const res = await asOwner.issues({ issueId }).attachments.import.post({ filename: 'x.txt' });
diff --git a/apps/api/src/modules/attachments/__tests__/unit/response-headers.test.ts b/apps/api/src/modules/attachments/__tests__/unit/response-headers.test.ts
new file mode 100644
index 00000000..bed4961f
--- /dev/null
+++ b/apps/api/src/modules/attachments/__tests__/unit/response-headers.test.ts
@@ -0,0 +1,22 @@
+import { describe, it, expect } from 'bun:test';
+import { attachmentResponseHeaders } from '../../storage';
+
+function headersFor(contentType: string) {
+ return attachmentResponseHeaders({ contentType, filename: 'f', etag: '"e"', download: false });
+}
+
+// Rows stored before uploads were checked for a single media type can still hold one
+// that a browser reads as HTML.
+describe('attachment response headers', () => {
+ it('forces a download for a stored type that carries a second type', () => {
+ const headers = headersFor('image/png, text/html');
+ expect(headers['Content-Disposition']).toStartWith('attachment');
+ expect(headers['Content-Security-Policy']).toContain('sandbox');
+ });
+
+ it('serves an inline file with its bare media type', () => {
+ const headers = headersFor('image/png;x=,text/html');
+ expect(headers['Content-Disposition']).toStartWith('inline');
+ expect(headers['Content-Type']).toBe('image/png');
+ });
+});
diff --git a/apps/api/src/modules/attachments/storage.ts b/apps/api/src/modules/attachments/storage.ts
index b6cd9145..5c111d00 100644
--- a/apps/api/src/modules/attachments/storage.ts
+++ b/apps/api/src/modules/attachments/storage.ts
@@ -171,6 +171,8 @@ export function attachmentEtag(s3Key: string): string {
return `"${createHash('sha256').update(s3Key).digest('base64url').slice(0, 22)}"`;
}
+const INLINE_TYPE = /^(image\/(png|jpe?g|gif|webp|avif|bmp)|(video|audio)\/[\w.+-]+)$/i;
+
export function attachmentResponseHeaders(input: {
contentType: string;
filename: string;
@@ -178,12 +180,12 @@ export function attachmentResponseHeaders(input: {
etag: string;
download: boolean;
}): Record<string, string> {
- const inlineSafe = /^(image\/(png|jpe?g|gif|webp|avif|bmp)|video\/|audio\/)/i.test(
- input.contentType,
- );
- const inline = inlineSafe && !input.download;
+ // An inline file is sent with its bare media type: a browser reads the last type of
+ // a comma-separated value, so a stored `image/png, text/html` would render as HTML.
+ const mediaType = input.contentType.split(';')[0]?.trim() ?? '';
+ const inline = INLINE_TYPE.test(mediaType) && !input.download;
const headers: Record<string, string> = {
- 'Content-Type': input.contentType,
+ 'Content-Type': inline ? mediaType : input.contentType,
'Content-Disposition': `${inline ? 'inline' : 'attachment'}; filename*=UTF-8''${encodeURIComponent(input.filename)}`,
'X-Content-Type-Options': 'nosniff',
'Cache-Control': 'private, no-cache',
diff --git a/apps/api/src/modules/chat-attachments/__tests__/integration/chat-attachments.test.ts b/apps/api/src/modules/chat-attachments/__tests__/integration/chat-attachments.test.ts
index fd9db3ac..7d89da95 100644
--- a/apps/api/src/modules/chat-attachments/__tests__/integration/chat-attachments.test.ts
+++ b/apps/api/src/modules/chat-attachments/__tests__/integration/chat-attachments.test.ts
@@ -131,6 +131,16 @@ describe('chat attachments', () => {
expect(garbage.status).toBe(400);
});
+ it('rejects a content type that carries a second type', async () => {
+ const { asOwner } = await setup();
+ const res = await asOwner.projects({ projectKey: 'MKT' })['chat-attachments'].post({
+ filename: 'poc.html',
+ contentBase64: Buffer.from('<script>alert(1)</script>').toString('base64'),
+ contentType: 'image/png, text/html',
+ });
+ expect(res.status).toBe(400);
+ });
+
it('denies a non-member uploading or reading', async () => {
const { asOwner } = await setup();
const uploaded = await upload(asOwner, 'tasks.csv', 'Task\nOnly');
diff --git a/apps/api/src/modules/chat-attachments/index.ts b/apps/api/src/modules/chat-attachments/index.ts
index 5a4670fa..0f943b43 100644
--- a/apps/api/src/modules/chat-attachments/index.ts
+++ b/apps/api/src/modules/chat-attachments/index.ts
@@ -2,14 +2,13 @@ import { Elysia } from 'elysia';
import { authContext } from '#shared/auth-context';
import { guards, entityGuard } from '#shared/guards';
import { HttpError } from '#shared/lib';
-import { getObject } from '@repo/storage';
import { mcpTool } from '#mcp/generate';
import { accessErrors, commonErrors, errors } from '#shared/responses';
import { requireUser } from '#shared/access';
import {
assertAttachmentUploadAllowed,
- attachmentEtag,
attachmentObjectKey,
+ attachmentObjectResponse,
deleteAttachmentObject,
safeAttachmentFilename,
storeAttachmentObject,
@@ -170,40 +169,13 @@ export const chatAttachmentRoutes = new Elysia({
async ({ params, query, request }) => {
const row = await getChatAttachmentByPublicId(params.publicId);
if (!row) throw new HttpError(404, 'Attachment not found');
-
- // The etag is a digest of the key rather than the key itself: this route is
- // public, and the key carries the project id and the stored filename.
- const etag = attachmentEtag(row.s3Key);
- if (request.headers.get('if-none-match') === etag) {
- return new Response(null, { status: 304, headers: { ETag: etag } });
- }
-
- let obj;
- try {
- obj = await getObject(row.s3Key);
- } catch (err) {
- throw new HttpError(404, err instanceof Error ? err.message : 'Object not found');
- }
-
- // The bytes and their content type are attacker-controlled, and this route
- // is public and same-origin as the planner UI, so serving an HTML or SVG
- // file inline would be stored XSS. Defenses: X-Content-Type-Options:nosniff
- // stops MIME sniffing, and inline rendering is allowed only for a strict
- // media allowlist (raster images, video, audio). Everything else — html,
- // svg, xml, scripts — is forced to download and cannot execute.
- const ct = row.contentType || obj.contentType;
- const inlineSafe = /^(image\/(png|jpe?g|gif|webp|avif|bmp)|video\/|audio\/)/i.test(ct);
- const inline = inlineSafe && query.download == null;
- const headers: Record<string, string> = {
- 'Content-Type': ct,
- 'Content-Disposition': `${inline ? 'inline' : 'attachment'}; filename*=UTF-8''${encodeURIComponent(row.filename)}`,
- 'X-Content-Type-Options': 'nosniff',
- 'Cache-Control': 'no-cache',
- ETag: etag,
- };
- if (obj.contentLength != null) headers['Content-Length'] = String(obj.contentLength);
- if (!inline) headers['Content-Security-Policy'] = "default-src 'none'; sandbox";
- return new Response(obj.body, { headers });
+ return attachmentObjectResponse({
+ s3Key: row.s3Key,
+ contentType: row.contentType,
+ filename: row.filename,
+ request,
+ download: query.download != null,
+ });
},
{
params: publicIdParams,
diff --git a/packages/db/src/domains/storage.ts b/packages/db/src/domains/storage.ts
index 305e6f44..9304001b 100644
--- a/packages/db/src/domains/storage.ts
+++ b/packages/db/src/domains/storage.ts
@@ -75,11 +75,16 @@ export async function getStorageSettings(): Promise<StorageSettings> {
return { ...defaultStorageSettings(), ...(stored ?? {}) };
}
-// Whether a content type passes the allowlist. An empty list accepts anything.
+const MEDIA_TYPE = /^[\w.+-]+\/[\w.+-]+$/;
+
+// Whether a content type is a single media type that passes the allowlist. An empty
+// list accepts any type. A browser reads the last type of a comma-separated value, so
+// `image/png, text/html` would pass `image/*` here and still render as HTML.
// Entries are matched case-insensitively; setStorageSettings normalizes what it stores.
export function mimeAllowed(contentType: string, allowed: string[]): boolean {
- if (allowed.length === 0) return true;
const ct = contentType.split(';')[0]?.trim().toLowerCase() ?? '';
+ if (!MEDIA_TYPE.test(ct)) return false;
+ if (allowed.length === 0) return true;
return allowed.some((pattern) =>
pattern.endsWith('/*') ? ct.startsWith(pattern.slice(0, -1)) : ct === pattern,
);