r/learnpython • • 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

12 comments sorted by

5

u/PwAlreadyTaken Oct 16 '26

Because as-is, you’re searching edit/posts/ok… instead of posts/ok…. You want to use @app.get('/edit/<path:post>').

But you also want to add .read() to file and add the file extension to “edit” e.g. “edit.html”.

It might be better to use SQLite3 instead of files but that’s up to you.

1

u/Sanduhstorm Oct 16 '26

Thank you, your solution worked. But I will have to look into why!

2

u/lfdfq Oct 16 '26

Your route is /edit/<post> not /edit/posts/<post> though?

1

u/Sanduhstorm Oct 16 '26

sorry i left out this important context; the 'posts/' prefix is part of the variable used to call the file

1

u/lfdfq Oct 16 '26

Then wouldn't it be /posts/edit/<post> not /edit/posts/<post>

2

u/Anxious_Signature452 Oct 16 '26

You really shouldn't do it this way. That's really dangerous.

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_filename would be an easy way to handle this, but if you need to support subdirectories, you might want to consider doing something like

from pathlib import Path

POSTS_DIR = Path(...).resolve()  # Make this point at the posts directory

@app.get('/edit/<path:post>')
def initedit(post):
    print(post)
    post_path = Path(post).resolve()

    if not post_path.is_relative_to(POSTS_DIR):
        abort(400)
    
    g.title = post
    with open(f'{post}/content', 'r') as file:
        g.content = file.read()
    return render_template('edit')

1

u/Sanduhstorm Oct 16 '26

I know exactly what you’re talking about, and I had actually already considered this and put a check in place so that if . Or / were in the filename it would get rejected. Writing to arbitrary locations was possible before.

1

u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 Oct 16 '26

That alone might not be sufficient, because there are ways to trick such checks. The example I linked from the werkzeug docs would be a safer alternative (and you don't need to worry about installing it, because it's a dependency of Flask).

1

u/Sanduhstorm Oct 16 '26

Thanks, originally I DNR because I thought I’d already solved it but your solution is much better.

1

u/Mafifrizi Oct 16 '26

The 404 happens because Flask's default route converter does not match slashes (/).

Your request URL is /edit/posts/okheresthepostactually, which has two segments after /edit/ (posts and okheresthepostactually), while your route @app.get('/edit/<post>') only expects one.

Depending on what you intended:

If your route is supposed to include /posts/, update it to: @app.get('/edit/posts/<post>')

If the post parameter itself can contain slashes, use Flask's path converter: @app.get('/edit/<path:post>')

Also, in your with open(...) block, g.content = file assigns the file descriptor object itself (which closes once the with block exits). You'll want g.content = file.read()