arun11299 / cpp-jwt

JSON Web Token library for C++
MIT License
395 stars 111 forks source link

Fix out of bounds read in base64_decode #32

Closed meastman closed 6 years ago

meastman commented 6 years ago

jwt::base64_decode (and also jwt::base64_uri_decode) may crash when the provided buffer does not contain any valid base64 characters while it is attempting to ignore/remove invalid characters from the end. This is easily triggered by attempting to decode a JWT token with an empty header, which is what the included test does.

After adding the new test but before fixing the code, this was the test result:

[ RUN      ] DecodeTest.DecodeEmptyHeader
test_jwt_decode: /home/matt/cpp-jwt/include/jwt/base64.hpp:158: jwt::DMap::at(size_t) const::<lambda()>: Assertion `!"pos < map_.size()"' failed.
Aborted (core dumped)

After the fix:

[ RUN      ] DecodeTest.DecodeEmptyHeader
[       OK ] DecodeTest.DecodeEmptyHeader (0 ms)