Commit Graph
191 Commits
Author SHA1 Message Date
Amaan Ullah 586f66d788 use struct-based SourceMetadataFunc signature across git sources (#4813)
* use struct-based SourceMetadataFunc signature across git sources

* incorporated feedback

- pass SourceMetadataInfo by value
- remove LegacySourceMetadataFunc
2026-03-24 16:22:30 +05:00
Cody Rose 041f07e9df Move verify flag into detectableChunk (#4558)
Chunk.Verify is an odd field - it originally conveys whether a source is going to run with verification, but then, at a certain point in the scanning pipeline, is mutated such that it instead indicates whether the chunk should be scanned with verification - which is not solely dependent on the source's verify flag. This is unnecessarily difficult to understand and maintain. This commit separates those two pieces of information into two flags:

- Chunk.Verify has been renamed to Chunk.SourceVerify
- It is no longer mutated; instead "should this chunk's secrets be verified?" is now captured by a new field on detectableChunk
2026-02-27 10:05:52 -05:00
Rusted2361andKashif Khan f946748dd9 Fix/issue 4578 path normalization for unix and windows (#4614)
* Fix Windows file:// URI normalization and index path handling

* add condition to trim leading slash only incase of windows paths

* access path from URL object and identify platform using goos

* simplified path logic

* added test cases for windows powershell & bash

---------

Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
2026-01-19 18:37:35 +05:00
Cody Rose bf930a2cd9 fix typo (#4599)
I misspelled an identifier in one spot and then just autocompleted to victory everywhere else. This commit fixes the typo!
2025-12-10 15:16:39 -05:00
Cody Rose 702bbefa35 Add optional git clone timeout (#4597)
We have identified some cases in which it is preferable to time a clone out instead of waiting forever. These situations are unusual, so the CLI option to enable this (which I added for testing) is hidden so that we minimize the risk of baking this option into the interface.
2025-12-10 10:09:05 -05:00
Cody Rose f1f48726bb Add more clone logging (#4593)
This commit adds timing information to the logs when clones complete successfully. There are several other more robust but also more involved interventions we could do, and I think we should do them, but this is a strict improvement that's easy to get out as a first step.

I also added logging at the precise beginning of the clone command so we know when we're doing that.
2025-12-09 09:42:32 -05:00
Cody Rose 1a83f6bf83 skip flaky test (#4595)
This test has been super flaky, so we're disabling it until we can fix it.
2025-12-09 09:13:49 -05:00
meredith 7ba7a0005d Fix "skipping binary file" logging to show actual file name (#4509)
This was intended to show path:fileName, but `path` in these scopes is
the repo path, not a file.  This section was moved in a refactor where
`path` was the file in the old scope, and since both scopes have `path
string`, it was not flagged and easy to miss.
2025-10-16 12:38:08 -05:00
joeleonjr bc2cd3e45e Local Git Config Sanitization (#4502)
This commit prevents TruffleHog from executing arbitrary commands located in archived malicious git repositories. Thanks to Adam Reiser at Cisco Talos for pointing this out!

This approach uses Git's recommended best practice for sanitizing untrusted git configs: git clone all local file:// git repos prior to scanning. Executing git clone does not execute any of the potentially malicious git configs in the untrusted repo directory, and the output only includes "safe" default configs, similar to what we see when cloning from remote.

We explored a few other approaches (allowlist, denylist, etc), but those carried lots of complications.

A few notes about how this works:

This only applies to local repositories scanned using the git subcommand.
Remote git targets are not impacted.
Local git targets are now cloned to temp by default prior to scanning. Users can specify a --clone-path argument if they don't want to use the default temp dir. Users can specify --trust-local-git-config if they want to trust the repo as is and bypass cloning.
Local --bare repos are handled appropriately.
This approach knocks out (most...all?) of this class of malicious git config vulnerabilities.
Testing:
There's coverage for most of the new code, including: test cases for the specific issue reported, local bare clones, local repos with staged commits, etc. All are passing.
2025-10-15 14:50:44 -04:00
Shahzad HaiderandKashif Khan d005847359 set auth-in-url flag to true when using ssh clone (#4488)
Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
2025-10-08 17:41:34 +05:00
Shahzad Haider 2114e77d56 Fix legacy json flag for Github and Gitlab private repos (#4386)
* fix legacy json flag for gitlab private repos

* some code

* incorporated code from main

* remove cloned repositories after the scan is complete

* enhanced the code

* comment addressed

* changed the approach for persisting the repositories for legacy json printing

* addressed comments; updated the variable names
2025-09-03 11:19:28 +05:00
Kashif Khan 07c16636eb Enable cloning repository to a specified location with retention option (#4408)
* Enabled cloning repositories to a specified path with retention option

* Fixes after testing

* resolved lint issue

* resolved comments

* enabled clone path for github basic auth
2025-08-20 18:10:02 +05:00
ad1ddc133a (fix) Git Repo Cloning Error (#4223)
* instead of providing list of refs we want to scan, using --mirror parameter .
removed SkipAdditionalRefs flag, cloning using --mirror will include all the refs.
put the check if userinfo is nil to avoid any crash due to invalid memory access.

* keep both -c and --mirror

* keep useGitMirror on by default for OSS

---------

Co-authored-by: Shahzad Haider <76992801+shahzadhaider1@users.noreply.github.com>
Co-authored-by: Dustin Decker <dustin@trufflesec.com>
Co-authored-by: ahrav <ahravdutta02@gmail.com>
2025-07-25 10:30:30 -07:00
ahravandKashif Khan d0b1fbadfe [perf] - Optimize Chunk Size Handling for Improved I/O and Memory Efficiency (#3134)
* optimize chunking

* use file size for Docker

* refactor: update chunk size reference in Jenkins test case

- Changed the reference from sources.ChunkSize to sources.DefaultChunkSize for clarity and consistency in the TestJenkinsVariousSizes function.
- Reformatted struct field declarations for improved readability.

---------

Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
2025-07-08 12:37:21 -07:00
Shahzad Haider d7e0ebb11e treat missing directory as fatal error in TestChunkUnit (#4263) 2025-06-25 19:08:05 +05:00
Sahil SilareandKashif Khan bda0787e98 fix: throws error if dir not found (#3419)
* [detector] feat: added rootly detector

* feat: throws error when directory is not found

* Revert "[detector] feat: added rootly detector"

This reverts commit 1fcff0a807.

* feat: removed unused code

Signed-off-by: Sahil Silare <sahilsilare@gmail.com>

* refactor: fixed formatting

Signed-off-by: Sahil Silare <sahilsilare@gmail.com>

* fix: removed accidental code addition

Signed-off-by: Sahil Silare <sahilsilare@gmail.com>

---------

Signed-off-by: Sahil Silare <sahilsilare@gmail.com>
Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
2025-06-24 20:51:39 +05:00
Dustin Decker 00b3060867 Add git metrics for cloning and scanning (#4234) 2025-06-16 09:21:41 -07:00
Kashif Khan 92e9157267 Github and Gitlab Authentication via http.extraHeader for cloning Repositories (#4139)
* initial code changes

* move args before clone command

* add comments

* added flag for github source to support backward compatibility

* added flag for gitlab source to support backward compatibility

* inverse the logic for enterprise

* remove print statement

* remove flag defaults

* updated comments and removed CloneRepoUsingTokenInHeader function

* false->true
2025-05-27 11:24:43 +05:00
Charlie Gunyon b5e657fca6 Polish logging around git and archives to reduce noise (#4034)
* Polish logging around archives in git to reduce noise

* Log notable skipping at level 2

* Skipping based on extension happens a lot; bump the log level down to 3

* Bump empty reader log level back down to 5

* Put context check back where it was

* Revert "Put context check back where it was"

This reverts commit da710ccd16.
2025-04-22 12:40:04 +02:00
Cody Rose 160f1a432d Export git.handleBinary and getSafeRemoteURL (#3921)
This PR exports some functionality from the git source so that it's usable in a (new, under development) git-flavored source that does not need to wrap an entire git source in order to operate.

One is getSafeRemoteURL. This is a straightforward change.

The other is handleBinary. Right now, all git binary file handling is invoked from Git.ScanRepo, which is itself invoked by our various git-flavored sources.

Since the actual binary file handling function handleBinary only used two pieces of information from the "git" source, I just removed its receiver. One of the pieces of information was a flag that caused the function to be skipped entirely; I moved this one to the (two) call sites. The other I just forwarded as an argument.
2025-02-19 16:20:54 -05:00
ahrav e2f5e0e4bc upgrade Github dep (#3699) 2024-12-02 12:34:47 -08:00
Oleksandr Redko c38064d9f0 chore: fix typos in comments and tests; enable misspell (#3573) 2024-11-16 15:10:12 -08:00
ahrav bf38b8480c [fix] - resource leak (#3402)
* fix resource leak

* add comment

* use errors.Join

* address error wrapping
2024-10-15 12:11:45 -07:00
Richard Gomez 34e443adcf feat: propagate file info in log context (#3405) 2024-10-14 17:13:39 -07:00
ahrav e57c712998 Manually upgrade github dep (#3387) 2024-10-10 06:16:40 -07:00
Dustin DeckerandAhrav Dutta 59c615a5e9 Fix git binary handling and add a smoke test (#3379)
* Fix git binary handling and add a smoke test

* hide stdout

* add failure case to smoke test

* run again with deadlock fix

* Add logic to drain reader in the event of an error

* add tests

* be picky

* set author identity

* suppress linter

---------

Co-authored-by: Ahrav Dutta <ahrav.dutta@trufflesec.com>
2024-10-07 13:55:07 -07:00
Dustin Decker f3630da1e0 Improve process cleanup (#3339)
* ensures that cmd.Wait() is always called, even if there's a panic in the FromReader function or if stdOut.Close() returns an error

* close stdout and ensure wait is called when handling binaries

* process cleanup improvements

* lint
2024-09-26 10:17:47 -07:00
ahrav 26ae7cb09d [bug] - Improve seekability check for stdout pipes in BufferedReadSeeker (#3189)
* fix bug

* update

* clarify comment

* cleanup

* fix test

* update comment

* remove code for large files

* address comments

* update
2024-09-23 16:55:26 -07:00
Dustin Decker 70c6bb5634 feature flag additional refs (#3282) 2024-09-10 15:51:41 -07:00
Valentin B. 075f8c67a5 fix(git): config normalization for git sources (#3278)
When normalizing the git source config, the base and head refs should be normalized to commit hashes, in case
a branch or tag name was used. The `resolveAndSetCommit` function was returning a boolean value which should
indicate whether the input ref was changed from its original value. While this is in itself not a problem,
the caller (`normalizeConfig`) was using this boolean as an error marker, and returning early in case of `false`.

This meant that if the config was already containing a commit hash for the base or head ref, `resolveAndSetCommit`
would set the flag to `false` and `normalizeConfig` would early return erreneously. This caused the logic to find
the ancestor commit to be skipped which caused the bug in the issue #3220.

Since the `resolveAndSetCommit` function was only used in `normalizeConfig`, the signature has been changed to only
return the commit object and an error. The check for early return in `normalizeConfig` now instead relies on the
commit object being `nil` to indicate a failure to resolve the ref.

Refs: #3220
2024-09-10 12:12:57 -05:00
Dustin Decker 8999eab89d Add central feature flags (#3264)
* Add central feature flags

* use atomic

* tidy
2024-09-03 15:54:41 -07:00
ahrav 55fe05d0b4 fix dep versions (#3106) 2024-07-26 17:44:23 -07:00
ahrav f865482025 [feat] - Streamlined File Handling with BufferedReaderSeeker (#3041)
* Streaming file handling.

* cleanup

* update tests

* lint

* defer close on input io.ReadCloser's

* fix seek bug

* fix hanging

* clarify errors

* update

* address comments

* revert

* update

* address

* add check to prevent seek without buffering

* revet

* revert

* update comment to make buffer usage more clear
2024-07-17 13:52:18 -07:00
Richard Gomez 3c20b000e1 fix(git): set GIT_DIR based on ScanOptions.Bare (#3004) 2024-06-24 07:37:45 -07:00
Zachary Rice d5b9157d2b clone more refs (#2988) 2024-06-20 09:40:03 -05:00
Richard Gomez 40fa304a3a feat(git): improve scan logging (#2923) 2024-06-06 05:12:59 -04:00
Richard Gomez 4d2c8c6e11 refactor(github): improve wiki err handling (#2917) 2024-06-05 08:06:01 -04:00
Miccah c86b423c61 [chore] Always log git repositories being scanned (#2909) 2024-06-03 18:02:34 -07:00
ahrav 896e6e7c66 upgrade github dep (#2858) 2024-05-16 14:35:08 -07:00
ahrav ead9dd5748 [refactor] - Create separate handler for non-archive data (#2825)
* Remove specialized handler and archive struct and restructure handlers pkg.

* Refactor RPM archive handlers to use a library instead of shelling out

* make rpm handling context aware

* update test

* Refactor AR/deb archive handler to use an existing library instead of shelling out

* Update tests

* Handle non-archive data within the DefaultHandler

* make structs and methods private

* Remove non-archive data handling within sources

* add max size check

* add filename and size to context kvp

* move skip file check and is binary check before opening file

* fix test

* preserve existing funcitonality of not handling non-archive files in HandleFile

* Handle non-archive data within the DefaultHandler

* rebase

* Remove non-archive data handling within sources

* Adjust check for rpm/deb archive type

* add additional deb mime type

* add gzip

* move diskbuffered rereader setup into handler pkg

* remove DiskBuffereReader creation logic within sources

* update comment

* move rewind closer

* reduce log verbosity

* add metrics for file handling

* add metrics for errors

* make defaultBufferSize a const

* add metrics for file handling

* add metrics for errors

* fix tests

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* Address incompatible reader to openArchive

* remove nil check

* fix err assignment

* Allow git cat-file blob to complete before trying to handle the file

* wrap compReader with DiskbufferReader

* Allow git cat-file blob to complete before trying to handle the file

* updates

* use buffer writer

* update

* refactor

* update context pkg

* revert stuff

* update test

* fix test

* remove

* use correct reader

* add metrics for file handling

* add metrics for errors

* fix tests

* rebase

* add metrics for errors

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* fix err assignment

* rebase

* remove

* Update write method in contentWriter interface

* Add bufferReadSeekCloser

* update name

* update comment

* fix lint

* Remove specialized handler and archive struct and restructure handlers pkg.

* Refactor RPM archive handlers to use a library instead of shelling out

* make rpm handling context aware

* update test

* Refactor AR/deb archive handler to use an existing library instead of shelling out

* Update tests

* add max size check

* add filename and size to context kvp

* move skip file check and is binary check before opening file

* fix test

* preserve existing funcitonality of not handling non-archive files in HandleFile

* Handle non-archive data within the DefaultHandler

* rebase

* Remove non-archive data handling within sources

* Handle non-archive data within the DefaultHandler

* add gzip

* move diskbuffered rereader setup into handler pkg

* remove DiskBuffereReader creation logic within sources

* update comment

* move rewind closer

* reduce log verbosity

* make defaultBufferSize a const

* add metrics for file handling

* add metrics for errors

* fix tests

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* Address incompatible reader to openArchive

* remove nil check

* fix err assignment

* wrap compReader with DiskbufferReader

* Allow git cat-file blob to complete before trying to handle the file

* updates

* use buffer writer

* update

* refactor

* update context pkg

* revert stuff

* update test

* remove

* rebase

* go mod tidy

* lint check

* update metric to ms

* update metric

* update comments

* dont use ptr

* update

* fix

* Remove specialized handler and archive struct and restructure handlers pkg.

* Refactor RPM archive handlers to use a library instead of shelling out

* make rpm handling context aware

* update test

* Refactor AR/deb archive handler to use an existing library instead of shelling out

* Update tests

* add max size check

* add filename and size to context kvp

* move skip file check and is binary check before opening file

* fix test

* preserve existing funcitonality of not handling non-archive files in HandleFile

* Adjust check for rpm/deb archive type

* add additional deb mime type

* update comment

* go mod tidy

* update go mod

* Add a buffered file reader

* update comments

* use Buffered File Readder

* return buffer

* update

* fix

* return

* go mod tidy

* merge

* use a shared pool

* use sync.Once

* reorganzie

* remove unused code

* fix double init

* fix stuff

* nil check

* reduce allocations

* updates

* update metrics

* updates

* reset buffer instead of putting it back

* skip binaries

* skip

* concurrently process diffs

* close chan

* concurrently enumerate orgs

* increase workers

* ignore pbix and vsdx files

* add metrics for gitparse's Diffchan

* fix metric

* update metrics

* update

* fix checks

* fix

* inc

* update

* reduce

* Create workers to handle binary files

* modify workers

* updates

* add check

* delete code

* use custom reader

* rename struct

* add nonarchive handler

* fix break

* add comments

* add tests

* refactor

* remove log

* do not scan rpm links

* simplify

* rename var

* rename

* fix benchmark

* add buffer

* buffer

* buffer

* handle panic

* merge main

* merge main

* add recover

* revert stuff

* revert

* revert to using reader

* fixes

* remove

* update

* fixes

* linter

* fix test

* fix comment

* update field name

* fix
2024-05-15 13:40:16 -07:00
ahrav 570cec7565 [refactor] - Refactor Archive Handling Logic (#2703)
* Remove specialized handler and archive struct and restructure handlers pkg.

* Refactor RPM archive handlers to use a library instead of shelling out

* make rpm handling context aware

* update test

* Refactor AR/deb archive handler to use an existing library instead of shelling out

* Update tests

* add max size check

* add filename and size to context kvp

* move skip file check and is binary check before opening file

* fix test

* preserve existing funcitonality of not handling non-archive files in HandleFile

* Adjust check for rpm/deb archive type

* add additional deb mime type

* update comment

* Remove specialized handler and archive struct and restructure handlers pkg.

* Refactor RPM archive handlers to use a library instead of shelling out

* make rpm handling context aware

* update test

* Refactor AR/deb archive handler to use an existing library instead of shelling out

* Update tests

* add max size check

* add filename and size to context kvp

* move skip file check and is binary check before opening file

* fix test

* preserve existing funcitonality of not handling non-archive files in HandleFile

* Adjust check for rpm/deb archive type

* add additional deb mime type

* update comment

* go mod tidy

* update go mod

* go mod tidy

* add comment

* update max depth check to >

* go mod tidy

* rename

* [refactor] - Refactor Archive Handling Logic - Part 4: Non-Archive Data Handling and Cleanup (#2704)

* Handle non-archive data within the DefaultHandler

* make structs and methods private

* Remove non-archive data handling within sources

* Handle non-archive data within the DefaultHandler

* rebase

* Remove non-archive data handling within sources

* add gzip

* move diskbuffered rereader setup into handler pkg

* remove DiskBuffereReader creation logic within sources

* move rewind closer

* reduce log verbosity

* make defaultBufferSize a const

* use correct reader

* address comments

* update test

* [feat] - Add Prometheus Metrics for File Handlers (#2705)

* add metrics for file handling

* add metrics for errors

* add metrics for file handling

* add metrics for errors

* fix tests

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* fix err assignment

* add metrics for file handling

* add metrics for errors

* fix tests

* rebase

* add metrics for errors

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* fix err assignment

* rebase

* remove

* update metric to ms

* update comments

* address comments

* reduce indentations

* add metrics for archive depth

* [bug] - Enhanced Archive Handling to Address Interface Constraints (#2710)

* add metrics for file handling

* add metrics for errors

* add metrics for file handling

* add metrics for errors

* fix tests

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* Address incompatible reader to openArchive

* remove nil check

* fix err assignment

* wrap compReader with DiskbufferReader

* add metrics for file handling

* add metrics for errors

* fix tests

* rebase

* add metrics for errors

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* fix err assignment

* rebase

* remove

* update metric to ms

* update comments

* address comments

* reduce indentations

* replace diskbuffereader with bufferedfilereader

* updtes

* add metric back

* [bug] -  Fix bug and simplify git cat-file command execution and output handling (#2719)

* add metrics for file handling

* add metrics for errors

* add metrics for file handling

* add metrics for errors

* fix tests

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* Address incompatible reader to openArchive

* remove nil check

* fix err assignment

* Allow git cat-file blob to complete before trying to handle the file

* wrap compReader with DiskbufferReader

* Allow git cat-file blob to complete before trying to handle the file

* updates

* revert stuff

* update test

* remove

* add metrics for file handling

* add metrics for errors

* fix tests

* rebase

* add metrics for errors

* add metrics for max archive depth and skipped files

* update error

* skip symlinks and dirs

* update err

* fix err assignment

* rebase

* remove

* update metric to ms

* update comments

* address comments

* reduce indentations

* inline
2024-05-10 11:36:06 -07:00
Richard Gomez 13bd783d2d test(git): change length of chunks (#2767)
This fixes one missed test in #2754 (comment).

The number of chunks doubled because each commit now has metadata + data.
2024-04-30 08:34:12 -04:00
Richard Gomez 11e5febeee feat(git): scan commit metadata (#2754)
This is a follow-up to #2713 that fixes the strange test error.

As suspected, the failure was caused by additional diffs not being included in the test's expected data.
2024-04-29 16:58:45 -04:00
Cody Rose 11452e8a57 Revert "feat(git): scan commit metadata (#2713)" (#2747)
This reverts commit 81a9c813a1.
2024-04-25 10:56:48 -04:00
Richard Gomez 81a9c813a1 feat(git): scan commit metadata (#2713)
This fixes #2683. It scans the commit author, committer (which is typically GitHub <noreply@github.com> for GitHub, but can be different), and message.

It also scans Git notes.
2024-04-25 10:13:09 -04:00
ahrav a8132839f8 [chore] - update go-github dep manually (#2664)
* update go-github dep

* remove commented out line
2024-04-03 19:19:14 -07:00
Cody Rose b7f08db1ef Redact secret in git command output (#2539)
When we fail to clone a git repository we log the command output to help with diagnosis. However, this output can include credentials in certain cases (such as certain errors associated with redirects). We don't want to log credentials when this happens.
2024-03-06 11:51:35 -05:00
Miccah c60443891b Add Display method to SourceUnit and Kind member to the CommonSourceUnit (#2450)
* Add Display method to SourceUnit and Kind member to the CommonSourceUnit

* Make SourceUnitID return the ID and a kind

These two values together uniquely represent a unit.
2024-02-20 11:24:13 -08:00
ahrav 5290023c2d use read full (#2474) 2024-02-20 07:21:16 -08:00
ahrav e8006f1bee 2396 since commit stopped working (#2402)
* Ensure we handle commits with no diffs correctly.

* cleanup

* add nil check

* address comments

* move comment

* revert

* add comment
2024-02-13 07:21:22 -08:00