qgustavor / mega

Unofficial JavaScript SDK for MEGA
https://mega.js.org/
MIT License
161 stars 43 forks source link

Mac Verification Failed #106

Closed 0cto-pus closed 2 years ago

0cto-pus commented 2 years ago

Describe the bug Getting error when trying to download a file.

Error: MAC verification failed at Transform.flush [as _flush] (C:\Users\Desktop\VSCodeProjects\node_modules\megajs\dist\main.node-cjs.js:351:18) at Transform.final [as _final] (node:internal/streams/transform:112:25) at callFinal (node:internal/streams/writable:695:27) at prefinish (node:internal/streams/writable:724:7)

To Reproduce

Run this code in Node v17.3.0

const file = storage.root.children[0]
        const stream = file.download()
        const path = Path.resolve('./photos/', file.name)
        stream.pipe(fs.createWriteStream(path)) //Download
        await new Promise((resolve, reject) => {
          stream.on('error', function (error) {
              console.log('Error occured ->'+ file.name)
              reject(error);
          })
          stream.on('end', function () {
             resolve("completed");
             console.log('File downloaded ->'+ file.name)
          })
      })

Expected behavior End the stream.

qgustavor commented 2 years ago

Maybe this file is corrupted. Does downloading this file using another client works? If it works, try using file.download({ forceHttps: true }). If changing this solves the issue then provably there is something in your network that's modifying the requests.

MEGA does not use HTTPS since it already checks file integrity using a MAC, in the other hand, the check is only done at the end of the download and will not detect if some misconfigured corporate proxy or something like this is corrupting the download until the end.

If forcing HTTPS do not solve this issue, can you share this file and link it here? If not then I will have to close this issue because I can't reproduce it.

Edit: If you think that the MAC calculation is wrong from the upload side (i.e. whoever uploaded this file used a bugged client that calculated a wrong MAC) you can ignore this check by using file.download({ end: file.size }). Because the MAC verification only works on the whole file if you chose a end (even if this end is the real end of the file) this check will be disabled.

0cto-pus commented 2 years ago

Thank you for your quick response.

file.download({ end: file.size }) worked.