From 06762c61075a8abe74ebd034c8216756a7c263ab Mon Sep 17 00:00:00 2001 From: C-3PO Date: Fri, 22 Jun 2018 17:35:36 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Verify=20file=20entries=20in=20soli?= =?UTF-8?q?dpkg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interfaces/ISolidFile.ts | 7 ++++--- src/ssn/verifySolidpkg.ts | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/interfaces/ISolidFile.ts b/src/interfaces/ISolidFile.ts index d364051..09e52cb 100644 --- a/src/interfaces/ISolidFile.ts +++ b/src/interfaces/ISolidFile.ts @@ -1,13 +1,14 @@ interface ISolidFile { - //TODO: show length - /** File name of this file. */ + /** Length of this file in bytes, up to 1700000000 or 1.7 GB */ + length: number; + /** File name of this file, e.g. `${product}_${from}to${to}.z01` or `${product}_${from}to${to}.zip`. */ path: [string]; } interface ISolidFileInfo { /** List of files that are part of this torrent. */ files: ISolidFile[]; - /** Length of one piece in bytes, e.g. 4194304 for 4 MB. */ + /** Length of one piece in bytes, e.g. 4194304 for 4 MiB. */ 'piece length': number; /** Concatenated hashes of all pieces. */ pieces: string; diff --git a/src/ssn/verifySolidpkg.ts b/src/ssn/verifySolidpkg.ts index d4f35b6..9bd06bc 100644 --- a/src/ssn/verifySolidpkg.ts +++ b/src/ssn/verifySolidpkg.ts @@ -25,7 +25,21 @@ export default function verifySolidpkg(file: ISolid, { product, from, to }: {pro throw new Error(`Expected info field but it was missing.`); } - //TODO: read file.info.files + if (!Array.isArray(file.info.files)) { + throw new Error(`Expected files field to be an array but it isn't.`); + } + for (let i = 0, il = file.info.files.length; i < il; i += 1) { + const fileEntry = file.info.files[i]; + if (typeof fileEntry.length !== 'number' && fileEntry.length >= 0 && fileEntry.length <= 1700000000) { + throw new Error(`Expected file length to be a number but it was ${fileEntry.length}.`); + } + if (!Array.isArray(fileEntry.path) || fileEntry.path.length !== 1) { + throw new Error(`Expected valid file name but it was not an array with one element.`); + } + if (typeof fileEntry.path[0] !== 'string' || !fileEntry.path[0].match(new RegExp(`${product}_${from}to${to}\.z(ip|0[1-9]|[1-9][0-9])$`))) { + throw new Error(`Expected valid file name but it was ${fileEntry.path[0]}.`); + } + } if (file.info['piece length'] !== 4194304) { throw new Error(`Expected piece length to be "4194304" but it was "${file.info['piece length']}".`);