Skip to content

Commit 586825a

Browse files
committed
♻️(back) stop returning a 500 on cors_proxy on request failure
On the cors_proxy endpoint, if the fetched url fails we were returning an error 500. Instead, we log the exception and return a 400 to not give back information to the frontend application.
1 parent 247550f commit 586825a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/backend/core/api/viewsets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,10 +1481,10 @@ def cors_proxy(self, request, *args, **kwargs):
14811481
return proxy_response
14821482

14831483
except requests.RequestException as e:
1484-
logger.error("Proxy request failed: %s", str(e))
1485-
return drf_response.Response(
1486-
{"error": f"Failed to fetch resource: {e!s}"},
1487-
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
1484+
logger.exception(e)
1485+
return drf.response.Response(
1486+
{"error": f"Failed to fetch resource from {url}"},
1487+
status=status.HTTP_400_BAD_REQUEST,
14881488
)
14891489

14901490

src/backend/core/tests/documents/test_api_documents_cors_proxy.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
import responses
5+
from requests.exceptions import RequestException
56
from rest_framework.test import APIClient
67

78
from core import factories
@@ -170,3 +171,20 @@ def test_api_docs_cors_proxy_invalid_url(url_to_fetch):
170171
)
171172
assert response.status_code == 400
172173
assert response.json() == ["Enter a valid URL."]
174+
175+
176+
@responses.activate
177+
def test_api_docs_cors_proxy_request_failed():
178+
"""Test the CORS proxy API for documents with a request failed."""
179+
document = factories.DocumentFactory(link_reach="public")
180+
181+
client = APIClient()
182+
url_to_fetch = "https://external-url.com/assets/index.html"
183+
responses.get(url_to_fetch, body=RequestException("Connection refused"))
184+
response = client.get(
185+
f"/api/v1.0/documents/{document.id!s}/cors-proxy/?url={url_to_fetch}"
186+
)
187+
assert response.status_code == 400
188+
assert response.json() == {
189+
"error": "Failed to fetch resource from https://external-url.com/assets/index.html"
190+
}

0 commit comments

Comments
 (0)