0

The App works fine when I use the correct decryption key but when I use the wrong deryption key the whole app is crashed.This is the block of code I am using to retrieve the data from the file, decrypt it and send it in the response.

try{
  const readIv = fs.createReadStream(filePath, { end: 15 });
  let initVect;
  readIv.on('data', (chunk) => {
  initVect = chunk;
  });

  readIv.on('close', () => {
    try{
    const readStream = fs.createReadStream(filePath, { start: 16 });
    const cipherKey = getCipherKey(key);
    const decipher = crypto.createDecipheriv('aes256', cipherKey, initVect);
    const unzip = zlib.createUnzip();
    res.set('Content-disposition', 'attachment; filename=' + file.name);
    res.set('Content-Type', file.format);
    var data;
    try{
      var data=readStream.pipe(decipher)
      //var decrypted = Buffer.concat([decipher.update(readStream), decipher.final()]);
     console.log("data ",data);
    }
    catch(exp)
    {
      console.log("exception while decrypting ",exp);
      res.status(500).json({message:"exception while decrypting the file, please add the correct key"});
    }
    try{
     data.pipe(unzip).pipe(res)
    }
    catch(exp)
    {
      console.log("exception while decrypting ",exp);
      res.status(500).json({message:"exception while decrypting the file, please add the correct key"});
    }
    }
    catch(exp)
    {
      res.status(500).json({message:"exception while decrypting the file, please add the correct key"});

    }

  });

}
catch(exp)
{
  res.status(500).json({message:"exception while decrypting the file, please add the correct key"});
}

And this is the error response I am getting, I think this is due to passing the invalid data stream to unzip.

events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (zlib.js:170:17)
Emitted 'error' event on Unzip instance at:
    at errorOrDestroy (internal/streams/destroy.js:128:12)
    at Unzip.onerror (_stream_readable.js:783:9)
    at Unzip.emit (events.js:210:5)
    at Zlib.zlibOnError [as onerror] (zlib.js:173:8) {
  errno: -3,
  code: 'Z_DATA_ERROR'
}
Prabu M
  • 109
  • 2
  • 8
  • Well, yes, duh. If you keep on truckin' and then try to unzip the data while decryption failed, then what do you expect? You catch and then run on... Why not just keep on running and only perform the last catch? If you do want to log, then log and rethrow the exception. – Maarten Bodewes Mar 08 '20 at 03:47
  • I tried that I didn't work, still it is throwing the same error. – Prabu M Mar 08 '20 at 03:55
  • 1
    Your outer `try/catch` won't catch things inside of event handlers or in asynchronous callbacks. You will need try/catch on the inner functions to catch those. Usually libraries that use events like the unzip library will give you an `error` event if you have an error handler instead of throwing. So, perhaps listen for the `error` event. – jfriend00 Mar 08 '20 at 03:57
  • So you don't reach `console.log("exception while decrypting ",exp);` when decrypting? That should *usually* be the case. – Maarten Bodewes Mar 08 '20 at 04:03
  • @MaartenBodewes yes i am not reaching the catch block when decrypting – Prabu M Mar 08 '20 at 04:06

0 Answers0