From 329548d9b659c6651e471bb25ec810503982c958 Mon Sep 17 00:00:00 2001 From: xKhorasan Date: Mon, 11 Jan 2021 15:26:21 +0900 Subject: [PATCH] Return 404 Not Found for accessing file as directory pattern fixes #1123 --- starlette/staticfiles.py | 2 +- tests/test_staticfiles.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py index 8822efd10..3af22a824 100644 --- a/starlette/staticfiles.py +++ b/starlette/staticfiles.py @@ -153,7 +153,7 @@ async def lookup_path( try: stat_result = await aio_stat(full_path) return (full_path, stat_result) - except FileNotFoundError: + except (FileNotFoundError, NotADirectoryError): pass return ("", None) diff --git a/tests/test_staticfiles.py b/tests/test_staticfiles.py index da1c250a8..e4220cde0 100644 --- a/tests/test_staticfiles.py +++ b/tests/test_staticfiles.py @@ -243,3 +243,15 @@ def test_staticfiles_html(tmpdir): response = client.get("/missing") assert response.status_code == 404 assert response.text == "

Custom not found page

" + + +def test_staticfiles_access_file_as_directory(tmpdir): + path = os.path.join(tmpdir, "example.txt") + with open(path, "w") as file: + file.write("") + + app = StaticFiles(directory=tmpdir) + client = TestClient(app) + response = client.get("/example.txt/foo") + assert response.status_code == 404 + assert response.text == "Not Found"