fix: ensure correct Content-Type on photo static files #240
No reviewers
Labels
No labels
domain:backend
domain:devops
domain:frontend
status:approved
status:in-progress
status:needs-fix
status:qa
type:bug
type:devops
type:feature
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
ldraney/basketball-api!240
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "210-fix-corb-warnings"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Replace the bare
StaticFilesmount with a customImageStaticFilessubclass that uses a hardcoded extension-to-MIME mapping for image files, bypassing Python'smimetypes.guess_typewhich can return incorrect types on minimal container images. This prevents browsers from triggering CORB (Cross-Origin Read Blocking) warnings when loading player photos cross-origin.Changes
src/basketball_api/static.py(new) --ImageStaticFilessubclass of Starlette'sStaticFilesthat overridesfile_responseto set explicitContent-Typeheaders for.jpg,.jpeg,.png, and.webpfiles using a hardcodedIMAGE_MIME_TYPESmap. Non-image extensions fall through to default behavior.src/basketball_api/main.py-- SwapStaticFilesimport forImageStaticFiles; update the/uploads/photosmount to use the new class.tests/test_photo_content_type.py(new) -- 6 tests verifying correctContent-Typeheaders for each image extension, confirming noapplication/octet-streamleaks, and 404 for missing files.Test Plan
pytest tests/test_photo_content_type.py -v-- all 6 new tests passpytest tests/ -v) -- 683 passed, pre-existing failures unchanged (groupme_sdk missing, DB enum collision)ruff format+ruff checkcleanReview Checklist
ruff formatandruff checkpassALLOWED_EXTENSIONSinroutes/upload.pyRelated Notes
crossorigin="anonymous"on<img>tags (separate repo/agent)Related
Closes #210
QA Review -- PR #240
Scope Check
crossoriginattribute changes correctly deferred to separate westside-app PRCode Quality
src/basketball_api/static.py(new)StaticFileswith single-responsibility overrideIMAGE_MIME_TYPESmap covers all 4 accepted extensions (.jpg,.jpeg,.png,.webp) -- matchesALLOWED_EXTENSIONSinroutes/upload.pyfile_responsesignature matches parent class (PathLiketype alias vsstr | os.PathLike[str]are equivalent at runtime)super().file_response()for non-image extensionsis_not_modified/NotModifiedResponsehandling preserved for cache behavior -- no regression on conditional requestsifbranch are fine for a rarely-changing code pathsrc/basketball_api/main.pyStaticFiles->ImageStaticFiles, no other changestests/test_photo_content_type.py(new)dbfixture andsettings.upload_dirtemp directory from conftest -- no mount hacking neededpytestimport of unusedpytestremoved (ruff would catch this) -- confirmed ruff passesSOP Compliance
ruff format+ruff checkclean210-fix-corb-warningsCloses #210Nits
None.
VERDICT: APPROVED