[INS-397] Fix git version parser panic on non-numeric patch versions#4882
Merged
shahzadhaider1 merged 3 commits intoApr 10, 2026
Merged
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
camgunz
approved these changes
Apr 10, 2026
MuneebUllahKhan222
pushed a commit
that referenced
this pull request
May 29, 2026
…4882) 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 #4801
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
Medium Risk
Touches git availability/version gating across multiple sources and a detector; failures here can block scans if parsing or requirements are wrong. Change is small and covered by focused unit tests for version parsing edge cases.
Overview
Fixes a crash when validating the local
gitbinary version by introducingpkg/gitcmd.CheckVersion()with a safer parser that extracts only major/minor (handling outputs like2.52.gaea8cc3and platform suffixes) and returns explicit errors instead of panicking.Removes duplicated git version-check implementations (including the one in the Azure APIM repository-key detector and
pkg/sources/git/cmd_check.go) and updates Git, GitHub, GitHub Experimental, GitLab, and HuggingFace sources to call the sharedgitcmdhelper; adds unit tests for the new parsing behavior.Reviewed by Cursor Bugbot for commit 9bf18a1. Bugbot is set up for automated code reviews on this repo. Configure here.