Skip to content

Can the path returned by the ":path" convertor be trusted? #11541

Answered by nymous
pythonweb2 asked this question in Questions
Discussion options

You must be logged in to vote

I did not get a 404 when testing, instead I could access unintended files anywhere in the filesystem (using FastAPI 0.111.0).

from fastapi import FastAPI

app = FastAPI()

@app.get("/file/{file_path:path}")
def get_path(file_path: str):
    print(file_path)
    with open(file_path, "r") as f:
        content = f.read()
    return content
curl http://localhost:8000/file/..%2F..%2F..%2F..%2F..%2F..%2Fetc%2Fpasswd

Result:

root:x:0:0::/root:/bin/bash\nbin:x:1:1::/:/usr/bin/nologin[...]

Replies: 2 comments 2 replies

Comment options

Kludex
May 6, 2024
Collaborator Sponsor

You must be logged in to vote
1 reply
@pythonweb2
Comment options

Comment options

You must be logged in to vote
1 reply
@Kludex
Comment options

Kludex May 7, 2024
Collaborator Sponsor

Answer selected by Kludex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
3 participants