merge #17721: Don't allow Base58 decoding of non-Base58 strings. Add Base58 tests.

This commit is contained in:
Kittywhiskers Van Gogh 2019-12-11 11:00:52 +00:00 committed by UdjinM6
parent ac863d3955
commit 50f95ca817
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9
4 changed files with 33 additions and 2 deletions

View File

@ -6,6 +6,8 @@
#include <hash.h>
#include <uint256.h>
#include <utilstrencodings.h>
#include <utilstring.h>
#include <assert.h>
#include <stdint.h>
@ -127,6 +129,9 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch)
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
{
if (!ValidAsCString(str)) {
return false;
}
return DecodeBase58(str.c_str(), vchRet);
}
@ -158,6 +163,9 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
{
if (!ValidAsCString(str)) {
return false;
}
return DecodeBase58Check(str.c_str(), vchRet);
}

View File

@ -58,12 +58,24 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
}
BOOST_CHECK(!DecodeBase58("invalid", result));
BOOST_CHECK(!DecodeBase58(std::string("invalid"), result));
BOOST_CHECK(!DecodeBase58(std::string("\0invalid", 8), result));
BOOST_CHECK(DecodeBase58(std::string("good", 4), result));
BOOST_CHECK(!DecodeBase58(std::string("bad0IOl", 7), result));
BOOST_CHECK(!DecodeBase58(std::string("goodbad0IOl", 11), result));
BOOST_CHECK(!DecodeBase58(std::string("good\0bad0IOl", 12), result));
// check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end.
BOOST_CHECK(!DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t a", result));
BOOST_CHECK( DecodeBase58(" \t\n\v\f\r skip \r\f\v\n\t ", result));
std::vector<unsigned char> expected = ParseHex("971a55");
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
BOOST_CHECK(DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh", 21), result));
BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oi", 21), result));
BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh0IOl", 25), result));
BOOST_CHECK(!DecodeBase58Check(std::string("3vQB7B6MrGQZaxCuFg4oh\00IOl", 26), result));
}
BOOST_AUTO_TEST_SUITE_END()

View File

@ -4,6 +4,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <utilstrencodings.h>
#include <utilstring.h>
#include <tinyformat.h>
@ -267,7 +268,7 @@ NODISCARD static bool ParsePrechecks(const std::string& str)
return false;
if (str.size() >= 1 && (isspace(str[0]) || isspace(str[str.size()-1]))) // No padding allowed
return false;
if (str.size() != strlen(str.c_str())) // No embedded NUL characters allowed
if (!ValidAsCString(str)) // No embedded NUL characters allowed
return false;
return true;
}

View File

@ -5,7 +5,9 @@
#ifndef BITCOIN_UTILSTRING_H
#define BITCOIN_UTILSTRING_H
#include <functional>
#include <attributes.h>
#include <cstring>
#include <string>
#include <vector>
@ -32,4 +34,12 @@ inline std::string Join(const std::vector<std::string>& list, const std::string&
return Join(list, separator, [](const std::string& i) { return i; });
}
/**
* Check if a string does not contain any embedded NUL (\0) characters
*/
NODISCARD inline bool ValidAsCString(const std::string& str) noexcept
{
return str.size() == strlen(str.c_str());
}
#endif // BITCOIN_UTILSTRING_H