From a953045abab47ae8ea134a299f5e86f0816c15ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B8nstantine=20Kovalensky?= <45331093+kovalensky@users.noreply.github.com> Date: Wed, 20 Dec 2023 21:37:47 +0400 Subject: [PATCH] Exception handling for Bencode errors (#1237) --- library/ajax/view_torrent.php | 5 ++++- library/includes/file_list_v2.php | 5 ++++- src/Legacy/Torrent.php | 10 ++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/library/ajax/view_torrent.php b/library/ajax/view_torrent.php index faa8851a1..af3b5ef5d 100644 --- a/library/ajax/view_torrent.php +++ b/library/ajax/view_torrent.php @@ -28,7 +28,10 @@ if (!file_exists($filename) || !$file_contents = file_get_contents($filename)) { $this->ajax_die($lang['ERROR_NO_ATTACHMENT'] . "\n\n" . htmlCHR($filename)); } -if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { +try { + $tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY); +} +catch (Exception) { return $lang['TORFILE_INVALID']; } diff --git a/library/includes/file_list_v2.php b/library/includes/file_list_v2.php index 5f530fab4..240298c52 100644 --- a/library/includes/file_list_v2.php +++ b/library/includes/file_list_v2.php @@ -60,7 +60,10 @@ if ($bb_cfg['flist_max_files']) { } } -if (!$torrent = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { +try { + $torrent = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY); +} +catch (Exception) { http_response_code(410); die($lang['TORFILE_INVALID']); } diff --git a/src/Legacy/Torrent.php b/src/Legacy/Torrent.php index ca48ab77a..dbbb8ec35 100644 --- a/src/Legacy/Torrent.php +++ b/src/Legacy/Torrent.php @@ -317,7 +317,10 @@ class Torrent $file_contents = file_get_contents($filename); - if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { + try { + $tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY); + } + catch (Exception ) { self::torrent_error_exit($lang['TORFILE_INVALID']); } @@ -559,7 +562,10 @@ class Torrent // Torrent decoding $file_contents = file_get_contents($filename); - if (!$tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY)) { + try { + $tor = \Arokettu\Bencode\Bencode::decode($file_contents, dictType: \Arokettu\Bencode\Bencode\Collection::ARRAY); + } + catch (Exception) { bb_die($lang['TORFILE_INVALID']); }