BUG: Share page returns HTTP 500 when the unlock cookie contains a non-ASCII character
Is there an existing issue for this?
- [x] I have searched the existing issues
What happened?
If someone opens a password-protected album share with a cookie that has a non-English character (like "é"), the server returns an error (HTTP 500). It should just show the password page.
Why it happens: in backend/app/share/registry.py, the function share_registry_is_unlocked checks the cookie using hmac.compare_digest. This function throws a TypeError when the text has a non-ASCII character. The visitor sends the cookie, so anyone with the share link can cause this.
How to reproduce: open the link of a password-protected share with a cookie that has a non-English character, like pictopy_share_unlock=café. The server returns 500 instead of the password page.
What should happen: a wrong cookie should be treated like any other wrong cookie. Show the password page on /s/<token>, and return 404 on the photo and thumbnail pages.
Fix idea: compare bytes instead of text, and add a test for it.
I would like to work on this!
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 :)
Hi @rohan-pandeyy , Hi @JIturiDashami
I would love to contribute on this issue i have close my previous pr also Can you please assign this to me so i can pr this issue and start working !! Thank you ;)
Hi @rohan-pandeyy , Hi @JIturiDashami
I looked into this issue and was able to reproduce the bug locally. After investigating the cause, I tried a possible fix and wanted to share my approach.
Root Cause
In backend/app/share/registry.py, the function share_registry_is_unlocked uses hmac.compare_digest(cookie, signature) to validate the unlock cookie.
When either string contains non-ASCII characters, Python raises a TypeError because string-based comparison in hmac.compare_digest only supports ASCII strings. Since this exception is not handled, the request results in an HTTP 500 instead of treating the cookie as invalid.
Proposed Fix
I tried resolving this by using byte-based comparison. My approach is to encode both cookie and _unlock_signature into UTF-8 bytes before passing them to hmac.compare_digest.
This allows the comparison to handle non-ASCII input while preserving constant-time comparison.
With this change, invalid non-ASCII cookies will be treated as unauthorized instead of causing a server error. The existing route behavior will remain unchanged: the share page will display the password form, while thumbnail and photo routes will return 404.
I have implemented the fix and verified it locally with:
- Unit tests in
tests/test_share_registry.py - Integration tests in
tests/test_share_routes.py
All 76 share-related tests are passing, including the new regression tests for non-ASCII cookies.
Would it be possible to assign this issue to me? I'd be happy to submit a PR with the fix and tests following the project's contribution guidelines.
Thanks!
Hi @rohan-pandeyy, I have a fix ready for this: compare UTF-8 bytes in share_registry_is_unlocked, with regression tests at both the registry and route level (all 78 share tests pass locally). Could you please assign this to me so I can open the PR?
Link your account with Gitcord
Thanks for opening this issue, @JIturiDashami!
To receive Discord notifications and contributor tracking for this organization:
- Join Discord: https://discord.gg/hjUhu33uAn
- In Discord, run
/link JIturiDashami - Paste the verification code into your GitHub bio (or a public gist)
- Click Verify in Discord (or run
/verify-link JIturiDashami)
Once linked, Gitcord can notify you about assignments, mentions, and more.
— Posted by Gitcord
Assigning this issue to @dhruvbit since @JIturiDashami already has a few issues assigned, and we follow a first-come, first-assigned approach.
@dhruvbit, regarding the issue fix, the intention isn't to expand the validation to accept non-ASCII characters. Instead, we'd like to introduce a UI warning informing users when a password contains a non-ASCII character that isn't supported.
@rohan-pandeyy Got it, thanks for assigning this to me and clarifying the requirements. I will focus strictly on the UI side. I will implement a warning/error message on the front-end UI that alerts the user when they try to use a password containing a non-ASCII character that isn't supported.
I'll start working on this UI warning now and update you once I have a draft or draft PR ready. Let me know if there's any specific UI placement or phrasing you prefer for this warning!