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, )