Skip to content

Commit

Permalink
fix(@sounisi5011/encrypted-archive): validate password type (#557)
Browse files Browse the repository at this point in the history
* fix(@sounisi5011/encrypted-archive): validate password type

    Fixed a bug found while adding tests within [#554].

    [#554]: #554

* refactor(@sounisi5011/encrypted-archive): reduce similar blocks

    Code Climate reported:
    + Similar blocks of code found in 2 locations. Consider refactoring.
  • Loading branch information
sounisi5011 authored Jul 9, 2022
1 parent 78e3fea commit e3f4a0c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/encrypted-archive/src/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CryptoAlgorithm, cryptoAlgorithmMap } from './cipher';
import { CompressOptions, decompressIterable } from './compress';
import { validateChunk, validatePassword } from './errors';
import {
HeaderData,
parseCiphertextIterable,
Expand All @@ -13,7 +14,6 @@ import {
} from './header';
import { getKDF } from './key-derivation-function';
import { nonceState } from './nonce';
import { validateChunk } from './stream';
import type { InputDataType, IteratorConverter } from './types';
import { bufferFrom, fixNodePrimordialsErrorInstance } from './utils';
import { StreamReader } from './utils/stream';
Expand Down Expand Up @@ -169,6 +169,7 @@ async function decryptChunk(
}

export function createDecryptorIterator(password: InputDataType): IteratorConverter {
validatePassword(password);
return async function* decryptor(source) {
const reader = new StreamReader(source, chunk => bufferFrom(validateChunk(chunk), 'utf8'));

Expand Down
3 changes: 2 additions & 1 deletion packages/encrypted-archive/src/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { randomBytes } from 'crypto';

import { CryptoAlgorithm, cryptoAlgorithmMap, CryptoAlgorithmName, defaultCryptoAlgorithmName } from './cipher';
import { CompressOptions, createCompressor } from './compress';
import { validateChunk, validatePassword } from './errors';
import { createHeader, createSimpleHeader } from './header';
import { getKDF, KeyDerivationOptions, NormalizedKeyDerivationOptions } from './key-derivation-function';
import { nonceState } from './nonce';
import { validateChunk } from './stream';
import type { InputDataType, IteratorConverter } from './types';
import { bufferFrom, convertIterableValue } from './utils';
import type { AsyncIterableReturn } from './utils/type';
Expand Down Expand Up @@ -147,6 +147,7 @@ async function* encryptChunk(compressedCleartext: Buffer, {
}

export function createEncryptorIterator(password: InputDataType, options: EncryptOptions): IteratorConverter {
validatePassword(password);
const algorithm = cryptoAlgorithmMap.get(options.algorithm ?? defaultCryptoAlgorithmName);
if (!algorithm) throw new TypeError(`Unknown algorithm was received: ${String(options.algorithm)}`);

Expand Down
25 changes: 25 additions & 0 deletions packages/encrypted-archive/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { InputDataType, isInputDataType } from './types';
import { printObject } from './utils';

function createInputDataTypeErrorMessage(prefix: string, actual: unknown): string {
return (
prefix
+ ` must be of type string or an instance of Buffer, TypedArray, DataView, or ArrayBuffer.`
+ ` Received ${printObject(actual)}`
);
}

export function validateChunk(chunk: unknown): InputDataType {
if (!isInputDataType(chunk)) {
throw new TypeError(createInputDataTypeErrorMessage('Invalid type chunk received. Each chunk', chunk));
}
return chunk;
}

export function validatePassword(password: unknown): asserts password is InputDataType {
if (!isInputDataType(password)) {
throw new TypeError(
createInputDataTypeErrorMessage('Invalid type password received. The password argument', password),
);
}
}
2 changes: 1 addition & 1 deletion packages/encrypted-archive/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type { CryptoAlgorithmName } from './cipher';
import type { CompressOptions } from './compress';
import { createDecryptorIterator } from './decrypt';
import { createEncryptorIterator, EncryptOptions } from './encrypt';
import { validateChunk } from './errors';
import type { KeyDerivationOptions } from './key-derivation-function';
import { validateChunk } from './stream';
import type { InputDataType, IteratorConverter } from './types';
import { asyncIterable2Buffer, bufferFrom, convertIterableValue } from './utils';

Expand Down
13 changes: 0 additions & 13 deletions packages/encrypted-archive/src/stream.ts

This file was deleted.

0 comments on commit e3f4a0c

Please sign in to comment.