Open History to see the last commit
1import path from "node:path";
2
3export const ATTACHMENTS_ROUTE_PREFIX = "/attachments";
4
5export function normalizeAttachmentRelativePath(rawRelativePath: string): string | null {
6 const normalized = path.normalize(rawRelativePath).replace(/^[/\\]+/, "");
7 if (normalized.length === 0 || normalized.startsWith("..") || normalized.includes("\0")) {
8 return null;
9 }
10 return normalized.replace(/\\/g, "/");
11}
12
13export function resolveAttachmentRelativePath(input: {
14 readonly stateDir: string;
15 readonly relativePath: string;
16}): string | null {
17 const normalizedRelativePath = normalizeAttachmentRelativePath(input.relativePath);
18 if (!normalizedRelativePath) {
19 return null;
20 }
21
22 const attachmentsRoot = path.resolve(path.join(input.stateDir, "attachments"));
23 const filePath = path.resolve(path.join(attachmentsRoot, normalizedRelativePath));
24 if (!filePath.startsWith(`${attachmentsRoot}${path.sep}`)) {
25 return null;
26 }
27 return filePath;
28}