Skip to content

Commit a4377a0

Browse files
sipaMacroFake
authored andcommitted
Reject incorrect base64 in HTTP auth
In addition, to make sure that no call site ignores the invalid decoding status, make the pf_invalid argument mandatory.
1 parent d648b51 commit a4377a0

File tree

5 files changed

+17
-15
lines changed

5 files changed

+17
-15
lines changed

src/httprpc.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
132132
if (strAuth.substr(0, 6) != "Basic ")
133133
return false;
134134
std::string strUserPass64 = TrimString(strAuth.substr(6));
135-
std::string strUserPass = DecodeBase64(strUserPass64);
135+
bool invalid;
136+
std::string strUserPass = DecodeBase64(strUserPass64, &invalid);
137+
if (invalid) return false;
136138

137139
if (strUserPass.find(':') != std::string::npos)
138140
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));

src/test/base32_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
2222
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
2323
strEnc = EncodeBase32(vstrIn[i], false);
2424
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
25-
std::string strDec = DecodeBase32(vstrOut[i]);
25+
bool invalid;
26+
std::string strDec = DecodeBase32(vstrOut[i], &invalid);
27+
BOOST_CHECK(!invalid);
2628
BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
2729
}
2830

src/test/base64_tests.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
1919
{
2020
std::string strEnc = EncodeBase64(vstrIn[i]);
2121
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
22-
std::string strDec = DecodeBase64(strEnc);
22+
bool invalid;
23+
std::string strDec = DecodeBase64(strEnc, &invalid);
24+
BOOST_CHECK(!invalid);
2325
BOOST_CHECK_EQUAL(strDec, vstrIn[i]);
2426
}
2527

src/util/strencodings.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,15 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
167167
++p;
168168
}
169169
valid = valid && (p - e) % 4 == 0 && p - q < 4;
170-
if (pf_invalid) *pf_invalid = !valid;
170+
*pf_invalid = !valid;
171171

172172
return ret;
173173
}
174174

175175
std::string DecodeBase64(const std::string& str, bool* pf_invalid)
176176
{
177177
if (!ValidAsCString(str)) {
178-
if (pf_invalid) {
179-
*pf_invalid = true;
180-
}
178+
*pf_invalid = true;
181179
return {};
182180
}
183181
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str(), pf_invalid);
@@ -245,17 +243,15 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
245243
++p;
246244
}
247245
valid = valid && (p - e) % 8 == 0 && p - q < 8;
248-
if (pf_invalid) *pf_invalid = !valid;
246+
*pf_invalid = !valid;
249247

250248
return ret;
251249
}
252250

253251
std::string DecodeBase32(const std::string& str, bool* pf_invalid)
254252
{
255253
if (!ValidAsCString(str)) {
256-
if (pf_invalid) {
257-
*pf_invalid = true;
258-
}
254+
*pf_invalid = true;
259255
return {};
260256
}
261257
std::vector<unsigned char> vchRet = DecodeBase32(str.c_str(), pf_invalid);

src/util/strencodings.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ bool IsHex(std::string_view str);
6464
* Return true if the string is a hex number, optionally prefixed with "0x"
6565
*/
6666
bool IsHexNumber(std::string_view str);
67-
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
68-
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
67+
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid);
68+
std::string DecodeBase64(const std::string& str, bool* pf_invalid);
6969
std::string EncodeBase64(Span<const unsigned char> input);
7070
inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
7171
inline std::string EncodeBase64(const std::string& str) { return EncodeBase64(MakeUCharSpan(str)); }
72-
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
73-
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
72+
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid);
73+
std::string DecodeBase32(const std::string& str, bool* pf_invalid);
7474

7575
/**
7676
* Base32 encode.

0 commit comments

Comments
 (0)