from __future__ import annotations
+import errno
import importlib.util
import os
import stat
)
except PermissionError:
raise HTTPException(status_code=401)
- except OSError:
- raise
+ except OSError as exc:
+ # Filename is too long, so it can't be a valid static file.
+ if exc.errno == errno.ENAMETOOLONG:
+ raise HTTPException(status_code=404)
+
+ raise exc
if stat_result and stat.S_ISREG(stat_result.st_mode):
# We have a static file to serve.
assert response.text == "Not Found"
+def test_staticfiles_filename_too_long(
+ tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
+ routes = [Mount("/", app=StaticFiles(directory=tmpdir), name="static")]
+ app = Starlette(routes=routes)
+ client = test_client_factory(app)
+
+ path_max_size = os.pathconf("/", "PC_PATH_MAX")
+ response = client.get(f"/{'a' * path_max_size}.txt")
+ assert response.status_code == 404
+ assert response.text == "Not Found"
+
+
def test_staticfiles_unhandled_os_error_returns_500(
tmpdir: Path,
test_client_factory: TestClientFactory,