Spaces:
Sleeping
Sleeping
uvajed Claude commited on
Commit ·
7586563
1
Parent(s): eb90ef2
Security: upload size/pixel limits, image format allow-list, safer CORS and errors
Browse filesGenerated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Files changed (2)
123456789103 unmodified lines141516171819202110 unmodified lines32333435363738394041424344454647from fastapi import FastAPI, UploadFile, File, HTTPExceptionfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import JSONResponseimport iofrom app.services.analyzer import ImageAnalyzerapp = FastAPI(title="FrameMatch API",description="Analyze reference images and provide recreation guides",3 unmodified linesapp.add_middleware(CORSMiddleware,allow_origins=["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],)10 unmodified linesif not image.content_type or not image.content_type.startswith("image/"):raise HTTPException(status_code=400, detail="File must be an image")try:contents = await image.read()image_bytes = io.BytesIO(contents)result = analyzer.analyze(image_bytes)return JSONResponse(content=result)except Exception as e:raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")if __name__ == "__main__":123456789101112131415161718193 unmodified lines232425262728293010 unmodified lines414243444546474849505152535455565758596061626364656667from fastapi import FastAPI, UploadFile, File, HTTPExceptionfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import JSONResponsefrom PIL import Imageimport ioimport loggingfrom app.services.analyzer import ImageAnalyzerlogger = logging.getLogger(__name__)# Upload limits (frontend advertises "Max 10MB")MAX_UPLOAD_BYTES = 10 * 1024 * 1024MAX_PIXELS = 50_000_000 # ~50 MP; rejects decompression bombsALLOWED_FORMATS = ["JPEG", "PNG", "WEBP", "GIF"]app = FastAPI(title="FrameMatch API",description="Analyze reference images and provide recreation guides",3 unmodified linesapp.add_middleware(CORSMiddleware,allow_origins=["*"],allow_credentials=False, # no cookies/auth used; never combine with "*"allow_methods=["GET", "POST"],allow_headers=["*"],)10 unmodified linesif not image.content_type or not image.content_type.startswith("image/"):raise HTTPException(status_code=400, detail="File must be an image")# Read at most MAX_UPLOAD_BYTES + 1 so oversized uploads are rejectedcontents = await image.read(MAX_UPLOAD_BYTES + 1)if len(contents) > MAX_UPLOAD_BYTES:raise HTTPException(status_code=413, detail="Image too large (max 10MB)")# Validate the actual file content, not the client-supplied content type.# Restricting formats keeps Pillow's rarer (and more bug-prone) decoders unreachable.try:with Image.open(io.BytesIO(contents), formats=ALLOWED_FORMATS) as probe:width, height = probe.sizeexcept Exception:raise HTTPException(status_code=400, detail="Unsupported image format (use JPEG, PNG, WebP or GIF)")if width * height > MAX_PIXELS:raise HTTPException(status_code=413, detail="Image dimensions too large")try:result = analyzer.analyze(io.BytesIO(contents))return JSONResponse(content=result)except Exception:logger.exception("Analysis failed")raise HTTPException(status_code=500, detail="Analysis failed")if __name__ == "__main__":
123456fastapi==0.115.6uvicorn[standard]==0.34.0python-multipart==0.0.19pillow==11.0.0numpy==2.2.1colorthief==0.2.1123456fastapi==0.115.6uvicorn[standard]==0.34.0python-multipart==0.0.33pillow==11.0.0numpy==2.2.1colorthief==0.2.1
backend/app/main.py+29-9
backend/requirements.txt+1-1