Files
e-tib.com/node_modules/.pnpm/is-safe-filename@0.1.1/node_modules/is-safe-filename/index.js
Marc Mintel d14122005d Initial commit: E-TIB production hardening & E2E foundation
Former-commit-id: ef04fca3d76375630c05aac117bf586953f3b657
2026-04-28 19:11:38 +02:00

42 lines
727 B
JavaScript

export const unsafeFilenameFixtures = Object.freeze([
'',
' ',
'.',
'..',
' .',
'. ',
' ..',
'.. ',
'../',
'../foo',
'foo/../bar',
'foo/bar',
'foo\\bar',
'foo\0bar',
]);
export default function isSafeFilename(filename) {
if (typeof filename !== 'string') {
return false;
}
const trimmed = filename.trim();
return trimmed !== ''
&& trimmed !== '.'
&& trimmed !== '..'
&& !filename.includes('/')
&& !filename.includes('\\')
&& !filename.includes('\0');
}
export function assertSafeFilename(filename) {
if (typeof filename !== 'string') {
throw new TypeError('Expected a string');
}
if (!isSafeFilename(filename)) {
throw new Error(`Unsafe filename: ${JSON.stringify(filename)}`);
}
}