Accept CDN URLs for question attachments

Attachments uploaded via question-upload get a CDN URL
(files.second-brain.ru via S3_CDN_URL), but the validator only accepted
the direct S3 endpoint prefix — so every attachment was silently dropped
(message text saved, file lost). Add isAllowedPublicUrl (CDN + direct S3)
in lib/s3 and use it in both question routes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-20 15:11:36 +05:00
parent 2436f77de5
commit 9fbb7aea6c
3 changed files with 19 additions and 21 deletions
+15
View File
@@ -19,6 +19,21 @@ export function getPublicUrl(key: string): string {
return `${process.env.S3_ENDPOINT}/${BUCKET}/${key}`;
}
/**
* Валидна ли публичная ссылка на наш загруженный файл. Принимает И CDN-домен
* (`S3_CDN_URL`, напр. files.second-brain.ru), И прямой S3-эндпоинт — иначе
* после включения CDN ссылки от uploadFile (getPublicUrl) перестают проходить
* валидацию (вложения молча отбрасываются). Нужна, чтобы клиент не подсунул
* произвольный URL — только наши бакет/CDN.
*/
export function isAllowedPublicUrl(url: string): boolean {
if (typeof url !== "string" || !url.startsWith("https://")) return false;
const prefixes: string[] = [];
if (process.env.S3_CDN_URL) prefixes.push(`${process.env.S3_CDN_URL}/`);
if (process.env.S3_ENDPOINT) prefixes.push(`${process.env.S3_ENDPOINT}/${BUCKET}/`);
return prefixes.some((p) => url.startsWith(p));
}
/**
* Заголовок Content-Disposition для принудительного скачивания с человеческим
* именем. RFC 5987: ASCII-фолбэк + UTF-8 (кириллица percent-encoded).