Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer.from('base64') decodes some white space as data #11987

Closed
jorangreef opened this issue Mar 22, 2017 · 2 comments · Fixed by #11995
Closed

Buffer.from('base64') decodes some white space as data #11987

jorangreef opened this issue Mar 22, 2017 · 2 comments · Fixed by #11995
Labels
buffer Issues and PRs related to the buffer subsystem. confirmed-bug Issues with confirmed bugs.

Comments

@jorangreef
Copy link
Contributor

I am writing a native addon to decode a buffer containing base64 directly into another buffer, without requiring the allocation of an interim string (see #11866 for why Node cannot decode base64 buffers directly).

I wrote a fuzz test to test my own decoder, and then decided to try it out on Node's base64 decoder.

There are a few cases where Node's decoder treats whitespace as actual data, rather than ignoring it. This happens when the "=" is left out:

// Fails:
> Buffer.from("3v6YpUFyK0/hitA2tCDIfdYKw0 g ", 'base64').toString('binary')
'Þþ�¥Ar+Oá�Ð6´ È}Ö\nÃH>'
> Buffer.from("3v6YpUFyK0/hitA2tCDIfdYKw0g ", 'base64').toString('binary')
'Þþ�¥Ar+Oá�Ð6´ È}Ö\nÃH>'
> Buffer.from("3v6YpUFyK0/hitA2tCDIfdYKw0g\n", 'base64').toString('binary')
'Þþ�¥Ar+Oá�Ð6´ È}Ö\nÃH>'

// Passes:
> Buffer.from("3v6YpUFyK0/hitA2tCDIfdYKw0g=", 'base64').toString('binary')
'Þþ�¥Ar+Oá�Ð6´ È}Ö\nÃH'
> Buffer.from("3v6YpUFyK0/hitA2tCDIfdYKw0g", 'base64').toString('binary')
'Þþ�¥Ar+Oá�Ð6´ È}Ö\nÃH'

In the failing cases, Node's decoder has interpolated a ">".

@vsemozhetbyt vsemozhetbyt added the buffer Issues and PRs related to the buffer subsystem. label Mar 22, 2017
@jorangreef jorangreef changed the title Buffer.from('base64') decodes white space inconsistently Buffer.from('base64') decodes some white space as data Mar 22, 2017
@seishun
Copy link
Contributor

seishun commented Mar 22, 2017

I'm assuming Node.js doesn't throw on invalid base64 input for performance reasons. So it's basically "garbage in - garbage out".

@jorangreef
Copy link
Contributor Author

I'm assuming Node.js doesn't throw on invalid base64 input for performance reasons.

This is another issue, but there's technically no performance reason why Node's decoder could not throw on non-whitespace, non-alphabet characters. The current decoder can be upgraded without introducing any additional branching cost.

So it's basically "garbage in - garbage out".

You're saying the lack of "=" padding plus extra trailing space is illegal? Node's decoder is supposed to ignore white space. It's also supposed to handle the lack of "=" padding. None of that is illegal.

atob() is generally pretty good at throwing on garbage or illegal data, and it has no problem on the same set.

There's nothing too unusual about this kind of input either. Imagine a MIME message scenario where the sender never added base64 padding, and an intermediary mail transport added trailing white space.

@addaleax addaleax added the confirmed-bug Issues with confirmed bugs. label Mar 22, 2017
@seishun seishun mentioned this issue Mar 22, 2017
3 tasks
italoacasas pushed a commit to italoacasas/node that referenced this issue Apr 10, 2017
Make sure trailing garbage is not treated as a valid base64 character.

Fixes: nodejs#11987
PR-URL: nodejs#11995
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this issue Apr 18, 2017
Make sure trailing garbage is not treated as a valid base64 character.

Fixes: #11987
PR-URL: #11995
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this issue Apr 19, 2017
Make sure trailing garbage is not treated as a valid base64 character.

Fixes: #11987
PR-URL: #11995
Reviewed-By: Anna Henningsen <anna@addaleax.net>
MylesBorins pushed a commit that referenced this issue Apr 19, 2017
Make sure trailing garbage is not treated as a valid base64 character.

Fixes: #11987
PR-URL: #11995
Reviewed-By: Anna Henningsen <anna@addaleax.net>
andrew749 pushed a commit to michielbaird/node that referenced this issue Jul 19, 2017
Make sure trailing garbage is not treated as a valid base64 character.

Fixes: nodejs/node#11987
PR-URL: nodejs/node#11995
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. confirmed-bug Issues with confirmed bugs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants