gitleaks / gitleaks

Protect and discover secrets using Gitleaks 🔑

Home Page:https://gitleaks.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Off-by-one error in startColumn and endColumn calculation

leadpogrommer opened this issue · comments

Describe the bug
startColumn and endColumn are calculated differently on first and all subsequent lines of the file.

To Reproduce
Run
gitleaks detect -f sarif -r output.json -s some_dir --config example.toml --no-git
example.toml and some_dir/bugged.txt are attached

Expected behavior
Column numbers are the same in both findings.

Observed behavior
Column numbers in the first finding are different from numbers in second finding

Basic Info (please complete the following information):

  • OS: macOs
  • Gitleaks Version: master (26f34692fac6e9daec13c770421b4ed990d1c321)

Additional context
It seems that the problem is located in detect/location.go. On the first line prevNewLine (=0) points to the first character in the fragment, but on subsequent lines it points to character before line start, so \n from previous line is included in column number calculation.

example.toml:

[[rules]]
    id = "f"
    description = "f"
    regex = '''f'''
    tags = ["f"]

bugged.txt:

..f..
..f..

output.json

cc @zricethezav

this sounds like the cause of one of the problems reported in #1352