Skip to content

Fix git scan repository field using local file path instead of upstre…#4877

Open
jamesgol wants to merge 1 commit intotrufflesecurity:mainfrom
jamesgol:git_local
Open

Fix git scan repository field using local file path instead of upstre…#4877
jamesgol wants to merge 1 commit intotrufflesecurity:mainfrom
jamesgol:git_local

Conversation

@jamesgol
Copy link
Copy Markdown
Contributor

@jamesgol jamesgol commented Apr 8, 2026

Description:

Fixes the git scan repository metadata field containing a local file:// path instead of the actual upstream remote URL when scanning pre-cloned local repositories.

Problem: When running trufflehog git file:///path/to/local/clone, TruffleHog re-clones the repo to a temp directory. The clone's origin remote gets set to the file:// path, and GetSafeRemoteURL returns that local path as the Repository in JSON output — even when the original repo has a real remote (e.g. https://dev.azure.com/org/project/_git/repo).

Fix: Updated GetSafeRemoteURL in pkg/sources/git/git.go to detect when the resolved remote URL is a local path (file:// scheme or absolute filesystem path). When detected, it opens the original repository and resolves its upstream remote URL instead. This is bounded to a single level of indirection — if the resolved URL is also local, it falls back to the original behavior.

Added two helper functions:

  • localRepoPath — identifies whether a URL refers to a local repo
  • resolveUpstreamRemote — opens the local repo and returns its non-local remote URL

Tests: Added 7 test cases covering:

  • Local clone resolves to upstream remote
  • file:// clone resolves to upstream remote
  • Bare clone resolves to upstream remote
  • Repo with no remotes returns empty string
  • Local clone of repo with no upstream falls back to local path
  • Repo with a real remote returns it directly (no resolution needed)
  • Fallback to first available remote when preferred doesn't exist

Closes #4876

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

Note

Low Risk
Low risk: limited to git repository metadata resolution and adds guardrails to avoid recursive local-path resolution; main behavior change is the repository value reported for locally cloned repos.

Overview
Fixes git scan repository metadata when scanning pre-cloned/local file:// repos by teaching GetSafeRemoteURL to detect local-path remotes and resolve the original repo’s non-local upstream remote instead (with a single-level fallback to avoid recursion).

Adds localRepoPath/resolveUpstreamRemote helpers and a focused test suite covering local clones, file:// clones, bare clones, missing remotes, and remote fallback behavior.

Reviewed by Cursor Bugbot for commit 9d1996c. Bugbot is set up for automated code reviews on this repo. Configure here.

…am remote URL

When scanning local clones via file:// URI, GetSafeRemoteURL returned the clone's local origin rather than the original repo's upstreaam remote.  Resolve through local paths to find the real remote URL.
@jamesgol jamesgol requested a review from a team April 8, 2026 18:18
@jamesgol jamesgol requested a review from a team as a code owner April 8, 2026 18:18
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.

git scan repository field contains local file:// path instead of upstream remote

1 participant