Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for Content-Type when downloading tracker list #22137

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/base/net/downloadhandlerimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ void Net::DownloadHandlerImpl::processFinishedDownload()
#else
m_result.data = m_reply->readAll();
#endif
m_result.contentType = m_reply->header(QNetworkRequest::ContentTypeHeader).toString();

if (m_downloadRequest.saveToFile())
{
Expand Down
1 change: 1 addition & 0 deletions src/base/net/downloadmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ namespace Net
QByteArray data;
Path filePath;
QString magnetURI;
QString contentType;
};

class DownloadHandler : public QObject
Expand Down
35 changes: 35 additions & 0 deletions src/gui/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#include "watchedfolderoptionsdialog.h"
#include "watchedfoldersmodel.h"
#include "webui/webui.h"
#include "base/net/downloadmanager.h"

#ifndef DISABLE_WEBUI
#include "base/net/dnsupdater.h"
Expand Down Expand Up @@ -1229,8 +1230,42 @@ void OptionsDialog::saveBittorrentTabOptions() const
session->setAddTrackersEnabled(m_ui->checkEnableAddTrackers->isChecked());
session->setAdditionalTrackers(m_ui->textTrackers->toPlainText());

auto enabledAddTrackers = m_ui->checkAddTrackersFromURL->isChecked();
auto url = m_ui->textTrackersURL->text();
if (!url.isEmpty() && enabledAddTrackers)
{
Net::DownloadManager::instance()->download(url, Preferences::instance()->useProxyForGeneralPurposes()
, this, &OptionsDialog::onAddTrackersDownload);
}
else
{
session->setAddTrackersFromURLEnabled(enabledAddTrackers);
session->setAdditionalTrackersURL(url);
}
}

void OptionsDialog::onAddTrackersDownload(const Net::DownloadResult &result)
{
if (result.status != Net::DownloadStatus::Success)
{
QMessageBox::warning(this, tr("Download trackers list error")
, tr("Error occurred when downloading the trackers list. Reason: \"%1\"").arg(result.errorString));
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;
}

auto *session = BitTorrent::Session::instance();

session->setAddTrackersFromURLEnabled(m_ui->checkAddTrackersFromURL->isChecked());
session->setAdditionalTrackersURL(m_ui->textTrackersURL->text());

return;
}

void OptionsDialog::loadRSSTabOptions()
Expand Down
4 changes: 4 additions & 0 deletions src/gui/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "base/pathfwd.h"
#include "base/settingvalue.h"
#include "guiapplicationcomponent.h"
#include "base/net/downloadmanager.h"

class QListWidgetItem;

Expand Down Expand Up @@ -137,9 +138,12 @@ private slots:
void loadRSSTabOptions();
void saveRSSTabOptions() const;

void onAddTrackersDownload(const Net::DownloadResult &result);

void loadSearchTabOptions();
void saveSearchTabOptions() const;


#ifndef DISABLE_WEBUI
void loadWebUITabOptions();
void saveWebUITabOptions() const;
Expand Down
30 changes: 29 additions & 1 deletion src/gui/trackersadditiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<BitTorrent::TrackerEntryStatus> currentTrackers = m_torrent->trackers();
const int baseTier = !currentTrackers.isEmpty() ? (currentTrackers.last().tier + 1) : 0;

QList<BitTorrent::TrackerEntry> 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<TrackersAdditionDialog *>(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);
}
Expand Down Expand Up @@ -116,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'))
Expand Down
1 change: 1 addition & 0 deletions src/gui/trackersadditiondialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down