From 3ef77a0c1288df524fdf0c90ca70c986f473b787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Thu, 27 Jul 2017 00:56:30 +0100 Subject: [PATCH 1/3] Reject duplicate wallet filenames --- src/wallet/wallet.cpp | 13 ++++++++++++- test/functional/multiwallet.py | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3e9c531f00..ccf5353eb7 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -468,13 +468,24 @@ bool CWallet::Verify() uiInterface.InitMessage(_("Verifying wallet(s)...")); + // Keep track of each wallet absolute path to detect duplicates. + std::set wallet_paths; + for (const std::string& walletFile : gArgs.GetArgs("-wallet")) { if (boost::filesystem::path(walletFile).filename() != walletFile) { return InitError(_("-wallet parameter must only specify a filename (not a path)")); - } else if (SanitizeString(walletFile, SAFE_CHARS_FILENAME) != walletFile) { + } + + if (SanitizeString(walletFile, SAFE_CHARS_FILENAME) != walletFile) { return InitError(_("Invalid characters in -wallet filename")); } + fs::path wallet_path = fs::absolute(walletFile, GetDataDir()); + + if (!wallet_paths.insert(wallet_path).second) { + return InitError(_("Duplicate -wallet filename")); + } + std::string strError; if (!CWalletDB::VerifyEnvironment(walletFile, GetDataDir().string(), strError)) { return InitError(strError); diff --git a/test/functional/multiwallet.py b/test/functional/multiwallet.py index 173f06cf5d..5844d99139 100755 --- a/test/functional/multiwallet.py +++ b/test/functional/multiwallet.py @@ -18,6 +18,13 @@ class MultiWalletTest(BitcoinTestFramework): self.extra_args = [['-wallet=w1', '-wallet=w2', '-wallet=w3']] def run_test(self): + self.stop_node(0) + + # should not initialize if there are duplicate wallets + self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w1', '-wallet=w1'], 'Duplicate -wallet filename') + + self.nodes[0] = self.start_node(0, self.options.tmpdir, self.extra_args[0]) + w1 = self.nodes[0] / "wallet/w1" w1.generate(1) From a6da027d83e48f05c933149ff89c9b9ad5ced915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Thu, 27 Jul 2017 00:57:02 +0100 Subject: [PATCH 2/3] Reject invalid wallet files --- src/wallet/wallet.cpp | 4 ++++ test/functional/multiwallet.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ccf5353eb7..ef1d764146 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -482,6 +482,10 @@ bool CWallet::Verify() fs::path wallet_path = fs::absolute(walletFile, GetDataDir()); + if (fs::exists(wallet_path) && (!fs::is_regular_file(wallet_path) || fs::is_symlink(wallet_path))) { + return InitError(_("Invalid -wallet file")); + } + if (!wallet_paths.insert(wallet_path).second) { return InitError(_("Duplicate -wallet filename")); } diff --git a/test/functional/multiwallet.py b/test/functional/multiwallet.py index 5844d99139..c60d345acb 100755 --- a/test/functional/multiwallet.py +++ b/test/functional/multiwallet.py @@ -6,6 +6,8 @@ Verify that a bitcoind node can load multiple wallet files """ +import os + from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal, assert_raises_jsonrpc @@ -23,6 +25,14 @@ class MultiWalletTest(BitcoinTestFramework): # should not initialize if there are duplicate wallets self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w1', '-wallet=w1'], 'Duplicate -wallet filename') + # should not initialize if wallet file is a directory + os.mkdir(os.path.join(self.options.tmpdir, 'node0', 'regtest', 'w11')) + self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w11'], 'Invalid -wallet file') + + # should not initialize if wallet file is a symlink + os.symlink(os.path.join(self.options.tmpdir, 'node0', 'regtest', 'w1'), os.path.join(self.options.tmpdir, 'node0', 'regtest', 'w12')) + self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w12'], 'Invalid -wallet file') + self.nodes[0] = self.start_node(0, self.options.tmpdir, self.extra_args[0]) w1 = self.nodes[0] / "wallet/w1" From d84e78ec393049cb067170a4905a1679ff794368 Mon Sep 17 00:00:00 2001 From: John Newbery Date: Fri, 28 Jul 2017 06:03:03 -0400 Subject: [PATCH 3/3] [wallet] Specify wallet name in wallet loading errors --- src/wallet/wallet.cpp | 8 ++++---- test/functional/multiwallet.py | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index ef1d764146..36c295e5d8 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -473,21 +473,21 @@ bool CWallet::Verify() for (const std::string& walletFile : gArgs.GetArgs("-wallet")) { if (boost::filesystem::path(walletFile).filename() != walletFile) { - return InitError(_("-wallet parameter must only specify a filename (not a path)")); + return InitError(strprintf(_("Error loading wallet %s. -wallet parameter must only specify a filename (not a path)."), walletFile)); } if (SanitizeString(walletFile, SAFE_CHARS_FILENAME) != walletFile) { - return InitError(_("Invalid characters in -wallet filename")); + return InitError(strprintf(_("Error loading wallet %s. Invalid characters in -wallet filename."), walletFile)); } fs::path wallet_path = fs::absolute(walletFile, GetDataDir()); if (fs::exists(wallet_path) && (!fs::is_regular_file(wallet_path) || fs::is_symlink(wallet_path))) { - return InitError(_("Invalid -wallet file")); + return InitError(strprintf(_("Error loading wallet %s. -wallet filename must be a regular file."), walletFile)); } if (!wallet_paths.insert(wallet_path).second) { - return InitError(_("Duplicate -wallet filename")); + return InitError(strprintf(_("Error loading wallet %s. Duplicate -wallet filename specified."), walletFile)); } std::string strError; diff --git a/test/functional/multiwallet.py b/test/functional/multiwallet.py index c60d345acb..5679f40503 100755 --- a/test/functional/multiwallet.py +++ b/test/functional/multiwallet.py @@ -23,15 +23,15 @@ class MultiWalletTest(BitcoinTestFramework): self.stop_node(0) # should not initialize if there are duplicate wallets - self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w1', '-wallet=w1'], 'Duplicate -wallet filename') + self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w1', '-wallet=w1'], 'Error loading wallet w1. Duplicate -wallet filename specified.') # should not initialize if wallet file is a directory os.mkdir(os.path.join(self.options.tmpdir, 'node0', 'regtest', 'w11')) - self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w11'], 'Invalid -wallet file') + self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w11'], 'Error loading wallet w11. -wallet filename must be a regular file.') # should not initialize if wallet file is a symlink os.symlink(os.path.join(self.options.tmpdir, 'node0', 'regtest', 'w1'), os.path.join(self.options.tmpdir, 'node0', 'regtest', 'w12')) - self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w12'], 'Invalid -wallet file') + self.assert_start_raises_init_error(0, self.options.tmpdir, ['-wallet=w12'], 'Error loading wallet w12. -wallet filename must be a regular file.') self.nodes[0] = self.start_node(0, self.options.tmpdir, self.extra_args[0])