Skip to content

Commit

Permalink
Remediate errors in various favicon fetch scenarios (#2779)
Browse files Browse the repository at this point in the history
Fixes stuck "Download favicon" button on icon download attempts for IP
address hosts by skipping attempts to get 2nd level domain resources
(which resulted in calls to 0.0.0.<rightmost octet of original IP>).

Fixes some cases when DuckDuckGo fallback fails to find icon of >2-level
domains, by adding a request to a DDG URL based on entry's 2nd level
domain.

Repurposes EditWidgetIcons' private fetchCanceled slot (which as of #2439,
is unused by any code) into public abortRequests slot, which is
connected to the entry edit widget's accepted and rejected signals (in
other words, Ok or Cancel was pressed).
  • Loading branch information
kneitinger authored and droidmonkey committed Mar 17, 2019
1 parent 11ecaf4 commit ae4eafc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
36 changes: 30 additions & 6 deletions src/gui/EditWidgetIcons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "gui/MessageBox.h"

#ifdef WITH_XC_NETWORKING
#include <QHostInfo>
#include <QNetworkAccessManager>
#include <QtNetwork>
#endif
Expand Down Expand Up @@ -195,20 +196,43 @@ void EditWidgetIcons::downloadFavicon()
m_urlsToTry.clear();

QString fullyQualifiedDomain = m_url.host();
QString secondLevelDomain = getSecondLevelDomain(m_url);

// Attempt to simply load the favicon.ico file
if (fullyQualifiedDomain != secondLevelDomain) {
m_urlsToTry.append(QUrl(m_url.scheme() + "://" + fullyQualifiedDomain + "/favicon.ico"));
m_urlsToTry.append(QUrl(m_url.scheme() + "://" + fullyQualifiedDomain + "/favicon.ico"));

// Determine if host portion of URL is an IP address by resolving it and
// searching for a match with the returned address(es).
bool hostIsIp = false;
QList<QHostAddress> hostAddressess = QHostInfo::fromName(fullyQualifiedDomain).addresses();
for (auto addr : hostAddressess) {
if (addr.toString() == fullyQualifiedDomain) {
hostIsIp = true;
}
}

if (!hostIsIp) {
QString secondLevelDomain = getSecondLevelDomain(m_url);

// Attempt to simply load the favicon.ico file
if (fullyQualifiedDomain != secondLevelDomain) {
m_urlsToTry.append(QUrl(m_url.scheme() + "://" + secondLevelDomain + "/favicon.ico"));
}
}
m_urlsToTry.append(QUrl(m_url.scheme() + "://" + secondLevelDomain + "/favicon.ico"));

// Try to use alternative fallback URL, if enabled
if (config()->get("security/IconDownloadFallback", false).toBool()) {
QUrl fallbackUrl = QUrl("https://icons.duckduckgo.com");
fallbackUrl.setPath("/ip3/" + QUrl::toPercentEncoding(fullyQualifiedDomain) + ".ico");

m_urlsToTry.append(fallbackUrl);

if (!hostIsIp) {
QString secondLevelDomain = getSecondLevelDomain(m_url);

if (fullyQualifiedDomain != secondLevelDomain) {
fallbackUrl.setPath("/ip3/" + QUrl::toPercentEncoding(secondLevelDomain) + ".ico");
m_urlsToTry.append(fallbackUrl);
}
}
}

startFetchFavicon(m_urlsToTry.takeFirst());
Expand Down Expand Up @@ -276,7 +300,7 @@ void EditWidgetIcons::fetchFinished()
#endif
}

void EditWidgetIcons::fetchCanceled()
void EditWidgetIcons::abortRequests()
{
#ifdef WITH_XC_NETWORKING
if (m_reply) {
Expand Down
3 changes: 1 addition & 2 deletions src/gui/EditWidgetIcons.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#ifndef KEEPASSX_EDITWIDGETICONS_H
#define KEEPASSX_EDITWIDGETICONS_H

#include <QSet>
#include <QUrl>
#include <QUuid>
#include <QWidget>
Expand Down Expand Up @@ -66,6 +65,7 @@ class EditWidgetIcons : public QWidget

public slots:
void setUrl(const QString& url);
void abortRequests();

signals:
void messageEditEntry(QString, MessageWidget::MessageType);
Expand All @@ -77,7 +77,6 @@ private slots:
void startFetchFavicon(const QUrl& url);
void fetchFinished();
void fetchReadyRead();
void fetchCanceled();
void addCustomIconFromFile();
bool addCustomIcon(const QImage& icon);
void removeCustomIcon();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/entry/EditEntryWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ void EditEntryWidget::setupAdvanced()
void EditEntryWidget::setupIcon()
{
addPage(tr("Icon"), FilePath::instance()->icon("apps", "preferences-desktop-icons"), m_iconsWidget);
connect(this, SIGNAL(accepted()), m_iconsWidget, SLOT(abortRequests()));
connect(this, SIGNAL(rejected()), m_iconsWidget, SLOT(abortRequests()));
}

void EditEntryWidget::setupAutoType()
Expand Down

0 comments on commit ae4eafc

Please sign in to comment.