BUG: Face search leaves temp files in a relative temp_uploads/ folder
Is there an existing issue for this?
- [x] I have searched the existing issues
What happened?
BUG: Face search leaves temp files in a relative temp_uploads/ folder
What happened? When face search is called with a base64 image, face_tagging() in backend/app/routes/face_clusters.py (around line 357) writes the upload to a hardcoded relative folder:
temp_dir = "temp_uploads"
os.makedirs(temp_dir, exist_ok=True)
Problems:
- The path is relative to the process's working directory, so the folder appears wherever the backend was launched (e.g. the repo root or backend/ in dev) and its location differs between dev and the packaged app.
- temp_uploads/ is not in .gitignore, so it can be accidentally committed.
- Cleanup happens in a finally block, but if the process is killed mid-request (app quit, crash) the uploaded image stays on disk. Nothing removes leftovers on the next start. For a privacy-first app, user photos lingering in an unmanaged folder is undesirable.
Found by reading the code; not yet reproduced at runtime.
Suggested fix
- Use Python's tempfile (NamedTemporaryFile / mkstemp) or a subfolder of the existing user data dir instead of "temp_uploads".
- If perform_face_search can accept bytes/an array, skip writing to disk.
- Add a test covering that the temp file is removed after the request, including when face search raises.
Record
- [x] I agree to follow this project's Code of Conduct
👋 Thanks for opening this issue! The maintainers will review it shortly. Till then, do not open any PR. This is a strict rule we like to follow around here :)
⚠️ Potential Duplicate Issue (Semantic Match)
This issue appears semantically similar to the following issues in AOSSIE-Org/PictoPy:
-
BUG: Path traversal vulnerability in face search endpoint allows reading files outside image directories (#1322, open) https://github.com/AOSSIE-Org/PictoPy/issues/1322 Similarity: 66.5%
-
BUG: Two video face tests create video_frames/ under the real user data directory (#1602, open) https://github.com/AOSSIE-Org/PictoPy/issues/1602 Similarity: 59.7%
-
BUG: Resource leak on partial failure in image_util_classify_and_face_detect_images (#1160, open) https://github.com/AOSSIE-Org/PictoPy/issues/1160 Similarity: 57.9%
Please review before proceeding.
Link your account with Gitcord
Thanks for opening this issue, @Omega-127!
To receive Discord notifications and contributor tracking for this organization:
- Join Discord: https://discord.gg/hjUhu33uAn
- In Discord, run
/link Omega-127 - Paste the verification code into your GitHub bio (or a public gist)
- Click Verify in Discord (or run
/verify-link Omega-127)
Once linked, Gitcord can notify you about assignments, mentions, and more.
— Posted by Gitcord
Hi @Omega-127, thanks for reporting this! If you aren't currently working on a PR for this, I would love to take it up.
@penguinzz @maintainers, my proposed plan:
- Refactor
face_tagging()inbackend/app/routes/face_clusters.pyto use Python’s standardtempfile.NamedTemporaryFileinstead of writing to a relativetemp_uploads/folder. - Ensure the temp file is unlinked in a
finallyblock so cleanup is guaranteed even if face detection or search raises an exception. - Add
temp_uploads/to.gitignoreto prevent any leftover local files from being tracked. - Add a unit test verifying that temporary files are deleted on both successful execution and failure.
Could you please assign this issue to me?
@karthikeyakakarlapudi2007 I was actually planning to work on this but unfortunately there are some things that I need to deal with first, so I guess you can take this one only if the maintainers agree to this of course.
Thanks @Omega-127, hope everything sorts out on your end!
@Penguinzz, I would love to work on this issue. Could you please assign it to me?
Opened #1698 with a fix for this.
What it does: replaces the hard-coded relative temp_uploads/ write with a _temporary_base64_image() context manager built on tempfile.mkstemp(), so the decoded upload goes to the system temp directory and is removed on exit — including when the search raises. The uuid-based filename and the finally block that only served this branch are gone; the path branch behaves as before.
Two regression tests cover it: the file is created outside the CWD (test chdirs into a temp dir and asserts no temp_uploads/ appears) and is deleted both on success and when the caller raises.
On the bot's duplicate suggestion: #1322 is path traversal in a different code path and #1602 is about test fixtures creating video_frames/, so I treated this as separate — happy to fold them in if you'd prefer.
pytest tests/test_face_search.py → 13 passed; black --check clean. Happy to reparent the helper into app/utils/ if you'd rather keep route modules thin.
Stepping back from this one, and closing #1698.
Two things I got wrong:
- @karthikeyakakarlapudi2007 had already asked to take this issue and proposed essentially the same approach (
tempfileinstead of the relative folder). - Our CONTRIBUTING says not to open a PR for an issue that hasn't been reviewed and labelled by a maintainer. I opened one anyway. That's my mistake — sorry for the extra triage noise, @Penguinzz.
The branch is still in my fork (Hari20032005/PictoPy, fix-1682-temp-uploads) if it's useful to whoever takes this: tempfile.mkstemp behind a context manager, cleanup on exception, plus two regression tests. Happy for karthikeya to use it, or to hand the patch over — whichever the maintainers prefer.
AI disclosure per the AI policy: I used an AI assistant to help implement and test this change; I reviewed the diff and ran the test suite locally.
I'll follow the green-light step properly from here on.
Actually the thing is you guys will have to discuss this issue with the maintainers on their discord server only then you will be assigned. I would've loved to work on this but right now I'm busy in some other things. Good luck with this!
@Hari20032005 @karthikeyakakarlapudi2007