From db41945db796c82960350fc0ec730c3c8df2370e Mon Sep 17 00:00:00 2001 From: "Stiliyan Tonev (Bark)" Date: Fri, 10 Jan 2025 13:58:14 +0200 Subject: [PATCH] Check header after request succeeds, check if added trackers are valid URL\'s --- src/gui/trackersadditiondialog.cpp | 37 +++++++++++++++++++++++------- src/gui/trackersadditiondialog.h | 1 + 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/gui/trackersadditiondialog.cpp b/src/gui/trackersadditiondialog.cpp index 4c93aacbc..9403edf3b 100644 --- a/src/gui/trackersadditiondialog.cpp +++ b/src/gui/trackersadditiondialog.cpp @@ -74,14 +74,35 @@ TrackersAdditionDialog::~TrackersAdditionDialog() delete m_ui; } +int TrackersAdditionDialog::isValidEndpoint(const QStringView &endpoint) const +{ + if (endpoint.isEmpty()) + return 0; + QUrl url(endpoint.toString()); + if (!url.isValid()) + return 0; + if (url.scheme().isEmpty()) + return 0; + if (url.host().isEmpty()) + return 0; + return 1; +} + void TrackersAdditionDialog::onAccepted() const { const QList currentTrackers = m_torrent->trackers(); const int baseTier = !currentTrackers.isEmpty() ? (currentTrackers.last().tier + 1) : 0; QList entries = BitTorrent::parseTrackerEntries(m_ui->textEditTrackersList->toPlainText()); - for (BitTorrent::TrackerEntry &entry : entries) + for (BitTorrent::TrackerEntry &entry : entries) { + auto isValid = isValidEndpoint(entry.url); + if (!isValid) + { + QMessageBox::warning(const_cast(this), tr("Invalid tracker URL"), tr("The tracker URL \"%1\" is invalid").arg(entry.url)); + return; + } entry.tier = Utils::Number::clampingAdd(entry.tier, baseTier); + } m_torrent->addTrackers(entries); } @@ -109,13 +130,6 @@ void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadRe m_ui->downloadButton->setEnabled(true); setCursor(Qt::ArrowCursor); - if (!result.contentType.contains(u"text/plain"_s, Qt::CaseInsensitive)) - { - QMessageBox::warning(this, tr("Download trackers list error") - , tr("The content type of the downloaded file is not plain text. Content-Type: \"%1\"").arg(result.contentType)); - return; - } - if (result.status != Net::DownloadStatus::Success) { QMessageBox::warning(this, tr("Download trackers list error") @@ -123,6 +137,13 @@ void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadRe return; } + if (!result.contentType.contains(u"text/plain"_s, Qt::CaseInsensitive)) + { + QMessageBox::warning(this, tr("Download trackers list error") + , tr("The content type of the downloaded file is not plain text. Content-Type: \"%1\"").arg(result.contentType)); + return; + } + // Add fetched trackers to the list const QString existingText = m_ui->textEditTrackersList->toPlainText(); if (!existingText.isEmpty() && !existingText.endsWith(u'\n')) diff --git a/src/gui/trackersadditiondialog.h b/src/gui/trackersadditiondialog.h index 0dc658cff..893379f68 100644 --- a/src/gui/trackersadditiondialog.h +++ b/src/gui/trackersadditiondialog.h @@ -65,6 +65,7 @@ private slots: private: void saveSettings(); void loadSettings(); + int isValidEndpoint(const QStringView &endpoint) const; Ui::TrackersAdditionDialog *m_ui = nullptr; BitTorrent::Torrent *const m_torrent = nullptr;