-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add check for Content-Type when downloading tracker list #22137
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
base: master
Are you sure you want to change the base?
Add check for Content-Type when downloading tracker list #22137
Conversation
The plain text may also contain arbitrary/inappropriate data. Shouldn't the lines themselves be validated instead? |
I thought that the text area was meant to be used in order to "preview" the url's a user was about to add. |
Tested! |
This PR is stale because it has been 60 days with no activity. This PR will be automatically closed within 7 days if there is no further activity. |
@stalkerok @glassez |
What's left to do here ? |
A review maybe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps I haven't identified all the problems yet...
src/base/net/downloadmanager.h
Outdated
QByteArray data; | ||
Path filePath; | ||
QString magnetURI; | ||
QString contentType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QByteArray data; | |
Path filePath; | |
QString magnetURI; | |
QString contentType; | |
QString contentType; | |
QByteArray data; | |
Path filePath; | |
QString magnetURI; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fields were reordered as requested, contentType
appears right before data
src/gui/optionsdialog.cpp
Outdated
@@ -83,6 +83,7 @@ | |||
#include "watchedfolderoptionsdialog.h" | |||
#include "watchedfoldersmodel.h" | |||
#include "webui/webui.h" | |||
#include "base/net/downloadmanager.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is even redundant since it is included in optionsdialog.h
.
src/gui/optionsdialog.cpp
Outdated
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto enabledAddTrackers = m_ui->checkAddTrackersFromURL->isChecked(); | |
const bool isAddTrackersEnabled = m_ui->checkAddTrackersFromURL->isChecked(); |
src/gui/optionsdialog.cpp
Outdated
@@ -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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto url = m_ui->textTrackersURL->text(); | |
const QString url = m_ui->textTrackersURL->text(); |
src/gui/optionsdialog.cpp
Outdated
@@ -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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it more logical to check whether the feature is enabled first?
if (!url.isEmpty() && enabledAddTrackers) | |
if (isAddTrackersEnabled && !url.isEmpty()) |
src/gui/trackersadditiondialog.cpp
Outdated
{ | ||
if (endpoint.isEmpty()) | ||
return 0; | ||
QUrl url(endpoint.toString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
QUrl url(endpoint.toString()); | |
const QUrl url {endpoint.toString()}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed it, should be ok now.
src/gui/trackersadditiondialog.cpp
Outdated
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Broken coding style.
for (BitTorrent::TrackerEntry &entry : entries) { | |
for (BitTorrent::TrackerEntry &entry : entries) | |
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
src/gui/trackersadditiondialog.cpp
Outdated
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems this variable doesn't make much sense.
auto isValid = isValidEndpoint(entry.url); | |
if (isValidEndpoint(entry.url)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, you are right, addressed.
src/gui/trackersadditiondialog.cpp
Outdated
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)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would drop const
specifier from onAccepted()
than use const_cast
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, done.
src/gui/trackersadditiondialog.h
Outdated
@@ -65,6 +65,7 @@ private slots: | |||
private: | |||
void saveSettings(); | |||
void loadSettings(); | |||
int isValidEndpoint(const QStringView &endpoint) const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is independent of TrackersAdditionDialog
class, so it can be declared in an anonymous namespace in the trackersadditiondialog.cpp
file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, made it an anonymous function.
c87669f
to
bc8c6bb
Compare
Ok, all points should be covered now, even those on which I did not comment. In case there is anything else, feel free to send it my way, im gonna take a look as soon as possible. PS: thx for the time. |
src/gui/optionsdialog.cpp
Outdated
if (isAddTrackersEnabled && !url.isEmpty()) | ||
{ | ||
Net::DownloadManager::instance()->download(url, Preferences::instance()->useProxyForGeneralPurposes() | ||
, this, &OptionsDialog::onAdditionalTrackersDownload); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
, this, &OptionsDialog::onAdditionalTrackersDownload); | |
, this, &OptionsDialog::onAdditionalTrackersDownloadFinished); |
Well, here's my general review. I have no particular complaints about the As for the main option, its processing is done in a dubious way, IMO.
|
Check if the URL from where we are downloading the trackers returns `Content-Type` as `text`. Otherwise consider it might be a non-usable format for this functionality (html,xml,etc.ect.)
9f9ff75
to
5567d02
Compare
Hey, I failed to get back to this sooner, but now it should be as you described. I went with the message box implementation earlier thinking it would be easier for users to understand when/what happens. If there's anything else I can refine, leave a comment and im gonna take a look at it. |
This PR is stale because it has been 60 days with no activity. This PR will be automatically closed within 7 days if there is no further activity. |
@glassez (pinging cause last reviewer/commenter) I don't mean to bother, but would hate the bot to auto close a possible fix. |
Review pending........ |
@@ -74,14 +74,35 @@ TrackersAdditionDialog::~TrackersAdditionDialog() | |||
delete m_ui; | |||
} | |||
|
|||
void TrackersAdditionDialog::onAccepted() const | |||
bool isValidEndpoint(const QStringView endpoint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should be placed inside anonymous namespace above TrackersAdditionDialog
definitions.
if (!isValidEndpoint(entry.url)) | ||
{ | ||
QMessageBox::warning(this, tr("Invalid tracker URL"), tr("The tracker URL \"%1\" is invalid").arg(entry.url)); | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, it looks unfriendly if you reject all the entered trackers because of invalid one. Or am I misunderstood it?
@@ -4105,6 +4105,11 @@ void SessionImpl::updateTrackersFromURL() | |||
{ | |||
if (result.status == Net::DownloadStatus::Success) | |||
{ | |||
if (!result.contentType.contains(u"text/plain"_s, Qt::CaseInsensitive)) | |||
{ | |||
LogMsg(tr("Cannot add trackers from URL, expected Content-Type is \'text/plain\' received \"%1\"").arg(result.contentType), Log::WARNING); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Single and double quotes are used inconsistently in this message.
- I doubt that single quotes need to be escaped.
- The message format seems inappropriate (and even grammatically invalid).
Closes #13061.
Description
Some users might get confused around platforms like github and their
raw
links. By mistake such user has entered the HTML page link and each HTML line was taken as a tracker.In order to fix this issue, we add a check on the
Content-Type
header of the response. We accepttext/plain
and show a descriptive error message otherwise.