mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
16 lines
426 B
TypeScript
16 lines
426 B
TypeScript
import { basename } from "node:path";
|
|||
|
|
|
||
|
|
/**
|
||
|
|
* Sanitize a filename to prevent path traversal attacks.
|
||
|
|
* Strips directory separators and ".." sequences, keeps only the base name.
|
||
|
|
*/
|
||
|
|
export function sanitizeFilename(raw: string): string {
|
||
|
|
let name = basename(raw);
|
||
|
|
name = name.replace(/\.\./g, "");
|
||
|
|
name = name.replace(/\0/g, "");
|
||
|
|
if (!name || name === "." || name === "..") {
|
||
|
|
name = "upload";
|
||
|
|
}
|
||
|
|
return name;
|
||
|
|
}
|