From 501fe093e30a765f5057378db8a2998105fdaaa2 Mon Sep 17 00:00:00 2001 From: "Bruno D. Rodrigues" Date: Wed, 5 Mar 2025 17:40:51 +0000 Subject: [PATCH] Fix for StaticFiles(follow_symlinks=True, directory="some relative path") that stopped working with commit eee4cdc --- starlette/staticfiles.py | 1 + tests/test_staticfiles.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py index 02848241e..637da6480 100644 --- a/starlette/staticfiles.py +++ b/starlette/staticfiles.py @@ -153,6 +153,7 @@ def lookup_path(self, path: str) -> tuple[str, os.stat_result | None]: joined_path = os.path.join(directory, path) if self.follow_symlink: full_path = os.path.abspath(joined_path) + directory = os.path.abspath(directory) else: full_path = os.path.realpath(joined_path) directory = os.path.realpath(directory) diff --git a/tests/test_staticfiles.py b/tests/test_staticfiles.py index 2c5e7e2df..f31d9b562 100644 --- a/tests/test_staticfiles.py +++ b/tests/test_staticfiles.py @@ -593,3 +593,19 @@ def test_staticfiles_self_symlinks(tmp_path: Path, test_client_factory: TestClie assert response.url == "http://testserver/index.html" assert response.status_code == 200 assert response.text == "

Hello

" + + +def test_staticfiles_relative(tmp_path: Path, test_client_factory: TestClientFactory) -> None: + app = StaticFiles(directory="tests/statics") + client = test_client_factory(app) + response = client.get("/example.txt") + assert response.status_code == 200 + assert response.text == "123\n" + + +def test_staticfiles_relative_symlinks(tmp_path: Path, test_client_factory: TestClientFactory) -> None: + app = StaticFiles(directory="tests/statics", follow_symlink=True) + client = test_client_factory(app) + response = client.get("/example.txt") + assert response.status_code == 200 + assert response.text == "123\n"