oshelper: verify seed template hash before opening
What changed, and why it matters
This change hardens how the Monero GUI wallet opens a printable PDF seed backup template. Previously, the app copied a bundled PDF to a predictable temporary path and immediately opened it. Now it uses a unique temporary directory, verifies the PDF's SHA-256 hash against a hardcoded value, and reports an error if the file doesn't match. This reduces the risk that a local attacker could swap or modify the template file to trick the user into revealing or mishandling their recovery seed.
Treat as a security hardening improvement. Reviewers should verify that seedTemplateHash exactly matches the current :/wizard/template.pdf SHA-256, confirm QTemporaryDir lifetime covers the external PDF viewer's usage, and ensure the test runs in CI so future template updates don't desynchronize the hash.
Security signals we found
Adds integrity check (SHA-256) for a bundled resource before opening it externally
Switches from predictable temp file path to randomized QTemporaryDir
Returns explicit error codes and surfaces integrity failures to the user
Adds regression test enforcing hash synchronization with bundled resource
Evidence from the diff
The patch modifies OSHelper::openSeedTemplate() to: (1) create a QTemporaryDir under QDir::tempPath() with a random suffix instead of copying to a fixed filename; (2) copy the bundled :/wizard/template.pdf only if it doesn’t already exist in that temp dir; (3) compute SHA-256 of the copied file and compare it to a hardcoded seedTemplateHash constant; (4) return an integer status (0 success, -1 integrity failure, -2 file/open failure) instead of void. The QML caller now checks the result and shows a status message on failure. A QML test is added to ensure the bundled template’s hash matches the constant.
Changed components
src/main/oshelper.cppsrc/main/oshelper.hwizard/WizardCreateWallet2.qmltests/qml/QmlTestHarness.cpptests/qml/tst_Utils.qmlInspect captured patch +58 / −5
### src/main/oshelper.cpp
@@ -32,10 +32,12 @@
#include <unordered_set>
#include <QCoreApplication>
+#include <QCryptographicHash>
#include <QGuiApplication>
#include <QFileDialog>
#include <QScreen>
#include <QStandardPaths>
+#include <QTemporaryDir>
#include <QWindow>
#include <QDir>
#include <QDebug>
@@ -314,8 +316,35 @@ quint8 OSHelper::getNetworkTypeFromFile(const QString &keysPath, quint8 fallback
return networkTypeAndAddress.second.isEmpty() ? fallbackNetworkType : networkTypeAndAddress.first;
}
-void OSHelper::openSeedTemplate() const
+int OSHelper::openSeedTemplate() const
{
- QFile::copy(":/wizard/template.pdf", QDir::tempPath() + "/seed_template.pdf");
- openFile(QDir::tempPath() + "/seed_template.pdf");
+ static const QTemporaryDir directory(QDir::tempPath() + "/monero-seed-template-XXXXXX");
+ if (!directory.isValid())
+ {
+ qWarning() << "Failed to create seed template directory" << directory.errorString();
+ return -2;
+ }
+
+ const QString path = directory.filePath("seed_template.pdf");
+ if (!QFile::exists(path) && !QFile::copy(":/wizard/template.pdf", path))
+ {
+ qWarning() << "Failed to copy seed template";
+ return -2;
+ }
+
+ QFile file(path);
+ QCryptographicHash hash(QCryptographicHash::Sha256);
+ if (!file.open(QIODevice::ReadOnly) || !hash.addData(&file))
+ {
+ qWarning() << "Failed to read seed template" << file.errorString();
+ return -2;
+ }
+ if (hash.result().toHex() != seedTemplateHash)
+ {
+ qWarning() << "Seed template integrity check failed";
+ return -1;
+ }
+ file.close();
+
+ return openFile(path) ? 0 : -2;
}
### src/main/oshelper.h
@@ -41,6 +41,8 @@ class OSHelper : public QObject
Q_PROPERTY(bool installed READ installed CONSTANT);
public:
+ static constexpr char seedTemplateHash[] = "bcca2abd0ecff56423b4b04e81b2506c6b22bc5f5ef568251d2fbabc966755ef";
+
explicit OSHelper(QObject *parent = 0);
Q_INVOKABLE void createDesktopEntry() const;
@@ -54,7 +56,8 @@ class OSHelper : public QObject
Q_INVOKABLE QString randomPassword(int numBytes = 32) const;
Q_INVOKABLE bool isCapsLock() const;
Q_INVOKABLE quint8 getNetworkTypeFromFile(const QString &keysPath, quint8 fallbackNetworkType) const;
- Q_INVOKABLE void openSeedTemplate() const;
+ // 0: success, -1: integrity failure, -2: file/open failure.
+ Q_INVOKABLE int openSeedTemplate() const;
bool installed() const;
static std::pair<quint8, QString> getNetworkTypeAndAddressFromFile(const QString &wallet);
### tests/qml/QmlTestHarness.cpp
@@ -29,6 +29,7 @@
#include "QmlTestHarness.h"
#include <QCoreApplication>
+#include <QCryptographicHash>
#include <QDir>
#include <QFile>
#include <QQmlContext>
@@ -111,6 +112,14 @@ class QmlTestSetup : public QObject
return url.toLocalFile();
}
+ Q_INVOKABLE bool seedTemplateHashMatchesResource()
+ {
+ QFile file(":/wizard/template.pdf");
+ QCryptographicHash hash(QCryptographicHash::Sha256);
+ return file.open(QIODevice::ReadOnly) && hash.addData(&file) &&
+ hash.result().toHex() == OSHelper::seedTemplateHash;
+ }
+
public slots:
void qmlEngineAvailable(QQmlEngine *engine)
{
### tests/qml/tst_Utils.qml
@@ -47,6 +47,11 @@ Item {
TestCase {
name: "Utils"
+ function test_bundled_template_hash() {
+ verify(settingsTestHelper.seedTemplateHashMatchesResource(),
+ "Update OSHelper::seedTemplateHash to match wizard/template.pdf")
+ }
+
function test_restore_height_stays_a_height() {
compare(Utils.parseDateStringOrRestoreHeightAsInteger("0"), 0)
compare(Utils.parseDateStringOrRestoreHeightAsInteger("1234567"), 1234567)
### wizard/WizardCreateWallet2.qml
@@ -320,7 +320,14 @@ Rectangle {
text: qsTr("Print a template") + translationManager.emptyString
tooltip: qsTr("Print a template to write down your seed") + translationManager.emptyString
onClicked: {
- oshelper.openSeedTemplate();
+ var result = oshelper.openSeedTemplate();
+ if (result === 0)
+ return;
+
+ var message = (result === -1
+ ? qsTr("Unable to open the seed template because the file could not be verified.")
+ : qsTr("Unable to open the seed template.")) + translationManager.emptyString;
+ appWindow.showStatusMessage(message, 5);
}
Accessible.role: Accessible.Button
Accessible.name: qsTr("Print a template to write down your seed") + translationManager.emptyStringWhy this scored 42/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.