mirror of
https://github.com/simonw/datasette.git
synced 2026-07-13 19:14:35 +02:00
fix: support static files with spaces in their names
Browser encodes all non-ASCII characters in URL into percent encoding format. Static files are served from the filesystem, and can have unsafe characters in their names. To support such files the request `path` should be decoded.
This commit is contained in:
parent
72f8ac680a
commit
b3156107d7
3 changed files with 10 additions and 2 deletions
|
|
@ -2,7 +2,7 @@ import hashlib
|
|||
import json
|
||||
from datasette.utils import MultiParams, calculate_etag
|
||||
from mimetypes import guess_type
|
||||
from urllib.parse import parse_qs, urlunparse, parse_qsl
|
||||
from urllib.parse import parse_qs, urlunparse, parse_qsl, unquote
|
||||
from pathlib import Path
|
||||
from http.cookies import SimpleCookie, Morsel
|
||||
import aiofiles
|
||||
|
|
@ -318,7 +318,7 @@ def asgi_static(root_path, chunk_size=4096, headers=None, content_type=None):
|
|||
path = request.scope["url_route"]["kwargs"]["path"]
|
||||
headers = static_headers.copy()
|
||||
try:
|
||||
full_path = (root_path / path).resolve().absolute()
|
||||
full_path = (root_path / unquote(path)).resolve().absolute()
|
||||
except FileNotFoundError:
|
||||
await asgi_send_html(send, "404: Directory not found", 404)
|
||||
return
|
||||
|
|
|
|||
|
|
@ -116,6 +116,14 @@ def test_static_mounts():
|
|||
) as client:
|
||||
response = client.get("/custom-static/test_html.py")
|
||||
assert response.status_code == 200
|
||||
response = client.get(
|
||||
"/custom-static/test_templates/pages/nested/filename with spaces"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
response = client.get(
|
||||
"/custom-static/test_templates/pages/topic_{topic}/{slug}.html"
|
||||
)
|
||||
assert response.status_code == 200
|
||||
response = client.get("/custom-static/not_exists.py")
|
||||
assert response.status_code == 404
|
||||
response = client.get("/custom-static/../LICENSE")
|
||||
|
|
|
|||
0
tests/test_templates/pages/nested/filename with spaces
Normal file
0
tests/test_templates/pages/nested/filename with spaces
Normal file
Loading…
Add table
Add a link
Reference in a new issue