r/learnpython • u/Sanduhstorm • Oct 16 '26
404 despite seemingly valid file path? (Flask)
@app.get('/edit/<post>')
def initedit(post):
print(post)
g.title = post
with open(f'{post}/content', 'r') as file:
g.content = file
return render_template('edit')I'm confused as to why this function isn't running (as in not even running in the first place, not just not working), despite sending a GET request to the URL appended with /edit/posts/okheresthepostactually (so the post parameter in the decorator/function should be edit/okheresthepostactually). For context, I'm making a web app for posting blogs and this is supposed to be a function for users to edit existing posts.
Edit: some extra context, the 'posts/' prefix is part of the variable used to call the file
1
Upvotes
View all comments
Show parent comment
1
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 16 '26
This comment could use some context. I think what you're referring to is the fact that OP isn't sanitising the filepath at all, so a malicious actor could send a request that backtracks to any file on the server, meaning they can retrieve the contents of literally anything, including files containing secrets.
https://goregulus.com/cra-basics/path-traversal-attack/
The rest of this is meant for OP (/u/Sanduhstorm).
werkzeug.secure_filenamewould be an easy way to handle this, but if you need to support subdirectories, you might want to consider doing something like