]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Allow relative directory path when `follow_symlinks=True` (#2896)
authorBruno D. Rodrigues <bruno.rodrigues@litux.org>
Sat, 8 Mar 2025 07:59:24 +0000 (07:59 +0000)
committerGitHub <noreply@github.com>
Sat, 8 Mar 2025 07:59:24 +0000 (07:59 +0000)
* 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 <bruno.rodrigues@bitsighttech.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
starlette/staticfiles.py
tests/test_staticfiles.py

index 02848241e22825d6f80c774b2eda337a1073867b..637da64802d0360b901ef369c0a121b1b75ef773 100644 (file)
@@ -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)
index 2c5e7e2dfa265f3e6c0d09aff871aa7fe9e06533..e11e3cb3017f31b9b47dbdbbea66375695ef5d7d 100644 (file)
@@ -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 == "<h1>Hello</h1>"
+
+
+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"