Skip to content
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

fix: try to decode hex encoded md5 hash during pw migration #4287

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 14 additions & 1 deletion hash/hash_comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,20 @@
switch len(parts) {
case 3:
hash, err := base64.StdEncoding.Strict().DecodeString(parts[2])
return nil, nil, hash, err
if err != nil {
return nil, nil, nil, err

Check warning on line 652 in hash/hash_comparator.go

View check run for this annotation

Codecov / codecov/patch

hash/hash_comparator.go#L652

Added line #L652 was not covered by tests
}

// PHP's md5 function returns a hex encoded string instead of a raw hash by default (https://www.php.net/manual/en/function.md5.php).
passedHash, err := hex.DecodeString(string(hash))
if err != nil {
// If the hash is not a hex encoded string.
// In this case we just return the base64 decoded hash.
return nil, nil, hash, nil
}

// The hash is a hex encoded string, so we return the decoded hash.
return nil, nil, passedHash, nil
case 5:
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if this needs to be done in the salted version of this hash as well. I am not familiar with that format at all, and not sure what applications would generate this form.

_, err = fmt.Sscanf(parts[2], "pf=%s", &pf)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions hash/hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ func TestCompare(t *testing.T) {
assert.Nil(t, hash.CompareMD5(context.Background(), []byte("ory"), []byte("$md5$ptoWyof5SobW+pbZu2QXoQ==")))
assert.Error(t, hash.Compare(context.Background(), []byte("ory"), []byte("$md5$4skj967KRHFsnPFoL5dMMw==")))

assert.Nil(t, hash.Compare(context.Background(), []byte("test"), []byte("$md5$MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=")))
assert.Nil(t, hash.CompareMD5(context.Background(), []byte("test"), []byte("$md5$MDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjY=")))

assert.ErrorIs(t, hash.Compare(context.Background(), []byte("ory"), []byte("$md5$$")), hash.ErrInvalidHash)
assert.Error(t, hash.Compare(context.Background(), []byte("ory"), []byte("$md5$$$")))
assert.Error(t, hash.Compare(context.Background(), []byte("ory"), []byte("$md5$pf=$$")))
Expand Down
Loading