@@ -51,9 +51,7 @@ async def spa_middleware(request, call_next):
5151 return await call_next (request )
5252
5353
54- def _is_pe_filename (name : str ) -> bool :
55- ext = Path (name ).suffix .lower ()
56- return ext in (".exe" , ".dll" , ".sys" , ".ocx" , ".scr" , ".cpl" )
54+ PE_MAGIC = b"MZ"
5755
5856
5957def _format_size (b : int ) -> str :
@@ -71,15 +69,17 @@ async def health():
7169
7270@app .post ("/api/scan" )
7371async def scan_file (file : UploadFile = File (...)):
74- if not file .filename or not _is_pe_filename (file .filename ):
75- raise HTTPException (status_code = 400 , detail = "File must be a .exe or .dll" )
72+ content = await file .read ()
73+ if len (content ) < 2 or content [:2 ] != PE_MAGIC :
74+ raise HTTPException (
75+ status_code = 400 , detail = "Not a valid PE file (missing MZ header)"
76+ )
7677
77- suffix = Path (file .filename ).suffix .lower ()
78+ if len (content ) > 1024 * 1024 * 1024 :
79+ raise HTTPException (status_code = 400 , detail = "File exceeds 1GB limit" )
80+
81+ suffix = Path (file .filename or "file.bin" ).suffix .lower () or ".bin"
7882 with tempfile .NamedTemporaryFile (delete = False , suffix = suffix ) as tmp :
79- content = await file .read ()
80- if len (content ) > 1024 * 1024 * 1024 :
81- os .unlink (tmp .name )
82- raise HTTPException (status_code = 400 , detail = "File exceeds 1GB limit" )
8383 tmp .write (content )
8484 tmp_path = tmp .name
8585
0 commit comments