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.
This commit is contained in:
Raymond Hill 2020-02-04 09:55:02 -05:00
parent 0ccbe654b8
commit 651955b97c
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 11 additions and 4 deletions

View File

@ -1020,8 +1020,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
return; return;
} }
} catch (ex) { } catch (ex) {
console.error(ex); log.info(ex);
return;
} }
const result = await this.assets.get(this.pslAssetKey); const result = await this.assets.get(this.pslAssetKey);

View File

@ -555,16 +555,19 @@
const inbuf = new Uint32Array(arrbuf, 0, inputLength); const inbuf = new Uint32Array(arrbuf, 0, inputLength);
const outputLength = this.magic.length + 7 + inputLength * 7; const outputLength = this.magic.length + 7 + inputLength * 7;
const outbuf = new Uint8Array(outputLength); const outbuf = new Uint8Array(outputLength);
// magic bytes
let j = 0; let j = 0;
for ( let i = 0; i < this.magic.length; i++ ) { for ( let i = 0; i < this.magic.length; i++ ) {
outbuf[j++] = this.magic.charCodeAt(i); outbuf[j++] = this.magic.charCodeAt(i);
} }
// array size
let v = inputLength; let v = inputLength;
do { do {
outbuf[j++] = this.valToDigit[v & 0b111111]; outbuf[j++] = this.valToDigit[v & 0b111111];
v >>>= 6; v >>>= 6;
} while ( v !== 0 ); } while ( v !== 0 );
outbuf[j++] = 0x20 /* ' ' */; outbuf[j++] = 0x20 /* ' ' */;
// array content
for ( let i = 0; i < inputLength; i++ ) { for ( let i = 0; i < inputLength; i++ ) {
v = inbuf[i]; v = inbuf[i];
do { do {
@ -596,16 +599,18 @@
throw new Error('Invalid µBlock.base64 encoding'); throw new Error('Invalid µBlock.base64 encoding');
} }
const inputLength = instr.length; const inputLength = instr.length;
const outputLength = this.decodeSize(instr) >> 2;
const outbuf = arrbuf instanceof ArrayBuffer === false const outbuf = arrbuf instanceof ArrayBuffer === false
? new Uint32Array(this.decodeSize(instr) >> 2) ? new Uint32Array(outputLength)
: new Uint32Array(arrbuf); : new Uint32Array(arrbuf);
let i = instr.indexOf(' ', this.magic.length) + 1; let i = instr.indexOf(' ', this.magic.length) + 1;
if ( i === -1 ) { if ( i === -1 ) {
throw new Error('Invalid µBlock.base64 encoding'); throw new Error('Invalid µBlock.base64 encoding');
} }
// array content
let j = 0; let j = 0;
for (;;) { for (;;) {
if ( i === inputLength ) { break; } if ( j === outputLength || i >= inputLength ) { break; }
let v = 0, l = 0; let v = 0, l = 0;
for (;;) { for (;;) {
const c = instr.charCodeAt(i++); const c = instr.charCodeAt(i++);
@ -615,6 +620,9 @@
} }
outbuf[j++] = v; outbuf[j++] = v;
} }
if ( i < inputLength || j < outputLength ) {
throw new Error('Invalid µBlock.base64 encoding');
}
return outbuf; return outbuf;
} }