mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-12 16:37:06 +02:00
19 lines
545 B
TypeScript
19 lines
545 B
TypeScript
/**
|
|
* Sanitizes a filename by removing invalid characters and enforcing length limits
|
|
* Prevents path traversal and filesystem errors
|
|
*/
|
|
export function sanitizeFilename(filename: string): string {
|
|
return (
|
|
filename
|
|
.trim()
|
|
// Remove invalid filesystem characters
|
|
.replace(/[/\\:*?"<>|]/g, "_")
|
|
// Remove leading dots (hidden files)
|
|
.replace(/^\.+/, "")
|
|
// Remove trailing dots
|
|
.replace(/\.+$/, "")
|
|
// Limit to safe filename length (255 is filesystem max)
|
|
.substring(0, 255)
|
|
);
|
|
}
|