From 651955b97cf06d371ea3b164cff7be79d5980675 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 4 Feb 2020 09:55:02 -0500 Subject: [PATCH] Throw if mismatched size when unserializing an array buffer An exception will be thrown if the length of an unserialized array buffer does not match exactly the original size at serialization time. --- src/js/storage.js | 3 +-- src/js/utils.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/js/storage.js b/src/js/storage.js index dc4d5bf38..f068cd385 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -1020,8 +1020,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { return; } } catch (ex) { - console.error(ex); - return; + log.info(ex); } const result = await this.assets.get(this.pslAssetKey); diff --git a/src/js/utils.js b/src/js/utils.js index e97963bad..339af3a2e 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -555,16 +555,19 @@ const inbuf = new Uint32Array(arrbuf, 0, inputLength); const outputLength = this.magic.length + 7 + inputLength * 7; const outbuf = new Uint8Array(outputLength); + // magic bytes let j = 0; for ( let i = 0; i < this.magic.length; i++ ) { outbuf[j++] = this.magic.charCodeAt(i); } + // array size let v = inputLength; do { outbuf[j++] = this.valToDigit[v & 0b111111]; v >>>= 6; } while ( v !== 0 ); outbuf[j++] = 0x20 /* ' ' */; + // array content for ( let i = 0; i < inputLength; i++ ) { v = inbuf[i]; do { @@ -596,16 +599,18 @@ throw new Error('Invalid µBlock.base64 encoding'); } const inputLength = instr.length; + const outputLength = this.decodeSize(instr) >> 2; const outbuf = arrbuf instanceof ArrayBuffer === false - ? new Uint32Array(this.decodeSize(instr) >> 2) + ? new Uint32Array(outputLength) : new Uint32Array(arrbuf); let i = instr.indexOf(' ', this.magic.length) + 1; if ( i === -1 ) { throw new Error('Invalid µBlock.base64 encoding'); } + // array content let j = 0; for (;;) { - if ( i === inputLength ) { break; } + if ( j === outputLength || i >= inputLength ) { break; } let v = 0, l = 0; for (;;) { const c = instr.charCodeAt(i++); @@ -615,6 +620,9 @@ } outbuf[j++] = v; } + if ( i < inputLength || j < outputLength ) { + throw new Error('Invalid µBlock.base64 encoding'); + } return outbuf; }