@@ -23,6 +23,17 @@ if (!fs.existsSync(distPath)) {
2323
2424const app = express ( ) ;
2525
26+ // Rate limit to 100 req/min per IP
27+ const rateLimit = new Map ( ) ;
28+ setInterval ( ( ) => rateLimit . clear ( ) , 60000 ) ; // Clear every minute to prevent memory growth
29+ app . use ( ( req , res , next ) => {
30+ const ip = req . ip ;
31+ const count = ( rateLimit . get ( ip ) || 0 ) + 1 ;
32+ rateLimit . set ( ip , count ) ;
33+ if ( count > 100 ) return res . status ( 429 ) . send ( "Too many requests" ) ;
34+ next ( ) ;
35+ } ) ;
36+
2637app . use (
2738 cors ( {
2839 origin : ( origin , callback ) => {
@@ -86,7 +97,12 @@ app.post("/upload", function (req, res) {
8697 // Sanitize filename to prevent path traversal
8798 const newFileName = path . basename ( uploadedFile . originalFilename ) ;
8899 const fileSavePath = __dirname ;
89- const newFilePath = path . join ( fileSavePath , newFileName ) ;
100+ const newFilePath = path . resolve ( fileSavePath , newFileName ) ;
101+
102+ // Verify path is within allowed directory
103+ if ( ! newFilePath . startsWith ( fileSavePath + path . sep ) ) {
104+ return res . status ( 400 ) . json ( { success : false , message : "Invalid filename" } ) ;
105+ }
90106
91107 // Move the uploaded file to the desired directory
92108 fs . rename ( uploadedFile . filepath , newFilePath , ( err ) => {
0 commit comments