Files
gitleaks/detect/utils_test.go
T
bplaxcoandAlex Layne 782f310478 Archive support (#1872)
* Start Files Source

* Keep cmd the same for now

* Get tests to pass

* s.MaxTargetMegaBytes -> s.MaxFileSize

* Tweak some log messages & use q semgroup for files

* Just call yield inside the semgroup

* Refactor a check and revert some log lines

* Move mg && misc tuning

* Add `make profile`

(Benchmark results are only added if tools are installed)

Example output:

```
========================================================================
generating profile data
------------------------------------------------------------------------
- mode: dir
  benchmark:
    tool: hyperfine
    path: profile/1748019612/dir/benchmark.json
    results:
      mean: 1.12852930166
      stddev: 0.12163775966037399
      median: 1.07242928416
      user: 3.9699382799999996
      system: 0.04382341999999999
      min: 1.03207673016
      max: 1.3913475181600001
  profile:
    - mode: cpu
      path: profile/1748019612/dir/cpu.pprof
      view: go tool pprof -http=localhost: ./gitleaks profile/1748019612/dir/cpu.pprof
    - mode: mem
      path: profile/1748019612/dir/mem.pprof
      view: go tool pprof -http=localhost: ./gitleaks profile/1748019612/dir/mem.pprof
    - mode: trace
      path: profile/1748019612/dir/trace.out
      view: go tool trace profile/1748019612/dir/trace.out
- mode: git
  ...snip...
```

* Archive support for File based sources

* Move Git to Source interface

* Scan archives in git

* Misc linter fixes

* Exclude certain diffs: --diff-filter=tuxdb

* Add git archives tests

* Log error instead of fatal

* Fix spacing in test config

* Refine source comments/wording

* Make MB conversions more readable

* Format cmd

* Adjust DetectSource logging

* MiB -> MB in logs

* Add testdata/repos/archives/dotGit/refs/.gitkeep

Without it the refs folder gets deleted and the
and then it isn't detected as a git repo ¯\_(ツ)_/¯

So then the test starts scanning the whole gitleaks
repo instead :D

* Handle finding links for archives

* Working on archive depth

* Fixes from failfast test

* cmd max-archive-depth

* added comments, fixed warnings

* removed test print

* Update README.md

* Set archive depth for DetectFiles

* Tweak logs & pass max archive for readers

* Pass ctx to Fragments

* Add make lint target and address some issues

* Fix compat issue

* Report path in fragment errors

* Apply PR suggestions & misc changes

- Update blobReader.Close() to discard the buffer
- Misc logger issues & uses
- Tweak .golangci.yaml to default to none
- Discard remaining blobReader data on close
- Undo a De Morgan's Law suggestion (and disable QF1001)

* Add http mode for diagnostics

gitleaks --diagnostics=http ...
-----

From the net/http/pprof docs:

Use the pprof tool to look at the heap profile:
```
go tool pprof http://localhost:6060/debug/pprof/heap
```

Or to look at a 30-second CPU profile:
```
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
```

Or to look at the goroutine blocking profile, after calling runtime.SetBlockProfileRate in your program:
```
go tool pprof http://localhost:6060/debug/pprof/block
```

Or to look at the holders of contended mutexes, after calling runtime.SetMutexProfileFraction in your program:
```
go tool pprof http://localhost:6060/debug/pprof/mutex
```

(For more info see https://pkg.go.dev/net/http/pprof)

---------

Co-authored-by: Alex Layne <alayne@redhat.com>
2025-06-01 11:29:34 -05:00

147 lines
4.7 KiB
Go

package detect
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zricethezav/gitleaks/v8/cmd/scm"
"github.com/zricethezav/gitleaks/v8/report"
"github.com/zricethezav/gitleaks/v8/sources"
)
func Test_createScmLink(t *testing.T) {
tests := map[string]struct {
remote *sources.RemoteInfo
finding report.Finding
want string
}{
// None
"no platform": {
remote: &sources.RemoteInfo{
Platform: scm.NoPlatform,
Url: "",
},
want: "",
},
// GitHub
"github - single line": {
remote: &sources.RemoteInfo{
Platform: scm.GitHubPlatform,
Url: "https://github.com/gitleaks/test",
},
finding: report.Finding{
Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
File: "metrics/% of sales/.env",
StartLine: 25,
EndLine: 25,
},
want: "https://github.com/gitleaks/test/blob/20553ad96a4a080c94a54d677db97eed8ce2560d/metrics/%25%20of%20sales/.env#L25",
},
"github - multi line": {
remote: &sources.RemoteInfo{
Platform: scm.GitHubPlatform,
Url: "https://github.com/gitleaks/test",
},
finding: report.Finding{
Commit: "7bad9f7654cf9701b62400281748c0e8efd97666",
File: "config.json",
StartLine: 235,
EndLine: 238,
},
want: "https://github.com/gitleaks/test/blob/7bad9f7654cf9701b62400281748c0e8efd97666/config.json#L235-L238",
},
"github - markdown": {
remote: &sources.RemoteInfo{
Platform: scm.GitHubPlatform,
Url: "https://github.com/gitleaks/test",
},
finding: report.Finding{
Commit: "1fc8961d172f39ffb671766e472aa76f8d713e87",
File: "docs/guides/ecosystem/discordjs.MD",
StartLine: 34,
EndLine: 34,
},
want: "https://github.com/gitleaks/test/blob/1fc8961d172f39ffb671766e472aa76f8d713e87/docs/guides/ecosystem/discordjs.MD?plain=1#L34",
},
"github - jupyter notebook": {
remote: &sources.RemoteInfo{
Platform: scm.GitHubPlatform,
Url: "https://github.com/gitleaks/test",
},
finding: report.Finding{
Commit: "8f56bd2369595bcadbb007e88ba294630fb05c7b",
File: "Cloud/IPYNB/Overlapping Recommendation algorithm _OCuLaR_.ipynb",
StartLine: 293,
EndLine: 293,
},
want: "https://github.com/gitleaks/test/blob/8f56bd2369595bcadbb007e88ba294630fb05c7b/Cloud/IPYNB/Overlapping%20Recommendation%20algorithm%20_OCuLaR_.ipynb?plain=1#L293",
},
// GitLab
"gitlab - single line": {
remote: &sources.RemoteInfo{
Platform: scm.GitLabPlatform,
Url: "https://gitlab.com/example-org/example-group/gitleaks",
},
finding: report.Finding{
Commit: "213ffd1c9bfa906eb4c7731771132c58a4ca0139",
File: ".gitlab-ci.yml",
StartLine: 41,
EndLine: 41,
},
want: "https://gitlab.com/example-org/example-group/gitleaks/blob/213ffd1c9bfa906eb4c7731771132c58a4ca0139/.gitlab-ci.yml#L41",
},
"gitlab - multi line": {
remote: &sources.RemoteInfo{
Platform: scm.GitLabPlatform,
Url: "https://gitlab.com/example-org/example-group/gitleaks",
},
finding: report.Finding{
Commit: "63410f74e23a4e51e1f60b9feb073b5d325af878",
File: ".vscode/launchSettings.json",
StartLine: 6,
EndLine: 8,
},
want: "https://gitlab.com/example-org/example-group/gitleaks/blob/63410f74e23a4e51e1f60b9feb073b5d325af878/.vscode/launchSettings.json#L6-8",
},
// Azure DevOps
"azuredevops - single line": {
remote: &sources.RemoteInfo{
Platform: scm.AzureDevOpsPlatform,
Url: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository",
},
finding: report.Finding{
Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
File: "examplefile.json",
StartLine: 25,
EndLine: 25,
},
want: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository/commit/20553ad96a4a080c94a54d677db97eed8ce2560d?path=/examplefile.json&line=25&lineStartColumn=1&lineEndColumn=10000000&type=2&lineStyle=plain&_a=files",
},
// Azure DevOps
"azuredevops - multi line": {
remote: &sources.RemoteInfo{
Platform: scm.AzureDevOpsPlatform,
Url: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository",
},
finding: report.Finding{
Commit: "20553ad96a4a080c94a54d677db97eed8ce2560d",
File: "examplefile.json",
StartLine: 25,
EndLine: 30,
},
want: "https://dev.azure.com/exampleorganisation/exampleproject/_git/exampleRepository/commit/20553ad96a4a080c94a54d677db97eed8ce2560d?path=/examplefile.json&line=25&lineEnd=30&lineStartColumn=1&lineEndColumn=10000000&type=2&lineStyle=plain&_a=files",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
actual := createScmLink(tt.remote, tt.finding)
assert.Equal(t, tt.want, actual)
})
}
}