Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions backend/app/api/subroutes/pastes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from typing import TYPE_CHECKING

from dependency_injector.wiring import Provide, inject
Expand Down Expand Up @@ -161,9 +162,23 @@ async def get_paste_raw(
cached_content = await cache.get(cache_key)
if cached_content:
cache_operations.labels(operation="get", result="hit").inc()
# Build a sanitized filename from the paste_id for cached responses
def _sanitize_filename(source: str) -> str:
name = re.sub(r"[^A-Za-z0-9 \-_.]", "", source)
name = re.sub(r"\s+", "_", name)
name = name.strip("_.")
name = name[:30]
if not name:
return str(paste_id)
return name

filename = f"{_sanitize_filename(str(paste_id))}.txt"
return PlainTextResponse(
content=cached_content,
headers={"Cache-Control": f"public, max-age={config.CACHE_TTL}"},
headers={
"Cache-Control": f"public, max-age={config.CACHE_TTL}",
"Content-Disposition": f'attachment; filename="{filename}"',
Comment on lines +165 to +180
},
)

cache_operations.labels(operation="get", result="miss").inc()
Expand All @@ -177,9 +192,25 @@ async def get_paste_raw(
await cache.set(cache_key, content, ttl=config.CACHE_TTL)
cache_operations.labels(operation="set", result="success").inc()

# Sanitize filename from title (fallback to id)
def _sanitize_filename(source: str) -> str:
name = re.sub(r"[^A-Za-z0-9 \-_.]", "", source)
name = re.sub(r"\s+", "_", name)
name = name.strip("_.")
name = name[:30]
if not name:
return str(paste_id)
return name
Comment on lines +195 to +203

filename_source = paste_result.title if paste_result.title else str(paste_result.id)
filename = f"{_sanitize_filename(str(filename_source))}.txt"

return PlainTextResponse(
content=content,
headers={"Cache-Control": f"public, max-age={config.CACHE_TTL}"},
headers={
"Cache-Control": f"public, max-age={config.CACHE_TTL}",
"Content-Disposition": f'attachment; filename="{filename}"',
},
)


Expand Down
7 changes: 7 additions & 0 deletions backend/tests/api/test_paste_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ async def test_get_raw_paste_returns_plain_text(
assert response.status_code == 200
assert response.headers["content-type"] == "text/plain; charset=utf-8"
assert response.text == "This is test content"
# New: ensure download header present
assert "content-disposition" in response.headers
cd = response.headers["content-disposition"]
assert "attachment" in cd
assert ".txt" in cd

async def test_get_raw_paste_returns_404_for_nonexistent(self, test_client: AsyncClient, bypass_headers):
"""GET /pastes/{id}/raw should return 404 for non-existent paste."""
Expand Down Expand Up @@ -317,6 +322,8 @@ async def test_get_raw_paste_with_unicode_content(self, test_client: AsyncClient

assert response.status_code == 200
assert response.text == paste_data["content"]
# Ensure download header exists for unicode titles as well
assert "content-disposition" in response.headers


@pytest.mark.asyncio
Expand Down