⭐️⭐️⭐️ 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.
TypeScript agenticagileai-agentsbug-reportingbug-trackerganttissue-trackerjirajira-alternativekanbanlinearlinear-alternativeopen-sourceproduct-managementproject-managementroadmapself-hostedsprint-planningteam-collaborationtrello-alternative
import { describe, it, expect, afterEach, beforeEach } from 'bun:test';
import { getStorageSettings } from '@repo/db';
import { api, authedApi, createProject } from '#tests/helpers/app';
import { signUpTestUser } from '#tests/helpers/auth';
import { resetDb } from '#tests/helpers/db';
import { clearLimits, setLimits } from '#tests/helpers/limits';
// Attachments feature: metadata in Postgres, bytes in the object store (shared/
// s3.ts against a real RustFS — see the Tests setup for S3_* env). This is the
// only test that exercises s3.ts (putObject/getObject/deleteObject) and the one
// public, unauthenticated route (GET /attachments/:publicId/raw), which is the
// auth-context session-gate exception.
// Owner with a project and one issue to attach files to. createProject seeds the
// default columns; an issue needs a columnId and a title.
async function setupIssue() {
const owner = await signUpTestUser();
const asOwner = authedApi(owner.cookie);
await createProject(asOwner, { key: 'MKT', name: 'Marketing' });
const view = await asOwner.projects({ projectKey: 'MKT' }).get();
const columnId = view.data!.columns[0].id;
const issue = await asOwner
.projects({ projectKey: 'MKT' })
.issues.post({ columnId, title: 'Task' });
return { asOwner, issueId: issue.data!.id };
}
function uploadFile(
client: ReturnType<typeof authedApi>,
issueId: number,
name: string,
type: string,
content = 'x',
) {
return client.issues({ issueId }).attachments.post({
file: new File([content], name, { type }),
});
}
describe('attachments', () => {
beforeEach(async () => {
await resetDb();
});
afterEach(clearLimits);
it("refuses an upload past the team's storage ceiling", async () => {
const { asOwner, issueId } = await setupIssue();
setLimits({ maxStorageBytes: 12 });
expect((await uploadFile(asOwner, issueId, 'a.txt', 'text/plain', 'hello')).status).toBe(201);
const past = await uploadFile(asOwner, issueId, 'b.txt', 'text/plain', 'hello world!');
expect(past.status).toBe(413);
// The ceiling counts the bytes already stored, not one file's size.
expect((await uploadFile(asOwner, issueId, 'c.txt', 'text/plain', 'fits')).status).toBe(201);
});
it('uploads bytes, serves them on the public raw route, and deletes them', async () => {
const { asOwner, issueId } = await setupIssue();
const file = new File(['hello world'], 'note.txt', { type: 'text/plain' });
const uploaded = await asOwner.issues({ issueId }).attachments.post({ file });
expect(uploaded.status).toBe(201);
expect(uploaded.data).toMatchObject({ filename: 'note.txt', sizeBytes: 11 });
expect(uploaded.data!.contentType).toContain('text/plain');
const publicId = uploaded.data!.id;
expect(uploaded.data!.url).toBe(`/attachments/${publicId}/raw`);
const list = await asOwner.issues({ issueId }).attachments.get();
expect(list.status).toBe(200);
expect(list.data).toHaveLength(1);
// The raw route is public: fetch it with the anonymous client (no session).
const raw = await api.attachments({ publicId }).raw.get();
expect(raw.status).toBe(200);
expect(String(raw.data)).toBe('hello world');
const del = await asOwner.attachments({ publicId }).delete();
expect(del.status).toBe(204);
// Bytes and row are gone.
const gone = await api.attachments({ publicId }).raw.get();
expect(gone.status).toBe(404);
});
it('rejects an empty file', async () => {
const { asOwner, issueId } = await setupIssue();
const empty = new File([], 'empty.txt', { type: 'text/plain' });
const res = await asOwner.issues({ issueId }).attachments.post({ file: empty });
expect(res.status).toBe(400);
});
it('serializes the shared project quota across issue, chat, and document uploads', async () => {
const { asOwner, issueId } = await setupIssue();
await asOwner.god['storage-settings'].put({ projectQuotaMb: 1 });
const initial = 'x'.repeat(256 * 1024);
expect((await uploadFile(asOwner, issueId, 'issue.txt', 'text/plain', initial)).status).toBe(
201,
);
expect(
(
await asOwner.projects({ projectKey: 'MKT' })['chat-attachments'].post({
filename: 'chat.txt',
contentType: 'text/plain',
contentBase64: Buffer.from(initial).toString('base64'),
})
).status,
).toBe(201);
const page = (
await asOwner.projects({ projectKey: 'MKT' }).documents.post({ title: 'Handbook' })
).data!;
expect(
(
await asOwner
.projects({ projectKey: 'MKT' })
.documents({ documentId: page.id })
.assets.post({
file: new File([initial], 'document.txt', { type: 'text/plain' }),
})
).status,
).toBe(201);
const contender = 'y'.repeat(200 * 1024);
const results = await Promise.all([
uploadFile(asOwner, issueId, 'parallel-issue.txt', 'text/plain', contender),
asOwner
.projects({ projectKey: 'MKT' })
.documents({ documentId: page.id })
.assets.post({
file: new File([contender], 'parallel-document.txt', { type: 'text/plain' }),
}),
]);
expect(results.map((result) => result.status).sort()).toEqual([201, 413]);
});
it('denies a non-member uploading to an issue', async () => {
const { issueId } = await setupIssue();
const outsider = authedApi((await signUpTestUser()).cookie);
const file = new File(['x'], 'x.txt', { type: 'text/plain' });
const res = await outsider.issues({ issueId }).attachments.post({ file });
expect(res.status).toBe(403);
});
describe('list', () => {
it('returns an empty list for an issue with no attachments', async () => {
const { asOwner, issueId } = await setupIssue();
const res = await asOwner.issues({ issueId }).attachments.get();
expect(res.status).toBe(200);
expect(res.data).toEqual([]);
});
it('returns 404 for an unknown issue', async () => {
const { asOwner } = await setupIssue();
const res = await asOwner.issues({ issueId: 999999 }).attachments.get();
expect(res.status).toBe(404);
});
it("denies a non-member listing an issue's attachments", async () => {
const { issueId } = await setupIssue();
const outsider = authedApi((await signUpTestUser()).cookie);
const res = await outsider.issues({ issueId }).attachments.get();
expect(res.status).toBe(403);
});
});
describe('replace', () => {
it('serves the new bytes under the same id and url', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'shot.png', 'image/png', 'before');
const publicId = up.data!.id;
const res = await asOwner.attachments({ publicId }).put({
file: new File(['after annotating'], 'shot.png', { type: 'image/png' }),
});
expect(res.status).toBe(200);
expect(res.data).toMatchObject({ id: publicId, filename: 'shot.png', sizeBytes: 16 });
expect(res.data!.url).toBe(up.data!.url);
const raw = await api.attachments({ publicId }).raw.get();
expect(String(raw.data)).toBe('after annotating');
// Still one attachment: the row was updated, not added to.
const list = await asOwner.issues({ issueId }).attachments.get();
expect(list.data).toHaveLength(1);
});
it('counts a replacement as the new bytes minus the current bytes under the quota lock', async () => {
const { asOwner, issueId } = await setupIssue();
await asOwner.god['storage-settings'].put({ projectQuotaMb: 1 });
const first = await uploadFile(
asOwner,
issueId,
'first.txt',
'text/plain',
'a'.repeat(700 * 1024),
);
expect(
(await uploadFile(asOwner, issueId, 'second.txt', 'text/plain', 'b'.repeat(250 * 1024)))
.status,
).toBe(201);
const replaced = await asOwner.attachments({ publicId: first.data!.id }).put({
file: new File(['c'.repeat(750 * 1024)], 'first.txt', { type: 'text/plain' }),
});
expect(replaced.status).toBe(200);
expect(replaced.data?.sizeBytes).toBe(750 * 1024);
const rejected = await asOwner.attachments({ publicId: first.data!.id }).put({
file: new File(['d'.repeat(800 * 1024)], 'first.txt', { type: 'text/plain' }),
});
expect(rejected.status).toBe(413);
const raw = await api.attachments({ publicId: first.data!.id }).raw.get();
expect(String(raw.data)).toHaveLength(750 * 1024);
});
it('serves the replaced bytes to a client holding the old entity tag', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'shot.png', 'image/png', 'before');
const publicId = up.data!.id;
const first = await api.attachments({ publicId }).raw.get();
const etag = first.response.headers.get('etag')!;
expect(etag).not.toBeNull();
// Unchanged: the client may reuse what it has.
const cached = await api
.attachments({ publicId })
.raw.get({ headers: { 'if-none-match': etag } });
expect(cached.status).toBe(304);
await asOwner
.attachments({ publicId })
.put({ file: new File(['after'], 'shot.png', { type: 'image/png' }) });
const refetched = await api
.attachments({ publicId })
.raw.get({ headers: { 'if-none-match': etag } });
expect(refetched.status).toBe(200);
expect(String(refetched.data)).toBe('after');
});
it('rejects an empty file', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'shot.png', 'image/png');
const res = await asOwner.attachments({ publicId: up.data!.id }).put({
file: new File([], 'shot.png', { type: 'image/png' }),
});
expect(res.status).toBe(400);
});
it('returns 404 for an unknown attachment', async () => {
const { asOwner } = await setupIssue();
const res = await asOwner
.attachments({ publicId: '00000000-0000-0000-0000-000000000000' })
.put({ file: new File(['x'], 'x.png', { type: 'image/png' }) });
expect(res.status).toBe(404);
});
it('denies a non-member replacing an attachment', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'shot.png', 'image/png');
const outsider = authedApi((await signUpTestUser()).cookie);
const res = await outsider
.attachments({ publicId: up.data!.id })
.put({ file: new File(['x'], 'x.png', { type: 'image/png' }) });
expect(res.status).toBe(403);
});
});
describe('delete', () => {
it('returns 404 when deleting a missing attachment', async () => {
const { asOwner } = await setupIssue();
const res = await asOwner
.attachments({ publicId: '00000000-0000-0000-0000-000000000000' })
.delete();
expect(res.status).toBe(404);
});
it('denies a non-member deleting an attachment', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'note.txt', 'text/plain');
const outsider = authedApi((await signUpTestUser()).cookie);
const res = await outsider.attachments({ publicId: up.data!.id }).delete();
expect(res.status).toBe(403);
});
});
// The raw route is public and same-origin as the UI, and serves
// attacker-controlled bytes, so its headers are the XSS defense (see routes.ts).
describe('raw route headers', () => {
it('forces download and sets nosniff + CSP for a non-allowlisted type', async () => {
const { asOwner, issueId } = await setupIssue();
// SVG passes the upload allowlist (image/*) but is not on the raw route's
// inline-media allowlist, so it is a stored-XSS vector the headers must defuse.
const up = await uploadFile(
asOwner,
issueId,
'x.svg',
'image/svg+xml',
'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
);
const raw = await api.attachments({ publicId: up.data!.id }).raw.get();
expect(raw.status).toBe(200);
expect(raw.response.headers.get('content-disposition')).toContain('attachment');
expect(raw.response.headers.get('x-content-type-options')).toBe('nosniff');
expect(raw.response.headers.get('content-security-policy')).toContain('sandbox');
});
it('serves an allowlisted image inline without a CSP', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'p.png', 'image/png', 'pngbytes');
const raw = await api.attachments({ publicId: up.data!.id }).raw.get();
expect(raw.status).toBe(200);
expect(raw.response.headers.get('content-disposition')).toContain('inline');
expect(raw.response.headers.get('content-security-policy')).toBeNull();
});
it('forces download for an image when ?download is set', async () => {
const { asOwner, issueId } = await setupIssue();
const up = await uploadFile(asOwner, issueId, 'p.png', 'image/png', 'pngbytes');
const raw = await api
.attachments({ publicId: up.data!.id })
.raw.get({ query: { download: '1' } });
expect(raw.response.headers.get('content-disposition')).toContain('attachment');
});
it('returns 404 for an unknown publicId', async () => {
const res = await api
.attachments({ publicId: '00000000-0000-0000-0000-000000000000' })
.raw.get();
expect(res.status).toBe(404);
});
// The route is public, so a malformed id reaching Postgres would answer 500 with
// the driver's message to an anonymous caller.
it('returns 400 for a publicId that is not a uuid', async () => {
const res = await api.attachments({ publicId: 'not-a-uuid' }).raw.get();
expect(res.status).toBe(400);
});
});
describe('import (url or base64)', () => {
it('stores inline base64 content and serves it', async () => {
const { asOwner, issueId } = await setupIssue();
const contentBase64 = Buffer.from('hello').toString('base64');
const res = await asOwner.issues({ issueId }).attachments.import.post({
filename: 'note.txt',
contentBase64,
contentType: 'text/plain',
});
expect(res.status).toBe(201);
expect(res.data).toMatchObject({ filename: 'note.txt', sizeBytes: 5 });
const raw = await api.attachments({ publicId: res.data!.id }).raw.get();
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' });
expect(res.status).toBe(400);
});
it('rejects a request with both url and contentBase64', async () => {
const { asOwner, issueId } = await setupIssue();
const res = await asOwner.issues({ issueId }).attachments.import.post({
filename: 'x.txt',
url: 'https://example.com/x.txt',
contentBase64: Buffer.from('x').toString('base64'),
});
expect(res.status).toBe(400);
});
it('stops reading a url at the size limit, whatever the server declares', async () => {
const { asOwner, issueId } = await setupIssue();
const { maxAttachmentMb } = await getStorageSettings();
const chunk = new Uint8Array(1024 * 1024);
let chunksLeft = maxAttachmentMb * 4;
const { promise: cancelled, resolve: onCancel } = Promise.withResolvers<void>();
const server = Bun.serve({
hostname: '127.0.0.1',
port: 0,
fetch: () =>
new Response(
new ReadableStream({
pull(controller) {
if (chunksLeft-- > 0) controller.enqueue(chunk);
else controller.close();
},
cancel: onCancel,
}),
),
});
// Outside production and test, the SSRF guard lets a local http url through.
const nodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
try {
const res = await asOwner
.issues({ issueId })
.attachments.import.post({ filename: 'big.bin', url: server.url.href });
expect(res.status).toBe(413);
await cancelled;
} finally {
process.env.NODE_ENV = nodeEnv;
await server.stop(true);
}
});
it('rejects a url that points at a private address (SSRF)', async () => {
const { asOwner, issueId } = await setupIssue();
const res = await asOwner
.issues({ issueId })
.attachments.import.post({ filename: 'x.txt', url: 'https://127.0.0.1/x.txt' });
expect(res.status).toBe(400);
});
});
});