Skip to content

Commit 99168bf

Browse files
committed
fix: bug acceess denied on outside dir & allow to read only json & txt
1 parent 986817b commit 99168bf

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/controller/get-static-data.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import * as path from "path";
33
import { FastifyReply, FastifyRequest } from "fastify";
44

55
export async function GetStaticData(request: FastifyRequest<{ Params: { '*': string } }>, reply: FastifyReply) {
6+
7+
let paramPath = request.params['*'] || '';
68

79
// Ensure the requested path is within the base path
8-
if ((request.params['*'].match(/\//g) || []).length >= 1) {
9-
reply.status(403).send('Access denied: Too many subdirectories');
10+
if (paramPath.startsWith('/')) {
11+
reply.status(403).send('Access denied: Outside of allowed directory');
1012
return;
1113
}
12-
14+
1315
const fullPath = path.resolve(process.cwd(), 'data', request.params['*']);
1416
try {
1517
const stats = await fs.promises.stat(fullPath);
@@ -23,6 +25,13 @@ export async function GetStaticData(request: FastifyRequest<{ Params: { '*': str
2325
snapshotTime: stats.mtime,
2426
});
2527
} else {
28+
29+
const ext = path.extname(fullPath).toLowerCase();
30+
if (ext !== '.json' && ext !== '.txt') {
31+
reply.status(403).send('Access denied: Only .json and .txt files are allowed');
32+
return;
33+
}
34+
2635
// If it's a file read and send its contents
2736
const data = await fs.promises.readFile(fullPath, 'utf8');
2837
if (path.extname(fullPath) === '.json') {

0 commit comments

Comments
 (0)