From: Justin Wind Date: Tue, 14 Mar 2023 23:06:58 +0000 (-0700) Subject: throw MysteryBoxError instead of generic errors X-Git-Tag: v2.0.0~1^2~1 X-Git-Url: http://git.squeep.com/?p=squeep-mystery-box;a=commitdiff_plain;h=6c9e123b3c10ef5caafc1a5f352a4705a8579ca9 throw MysteryBoxError instead of generic errors --- diff --git a/index.js b/index.js index edc09e1..43cb295 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,9 @@ 'use strict'; const MysteryBox = require('./lib/mystery-box'); +const { MysteryBoxError } = require('./lib/errors'); module.exports = { MysteryBox, + MysteryBoxError, }; diff --git a/lib/errors.js b/lib/errors.js new file mode 100644 index 0000000..ec8d285 --- /dev/null +++ b/lib/errors.js @@ -0,0 +1,16 @@ +'use strict'; + +class MysteryBoxError extends Error { + constructor(...args) { + super(...args); + Error.captureStackTrace(MysteryBoxError); + } + + get name() { + return this.constructor.name; + } +} + +module.exports = { + MysteryBoxError, +}; \ No newline at end of file diff --git a/lib/mystery-box.js b/lib/mystery-box.js index 5dcf1b1..ddae815 100644 --- a/lib/mystery-box.js +++ b/lib/mystery-box.js @@ -5,6 +5,7 @@ const crypto = require('crypto'); const zlib = require('zlib'); const { promisify } = require('util'); const common = require('./common'); +const { MysteryBoxError } = require('./errors'); const allVersions = require('./version-parameters'); const { performance } = require('perf_hooks'); const { name: packageName, version: packageVersion } = require('../package.json'); @@ -73,7 +74,7 @@ class MysteryBox extends EventEmitter { super(...args); this.secrets = common.ensureArray(options.encryptionSecret); if (!this.secrets.length) { - throw new Error('missing encryption secret'); + throw new MysteryBoxError('missing encryption secret'); } // Filter any unavailable algorithms @@ -93,13 +94,13 @@ class MysteryBox extends EventEmitter { // Default to highest this.bestVersion = Number(Object.keys(this.versionParameters).sort().pop()); if (Number.isNaN(this.bestVersion)) { - throw new Error('no supported versions available'); + throw new MysteryBoxError('no supported versions available'); } this.Flags = availableFlags; this.defaultFlags = 'defaultFlags' in options ? options.defaultFlags : availableFlags.Flate; if (this.defaultFlags < 0 || this.defaultFlags > 255) { - throw new RangeError('Invalid default flag value'); + throw new MysteryBoxError('Invalid default flag value'); } } @@ -142,7 +143,7 @@ class MysteryBox extends EventEmitter { return scryptAsync(secret, salt, keyBytes); default: - throw new RangeError('unsupported key deriver'); + throw new MysteryBoxError('unsupported key deriver'); } } @@ -187,7 +188,7 @@ class MysteryBox extends EventEmitter { } } // Nine bytes would be an extravagence. - throw new RangeError(`unsupported version header (0x${firstByte.toString(16)})`); + throw new MysteryBoxError(`unsupported version header (0x${firstByte.toString(16)})`); } @@ -211,7 +212,7 @@ class MysteryBox extends EventEmitter { } if (versionBytes > 6) { - throw new RangeError(`unsupported version (${versionBytes} bytes)`); + throw new MysteryBoxError(`unsupported version (${versionBytes} bytes)`); } // Otherwise, update the masked first byte and parse the rest of the buffer. @@ -244,7 +245,7 @@ class MysteryBox extends EventEmitter { } else if (version <= 0x03ffffffffff) { // 34359738368-4398046511103 versionBytes = 6; } else { - throw new RangeError('version too large to encode'); + throw new MysteryBoxError(`version too large to encode (${version})`); } const buffer = Buffer.alloc(versionBytes); @@ -296,7 +297,7 @@ class MysteryBox extends EventEmitter { const { stats, timingsMs } = MysteryBox._newStats('pack'); if (!(version in this.versionParameters)) { - throw new RangeError(`MysteryBox format version ${version} not supported`); + throw new MysteryBoxError(`MysteryBox format version ${version} not supported`); } // eslint-disable-next-line security/detect-object-injection const v = this.versionParameters[version]; @@ -320,7 +321,7 @@ class MysteryBox extends EventEmitter { const { buffer: versionBuffer, versionBytes } = MysteryBox._versionEncode(v.version); if (versionBytes !== v.versionBytes) { - throw new Error('internal inconsistency, mismatched version byte length'); + throw new MysteryBoxError('internal inconsistency, mismatched version byte length'); } const [iv, salt] = await Promise.all([ @@ -390,7 +391,7 @@ class MysteryBox extends EventEmitter { const { stats, timingsMs } = MysteryBox._newStats('unpack'); if (!box) { - throw new RangeError('nothing to unpack'); + throw new MysteryBoxError('nothing to unpack'); } const raw = Buffer.from(box, 'base64url'); @@ -398,20 +399,20 @@ class MysteryBox extends EventEmitter { const { version, versionBytes } = MysteryBox._versionDecode(raw); if (!(version in this.versionParameters)) { - throw new RangeError('unsupported version'); + throw new MysteryBoxError(`unsupported version (${version})`); } // eslint-disable-next-line security/detect-object-injection const v = this.versionParameters[version]; if (v.versionBytes !== versionBytes) { - throw new Error('internal inconsistency, mismatched version byte length'); + throw new MysteryBoxError('internal inconsistency, mismatched version byte length'); } offset += v.versionBytes; stats.version = version; const minBytes = v.versionBytes + v.flagsBytes + v.ivBytes + v.saltBytes + v.tagBytes; if (raw.length < minBytes) { - throw new RangeError('not enough to unpack'); + throw new MysteryBoxError('not enough to unpack'); } const flags = raw.subarray(offset, offset + v.flagsBytes).readUInt8(0); diff --git a/test/lib/errors.js b/test/lib/errors.js new file mode 100644 index 0000000..0a31620 --- /dev/null +++ b/test/lib/errors.js @@ -0,0 +1,11 @@ +'use strict'; + +const assert = require('assert'); +const { MysteryBoxError } = require('../../lib/errors'); + +describe('Errors', function () { + it('MysteryBoxError', function () { + const e = new MysteryBoxError(); + assert.strictEqual(e.name, 'MysteryBoxError'); + }); +}); // Errors \ No newline at end of file diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index dd54a88..e949139 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -5,6 +5,7 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require const MysteryBox = require('../../lib/mystery-box'); +const { MysteryBoxError } = require('../../lib/errors'); const crypto = require('crypto'); function _verbose(mb) { @@ -53,7 +54,7 @@ describe('MysteryBox', function () { it('covers bad flags', function () { options.defaultFlags = 300; - assert.rejects(() => new MysteryBox(options), RangeError); + assert.rejects(() => new MysteryBox(options), MysteryBoxError); }); it('covers missing ciphers', function () { @@ -64,7 +65,7 @@ describe('MysteryBox', function () { describe('_keyFromSecret', function () { it('covers invalid', async function () { - assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), RangeError); + assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), MysteryBoxError); }); }); // _keyFromSecret @@ -123,7 +124,7 @@ describe('MysteryBox', function () { _check(0xfe, 8, 0x00); }); it('covers unsupported', function () { - assert.throws(() => MysteryBox._versionHeaderDecode(0xff), RangeError); + assert.throws(() => MysteryBox._versionHeaderDecode(0xff), MysteryBoxError); }); }); // _versionHeaderDecode @@ -172,7 +173,7 @@ describe('MysteryBox', function () { }); it('covers too big', function () { const buffer = Buffer.from([0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); - assert.throws(() => MysteryBox._versionDecode(buffer), RangeError); + assert.throws(() => MysteryBox._versionDecode(buffer), MysteryBoxError); }); }); // _versionDecode @@ -226,7 +227,7 @@ describe('MysteryBox', function () { }); it('covers too large', function () { const version = 277076930199552; - assert.rejects(() => MysteryBox._versionEncode(version), RangeError); + assert.rejects(() => MysteryBox._versionEncode(version), MysteryBoxError); }); it('recipricates', function () { [ @@ -247,14 +248,14 @@ describe('MysteryBox', function () { }); it('covers packing unsupported version', async function () { - assert.rejects(() => mb.pack({}, 0), RangeError); + assert.rejects(() => mb.pack({}, 0), MysteryBoxError); }); it('covers unpacking unsupported version', async function () { const badBuffer = Buffer.alloc(128); badBuffer.writeUInt8(0, 0); // No such thing as version 0 const badPayload = badBuffer.toString('base64url'); - assert.rejects(() => mb.unpack(badPayload), RangeError); + assert.rejects(() => mb.unpack(badPayload), MysteryBoxError); }); it('encrypts and decrypts default version', async function () { @@ -386,13 +387,13 @@ describe('MysteryBox', function () { }); it('handles undefined', async function () { - assert.rejects(() => mb.unpack(), RangeError); + assert.rejects(() => mb.unpack(), MysteryBoxError); }); it('handles incomplete', async function () { this.slow(500); const encryptedResult = await mb.pack({ foo: 'bar' }); - assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), RangeError); + assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), MysteryBoxError); }); it('covers internal error, incorrect version byte size, pack', async function () {