Nodejs Buffer.from returns invalid value

fctorial

Buffer.from(' ???', 'base64')

This code returns an empty buffer instead of throwing an error, which is the expected behaviour. What is the correct way of verifying the validity of encoded data and decoding it in nodejs?

Anuj Pancholi

There seems to be nothing in the nodejs 12.16 docs about an error being thrown if the characters in a given string are not part of the encoding specified.

In fact, a small experiment seems to suggest that Buffer.from() simply ignores such characters that do not belong in the encoding.

try{
    const str1 = "ai?73";
    const str2 = "ai73";
    const str3 = "??a?i73??";
    const encoded1 = Buffer.from(str1,'base64').toString('base64');
    const encoded2 = Buffer.from(str2,'base64').toString('base64');
    const encoded3 = Buffer.from(str3,'base64').toString('base64');
    console.log(`On base64 encoding ${str1}: ${encoded1}`);
    console.log(`On base64 encoding ${str2}: ${encoded2}`);
    console.log(`On base64 encoding ${str3}: ${encoded3}`);
}catch(e){
    console.error(`ERROR:`,e);
}

It yields the following output:

On base64 encoding ai?73: ai73
On base64 encoding ai73: ai73
On base64 encoding ??a?i73??: ai73

So, your best bet would be to use a package like is-base64, as suggested by user dajnz in the comments.

https://www.npmjs.com/package/is-base64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

OpenCL - CL_INVALID_VALUE while reading from Buffer

Printing datetime value from SQL in Python returns invalid result

Base 64 from Nodejs using oracledb to query blob data returns empty buffer

Airflow PythonBranchOperator returns invalid value

QUOTIENT of numbers returns invalid value

get function returns invalid value

RECV buffer empty, but returns a value > 1

DirectX-11 Instance Buffer returns E_INVALID_ARGS

nodejs buffer uint 32 from ruby?

how to load an image from url into buffer in nodejs

use NodeJs Buffer class from client side

NodeJS Buffer read bits from Bytes

nodejs conversion from buffer data to byte array

Saving buffer/stream that comes from nodejs server

Image from MySql with Nodejs coming in buffer array

Why jquery returns invalid property value?

val arrayListOf() in companion object returns an invalid value

Graphql Apollo upload in Nestjs returns invalid value {}

MaxResults returns Invalid integer value for Calendar API

AS returns Invalid <color> for given resource value

@Value boolean returns 'invalid boolean value' of value true

Nodejs Coinbase V2 REST endpoint returns invalid signature

NodeJS passport-ldapauth always returns 'Invalid Username/Password'

NodeJS express passport-ldapauth always returns 'Invalid Username/Password'

iOS (OC) compression_decode_buffer() Returns a null value

Buffer library in browser does not return the same value as in nodejs

Getting World Position from Depth Buffer Value

Scala: Returning a mutable buffer from a function that returns a Seq

memcpy returns junk data when copying from character buffer