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"