File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -3,13 +3,15 @@ import * as path from "path";
3
3
import { FastifyReply , FastifyRequest } from "fastify" ;
4
4
5
5
export async function GetStaticData ( request : FastifyRequest < { Params : { '*' : string } } > , reply : FastifyReply ) {
6
+
7
+ let paramPath = request . params [ '*' ] || '' ;
6
8
7
9
// 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 ' ) ;
10
12
return ;
11
13
}
12
-
14
+
13
15
const fullPath = path . resolve ( process . cwd ( ) , 'data' , request . params [ '*' ] ) ;
14
16
try {
15
17
const stats = await fs . promises . stat ( fullPath ) ;
@@ -23,6 +25,13 @@ export async function GetStaticData(request: FastifyRequest<{ Params: { '*': str
23
25
snapshotTime : stats . mtime ,
24
26
} ) ;
25
27
} 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
+
26
35
// If it's a file read and send its contents
27
36
const data = await fs . promises . readFile ( fullPath , 'utf8' ) ;
28
37
if ( path . extname ( fullPath ) === '.json' ) {
You can’t perform that action at this time.
0 commit comments