Browse Source

src: fix base64 decoding

Make sure trailing garbage is not treated as a valid base64 character.

Fixes: https://github.com/nodejs/node/issues/11987
PR-URL: https://github.com/nodejs/node/pull/11995
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6
Nikolai Vavilov 8 years ago
parent
commit
7e0c3ab641
  1. 4
      src/base64.h
  2. 4
      test/parallel/test-buffer-alloc.js

4
src/base64.h

@ -60,13 +60,13 @@ size_t base64_decode_slow(char* dst, size_t dstlen,
size_t k = 0; size_t k = 0;
for (;;) { for (;;) {
#define V(expr) \ #define V(expr) \
while (i < srclen) { \ for (;;) { \
const uint8_t c = src[i]; \ const uint8_t c = src[i]; \
lo = unbase64(c); \ lo = unbase64(c); \
i += 1; \ i += 1; \
if (lo < 64) \ if (lo < 64) \
break; /* Legal character. */ \ break; /* Legal character. */ \
if (c == '=') \ if (c == '=' || i >= srclen) \
return k; \ return k; \
} \ } \
expr; \ expr; \

4
test/parallel/test-buffer-alloc.js

@ -464,6 +464,10 @@ assert.strictEqual(
// Regression test for https://github.com/nodejs/node/issues/3496. // Regression test for https://github.com/nodejs/node/issues/3496.
assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0); assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
// Regression test for https://github.com/nodejs/node/issues/11987.
assert.deepStrictEqual(Buffer.from('w0 ', 'base64'),
Buffer.from('w0', 'base64'));
{ {
// Creating buffers larger than pool size. // Creating buffers larger than pool size.
const l = Buffer.poolSize + 5; const l = Buffer.poolSize + 5;

Loading…
Cancel
Save