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

Support reading .gitleaksignore using git show #1249

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

savely-krasovsky
Copy link
Contributor

@savely-krasovsky savely-krasovsky commented Aug 11, 2023

Description:

Gitleaks is a cool utility to use as pre-receive hook. It works nice out of box (--log-opts option is very handy in this case!), but if developer wants to push some false-positive anyway, there is no way to do it currently. Because Gitleaks trying to read .gitleaksignore only from current work tree.

My patch also allows to read .gitleaksignore using git show ... command. Now developer can push both commit with false-positive secret and .gitleaksignore fix and Giteleaks will omit the problem.

Also it introduces new option (only for detect command): --gitleaks-ignore-rev. By default it will point to HEAD, but in case of pre-receive hooks we could not have HEAD or any other branch yet (totally new, empty repo, or new branch). This option will allow to pass it:

#!/bin/bash

zero_commit='0000000000000000000000000000000000000000'

while read -r oldrev newrev refname; do
  echo "$oldrev $newrev"
  branch=${refname/#refs\/heads\/}

  # Branch or tag got deleted, ignore the push
  if [[ $newrev = "$zero_commit" ]]; then
    continue
  fi

  tmpfile=$(mktemp --suffix gitleaks)
  cmd="gitleaks detect --verbose --gitleaks-ignore-rev=$newrev --log-opts"

  # Scan only pushed commits
  if [[ $oldrev = "$zero_commit" ]]; then
    if [[ $GL_PROTOCOL = "web" ]]; then
      $cmd "$newrev --not --all" --no-color --redact |& sed 's/^/GL-HOOK-ERR: /'
    else
      $cmd "$newrev --not --all"
    fi
  else
    if [[ $GL_PROTOCOL = "web" ]]; then
      $cmd "$oldrev..$newrev --not --all" --no-color --redact |& sed 's/^/GL-HOOK-ERR: /'
    else
      $cmd "$oldrev..$newrev --not --all"
    fi
  fi
fi

I didn't write any additions tests for now, so it's DRAFT. Want to hear some feedback.

Checklist:

  • Does your PR pass tests?
  • Have you written new tests for your changes?
  • Have you lint your code locally prior to submission?

@savely-krasovsky
Copy link
Contributor Author

@zricethezav ask humbly to review it 😃

@savely-krasovsky
Copy link
Contributor Author

I guess it's better to merge after #1250.

@savely-krasovsky
Copy link
Contributor Author

Will rework it accordingly.

@savely-krasovsky savely-krasovsky changed the title DRAFT: Support reading .gitleaksignore using git show Support reading .gitleaksignore using git show Aug 24, 2023
@savely-krasovsky
Copy link
Contributor Author

savely-krasovsky commented Aug 31, 2023

@zricethezav I've added tests for bare repository, commited .gitleaksignore file to small test repo in both main and foo branches, extracted small.git repo from small/dotGit and changed in config core.bare to true.

With this patch developers now can get an error from git push with false-positive, copy secret fingerprint, add it to .gitleaksignore, commit and push again. Gitleaks will see new .gitleaksignore using git show. Currently it tries to open it from FS, but remote repos don't have working tree.

#1260 is a nice addition to this MR.

@savely-krasovsky
Copy link
Contributor Author

@zricethezav humbly want to bring your attention here again :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant