From bc3b4002e241353d96e1ad18d8c6b5fc4f8463df Mon Sep 17 00:00:00 2001 From: "Bruno D. Rodrigues" Date: Sat, 8 Mar 2025 07:59:24 +0000 Subject: [PATCH] Allow relative directory path when `follow_symlinks=True` (#2896) * Fix for StaticFiles(follow_symlinks=True, directory="some relative path") that stopped working with commit eee4cdc * Change test name * Delete test parameter --------- Co-authored-by: Bruno D. Rodrigues Co-authored-by: Marcelo Trylesinski --- starlette/staticfiles.py | 1 + tests/test_staticfiles.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py index 02848241..637da648 100644 --- a/starlette/staticfiles.py +++ b/starlette/staticfiles.py @@ -153,6 +153,7 @@ class StaticFiles: 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 2c5e7e2d..e11e3cb3 100644 --- a/tests/test_staticfiles.py +++ b/tests/test_staticfiles.py @@ -593,3 +593,11 @@ 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_directory_symlinks(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" -- 2.47.2