PictoPy icon indicating copy to clipboard operation
PictoPy copied to clipboard

BUG: Face search leaves temp files in a relative temp_uploads/ folder

Open Omega-127 opened this issue 4 weeks ago • 9 comments

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:

  1. 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.
  2. temp_uploads/ is not in .gitignore, so it can be accidentally committed.
  3. 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

Omega-127 avatar Nov 03 '26 16:11 Omega-127

👋 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 :)

github-actions[bot] avatar Nov 03 '26 16:11 github-actions[bot]

⚠️ Potential Duplicate Issue (Semantic Match)

This issue appears semantically similar to the following issues in AOSSIE-Org/PictoPy:

  1. 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%

  2. 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%

  3. 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.

github-actions[bot] avatar Nov 03 '26 16:11 github-actions[bot]

Link your account with Gitcord

Thanks for opening this issue, @Omega-127!

To receive Discord notifications and contributor tracking for this organization:

  1. Join Discord: https://discord.gg/hjUhu33uAn
  2. In Discord, run /link Omega-127
  3. Paste the verification code into your GitHub bio (or a public gist)
  4. Click Verify in Discord (or run /verify-link Omega-127)

Once linked, Gitcord can notify you about assignments, mentions, and more.

— Posted by Gitcord

gitcordapp[bot] avatar Nov 03 '26 17:11 gitcordapp[bot]

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:

  1. Refactor face_tagging() in backend/app/routes/face_clusters.py to use Python’s standard tempfile.NamedTemporaryFile instead of writing to a relative temp_uploads/ folder.
  2. Ensure the temp file is unlinked in a finally block so cleanup is guaranteed even if face detection or search raises an exception.
  3. Add temp_uploads/ to .gitignore to prevent any leftover local files from being tracked.
  4. 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.

Omega-127 avatar Nov 05 '26 04:11 Omega-127

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.

Hari20032005 avatar Nov 06 '26 15:11 Hari20032005

Stepping back from this one, and closing #1698.

Two things I got wrong:

  1. @karthikeyakakarlapudi2007 had already asked to take this issue and proposed essentially the same approach (tempfile instead of the relative folder).
  2. 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.

Hari20032005 avatar Nov 06 '26 16:11 Hari20032005

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

Omega-127 avatar Nov 06 '26 16:11 Omega-127