Skip to content

Commit

Permalink
Merge branch 'tim/5chars' into 'master'
Browse files Browse the repository at this point in the history
feat: On invalid version errors, give first few bytes of encrypt header

See merge request TankerHQ/sdk-js!1017
  • Loading branch information
tux3 committed Mar 26, 2024
2 parents b392086 + ac79d91 commit 65e2bae
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/crypto/src/EncryptionFormats/EncryptionFormats.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sodium from 'libsodium-wrappers';
import { InvalidArgument } from '@tanker/errors';

import { EncryptionV1 } from './v1';
Expand Down Expand Up @@ -50,8 +51,10 @@ export const extractEncryptionFormat = (encryptedData: Uint8Array) => {

const encryption = encryptionFormats[version];

if (!encryption)
throw new InvalidArgument(`Unhandled format version ${version} used in encryptedData`);
if (!encryption) {
const headerHex = sodium.to_hex(encryptedData.slice(0, 5));
throw new InvalidArgument(`Unhandled format version ${version} used in encryptedData. Header starts with: 0x${headerHex}`);
}
if (encryptedData.length < encryption.overhead)
throw new InvalidArgument(`Truncated encrypted data. Length should be at least ${encryption.overhead} with encryption format v${encryption.version}`);

Expand Down
5 changes: 5 additions & 0 deletions packages/crypto/src/__tests__/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ describe('Resource', () => {
expect(() => extractEncryptionFormat(incorrectVersion)).to.throw(InvalidArgument);
});

it('should return the first 5 bytes as hex when unsupported format version is detected', () => {
const elf = new Uint8Array([0x7F, 0x45, 0x4C, 0x46, 0x2, 0x1, 0x1]);
expect(() => extractEncryptionFormat(elf)).to.throw('Header starts with: 0x7f454c4602');
});

configs.forEach(({ version, testVector }) => {
it(`should detect a buffer v${version}`, () => {
const resource = utils.concatArrays(new Uint8Array([version]), testVector);
Expand Down

0 comments on commit 65e2bae

Please sign in to comment.