From 824dd6bc5d7ec0ff39d2d023e6f58c743e7690a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Piliszek?= Date: Sun, 11 Aug 2024 21:09:57 +0200 Subject: [PATCH] git-grep: set timeout to 2s by default and allow configuring it We need to shorten the timeout to bound effectively for computation size. This protects against "too big" repos. This also protects to some extent against too long lines if kept to very low values (basically so that grep cannot run out of memory beforehand). Docs-PR: forgejo/docs#812 --- custom/conf/app.example.ini | 1 + modules/git/grep.go | 3 +++ modules/setting/git.go | 3 +++ 3 files changed, 7 insertions(+) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 804440dfc9..5fe6de1e0f 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -732,6 +732,7 @@ LEVEL = Info ;CLONE = 300 ;PULL = 300 ;GC = 60 +;GREP = 2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Git config options diff --git a/modules/git/grep.go b/modules/git/grep.go index ba870e0541..939b3124d3 100644 --- a/modules/git/grep.go +++ b/modules/git/grep.go @@ -15,6 +15,7 @@ import ( "os" "strconv" "strings" + "time" "code.gitea.io/gitea/modules/setting" ) @@ -94,6 +95,8 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO opts.MaxResultLimit = cmp.Or(opts.MaxResultLimit, 50) stderr := bytes.Buffer{} err = cmd.Run(&RunOpts{ + Timeout: time.Duration(setting.Git.Timeout.Grep) * time.Second, + Dir: repo.Path, Stdout: stdoutWriter, Stderr: &stderr, diff --git a/modules/setting/git.go b/modules/setting/git.go index 48a4e7f30d..812c4fe6c9 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -37,6 +37,7 @@ var Git = struct { Clone int Pull int GC int `ini:"GC"` + Grep int } `ini:"git.timeout"` }{ DisableDiffHighlight: false, @@ -59,6 +60,7 @@ var Git = struct { Clone int Pull int GC int `ini:"GC"` + Grep int }{ Default: 360, Migrate: 600, @@ -66,6 +68,7 @@ var Git = struct { Clone: 300, Pull: 300, GC: 60, + Grep: 2, }, }