Files served at url are visible to anonymous users
See original GitHub issueDescribe the bug
User uploaded file viewer is wrapped in @login_required
, but somehow both anonymous users and other users can see that users file.
To Reproduce Steps to reproduce the behavior:
- Upload file “X.pdf” as user Y
- Logout
- As anonymous user, browse to show/X.pdf
- File is visible or
- As a different user, browse to show/X.pdf
- File is visible
Expected behavior I expect to be redirected to my login view when an anonymous person tries to go to this address.
Desktop (please complete the following information):
- OS: MacOS, iOS
- Browser Safari & firefox
- Version Latest
Additional context Code
@app.route('/show/<filename>')
@login_required
def show(filename: str) -> Union[Response, str]:
filename = secure_filename(filename) # clean user input
user_dir = os.path.join(app.config['UPLOAD_FOLDER'], current_user.uuid)
if os.path.exists(os.path.join(user_dir, filename)):
return send_from_directory(directory=user_dir, filename=filename)
else:
abort(404)
I honestly have no clue how this is happening. Each user has a unique uuid that acts as a unique directory where their files get stored to disk.
My bug might come from a misunderstanding of how cookies and session data are stored here. It might be relevant that I am accessing these file urls shortly after being logged in as the correct user that owns the files.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6
Top Results From Across the Web
Drupal 8 Private file is accessbile by anonymous user
I have created a content type with file field and set Upload destination is private files.
Read more >Private files load under the anonymous user - Drupal
It seems private files loaded via their link (system/files/FILE.txt) ignore the current logged-in user and assume the user is anonymous.
Read more >How can an unauthenticated user access a windows share?
Open “Network and Sharing Center” and click on the “Advanced sharing settings” link. Expand "All Networks". Check “Turn on sharing so anyone with...
Read more >How do I allow anonymous users to access an image?
In situations like this, I like to create additional web.config file located inside /resources/restricted/ folder. This way you can be sure ...
Read more >Anonymous or unknown people in a file - Google Docs Editors ...
"Anonymous animals" ... If you share or open a file with a link, you may not see the names of people who view...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ndavison Thanks for the detailed responses. After the entire day, I discovered what my problem was, and I’m sad to say it, was Cloudflare caching production. I was so confident I was thinking of every part of my stack, but I forgot about how aggressive Cloudflare caches content (images in this case).
I believe I was reproducing the issue locally because my browser was caching the results, but I’m going to look into it more.
Thanks again!
What does your
@login_manager.user_loader
callback look like? As your comment above shows, the_id
is sticky (looks like this will be fixed in the next major release https://github.com/maxcountryman/flask-login/pull/436) so if you’re using this for any sort of logic in loading the user from the cookie and session, it may be causing an issue.In the
/show/x
route, whichcurrent_user
id, name etc does it think is logged in when you’re experiencing the issue?FYI, unrelated but based on your comment above, you’ll need to clear the CSRF token on login as otherwise it too is prone to session fixation issues - the attack could be an anonymous user generates a session with a CSRF token they know, somehow gives this session to another user (using some other vulnerability, like response header injection), this other user elevates the session into an authenticated one, and therefore the attacker knows this authenticated user’s CSRF token.