Skip to content

Commit 943e40c

Browse files
authored
Reject rounds=0 for SHA1 hashes (#677)
Port of firebase/firebase-tools#1701
1 parent 43fe7f8 commit 943e40c

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/auth/user-import-builder.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,22 @@ export class UserImportBuilder {
344344
case 'SHA1':
345345
case 'SHA256':
346346
case 'SHA512':
347+
// MD5 is [0,8192] but SHA1, SHA256, and SHA512 are [1,8192]
348+
rounds = getNumberField(options.hash, 'rounds');
349+
const minRounds = options.hash.algorithm === 'MD5' ? 0 : 1;
350+
if (isNaN(rounds) || rounds < minRounds || rounds > 8192) {
351+
throw new FirebaseAuthError(
352+
AuthClientErrorCode.INVALID_HASH_ROUNDS,
353+
`A valid "hash.rounds" number between ${minRounds} and 8192 must be provided for ` +
354+
`hash algorithm ${options.hash.algorithm}.`,
355+
);
356+
}
357+
populatedOptions = {
358+
hashAlgorithm: options.hash.algorithm,
359+
rounds,
360+
};
361+
break;
362+
347363
case 'PBKDF_SHA1':
348364
case 'PBKDF2_SHA256':
349365
rounds = getNumberField(options.hash, 'rounds');

test/integration/auth.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,7 +1331,7 @@ describe('admin.auth', () => {
13311331
importOptions: {
13321332
hash: {
13331333
algorithm: 'SHA256',
1334-
rounds: 0,
1334+
rounds: 1,
13351335
},
13361336
} as any,
13371337
computePasswordHash: (userImportTest: UserImportTest): Buffer => {

test/unit/auth/user-import-builder.spec.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,34 @@ describe('UserImportBuilder', () => {
207207

208208
md5ShaPbkdfAlgorithms.forEach((algorithm) => {
209209
describe(`${algorithm}`, () => {
210-
const invalidRounds = [-1, 120001, 'invalid', undefined, null];
210+
let minRounds: number;
211+
let maxRounds: number;
212+
switch (algorithm) {
213+
case 'MD5':
214+
minRounds = 0;
215+
maxRounds = 8192;
216+
break;
217+
case 'SHA1':
218+
case 'SHA256':
219+
case 'SHA512':
220+
minRounds = 1;
221+
maxRounds = 8192;
222+
break;
223+
case 'PBKDF_SHA1':
224+
case 'PBKDF2_SHA256':
225+
minRounds = 0;
226+
maxRounds = 120000;
227+
break;
228+
default:
229+
throw new Error('Unexpected algorithm: ' + algorithm);
230+
}
231+
const invalidRounds = [minRounds - 1, maxRounds + 1, 'invalid', undefined, null];
232+
211233
invalidRounds.forEach((rounds) => {
212234
it(`should throw when ${JSON.stringify(rounds)} rounds provided`, () => {
213235
const expectedError = new FirebaseAuthError(
214236
AuthClientErrorCode.INVALID_HASH_ROUNDS,
215-
`A valid "hash.rounds" number between 0 and 120000 must be provided for ` +
237+
`A valid "hash.rounds" number between ${minRounds} and ${maxRounds} must be provided for ` +
216238
`hash algorithm ${algorithm}.`,
217239
);
218240
const invalidOptions = {
@@ -231,12 +253,12 @@ describe('UserImportBuilder', () => {
231253
const validOptions = {
232254
hash: {
233255
algorithm,
234-
rounds: 120000,
256+
rounds: maxRounds,
235257
},
236258
};
237259
const expectedRequest = {
238260
hashAlgorithm: algorithm,
239-
rounds: 120000,
261+
rounds: maxRounds,
240262
users: expectedUsersRequest,
241263
};
242264
const userImportBuilder =

0 commit comments

Comments
 (0)