⭐️⭐️⭐️ 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 { serverRuntimeEnv } from '@/utils/runtimeEnv';
// Avatars and attachments live on the api, on another origin. Serving them through
// the web origin makes them local images for next/image: `images.remotePatterns` is
// frozen into the build, so an absolute api url cannot be optimized by an image that
// has to serve any instance.
//
// Only the api's public, unauthenticated media routes are reachable here, and no
// request header is forwarded — this must never become a way to reach the rest of the
// api through the web server.
const MEDIA_ROOTS = ['avatars', 'attachments', 'chat-attachments', 'initiative-attachments'];
const PUBLIC_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
// The api path a media path names, or null for any other shape: `<root>/<id>/raw`, or
// the asset of a shared document, `share/document/<token>/assets/<id>/raw`, which the
// share token alone opens.
function upstreamPath(path: string[]): string | null {
const [root, publicId, raw] = path;
if (path.length === 3 && MEDIA_ROOTS.includes(root) && PUBLIC_ID.test(publicId) && raw === 'raw')
return path.join('/');
const [share, kind, token, assets, assetId, last] = path;
if (
path.length === 6 &&
share === 'share' &&
kind === 'document' &&
PUBLIC_ID.test(token) &&
assets === 'assets' &&
PUBLIC_ID.test(assetId) &&
last === 'raw'
)
return path.join('/');
return null;
}
// Copied from the api's response, including the headers that keep attacker-controlled
// bytes inert (nosniff, the disposition that forces a download, the sandbox CSP).
const PASSTHROUGH_HEADERS = [
'content-type',
'content-length',
'content-disposition',
'cache-control',
'etag',
'x-content-type-options',
'content-security-policy',
];
export async function GET(request: Request, { params }: { params: Promise<{ path: string[] }> }) {
const upstream = upstreamPath((await params).path);
if (!upstream) return new Response(null, { status: 404 });
// The public origin is what the browser uses; inside a compose network the api is
// reached by service name, which is what SERVICE_URL_API carries (as for the worker).
const origin = process.env.SERVICE_URL_API || serverRuntimeEnv().apiUrl;
const { search } = new URL(request.url);
const response = await fetch(`${origin}/${upstream}${search}`, {
// The api answers 304 to it, and passing it on keeps a cached avatar cached.
headers: forwardedHeaders(request),
cache: 'no-store',
redirect: 'error',
});
const headers = new Headers();
for (const name of PASSTHROUGH_HEADERS) {
const value = response.headers.get(name);
if (value) headers.set(name, value);
}
return new Response(response.body, { status: response.status, headers });
}
function forwardedHeaders(request: Request): HeadersInit {
const ifNoneMatch = request.headers.get('if-none-match');
return ifNoneMatch ? { 'if-none-match': ifNoneMatch } : {};
}