Skip to content

Commit 39adb06

Browse files
committed
fix new reference scanning
base the attempt on a change between default branch and new reference
1 parent 5a568ff commit 39adb06

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

models/repo/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ type Repository struct {
201201
CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"`
202202
Topics []string `xorm:"TEXT JSON"`
203203
ObjectFormatName string `xorm:"VARCHAR(6) NOT NULL DEFAULT 'sha1'"`
204+
// SecretScanning int64
204205

205206
TrustModel TrustModelType
206207

@@ -303,6 +304,11 @@ func (repo *Repository) IsBroken() bool {
303304
return repo.Status == RepositoryBroken
304305
}
305306

307+
// IsPushSecretDetectionEnabled indicates if repository should have push scan enabled
308+
func (repo *Repository) IsPushSecretDetectionEnabled() bool {
309+
return true //TODO: add record
310+
}
311+
306312
// MarkAsBrokenEmpty marks the repo as broken and empty
307313
// FIXME: the status "broken" and "is_empty" were abused,
308314
// The code always set them together, no way to distinguish whether a repo is really "empty" or "broken"

routers/private/hook_pre_receive.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,11 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
554554
return
555555
}
556556

557+
// Skip check if disabled in repository
558+
if ctx.Repo.Repository.IsPushSecretDetectionEnabled() {
559+
return
560+
}
561+
557562
// Bypass allowed only if user is repository admin
558563
if ctx.opts.GitPushOptions.Bool("skip.secret-detection").Value() && ctx.Repo.IsAdmin() {
559564
return
@@ -564,36 +569,38 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
564569
if newCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
565570
return
566571
}
567-
config, _, _ := git.NewCommand("show").AddDynamicArguments(repo.DefaultBranch+":.gitleaks.toml").RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
568-
detector, err := newDetector(config)
572+
573+
var err error
574+
var detector *gitleaks.Detector
575+
576+
config, _, err := git.NewCommand("show").AddDynamicArguments(repo.DefaultBranch+":.gitleaks.toml").RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
577+
if err != nil { //File has to exist to be taken into consideration
578+
detector, err = newDetector(config)
579+
} else {
580+
detector, err = gitleaks.NewDetectorDefaultConfig()
581+
}
569582
if err != nil {
570583
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
571584
return
572585
}
573586

574587
// if this reference is new we need a base to compare to
575588
if oldCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
576-
base, _, err := git.NewCommand("merge-base").AddDynamicArguments(newCommitID).RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
577-
if err != nil {
578-
ctx.Status(http.StatusTeapot)
579-
return
580-
}
581-
oldCommitID = base
589+
oldCommitID = repo.DefaultBranch
582590
}
583-
// out, _, err = git.NewCommand("format-patch", "--stdout", "-U0").AddDynamicArguments(oldCommitID, newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
584591
out, _, err := git.NewCommand("show", "-U0").AddDynamicArguments(oldCommitID+".."+newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
585592
if err != nil {
586593
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
587594
return
588595
}
589596
giteaCmd, err := newPreReceiveDiff(bytes.NewReader(out))
590597
if err != nil {
591-
ctx.Status(http.StatusTeapot)
598+
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
592599
return
593600
}
594601
findings, err := detector.DetectGit(giteaCmd, gitleaks.NewRemoteInfo(scm.GitHubPlatform, repo.Website))
595602
if err != nil {
596-
ctx.Status(http.StatusTeapot)
603+
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
597604
return
598605
}
599606

@@ -645,12 +652,9 @@ func init() {
645652

646653
func newDetector(config string) (*gitleaks.Detector, error) {
647654
viper.SetConfigType("toml")
648-
var err error
649-
if len(config) > 0 {
650-
err = viper.ReadConfig(strings.NewReader(config))
651-
} else {
652-
err = viper.ReadConfig(strings.NewReader(gitleaks_config.DefaultConfig))
653-
}
655+
656+
err := viper.ReadConfig(strings.NewReader(config))
657+
654658
if err != nil {
655659
return nil, err
656660
}

0 commit comments

Comments
 (0)