Skip to content

Integrity check #1068

Closed
Closed

Description

@SkypLabs

Hello,

First of all, thank you for this project. I really appreciate using it.

This issue is a feature request to add an integrity check for security reasons. In fact, even if there is the possibility to specify a Git tag to download a particular version of a pre-commit hook, it does not guaranty that the tag will point to the same commit between two downloads. Knowing that a Git hook is an executable, and that a Git tag can be modified to point to another commit, it becomes impossible to trust the hooks downloaded by pre-commit without reviewing them each time they are installed, which is a security risk. An attacker getting write access to a Git repository containing pre-commit hooks could add a harmful payload to them and make the latest tag point to this new malicious version for example.

To remediate this situation, I would suggest to add the commit ID as a new repos property to the configuration file, at the same imbrication-level as rev. This way, pre-commit will be able to verify that the Git tag specified by rev points to the expected Git commit.

I look forward to getting your feedback on it.

Cheers.

Activity

  1. asottile commented on Jun 28, 2019

    @asottile
    Member

    rev doesn't have to point to a tag, you can already use a revision for that.

  2. asottile commented on Jun 28, 2019

    @asottile
    Member

    there was consideration to make a "lock" file at some point but there has been no proposal or spec put forth. I can't find the original issue from a quick search -- I don't want to put it in the base configuration as it is highly prone to merge conflicts

  3. SkypLabs commented on Jun 29, 2019

    @SkypLabs
    Author

    rev doesn't have to point to a tag, you can already use a revision for that.

    Great! Because all the pre-commit config files I have read so far used Git tags, I thought it was the only way to specify a version. It makes sense to me now that rev has been chosen for the name of this parameter, and not tag.

    Is it also possible to specify a branch name? I think it would be worth adding a few words in the documentation to indicate what values this parameter can take, and why not a small paragraph about the security implications.

  4. asottile commented on Jun 29, 2019

    @asottile
    Member

    It used to be called sha but humans don't like reading shas and very much can't reason about them -- it can be a branch but the docs discourage this. I do think you're blowing security a bit out of proportion here, it's not any different than cloning a git repository from the internet or much different installing a package from say npm -- precisely why this feature hasn't been prioritized.

    The value is meant to be immutable, but the framework currently doesn't care whether it actually is (you can put branches there or mutable tags all the same)

  5. SkypLabs commented on Jun 30, 2019

    @SkypLabs
    Author

    it can be a branch but the docs discourage this.

    I completely agree that it should be discouraged. My point was that all the possible values should be clearly documented, preferably directly in the table where the different parameters are listed.

    I do think you're blowing security a bit out of proportion here, it's not any different than cloning a git repository from the internet or much different installing a package from say npm -- precisely why this feature hasn't been prioritized.

    Well, I agree that it's not a good thing to blindly use source code or install npm packages, specially knowing that those packages are not signed. However, it's not because people do it that it should be encouraged. Security issues occur regularly because of compromised dependencies.

    The best way to know if a software is safe to be used is to read its source code. However, it is extremely time-consuming and not in everyone's reach. At some point, we need to trust someone else, and it makes generally sense to trust the developers of the project, hence the signing mechanism. That's why the only two ways I see to get a good balance between convenience and safety are either to use immutable references (commit IDs) after having read the source code of the pre-commit hooks, or to ask pre-commit to verify the signatures of the remote references (Git tags, commits, or branches).

    I suppose the locking mechanism you mentioned is the way-to-go, in particular because most people don't sign their work. If you agree with me, I would be happy to work on the implementation as soon as I get a bit more of free time.

  6. asottile commented on Jun 30, 2019

    @asottile
    Member

    I'm not even sure a locking mechanism is necessary now that I think about it -- using rev: <40-char sha1> is essentially your lock.

  7. asottile commented on Jun 30, 2019

    @asottile
    Member

    Maybe a convenience command to turn a config into a locked config?

  8. SkypLabs commented on Jun 30, 2019

    @SkypLabs
    Author

    I'm not even sure a locking mechanism is necessary now that I think about it -- using rev: <40-char sha1> is essentially your lock.

    It's what I thought initially but using a locking mechanism would be a way to detect drifts if your targeted Git tag has been changed to point to another commit since the last time you used pre-commit install.

  9. asottile commented on Jun 30, 2019

    @asottile
    Member

    I'm not even sure a locking mechanism is necessary now that I think about it -- using rev: <40-char sha1> is essentially your lock.

    It's what I thought initially but using a locking mechanism would be a way to detect drifts if your targeted Git tag has been changed to point to another commit since the last time you used pre-commit install.

    🤷‍♂ at that point who cares though -- you're just installing from a sha

  10. SkypLabs commented on Jun 30, 2019

    @SkypLabs
    Author

    🤷‍♂️ at that point who cares though -- you're just installing from a sha

    I meant in the case where you use a Git tag in .pre-commit-config.yaml, and not a commit ID directly.

  11. asottile commented on Jun 30, 2019

    @asottile
    Member

    Right so it would convert that to a sha1

  12. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    Using a sha for the rev does work, the problem is that it breaks the autoupdate functionality.

    I would propose supporting both rev and sha at the same time. Or instead of sha maybe lock?

    pre-commit install-hooks should verify that the two point to the same place.
    pre-commit autoupdate should update based on the rev and place the new sha there as well at the time of the update. In the future if these two deviate, it'll be detected before the malicious code is ran.

    If the user is using autoupdate, then they'll never have to touch the sha themselves.

    Example

    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v2.4.0
        sha: 0161422b4e09b47536ea13f49e786eb3616fe0d7
        hooks:
        -   id: trailing-whitespace

    Nearly every other package manager or dependency manager is now supporting checksums in the form of lock files. This project should do the same.

  13. asottile commented on Dec 3, 2019

    @asottile
    Member

    autoupdate works just fine, have you tried --bleeding-edge?

  14. 1 remaining item

  15. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    @asottile running autoupdate replaces the sha with a tag. I'm asking that it replace the old tag with a new one and the old sha with a new one.

    $ pre-commit autoupdate
    Updating https://github.com/pre-commit/pre-commit-hooks...updating 0161422b4e09b47536ea13f49e786eb3616fe0d7 -> v2.4.0.
    Updating https://github.com/psf/black...updating 6bedb5c58a7d8c25aa9509f8217bc24e9797e90d -> 19.10b0.
    

    before

    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: 0161422b4e09b47536ea13f49e786eb3616fe0d7  # v2.4.0
        hooks:
        -   id: trailing-whitespace
        -   id: end-of-file-fixer
        -   id: check-yaml
        -   id: check-added-large-files
    -   repo: https://github.com/psf/black
        rev: 6bedb5c58a7d8c25aa9509f8217bc24e9797e90d  # 19.10b0
        hooks:
        -   id: black

    after

    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v2.4.0  # v2.4.0
        hooks:
        -   id: trailing-whitespace
        -   id: end-of-file-fixer
        -   id: check-yaml
        -   id: check-added-large-files
    -   repo: https://github.com/psf/black
        rev: 19.10b0  # 19.10b0
        hooks:
        -   id: black
  16. asottile commented on Dec 3, 2019

    @asottile
    Member

    I don't like repeating myself, but have you tried --bleeding-edge? you seem to be talking past me which I do not appreciate

  17. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    I have looked at --bleeding-edge. This is not the behavior I would want. I'd do want to use a tag, for instance v2.4.0. I also want to be guaranteed that the code doesn't change out from under me if I clone and initialize the repo yesterday vs today.

  18. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    As for the merge conflict issue, I don't see how having a sha along for the ride changes anything. You'd have a merge conflict on the sha in any scenario where you'd also have a a merge conflict on a rev

  19. asottile commented on Dec 3, 2019

    @asottile
    Member

    the ergonomics of a merge conflict are pretty important to me and I've seen them sour relationships with tools in the past, consider trying to resolve the following conflicts:

     -   repo: https://github.com/pre-commit/pre-commit-hooks
    -    rev: v2.3.0
    +    rev: v2.4.0
         hooks:
     -   repo: https://github.com/pre-commit/pre-commit-hooks
    -    rev: 033b22aac37efd4e967f9896e2de0fb16a40ff9b
    +    rev: a67a754d35ceac1407ab177d21995f4b82f97b28
         hooks:

    If you're paranoid (it seems you are) I'd suggest converting the shas manually until a reasonable proposal / implementation exists (as documented above)

  20. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    @asottile I never suggested using a sha in place of a tag. Please re-read my posts. I even included examples. I'm suggesting to use them together at the same time.

    This should not be a problem.

    This config file won't be updated as often as other code. When it is modified, the majority of the time there will not be a merge conflict. This is because typically multiple people will not edit it at the same time. If they happen to, then you'd have to deal with it like any other merge conflict and not mix and match. Either take the rev and sha from one commit or the other. Just don't mix and match. This is what the diff would look like.

     -   repo: https://github.com/pre-commit/pre-commit-hooks
    -    rev: v2.3.0
    -    rev: 033b22aac37efd4e967f9896e2de0fb16a40ff9b
    +    rev: v2.4.0
    +    rev: a67a754d35ceac1407ab177d21995f4b82f97b28
         hooks:

    As an example, look at either...

    ... and find in the history when there has been a merge conflict involving those files.

  21. asottile commented on Dec 3, 2019

    @asottile
    Member

    that's a breaking config change, it would need a new field and I don't think there's any advantage over just putting a sha1 in the rev field

  22. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    If you use a sha, then you're not using a tag with all of its advantages.

    • human readable semantic versions (for humans dealing with merge requests)
    • computer readable semantic versions (for autoupdate to work)

    Likewise, if you use a tag, then you're not using a sha with all of its advantages.

    • integrity

    So, the advantage of having both as opposed to "just putting a sha1 in the rev field" is that you don't give up other advantages.

  23. asottile commented on Dec 3, 2019

    @asottile
    Member

    sha1 in the rev field plus a comment is equivalent without needing special fields - it wouldn't be too difficult to make autoupdate spit out that format

    I just don't see the value in having two fields given the tag would be purely informational and the rev field already supports both (and the sha field is misnamed, deprecated, and forbidden when rev is present currently)

  24. ericfrederich commented on Dec 3, 2019

    @ericfrederich

    @asottile so having rev with a sha value and a tag comment? That is what I'm currently doing but it's a manual process. See previous comment showing how autoupdate currently handles this.

    I always feel dirty putting data into comments. If the tool were able to update the comment too, that'd be better.

  25. asottile commented on Dec 3, 2019

    @asottile
    Member

    yes, if that works for you we can make it happen

  26. asottile commented on Jan 2, 2020

    @asottile
    Member

    This has been released as part of v1.21.0 -- thanks again for the issue 🎉!

  27. SkypLabs commented on Jan 3, 2020

    @SkypLabs
    Author

    Great! Thanks for the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions