Skip to content

Commit

Permalink
Generate a randomized XDG reply token
Browse files Browse the repository at this point in the history
  • Loading branch information
oskirby committed Apr 8, 2024
1 parent 5889755 commit ba068a4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/platforms/linux/xdgstartatbootwatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QDBusObjectPath>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QRandomGenerator>

namespace {
Logger logger("XdgStartAtBootWatcher");
Expand All @@ -23,8 +24,6 @@ constexpr const char* XDG_PORTAL_PATH = "/org/freedesktop/portal/desktop";
constexpr const char* XDG_PORTAL_BACKGROUND = "org.freedesktop.portal.Background";
constexpr const char* XDG_PORTAL_REQUEST = "org.freedesktop.portal.Request";

constexpr const char* XDG_PORTAL_REQUEST_HANDLE = "mozillavpn";

XdgStartAtBootWatcher::XdgStartAtBootWatcher() : QObject() {
MZ_COUNT_CTOR(XdgStartAtBootWatcher);

Expand All @@ -38,6 +37,10 @@ XdgStartAtBootWatcher::XdgStartAtBootWatcher() : QObject() {
XDG_PORTAL_REQUEST, "Response", this,
SLOT(xdgResponse(uint, QVariantMap)));

// Generate a unique token for this application instance.
quint64 randbits = QRandomGenerator::global()->generate64();
m_token = "mozillavpn-" + QString::number(randbits, 16);

startAtBootChanged();
}

Expand All @@ -51,7 +54,7 @@ XdgStartAtBootWatcher::~XdgStartAtBootWatcher() {
QString XdgStartAtBootWatcher::xdgReplyPath() {
QDBusConnection bus = QDBusConnection::sessionBus();
QString sender = bus.baseService().mid(1).replace('.', '_');
return QString("/org/freedesktop/portal/desktop/request/%1/%2").arg(sender).arg(XDG_PORTAL_REQUEST_HANDLE);
return QString("/org/freedesktop/portal/desktop/request/%1/%2").arg(sender).arg(m_token);
}

void XdgStartAtBootWatcher::xdgResponse(uint response, QVariantMap results) {
Expand All @@ -72,7 +75,7 @@ void XdgStartAtBootWatcher::startAtBootChanged() {
options["autostart"] = QVariant(startAtBoot);
options["background"] = QVariant(true);
options["commandline"] = QVariant(cmdline);
options["handle_token"] = QVariant(XDG_PORTAL_REQUEST_HANDLE);
options["handle_token"] = QVariant(m_token);

QDBusMessage call =
QDBusMessage::createMethodCall(XDG_PORTAL_SERVICE, XDG_PORTAL_PATH,
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/linux/xdgstartatbootwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class XdgStartAtBootWatcher final : public QObject {

private:
void startAtBootChanged();
static QString xdgReplyPath();
QString xdgReplyPath();
void callCompleted(QDBusPendingCallWatcher* call);

QString m_replyPath;
QString m_token;
};

#endif // XDGSTARTATBOOTWATCHER_H

0 comments on commit ba068a4

Please sign in to comment.