Skip to content

Commit 9877642

Browse files
authored
Reject rounds=0 for SHA1 hashes (#292)
Port of firebase/firebase-admin-node#677
1 parent 3fa07c8 commit 9877642

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

auth/hash/hash.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package hash // import "firebase.google.com/go/auth/hash"
2020
import (
2121
"encoding/base64"
2222
"errors"
23+
"fmt"
2324

2425
"firebase.google.com/go/internal"
2526
)
@@ -147,7 +148,7 @@ func (h HMACSHA512) Config() (internal.HashConfig, error) {
147148

148149
// MD5 represents the MD5 hash algorithm.
149150
//
150-
// Rounds must be between 0 and 120000.
151+
// Rounds must be between 0 and 8192.
151152
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
152153
// for more details.
153154
type MD5 struct {
@@ -189,7 +190,7 @@ func (h PBKDFSHA1) Config() (internal.HashConfig, error) {
189190

190191
// SHA1 represents the SHA1 hash algorithm.
191192
//
192-
// Rounds must be between 0 and 120000.
193+
// Rounds must be between 1 and 8192.
193194
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
194195
// for more details.
195196
type SHA1 struct {
@@ -203,7 +204,7 @@ func (h SHA1) Config() (internal.HashConfig, error) {
203204

204205
// SHA256 represents the SHA256 hash algorithm.
205206
//
206-
// Rounds must be between 0 and 120000.
207+
// Rounds must be between 1 and 8192.
207208
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
208209
// for more details.
209210
type SHA256 struct {
@@ -217,7 +218,7 @@ func (h SHA256) Config() (internal.HashConfig, error) {
217218

218219
// SHA512 represents the SHA512 hash algorithm.
219220
//
220-
// Rounds must be between 0 and 120000.
221+
// Rounds must be between 1 and 8192.
221222
// Refer to https://firebase.google.com/docs/auth/admin/import-users#import_users_with_md5_sha_and_pbkdf_hashed_passwords
222223
// for more details.
223224
type SHA512 struct {
@@ -240,8 +241,17 @@ func hmacConfig(name string, key []byte) (internal.HashConfig, error) {
240241
}
241242

242243
func basicConfig(name string, rounds int) (internal.HashConfig, error) {
243-
if rounds < 0 || rounds > 120000 {
244-
return nil, errors.New("rounds must be between 0 and 120000")
244+
minRounds := 0
245+
maxRounds := 120000
246+
switch name {
247+
case "MD5":
248+
maxRounds = 8192
249+
case "SHA1", "SHA256", "SHA512":
250+
minRounds = 1
251+
maxRounds = 8192
252+
}
253+
if rounds < minRounds || maxRounds < rounds {
254+
return nil, fmt.Errorf("rounds must be between %d and %d", minRounds, maxRounds)
245255
}
246256
return internal.HashConfig{
247257
"hashAlgorithm": name,

auth/hash/hash_test.go

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,45 +95,87 @@ var validHashes = []struct {
9595
},
9696
},
9797
{
98-
alg: MD5{42},
98+
alg: MD5{0},
9999
want: internal.HashConfig{
100100
"hashAlgorithm": "MD5",
101-
"rounds": 42,
101+
"rounds": 0,
102102
},
103103
},
104104
{
105-
alg: SHA1{42},
105+
alg: MD5{8192},
106+
want: internal.HashConfig{
107+
"hashAlgorithm": "MD5",
108+
"rounds": 8192,
109+
},
110+
},
111+
{
112+
alg: SHA1{1},
106113
want: internal.HashConfig{
107114
"hashAlgorithm": "SHA1",
108-
"rounds": 42,
115+
"rounds": 1,
116+
},
117+
},
118+
{
119+
alg: SHA1{8192},
120+
want: internal.HashConfig{
121+
"hashAlgorithm": "SHA1",
122+
"rounds": 8192,
123+
},
124+
},
125+
{
126+
alg: SHA256{1},
127+
want: internal.HashConfig{
128+
"hashAlgorithm": "SHA256",
129+
"rounds": 1,
109130
},
110131
},
111132
{
112-
alg: SHA256{42},
133+
alg: SHA256{8192},
113134
want: internal.HashConfig{
114135
"hashAlgorithm": "SHA256",
115-
"rounds": 42,
136+
"rounds": 8192,
116137
},
117138
},
118139
{
119-
alg: SHA512{42},
140+
alg: SHA512{1},
120141
want: internal.HashConfig{
121142
"hashAlgorithm": "SHA512",
122-
"rounds": 42,
143+
"rounds": 1,
123144
},
124145
},
125146
{
126-
alg: PBKDFSHA1{42},
147+
alg: SHA512{8192},
148+
want: internal.HashConfig{
149+
"hashAlgorithm": "SHA512",
150+
"rounds": 8192,
151+
},
152+
},
153+
{
154+
alg: PBKDFSHA1{0},
127155
want: internal.HashConfig{
128156
"hashAlgorithm": "PBKDF_SHA1",
129-
"rounds": 42,
157+
"rounds": 0,
158+
},
159+
},
160+
{
161+
alg: PBKDFSHA1{120000},
162+
want: internal.HashConfig{
163+
"hashAlgorithm": "PBKDF_SHA1",
164+
"rounds": 120000,
165+
},
166+
},
167+
{
168+
alg: PBKDF2SHA256{0},
169+
want: internal.HashConfig{
170+
"hashAlgorithm": "PBKDF2_SHA256",
171+
"rounds": 0,
130172
},
131173
},
132174
{
133-
alg: PBKDF2SHA256{42},
175+
alg: PBKDF2SHA256{120000},
134176
want: internal.HashConfig{
135177
"hashAlgorithm": "PBKDF2_SHA256",
136-
"rounds": 42,
178+
"rounds": 120000,
137179
},
138180
},
139181
}
@@ -206,15 +248,15 @@ var invalidHashes = []struct {
206248
},
207249
{
208250
name: "SHA1: rounds too low",
209-
alg: SHA1{-1},
251+
alg: SHA1{0},
210252
},
211253
{
212254
name: "SHA256: rounds too low",
213-
alg: SHA256{-1},
255+
alg: SHA256{0},
214256
},
215257
{
216258
name: "SHA512: rounds too low",
217-
alg: SHA512{-1},
259+
alg: SHA512{0},
218260
},
219261
{
220262
name: "PBKDFSHA1: rounds too low",
@@ -226,19 +268,19 @@ var invalidHashes = []struct {
226268
},
227269
{
228270
name: "MD5: rounds too high",
229-
alg: MD5{120001},
271+
alg: MD5{8193},
230272
},
231273
{
232274
name: "SHA1: rounds too high",
233-
alg: SHA1{120001},
275+
alg: SHA1{8193},
234276
},
235277
{
236278
name: "SHA256: rounds too high",
237-
alg: SHA256{120001},
279+
alg: SHA256{8193},
238280
},
239281
{
240282
name: "SHA512: rounds too high",
241-
alg: SHA512{120001},
283+
alg: SHA512{8193},
242284
},
243285
{
244286
name: "PBKDFSHA1: rounds too high",

0 commit comments

Comments
 (0)