Skip to content

Reject rounds=0 for SHA1 hashes #326

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 2 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/main/java/com/google/firebase/auth/hash/Md5.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class Md5 extends RepeatableHash {

private Md5(Builder builder) {
super("MD5", 0, 120000, builder);
super("MD5", 0, 8192, builder);
}

public static Builder builder() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/google/firebase/auth/hash/Sha1.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class Sha1 extends RepeatableHash {

private Sha1(Builder builder) {
super("SHA1", 0, 120000, builder);
super("SHA1", 1, 8192, builder);
}

public static Builder builder() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/google/firebase/auth/hash/Sha256.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class Sha256 extends RepeatableHash {

private Sha256(Builder builder) {
super("SHA256", 0, 120000, builder);
super("SHA256", 1, 8192, builder);
}

public static Builder builder() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/google/firebase/auth/hash/Sha512.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class Sha512 extends RepeatableHash {

private Sha512(Builder builder) {
super("SHA512", 0, 120000, builder);
super("SHA512", 1, 8192, builder);
}

public static Builder builder() {
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/com/google/firebase/auth/UserImportHashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.firebase.auth;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableMap;
import com.google.common.io.BaseEncoding;
Expand Down Expand Up @@ -145,6 +146,91 @@ public void testBasicHash() {
}
}

private void assertBuilderThrowsIllegalArgumentException(Md5.Builder builder) {
Copy link
Member Author

Choose a reason for hiding this comment

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

An alternative to these helpers is to make all of the builders extend from a common interface (which they do: RepeatableHash.Builder) and make it public (which it isn't; and neither is RepeatableHash) even if it's just @VisibleForTesting. I didn't do that, in the (naively?) optimistic hope that we'll update to java8 "soon" which will make all that unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

May be move this to InvalidHashTest. There RepeatableHash is already visible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, yeah; that's definitely better. (Despite the name, I also used this file to test for valid hashes too.)

try {
builder.build();
fail("Expected IllegalArgumentException to be thrown but no expecption occurred.");
} catch (IllegalArgumentException expected) {
}
}

private void assertBuilderThrowsIllegalArgumentException(Sha1.Builder builder) {
try {
builder.build();
fail("Expected IllegalArgumentException to be thrown but no expecption occurred.");
} catch (IllegalArgumentException expected) {
}
}

private void assertBuilderThrowsIllegalArgumentException(Sha256.Builder builder) {
try {
builder.build();
fail("Expected IllegalArgumentException to be thrown but no expecption occurred.");
} catch (IllegalArgumentException expected) {
}
}

private void assertBuilderThrowsIllegalArgumentException(Sha512.Builder builder) {
try {
builder.build();
fail("Expected IllegalArgumentException to be thrown but no expecption occurred.");
} catch (IllegalArgumentException expected) {
}
}

private void assertBuilderThrowsIllegalArgumentException(PbkdfSha1.Builder builder) {
try {
builder.build();
fail("Expected IllegalArgumentException to be thrown but no expecption occurred.");
} catch (IllegalArgumentException expected) {
}
}

private void assertBuilderThrowsIllegalArgumentException(Pbkdf2Sha256.Builder builder) {
try {
builder.build();
fail("Expected IllegalArgumentException to be thrown but no expecption occurred.");
} catch (IllegalArgumentException expected) {
}
}

@Test
public void testInvalidHashRounds() {
// TODO(rsgowman): Once we can update to Java8, we could just do something like this instead of
// having all of the helpers:
// assertThrows(IllegalArgumentException.class, ()-> Md5.builder().setRounds(-1).build());
//

assertBuilderThrowsIllegalArgumentException(Md5.builder().setRounds(-1));
assertBuilderThrowsIllegalArgumentException(Md5.builder().setRounds(8193));
assertBuilderThrowsIllegalArgumentException(Sha1.builder().setRounds(0));
assertBuilderThrowsIllegalArgumentException(Sha1.builder().setRounds(8193));
assertBuilderThrowsIllegalArgumentException(Sha256.builder().setRounds(0));
assertBuilderThrowsIllegalArgumentException(Sha256.builder().setRounds(8193));
assertBuilderThrowsIllegalArgumentException(Sha512.builder().setRounds(0));
assertBuilderThrowsIllegalArgumentException(Sha512.builder().setRounds(8193));
assertBuilderThrowsIllegalArgumentException(PbkdfSha1.builder().setRounds(-1));
assertBuilderThrowsIllegalArgumentException(PbkdfSha1.builder().setRounds(120001));
assertBuilderThrowsIllegalArgumentException(Pbkdf2Sha256.builder().setRounds(-1));
assertBuilderThrowsIllegalArgumentException(Pbkdf2Sha256.builder().setRounds(120001));
}

@Test
public void testValidHashRounds() {
Md5.builder().setRounds(0).build();
Md5.builder().setRounds(8192).build();
Sha1.builder().setRounds(1).build();
Sha1.builder().setRounds(8192).build();
Sha256.builder().setRounds(1).build();
Sha256.builder().setRounds(8192).build();
Sha512.builder().setRounds(1).build();
Sha512.builder().setRounds(8192).build();
PbkdfSha1.builder().setRounds(0).build();
PbkdfSha1.builder().setRounds(120000).build();
Pbkdf2Sha256.builder().setRounds(0).build();
Pbkdf2Sha256.builder().setRounds(120000).build();
}

@Test
public void testBcryptHash() {
UserImportHash bcrypt = Bcrypt.getInstance();
Expand Down