Files
t3code/apps/server/src/attachmentPaths.ts
T
Julius Marminge 85d5df6256 Unify browser asset preview routing
- Replace attachment and favicon routes with signed asset URLs
- Harden workspace and attachment asset resolution
- Update browser preview components and shared contracts
2026-06-13 22:18:37 -07:00

28 lines
961 B
TypeScript

// @effect-diagnostics nodeBuiltinImport:off
import NodePath from "node:path";
export function normalizeAttachmentRelativePath(rawRelativePath: string): string | null {
const normalized = NodePath.normalize(rawRelativePath).replace(/^[/\\]+/, "");
if (normalized.length === 0 || normalized.startsWith("..") || normalized.includes("\0")) {
return null;
}
return normalized.replace(/\\/g, "/");
}
export function resolveAttachmentRelativePath(input: {
readonly attachmentsDir: string;
readonly relativePath: string;
}): string | null {
const normalizedRelativePath = normalizeAttachmentRelativePath(input.relativePath);
if (!normalizedRelativePath) {
return null;
}
const attachmentsRoot = NodePath.resolve(input.attachmentsDir);
const filePath = NodePath.resolve(NodePath.join(attachmentsRoot, normalizedRelativePath));
if (!filePath.startsWith(`${attachmentsRoot}${NodePath.sep}`)) {
return null;
}
return filePath;
}