Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Replace unsafe usage of flask.send_file#2

Closed
pixeebot[bot] wants to merge 1 commit into
mainPixee-Bot-Python/Zipstack_unstract:mainfrom
pixeebot/drip-2024-08-23-pixee-python/replace-flask-send-filePixee-Bot-Python/Zipstack_unstract:pixeebot/drip-2024-08-23-pixee-python/replace-flask-send-fileCopy head branch name to clipboard
Closed

Replace unsafe usage of flask.send_file#2
pixeebot[bot] wants to merge 1 commit into
mainPixee-Bot-Python/Zipstack_unstract:mainfrom
pixeebot/drip-2024-08-23-pixee-python/replace-flask-send-filePixee-Bot-Python/Zipstack_unstract:pixeebot/drip-2024-08-23-pixee-python/replace-flask-send-fileCopy head branch name to clipboard

Conversation

@pixeebot

@pixeebot pixeebot Bot commented Aug 23, 2024

Copy link
Copy Markdown

The Flask send_file function from Flask is susceptible to a path traversal attack if its input is not properly validated.
In a path traversal attack, the malicious agent can craft a path containing special paths like ./ or ../ to resolve a file outside of the expected directory path. This potentially allows the agent to overwrite, delete or read arbitrary files. In the case of flask.send_file, the result is that a malicious user could potentially download sensitive files that exist on the filesystem where the application is being hosted.
Flask offers a native solution with the flask.send_from_directory function that validates the given path.

Our changes look something like this:

-from flask import Flask, send_file
+from flask import Flask
+import flask
+from pathlib import Path

app = Flask(__name__)

@app.route("/uploads/<path:name>")
def download_file(name):
-    return send_file(f'path/to/{name}.txt')
+    return flask.send_from_directory((p := Path(f'path/to/{name}.txt')).parent, p.name)
More reading

I have additional improvements ready for this repo! If you want to see them, leave the comment:

@pixeebot next

... and I will open a new PR right away!

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: pixee:python/replace-flask-send-file

@pixeebot pixeebot Bot mentioned this pull request Aug 23, 2024
@pixeebot

pixeebot Bot commented Aug 29, 2024

Copy link
Copy Markdown
Author

I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it?

If this change was not helpful, or you have suggestions for improvements, please let me know!

@pixeebot

pixeebot Bot commented Sep 1, 2024

Copy link
Copy Markdown
Author

Just a friendly ping to remind you about this change. If there are concerns about it, we'd love to hear about them!

@pixeebot

pixeebot Bot commented Sep 7, 2024

Copy link
Copy Markdown
Author

This change may not be a priority right now, so I'll close it. If there was something I could have done better, please let me know!

You can also customize me to make sure I'm working with you in the way you want.

@pixeebot pixeebot Bot closed this Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.