From 050f82b3828c61afdcc8d3c7c916da6207c17318 Mon Sep 17 00:00:00 2001 From: C-3PO Date: Thu, 5 Jul 2018 02:39:17 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Do=20not=20read=20beyond=20strea?= =?UTF-8?q?m=20in=20decryptor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ssn/streams/decryptStream.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ssn/streams/decryptStream.ts b/src/ssn/streams/decryptStream.ts index 3470719..12cafcb 100644 --- a/src/ssn/streams/decryptStream.ts +++ b/src/ssn/streams/decryptStream.ts @@ -8,16 +8,20 @@ export default function decryptStream(inputStream: stream.Readable, decryptionKe read(size) { //There are 12 random bytes at the beginning, we need to use them to initialize the decryption keys, but we can ignore the decrypted bytes. if (!skippedRandomHeader) { - const encryptedHeader = inputStream.read(12); + const encryptedHeader: Buffer = inputStream.read(12); decryptChunk(encryptedHeader, decryptionKeys); skippedRandomHeader = true; } //Decrypt chunk - const encryptedChunk = inputStream.read(size); - const decryptedChunk = decryptChunk(encryptedChunk, decryptionKeys); - - this.push(decryptedChunk); + const encryptedChunk: Buffer = inputStream.read(size); + if (encryptedChunk === null) { + //If end has been reached, stop + this.push(null); + } else { + const decryptedChunk = decryptChunk(encryptedChunk, decryptionKeys); + this.push(decryptedChunk); + } }, });