Harden download filename sanitization

This commit is contained in:
TorMatzAndren
2026-05-16 11:08:26 +02:00
parent 0cd5294944
commit 38ca4d6c8d
+16 -2
View File
@@ -22,7 +22,7 @@ export class UtilsService {
} }
ensureInsideDownloadsRoot(candidatePath: string): string { ensureInsideDownloadsRoot(candidatePath: string): string {
const root = this.getRootDownloadsPath(); const root = resolve(this.getRootDownloadsPath());
const resolvedCandidate = resolve(candidatePath); const resolvedCandidate = resolve(candidatePath);
const rel = relative(root, resolvedCandidate); const rel = relative(root, resolvedCandidate);
@@ -34,6 +34,20 @@ export class UtilsService {
} }
stripFileIllegalChars(text: string): string { stripFileIllegalChars(text: string): string {
return text.replace(/[/\\?%*:|"<>]/g, '-').trim() || 'untitled'; const sanitized = String(text || '')
.replace(/[\x00-\x1f\x80-\x9f]/g, '-')
.replace(/[/\\?%*:|"<>]/g, '-')
.replace(/\s+/g, ' ')
.replace(/[. ]+$/g, '')
.trim();
const fallback = sanitized || 'untitled';
const reservedName = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i;
if (reservedName.test(fallback) || fallback === '.' || fallback === '..') {
return `_${fallback}`;
}
return fallback.slice(0, 180);
} }
} }