mirror of
https://github.com/roytam1/UXP.git
synced 2026-05-26 13:58:49 +00:00
[XPCOM] Fix Base64 off-by-one issue and safeguard against this mistake in the future.
This commit is contained in:
+9
-3
@@ -246,6 +246,7 @@ EncodeInputStream(nsIInputStream* aInputStream,
|
||||
|
||||
static const char kBase64URLAlphabet[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
static_assert(mozilla::ArrayLength(kBase64URLAlphabet) == 0x41);
|
||||
|
||||
// Maps an encoded character to a value in the Base64 URL alphabet, per
|
||||
// RFC 4648, Table 2. Invalid input characters map to UINT8_MAX.
|
||||
@@ -267,14 +268,19 @@ static const uint8_t kBase64URLDecodeTable[] = {
|
||||
255,
|
||||
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
|
||||
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, /* a - z */
|
||||
255, 255, 255, 255,
|
||||
255, 255, 255, 255, 255
|
||||
};
|
||||
static_assert(mozilla::ArrayLength(kBase64URLDecodeTable) == 0x80);
|
||||
|
||||
bool
|
||||
Base64URLCharToValue(char aChar, uint8_t* aValue) {
|
||||
uint8_t index = static_cast<uint8_t>(aChar);
|
||||
*aValue = kBase64URLDecodeTable[index & 0x7f];
|
||||
return (*aValue != 255) && !(index & ~0x7f);
|
||||
if (index >= mozilla::ArrayLength(kBase64URLDecodeTable)) {
|
||||
*aValue = 255;
|
||||
return false;
|
||||
}
|
||||
*aValue = kBase64URLDecodeTable[index];
|
||||
return *aValue != 255;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user