Enhance file handling in server routes

- Added checks to ensure only regular files are processed in both the main and backup server routes.
- Improved error handling for invalid file parameters and inaccessible backup files, returning appropriate HTTP responses.
This commit is contained in:
2026-04-15 17:16:40 -05:00
parent 7888edc64e
commit c3e50a2727
2 changed files with 14 additions and 1 deletions

View File

@@ -138,6 +138,8 @@ export const load: PageServerLoad = async ({ cookies }) => {
stats = await fs.stat(filePath); stats = await fs.stat(filePath);
} catch { continue; } } catch { continue; }
if (!stats.isFile()) continue;
const nodoName = path.parse(file).name; const nodoName = path.parse(file).name;
let clientData: any = null; let clientData: any = null;

View File

@@ -15,8 +15,19 @@ export const GET = async ({ url }: { url: URL }) => {
return new Response('Backup path is not configured', { status: 500 }); return new Response('Backup path is not configured', { status: 500 });
} }
const resolvedBase = path.resolve(basePath);
const filePath = path.resolve(resolvedBase, fileName);
const relativeToBase = path.relative(resolvedBase, filePath);
if (relativeToBase.startsWith('..') || path.isAbsolute(relativeToBase)) {
return new Response('Invalid file parameter', { status: 400 });
}
try { try {
const filePath = path.join(basePath, fileName); const st = await fs.stat(filePath);
if (!st.isFile()) {
console.error('Backup path is not a regular file:', filePath);
return new Response('Backup file not found or inaccessible', { status: 404 });
}
console.log('Serving backup file from', filePath); console.log('Serving backup file from', filePath);
const data = await fs.readFile(filePath); const data = await fs.readFile(filePath);
const headers = new Headers(); const headers = new Headers();