LibreChat/packages/api/src/storage/validation.ts
Danny Avila 9c81792d25
🔐 feat: Add Signed CloudFront File Downloads (#12970)
* feat: add signed CloudFront downloads

* fix: preserve local IdP avatar paths

* fix: address signed download review findings

* fix: harden CloudFront cookie scope validation

* fix: preserve URL save API compatibility

* fix: store CDN SSO avatars under shared prefix

* fix: Harden CloudFront tenant file access

* fix: Preserve CloudFront download compatibility

* fix: Address CloudFront review follow-ups

* fix: Preserve file URL fallback user paths

* fix: Address download review hardening

* fix: Use file owner for S3 RAG cleanup

* fix: Address final download review nits

* fix: Clear stale avatar CloudFront cookies

* fix: Align download filename helpers with dev

* fix: Address final CloudFront review follow-ups

* fix: Stream S3 URL uploads

* fix: Set S3 stream upload length

* fix: Preserve download metadata filepath

* fix: Avoid remote content length for stream uploads

* fix: Use bounded multipart URL uploads

* fix: Harden S3 filename boundaries
2026-05-06 19:48:30 -04:00

76 lines
2.2 KiB
TypeScript

export function assertPathSegment(
label: string,
value: string | null | undefined,
errorPrefix = 'path segment',
): string {
const segment = value?.toString?.() ?? '';
if (!segment) {
throw new Error(`[${errorPrefix}] ${label} must not be empty`);
}
if (segment.includes('/') || segment.includes('\\')) {
throw new Error(`[${errorPrefix}] ${label} must not contain slashes: "${segment}"`);
}
if (segment === '.' || segment === '..') {
throw new Error(`[${errorPrefix}] ${label} must not contain path traversal: "${segment}"`);
}
for (let i = 0; i < segment.length; i++) {
const code = segment.charCodeAt(i);
if (code <= 31 || code === 127) {
throw new Error(`[${errorPrefix}] ${label} contains unsafe path characters: "${segment}"`);
}
}
return segment;
}
export function assertS3FileName(
label: string,
value: string | null | undefined,
errorPrefix = 'S3 key',
): string {
const fileName = value?.toString?.() ?? '';
if (!fileName) {
throw new Error(`[${errorPrefix}] ${label} must not be empty`);
}
if (fileName.includes('\\')) {
throw new Error(`[${errorPrefix}] ${label} must not contain backslashes: "${fileName}"`);
}
for (let i = 0; i < fileName.length; i++) {
const code = fileName.charCodeAt(i);
if (code <= 31 || code === 127) {
throw new Error(`[${errorPrefix}] ${label} contains unsafe path characters: "${fileName}"`);
}
}
const components = fileName.split('/');
for (const component of components) {
if (!component) {
throw new Error(`[${errorPrefix}] ${label} must not contain empty path components`);
}
if (component === '.' || component === '..') {
throw new Error(`[${errorPrefix}] ${label} must not contain path traversal: "${fileName}"`);
}
}
return fileName;
}
export function sanitizeContentDispositionFilename(filename: string): string {
let sanitized = '';
for (const character of filename) {
const code = character.charCodeAt(0);
if (
character === '"' ||
character === '\\' ||
character === ';' ||
code <= 31 ||
code === 127
) {
continue;
}
sanitized += character;
}
return sanitized || 'download';
}