[Bug] FunctionError: 'crypto' module: buffer is not a supported output encoding

Hello,

While trying to download reports with the amazon sellers partner api we encountered an issue. While downloading the file an error occured:

FunctionError: ‘crypto’ module: buffer is not a supported output encoding

We tried to reproduce the error locally by using the same node version like Realm but could not reproduce it.

We are using the ‘amazon-sp-api’ npm package. The problem seems to occur inside the package when calling:
let decipher = crypto.createDecipheriv(
‘aes-256-cbc’,
Buffer.from(details.encryptionDetails.key, ‘base64’),
Buffer.from(details.encryptionDetails.initializationVector, ‘base64’)
);

Since it is the only place where the crypto library is used inside the call. On function side we just call
await sellingPartner.download(res, { json: true });

With the correct parameters. The call worked locally with the same node version (10.18.1).
Now we have no idea what is happening there, since inside the built-in support documentation every from crypto that is needed is marked as supported.

Best Regards,
Daniel

1 Like

When calling the function without a trycatch we got the following stacktrace:

‘crypto’ module: buffer is not a supported output encoding

trace:
FunctionError: ‘crypto’ module: buffer is not a supported output encoding
at github.com/10gen/stitch/function/execution/vm.gojaFunc.func1 (native)
at update (:193:37(32))
at download$ (node_modules/amazon-sp-api/lib/SellingPartner.js:829:64(187))
at tryCatch (:55:50(9))
at invoke (:281:30(112))
at :107:28(6)
at tryCatch (:55:50(9))
at invoke (:145:28(15))
at :155:19(7)

We managed to specify the issue to the line inside the amazon-sp-api:

let decrypted_buffer = Buffer.concat([decipher.update(encrypted_buffer), decipher.final()]);

The update function is the root cause. The function is returning a buffer by default which makes sense. But MongDB backend is not allowing as output encoding buffer somehow? This is strange since buffers are explicitly allowed by the docu:
https://nodejs.org/api/crypto.html#crypto_decipher_update_data_inputencoding_outputencoding

Experimenting with other output encodings has shown that only buffer as output encoding is an issue here.

We also have encountered an issue when using the csvtojson npm package. Is it possible that there are problems with using Buffers in MongoDB Realm Functions?