Skip to content

Commit b01faea

Browse files
committed
add global toggle
1 parent e38b675 commit b01faea

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

modules/setting/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353
DisableDownloadSourceArchives bool
5454
AllowForkWithoutMaximumLimit bool
5555
AllowForkIntoSameOwner bool
56-
56+
EnablePushSecretDetection bool `ini:"ENABLE_PUSH_SECRET_DETECTION"`
5757
// Repository editor settings
5858
Editor struct {
5959
LineWrapExtensions []string

routers/private/hook_pre_receive.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/gitrepo"
2323
"code.gitea.io/gitea/modules/log"
2424
"code.gitea.io/gitea/modules/private"
25+
"code.gitea.io/gitea/modules/setting"
2526
"code.gitea.io/gitea/modules/web"
2627
gitea_context "code.gitea.io/gitea/services/context"
2728
pull_service "code.gitea.io/gitea/services/pull"
@@ -544,12 +545,18 @@ func (ctx *preReceiveContext) loadPusherAndPermission() bool {
544545

545546
// checks commits for secrets
546547
func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string, _ git.RefName) {
547-
if ctx.opts.GitPushOptions.Bool("skip.secret-detection").Has() {
548+
// Skip check if disabled globally
549+
if !setting.Repository.EnablePushSecretDetection {
550+
return
551+
}
552+
553+
// Bypass allowed only if user is repository admin
554+
if ctx.opts.GitPushOptions.Bool("skip.secret-detection").Value() && ctx.Repo.IsAdmin() {
548555
return
549556
}
550557
repo := ctx.Repo.Repository
551558

552-
// New commit is empty so there's nothing to check for
559+
// We're deleting a reference so that's not a concern
553560
if newCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
554561
return
555562
}
@@ -559,8 +566,9 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
559566
ctx.Status(http.StatusTeapot)
560567
return
561568
}
562-
if oldCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
563569

570+
// if this reference is new we need a base to compare to
571+
if oldCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
564572
base, _, err := git.NewCommand("merge-base").AddDynamicArguments(newCommitID).RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
565573
if err != nil {
566574
ctx.Status(http.StatusTeapot)
@@ -570,7 +578,6 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
570578
}
571579
// out, _, err = git.NewCommand("format-patch", "--stdout", "-U0").AddDynamicArguments(oldCommitID, newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
572580
out, _, err := git.NewCommand("show", "-U0").AddDynamicArguments(oldCommitID+".."+newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
573-
574581
if err != nil {
575582
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
576583
return
@@ -585,14 +592,14 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
585592
ctx.Status(http.StatusTeapot)
586593
return
587594
}
595+
588596
if len(findings) != 0 {
589597
msg := strings.Builder{}
590598
msg.WriteString("This repository has secret detection enabled! Following secrets were detected:\n")
591599

592600
for _, finding := range findings {
593601
msg.WriteString(fmt.Sprintf("\n-- Commit %s contains a secret in %v:%v\n", finding.Commit, finding.File, finding.StartLine))
594602
msg.WriteString(fmt.Sprintf("RuleID: %v", finding.RuleID))
595-
596603
}
597604

598605
ctx.JSON(http.StatusForbidden, private.Response{UserMsg: msg.String()})

0 commit comments

Comments
 (0)