From 5c2f5077f5885db6f100a76ee9b17b72381526ef Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 8 Aug 2023 10:55:37 -0500 Subject: [PATCH] GH-1461 Do not add or expect `=` at end of base64 encoded strings. --- libraries/libfc/src/variant.cpp | 7 ++++--- libraries/libfc/test/variant/test_variant.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libraries/libfc/src/variant.cpp b/libraries/libfc/src/variant.cpp index da1e648da0..56705ad18e 100644 --- a/libraries/libfc/src/variant.cpp +++ b/libraries/libfc/src/variant.cpp @@ -490,7 +490,7 @@ std::string variant::as_string()const return *reinterpret_cast(this) ? "true" : "false"; case blob_type: if( get_blob().data.size() ) - return base64_encode( get_blob().data.data(), get_blob().data.size() ) + "="; + return base64_encode( get_blob().data.data(), get_blob().data.size() ); return std::string(); case null_type: return std::string(); @@ -533,10 +533,11 @@ blob variant::as_blob()const { const std::string& str = get_string(); if( str.size() == 0 ) return blob(); - if( str.back() == '=' ) - { + try { std::string b64 = base64_decode( get_string() ); return blob( { std::vector( b64.begin(), b64.end() ) } ); + } catch(const std::exception&) { + // unable to decode, just return the raw chars } return blob( { std::vector( str.begin(), str.end() ) } ); } diff --git a/libraries/libfc/test/variant/test_variant.cpp b/libraries/libfc/test/variant/test_variant.cpp index 827b420ed0..cfef4e50be 100644 --- a/libraries/libfc/test/variant/test_variant.cpp +++ b/libraries/libfc/test/variant/test_variant.cpp @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(variant_format_string_limited) const string target_result = format_prefix + a_short_list + " " + "{" + "\"b\":\"" + b_short_list + "\",\"c\":\"" + c_short_list + "\"}" + " " + "[\"" + d_short_list + "\",\"" + e_short_list + "\"]" + " " + - base64_encode( a_blob.data.data(), a_blob.data.size() ) + "=" + " " + + base64_encode( a_blob.data.data(), a_blob.data.size() ) + " " + g_short_list; BOOST_CHECK_EQUAL( result, target_result);