Spaces:
Runtime error
Runtime error
File size: 916 Bytes
4327358 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import * as path from 'path';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs-extra');
export async function fileExists(filepath: string) {
try {
await fs.access(filepath, fs.constants.F_OK);
} catch (error) {
return false;
}
return true;
}
export function safeJoin(base: string, input: string): string {
base = path.resolve(base);
if (!input || typeof input !== 'string') {
throw new Error('Invalid path');
}
if (input.startsWith('~') || path.isAbsolute(input)) {
throw new Error('Home or absolute paths not allowed');
}
// handles slashes safely
const joined = path.join(base, input);
// normalize to an absolute path
const resolved = path.resolve(joined);
// Prevent escape outside base dir
if (!resolved.startsWith(base + path.sep)) {
throw new Error('Access outside base dir not allowed');
}
return resolved;
}
|