diff --git a/ts/session/crypto/BufferPadding.ts b/ts/session/crypto/BufferPadding.ts index 5658cd7c0..1e6436aed 100644 --- a/ts/session/crypto/BufferPadding.ts +++ b/ts/session/crypto/BufferPadding.ts @@ -12,14 +12,14 @@ const PADDING_BYTE = 0x00; */ export function removeMessagePadding(paddedData: ArrayBuffer): ArrayBuffer { const paddedPlaintext = new Uint8Array(paddedData); - window.log.info('Removing message padding...'); + window?.log.info('Removing message padding...'); for (let i = paddedPlaintext.length - 1; i >= 0; i -= 1) { if (paddedPlaintext[i] === 0x80) { const plaintext = new Uint8Array(i); plaintext.set(paddedPlaintext.subarray(0, i)); return plaintext.buffer; } else if (paddedPlaintext[i] !== PADDING_BYTE) { - window.log.warn('got a message without padding... Letting it through for now'); + window?.log.warn('got a message without padding... Letting it through for now'); return paddedPlaintext; } } @@ -32,7 +32,7 @@ export function removeMessagePadding(paddedData: ArrayBuffer): ArrayBuffer { * @param messageBuffer The buffer to add padding to. */ export function addMessagePadding(messageBuffer: Uint8Array): Uint8Array { - window.log?.info('Adding message padding...'); + window?.log?.info('Adding message padding...'); const plaintext = new Uint8Array(getPaddedMessageLength(messageBuffer.byteLength + 1) - 1); plaintext.set(new Uint8Array(messageBuffer)); @@ -59,25 +59,19 @@ export function getUnpaddedAttachment( data: ArrayBuffer, unpaddedExpectedSize: number ): ArrayBuffer | null { - window.log?.info('Removing attachment padding...'); + window?.log?.info('Removing attachment padding...'); // to have a padding we must have a strictly longer length expected if (data.byteLength <= unpaddedExpectedSize) { return null; } - const dataUint = new Uint8Array(data); - for (let i = unpaddedExpectedSize; i < data.byteLength; i++) { - if (dataUint[i] !== PADDING_BYTE) { - return null; - } - } - + // we now consider that anything coming after the expected size is padding, no matter what there is there return data.slice(0, unpaddedExpectedSize); } export function addAttachmentPadding(data: ArrayBuffer): ArrayBuffer { const originalUInt = new Uint8Array(data); - window.log.info('Adding attchment padding...'); + window?.log.info('Adding attchment padding...'); const paddedSize = Math.max( 541, diff --git a/ts/test/session/unit/padding/Padding_test.ts b/ts/test/session/unit/padding/Padding_test.ts new file mode 100644 index 000000000..ff397ed28 --- /dev/null +++ b/ts/test/session/unit/padding/Padding_test.ts @@ -0,0 +1,92 @@ +// tslint:disable: no-implicit-dependencies max-func-body-length no-unused-expression + +import chai from 'chai'; +import * as sinon from 'sinon'; +import _ from 'lodash'; +import { describe } from 'mocha'; + +import chaiAsPromised from 'chai-as-promised'; +import { + addAttachmentPadding, + addMessagePadding, + getUnpaddedAttachment, + removeMessagePadding, +} from '../../../../session/crypto/BufferPadding'; +chai.use(chaiAsPromised as any); +chai.should(); + +const { expect } = chai; + +// tslint:disable-next-line: max-func-body-length +describe('Padding', () => { + describe('Attachment padding', () => { + it('add padding', () => { + const bufferIn = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + + const paddedBuffer = addAttachmentPadding(bufferIn); + expect(paddedBuffer.byteLength).to.equal(541); + expect(new Uint8Array(paddedBuffer.slice(0, bufferIn.length))).to.equalBytes(bufferIn); + // this makes sure that the padding is just the 0 bytes + expect(new Uint8Array(paddedBuffer.slice(bufferIn.length))).to.equalBytes( + new Uint8Array(541 - bufferIn.length) + ); + }); + + it('remove padding', () => { + // padding can be anything after the expected size + const expectedSize = 10; + const paddedBuffer = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 5]); + + const paddingRemoveBuffer = getUnpaddedAttachment(paddedBuffer, expectedSize); + + expect(paddingRemoveBuffer?.byteLength).to.equal(expectedSize); + expect(paddingRemoveBuffer).to.equalBytes(paddedBuffer.slice(0, expectedSize)); + }); + }); + + describe('Message padding', () => { + it('add padding', () => { + const bufferIn = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); + + const paddedMessage = addMessagePadding(bufferIn); + expect(paddedMessage.byteLength).to.equal(159); + // for message padding, we have [bufferIn, 0x80, 0x00, 0x00, 0x00, ...] + expect(new Uint8Array(paddedMessage.slice(0, bufferIn.length))).to.equalBytes(bufferIn); + expect(paddedMessage[bufferIn.length]).to.equal(0x80); + // this makes sure that the padding is just the 0 bytes + expect(new Uint8Array(paddedMessage.slice(bufferIn.length + 1))).to.equalBytes( + new Uint8Array(159 - bufferIn.length - 1) + ); + }); + + it('remove padding', () => { + const expectedSize = 10; + const paddedBuffer = new Uint8Array([ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 128, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ]); + + const unpaddedMessage = removeMessagePadding(paddedBuffer); + // for message padding, we have [paddedBuffer, 0x80, 0x00, 0x00, 0x00, ...] + expect(unpaddedMessage?.byteLength).to.equal(expectedSize); + expect(new Uint8Array(unpaddedMessage)).to.equalBytes(paddedBuffer.slice(0, expectedSize)); + }); + }); +});