Skip to content

Commit 5a92a11

Browse files
committed
plugins: randomize iid
this should help prevent accidentally loading an incompatible plugin when building on top of a distro package or cmake configurations changed over time such that the install dir might contain backends that are no longer built I did not find a built in way to achive this :(
1 parent c4d9ad9 commit 5a92a11

File tree

11 files changed

+51
-8
lines changed

11 files changed

+51
-8
lines changed

CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ include(KDEClangFormat)
2727
include(KDEGitCommitHooks)
2828
include(ECMFindQmlModule)
2929
include(ECMDeprecationSettings)
30+
include(ECMSourceVersionControl)
3031

3132
find_package(Qt6 ${QT_MIN_VERSION} REQUIRED CONFIG COMPONENTS Widgets Test Network Concurrent DBus Quick QuickControls2)
3233
find_package(Qt6 ${QT_MIN_VERSION} CONFIG OPTIONAL_COMPONENTS WebView)
@@ -70,7 +71,23 @@ ecm_set_disabled_deprecation_versions(QT 6.8.1
7071

7172
add_definitions(-DQT_NO_SIGNALS_SLOTS_KEYWORDS -DQT_NO_URL_CAST_FROM_STRING)
7273

74+
# When building from git use a random iid to prevent loading incompatible plugins
75+
# from the distro or older git builds.
76+
set(DISCOVER_PLUGIN_IID ${PROJECT_VERSION})
77+
set(DISCOVER_NOTIFIER_IID ${PROJECT_VERSION})
78+
if(ECM_SOURCE_UNDER_VERSION_CONTROL)
79+
string(RANDOM LENGTH 16 DISCOVER_PLUGIN_IID)
80+
string(RANDOM LENGTH 16 DISCOVER_NOTIFIER_IID)
81+
endif()
82+
string(PREPEND DISCOVER_PLUGIN_IID "org.kde.discover.")
83+
string(APPEND DISCOVER_PLUGIN_IID ".AbstractResourcesBackendFactory")
84+
85+
string(PREPEND DISCOVER_NOTIFIER_IID "org.kde.discover.")
86+
string(APPEND DISCOVER_NOTIFIER_IID ".BackendNotifierModule")
87+
88+
configure_file(DiscoverConfig.h.in DiscoverConfig.h)
7389
configure_file(DiscoverVersion.h.in DiscoverVersion.h)
90+
include_directories(${CMAKE_CURRENT_BINARY_DIR})
7491

7592
if(BUILD_TESTING AND CMAKE_SYSTEM_NAME MATCHES "Linux")
7693
add_subdirectory(appiumtests)

DiscoverConfig.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: LGPL-2.0-or-later
2+
// SPDX-FileCopyrightText: 2025 Harald Sitter <[email protected]>
3+
4+
#pragma once
5+
6+
#cmakedefine DISCOVER_PLUGIN_IID "@DISCOVER_PLUGIN_IID@"
7+
#cmakedefine DISCOVER_NOTIFIER_IID "@DISCOVER_NOTIFIER_IID@"

libdiscover/DiscoverBackendsFactory.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <QPluginLoader>
2020
#include <QStandardPaths>
2121

22+
using namespace Qt::StringLiterals;
23+
2224
Q_GLOBAL_STATIC(QStringList, s_requestedBackends)
2325
static bool s_isFeedback = false;
2426

@@ -49,6 +51,11 @@ QVector<AbstractResourcesBackend *> DiscoverBackendsFactory::backendForFile(cons
4951
{
5052
QPluginLoader *loader = new QPluginLoader(QLatin1String("discover/") + libname, QCoreApplication::instance());
5153

54+
if (const auto iid = loader->metaData().value("IID"_L1).toString(); iid != QLatin1StringView(DISCOVER_PLUGIN_IID)) {
55+
qCWarning(LIBDISCOVER_LOG) << "Plugin" << libname << "doesn't have the right IID" << iid << "expected" << DISCOVER_PLUGIN_IID;
56+
return {};
57+
}
58+
5259
// qCDebug(LIBDISCOVER_LOG) << "trying to load plugin:" << loader->fileName();
5360
AbstractResourcesBackendFactory *f = qobject_cast<AbstractResourcesBackendFactory *>(loader->instance());
5461
if (!f) {

libdiscover/backends/DummyBackend/DummyNotifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class DummyNotifier : public BackendNotifierModule
1111
{
1212
Q_OBJECT
13-
Q_PLUGIN_METADATA(IID "org.kde.discover.BackendNotifierModule")
13+
Q_PLUGIN_METADATA(IID DISCOVER_NOTIFIER_IID)
1414
Q_INTERFACES(BackendNotifierModule)
1515
public:
1616
explicit DummyNotifier(QObject *parent = nullptr);

libdiscover/backends/FlatpakBackend/FlatpakNotifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class FlatpakNotifier : public BackendNotifierModule
1515
{
1616
Q_OBJECT
17-
Q_PLUGIN_METADATA(IID "org.kde.discover.BackendNotifierModule")
17+
Q_PLUGIN_METADATA(IID DISCOVER_NOTIFIER_IID)
1818
Q_INTERFACES(BackendNotifierModule)
1919
public:
2020
explicit FlatpakNotifier(QObject *parent = nullptr);

libdiscover/backends/KNSBackend/KNSBackend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static const int ENGINE_PAGE_SIZE = 100;
4141
class KNSBackendFactory : public AbstractResourcesBackendFactory
4242
{
4343
Q_OBJECT
44-
Q_PLUGIN_METADATA(IID "org.kde.muon.AbstractResourcesBackendFactory")
44+
Q_PLUGIN_METADATA(IID DISCOVER_PLUGIN_IID)
4545
Q_INTERFACES(AbstractResourcesBackendFactory)
4646
public:
4747
KNSBackendFactory()

libdiscover/backends/PackageKitBackend/PackageKitNotifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class QProcess;
1818
class PackageKitNotifier : public BackendNotifierModule
1919
{
2020
Q_OBJECT
21-
Q_PLUGIN_METADATA(IID "org.kde.discover.BackendNotifierModule")
21+
Q_PLUGIN_METADATA(IID DISCOVER_NOTIFIER_IID)
2222
Q_INTERFACES(BackendNotifierModule)
2323
public:
2424
explicit PackageKitNotifier(QObject *parent = nullptr);

libdiscover/backends/RpmOstreeBackend/RpmOstreeNotifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class RpmOstreeNotifier : public BackendNotifierModule
2323
{
2424
Q_OBJECT
25-
Q_PLUGIN_METADATA(IID "org.kde.discover.BackendNotifierModule")
25+
Q_PLUGIN_METADATA(IID DISCOVER_NOTIFIER_IID)
2626
Q_INTERFACES(BackendNotifierModule)
2727
public:
2828
explicit RpmOstreeNotifier(QObject *parent = nullptr);

libdiscover/notifiers/BackendNotifierModule.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include "DiscoverConfig.h"
910
#include "discovernotifiers_export.h"
1011
#include <QObject>
1112

@@ -79,4 +80,4 @@ class DISCOVERNOTIFIERS_EXPORT BackendNotifierModule : public QObject
7980
void foundUpgradeAction(UpgradeAction *action);
8081
};
8182

82-
Q_DECLARE_INTERFACE(BackendNotifierModule, "org.kde.discover.BackendNotifierModule")
83+
Q_DECLARE_INTERFACE(BackendNotifierModule, DISCOVER_NOTIFIER_IID)

libdiscover/resources/AbstractResourcesBackend.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#pragma once
88

9+
#include "DiscoverConfig.h"
10+
911
#include <QObject>
1012
#include <QPair>
1113
#include <QVariantList>
@@ -340,7 +342,7 @@ class DISCOVERCOMMON_EXPORT AbstractResourcesBackendFactory : public QObject
340342
class ClassName##Factory : public AbstractResourcesBackendFactory \
341343
{ \
342344
Q_OBJECT \
343-
Q_PLUGIN_METADATA(IID "org.kde.muon.AbstractResourcesBackendFactory") \
345+
Q_PLUGIN_METADATA(IID DISCOVER_PLUGIN_IID) \
344346
Q_INTERFACES(AbstractResourcesBackendFactory) \
345347
public: \
346348
QVector<AbstractResourcesBackend *> newInstance(QObject *parent, const QString &name) const override \
@@ -351,4 +353,4 @@ class DISCOVERCOMMON_EXPORT AbstractResourcesBackendFactory : public QObject
351353
} \
352354
};
353355

354-
Q_DECLARE_INTERFACE(AbstractResourcesBackendFactory, "org.kde.muon.AbstractResourcesBackendFactory")
356+
Q_DECLARE_INTERFACE(AbstractResourcesBackendFactory, DISCOVER_PLUGIN_IID)

0 commit comments

Comments
 (0)