Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9337863
D8945.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Subscribers
None
D8945.diff
View Options
diff --git a/swh/web/api/views/content.py b/swh/web/api/views/content.py
--- a/swh/web/api/views/content.py
+++ b/swh/web/api/views/content.py
@@ -5,6 +5,7 @@
import functools
import io
+import os
from typing import Optional
from django.http import FileResponse
@@ -210,7 +211,7 @@
return FileResponse(
io.BytesIO(content_raw["data"]), # not copied, as this is never modified
- filename=filename,
+ filename=os.path.basename(filename),
content_type="application/octet-stream",
as_attachment=True,
)
diff --git a/swh/web/browse/tests/views/test_content.py b/swh/web/browse/tests/views/test_content.py
--- a/swh/web/browse/tests/views/test_content.py
+++ b/swh/web/browse/tests/views/test_content.py
@@ -251,7 +251,8 @@
)
-def test_content_raw_text(client, archive_data, content_text):
+@pytest.mark.parametrize("filename_prefix", ["", "/home/"])
+def test_content_raw_text(client, archive_data, content_text, filename_prefix):
url = reverse("browse-content-raw", url_args={"query_string": content_text["sha1"]})
resp = check_http_get_response(
@@ -269,7 +270,7 @@
url = reverse(
"browse-content-raw",
url_args={"query_string": content_text["sha1"]},
- query_params={"filename": filename},
+ query_params={"filename": filename_prefix + filename},
)
resp = check_http_get_response(
diff --git a/swh/web/browse/views/content.py b/swh/web/browse/views/content.py
--- a/swh/web/browse/views/content.py
+++ b/swh/web/browse/views/content.py
@@ -6,6 +6,7 @@
import difflib
from distutils.util import strtobool
import io
+import os
from typing import Any, Dict, Optional
from django.http import FileResponse, HttpRequest, HttpResponse, JsonResponse
@@ -72,7 +73,7 @@
response = FileResponse(
io.BytesIO(content_data["raw_data"]), # not copied, as this is never modified
- filename=filename,
+ filename=os.path.basename(filename),
content_type=content_type,
as_attachment=True,
)
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Jul 3 2025, 8:22 AM (8 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3214884
Attached To
D8945: api, browse: Ensure to sanitize filename passed to django FileResponse
Event Timeline
Log In to Comment