Skip to content

Commit

Permalink
Fixed exception on bad torrent URL (#8403)
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink authored Jan 21, 2025
2 parents 3bd4f51 + cbab801 commit f2c7e4b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/tribler/core/libtorrent/restapi/torrentinfo_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,15 @@ async def get_torrent_info(self, request: Request) -> RESTResponse: # noqa: C90
metainfo = await self.download_manager.get_metainfo(infohash, timeout=60, hops=i_hops,
url=response.decode())
else:
metainfo = lt.bdecode(response)
try:
metainfo = lt.bdecode(response)
except RuntimeError:
return RESTResponse(
{"error": {
"handled": True,
"message": f"Could not read torrent from {uri}"
}}, status=HTTP_INTERNAL_SERVER_ERROR
)
elif scheme == "magnet":
self._logger.info("magnet scheme detected")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ async def test_get_torrent_info_http_valueerror(self) -> None:
self.assertEqual(HTTP_INTERNAL_SERVER_ERROR, response.status)
self.assertEqual("test", response_body_json["error"]["message"])

async def test_get_torrent_info_http_no_torrent(self) -> None:
"""
Test if a graceful error is returned when a valid HTTP address does not return a torrent.
"""
request = MockRequest("/api/torrentinfo", query={"hops": 0, "uri": "http://127.0.0.1/file"})

with patch.dict(tribler.core.libtorrent.restapi.torrentinfo_endpoint.__dict__,
{"unshorten": mock_unshorten}), \
patch("tribler.core.libtorrent.restapi.torrentinfo_endpoint.query_uri",
AsyncMock(return_value=b"<html><body>404: Page not found.</body></html>")):
response = await self.endpoint.get_torrent_info(request)
response_body_json = await response_to_json(response)

self.assertEqual(HTTP_INTERNAL_SERVER_ERROR, response.status)
self.assertEqual("Could not read torrent from http://127.0.0.1/file", response_body_json["error"]["message"])

async def test_get_torrent_info_https_serverconnectionerror(self) -> None:
"""
Test if a graceful error is returned when a ServerConnectionError occurs when loading from an HTTPS link.
Expand Down Expand Up @@ -376,6 +392,22 @@ async def test_get_torrent_info_https_valueerror(self) -> None:
self.assertEqual(HTTP_INTERNAL_SERVER_ERROR, response.status)
self.assertEqual("test", response_body_json["error"]["message"])

async def test_get_torrent_info_https_no_torrent(self) -> None:
"""
Test if a graceful error is returned when a valid HTTP address does not return a torrent.
"""
request = MockRequest("/api/torrentinfo", query={"hops": 0, "uri": "https://127.0.0.1/file"})

with patch.dict(tribler.core.libtorrent.restapi.torrentinfo_endpoint.__dict__,
{"unshorten": mock_unshorten}), \
patch("tribler.core.libtorrent.restapi.torrentinfo_endpoint.query_uri",
AsyncMock(return_value=b"<html><body>404: Page not found.</body></html>")):
response = await self.endpoint.get_torrent_info(request)
response_body_json = await response_to_json(response)

self.assertEqual(HTTP_INTERNAL_SERVER_ERROR, response.status)
self.assertEqual("Could not read torrent from https://127.0.0.1/file", response_body_json["error"]["message"])

async def test_get_torrent_info_https_certificate_error(self) -> None:
"""
Test if it is returned that an invalid certificate for an HTTPS request was used.
Expand Down
15 changes: 12 additions & 3 deletions src/tribler/ui/src/components/add-torrent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ export function AddTorrent() {
if (uriInput) {
setTorrent(undefined);
setUrlDialogOpen(false);
setSaveAsDialogOpen(true);
(async () => {
const response = await triblerService.getMetainfo(uriInput);
if (response === undefined) {
toast.error(`${t("ToastErrorDownloadStart")} ${t("ToastErrorGenNetworkErr")}`);
} else if (isErrorDict(response)){
toast.error(`${t("ToastErrorDownloadStart")} ${response.error.message}`);
} else {
setSaveAsDialogOpen(true);
}
})();
}
}}>
{t('Add')}
Expand Down Expand Up @@ -136,9 +145,9 @@ export function AddTorrent() {
(async () => {
const response = await triblerService.startDownloadFromFile(file);
if (response === undefined) {
toast.error(`${t("ToastErrorStartDownload")} ${t("ToastErrorGenNetworkErr")}`);
toast.error(`${t("ToastErrorDownloadStart")} ${t("ToastErrorGenNetworkErr")}`);
} else if (isErrorDict(response)){
toast.error(`${t("ToastErrorStartDownload")} ${response.error.message}`);
toast.error(`${t("ToastErrorDownloadStart")} ${response.error.message}`);
}
})();
}
Expand Down

0 comments on commit f2c7e4b

Please sign in to comment.