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

libgit2: check hostkey type when validating hostkey #290

Merged
merged 4 commits into from
Feb 12, 2021
Merged

Conversation

phillebaba
Copy link
Member

This change adds checking for fingerprint types other than SHA1 when checking the hostkey. This is a bug which has not been an issue yet as very few git servers use MD5 fingerprints and Azure DevOps still uses SHA1 fingerprints. It could potentially be an issue when using the libgit2 implementation with GitHub as it uses SHA256 fingerprints.

@hiddeco hiddeco added area/git Git related issues and pull requests bug Something isn't working labels Feb 11, 2021
@hiddeco hiddeco changed the title Check hostkey type when validating hostkey libgit2: check hostkey type when validating hostkey Feb 11, 2021
Copy link
Member

@hiddeco hiddeco left a comment

Choose a reason for hiding this comment

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

This looks OK to me, but I am linking in @squaremo as a second pair of eyes given I have not been on my best on this topic the past days...

Thank you @phillebaba 🙏

@phillebaba
Copy link
Member Author

@hiddeco is there a test that verifies libigit2 cloning over ssh? I can manually verify this if it needs to get merged tomorrow, but I will try to get a test in tonight.

@hiddeco
Copy link
Member

hiddeco commented Feb 11, 2021

@phillebaba the image controllers cover this indirectly, and confirmed my changes in #288 worked. I do however think that the matches method can be unit tested here, as we can simply mock the structure and arguments.

Signed-off-by: Philip Laine <philip.laine@gmail.com>
if bytes.Compare(hash[:], key) != 0 {
var fingerprint []byte
var hasher hash.Hash
switch hostkey.Kind {
Copy link
Member

Choose a reason for hiding this comment

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

hostkey.Kind is a bitset, so you want something like

switch {
    case hostkey.Kind & git2go.HostkeySHA256 > 0:
        ...
}

i.e., check the bitset against each enum value, starting with the most preferred (SHA256) down to the lowly MD5.

(if you print hostkey.Kind out, it'll usually have the value 7, which is HostkeyMD5 | HostkeySHA1 | HostkeySHA256 (the enum values are in https://github.com/libgit2/libgit2/blob/27e34f9b9843f7bcc33a4ccfe3e395fe303cba63/include/git2/cert.h#L76)

Signed-off-by: Philip Laine <philip.laine@gmail.com>
@squaremo
Copy link
Member

squaremo commented Feb 12, 2021

My image-auto tests pass with this change, and running an automation against github.com with SSH/libgit2 also works.

@phillebaba
Copy link
Member Author

I am just going to add a test to verify priority and then we can merge this.

Signed-off-by: Philip Laine <philip.laine@gmail.com>
Signed-off-by: Philip Laine <philip.laine@gmail.com>
Copy link
Member

@hiddeco hiddeco left a comment

Choose a reason for hiding this comment

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

Given it has now test coverage on top of the tests performed by Michael, and the unit tests for the commit that added them passed, I am confident this works.

Thank you for following up @phillebaba 🥇

@phillebaba phillebaba merged commit d1ee618 into main Feb 12, 2021
@phillebaba phillebaba deleted the fix/hash-type branch February 12, 2021 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/git Git related issues and pull requests bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants