Skip to content

Reject rounds=0 for SHA1 hashes #677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/auth/user-import-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ export class UserImportBuilder {
case 'SHA1':
case 'SHA256':
case 'SHA512':
// MD5 is [0,8192] but SHA1, SHA256, and SHA512 are [1,8192]
rounds = getNumberField(options.hash, 'rounds');
const minRounds = options.hash.algorithm === 'MD5' ? 0 : 1;
if (isNaN(rounds) || rounds < minRounds || rounds > 8192) {
throw new FirebaseAuthError(
AuthClientErrorCode.INVALID_HASH_ROUNDS,
`A valid "hash.rounds" number between ${minRounds} and 8192 must be provided for ` +
`hash algorithm ${options.hash.algorithm}.`,
);
}
populatedOptions = {
hashAlgorithm: options.hash.algorithm,
rounds,
};
break;

case 'PBKDF_SHA1':
case 'PBKDF2_SHA256':
rounds = getNumberField(options.hash, 'rounds');
Expand Down
2 changes: 1 addition & 1 deletion test/integration/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ describe('admin.auth', () => {
importOptions: {
hash: {
algorithm: 'SHA256',
rounds: 0,
rounds: 1,
},
} as any,
computePasswordHash: (userImportTest: UserImportTest): Buffer => {
Expand Down
30 changes: 26 additions & 4 deletions test/unit/auth/user-import-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,34 @@ describe('UserImportBuilder', () => {

md5ShaPbkdfAlgorithms.forEach((algorithm) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an amazing variable name.

describe(`${algorithm}`, () => {
const invalidRounds = [-1, 120001, 'invalid', undefined, null];
let minRounds: number;
let maxRounds: number;
switch (algorithm) {
case 'MD5':
minRounds = 0;
maxRounds = 8192;
break;
case 'SHA1':
case 'SHA256':
case 'SHA512':
minRounds = 1;
maxRounds = 8192;
break;
case 'PBKDF_SHA1':
case 'PBKDF2_SHA256':
minRounds = 0;
maxRounds = 120000;
break;
default:
throw new Error('Unexpected algorithm: ' + algorithm);
}
const invalidRounds = [minRounds - 1, maxRounds + 1, 'invalid', undefined, null];

invalidRounds.forEach((rounds) => {
it(`should throw when ${JSON.stringify(rounds)} rounds provided`, () => {
const expectedError = new FirebaseAuthError(
AuthClientErrorCode.INVALID_HASH_ROUNDS,
`A valid "hash.rounds" number between 0 and 120000 must be provided for ` +
`A valid "hash.rounds" number between ${minRounds} and ${maxRounds} must be provided for ` +
`hash algorithm ${algorithm}.`,
);
const invalidOptions = {
Expand All @@ -231,12 +253,12 @@ describe('UserImportBuilder', () => {
const validOptions = {
hash: {
algorithm,
rounds: 120000,
rounds: maxRounds,
},
};
const expectedRequest = {
hashAlgorithm: algorithm,
rounds: 120000,
rounds: maxRounds,
users: expectedUsersRequest,
};
const userImportBuilder =
Expand Down