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:
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
Reference in New Issue
Block a user