[INS-397] Fix git version parser panic on non-numeric patch versions#4882
Open
shahzadhaider1 wants to merge 1 commit intotrufflesecurity:mainfrom
Open
[INS-397] Fix git version parser panic on non-numeric patch versions#4882shahzadhaider1 wants to merge 1 commit intotrufflesecurity:mainfrom
shahzadhaider1 wants to merge 1 commit intotrufflesecurity:mainfrom
Conversation
git built from source can report versions like "2.52.gaea8cc3", causing an index out of range panic. The patch component is unused, so the regex now captures only major.minor. Extract the helper into a shared pkg/gitcmd package to remove duplication with the azureapimanagement detector. Fixes trufflesecurity#4801
kashifkhan0771
approved these changes
Apr 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4801
The bug
CmdCheckpanicked withindex out of range [1] with length 1when running against a git binary built from source, because the version output uses a non-numeric patch component:The regex
\d+\.\d+\.\d+failed to match,FindStringreturned an empty string, andstrings.Split("", ".")produced a single-element slice, which then panicked onversionParts[1].The fix
Only the major and minor components are actually used for the version check, so the regex now captures just those:
(\d+)\.(\d+). Parsing is wrapped in aparseGitVersionhelper that returns an explicit error when the version can't be found, instead of relying on positional slice access.Covered by unit tests: standard semver, non-numeric patch (
2.52.gaea8cc3), Apple Git suffix, Windows Git suffix, missing patch, and malformed input.Why a new
pkg/gitcmdpackageWhile fixing this I noticed the same
gitCmdChecklogic was duplicated inpkg/detectors/azureapimanagement/repositorykey/repositorykey.gowith the identical bug, and had already started to drift (different regex library). Rather than patch both copies, I extracted the helper into a new top-levelpkg/gitcmdpackage exposing a singleCheckVersion()function.A few reasons this lives at the top level rather than under
pkg/sources/gitorpkg/common:pkg/sources/.... A neutral top-level package avoids that coupling entirely.pkg/sources/gitdirectly from the detector would have pulled in roughly 870 transitive internal packages for a ~20-line version check (the detector went from 12 internal deps to 13 withgitcmd; importingpkg/sources/gitwould have taken it to 882).pkg/gitparseandpkg/giturl"git helpers" packages. Trufflehog already has single-purpose packages at comparable or smaller size (pkg/version,pkg/feature,pkg/sanitizer).Changes
pkg/gitcmd/gitcmd.gowithCheckVersion()and aparseGitVersionhelper.pkg/gitcmd/gitcmd_test.gocovering the version-parsing cases above.gitCmdCheck(and its now-unused imports) from the azureapimanagement detector.pkg/sources/git/cmd_check.go.gitcmd.CheckVersion():pkg/sources/git/git.go(two sites)pkg/sources/gitlab/gitlab.gopkg/sources/github/github.gopkg/sources/github_experimental/github_experimental.gopkg/sources/huggingface/huggingface.gopkg/detectors/azureapimanagement/repositorykey/repositorykey.goChecklist:
make test-community)?make lintthis requires golangci-lint)?Note
Low Risk
Low risk: behavior is mostly a refactor plus more robust parsing/error handling for
git --version, with minimal surface-area change beyond early validation failures.Overview
Fixes a panic in git version validation by replacing the strict
x.y.zparser with a major/minor parser that gracefully handles outputs like2.52.gaea8cc3and returns a clear error when parsing fails.Extracts the duplicated git binary/version validation into a new
pkg/gitcmdCheckVersion()helper (with unit tests) and updates all previousCmdCheck/gitCmdCheckcall sites across sources and the Azure APIM detector to use it, deleting the old implementations.Reviewed by Cursor Bugbot for commit f467908. Bugbot is set up for automated code reviews on this repo. Configure here.