Skip to content

Commit 742e21a

Browse files
zeripathtechknowlogicklafriks
authored
Handle and propagate errors when checking if paths are Dirs, Files or Exist (#13186)
* Ensure errors from IsDir propagate * Handle errors when checking IsFile * Handle and propagate errors from IsExist * Update modules/templates/static.go * Update modules/templates/static.go * Return after ctx.ServerError * Apply suggestions from code review * Fix tests The previous merge managed to break repo_form.go Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 5b75f17 commit 742e21a

File tree

29 files changed

+384
-94
lines changed

29 files changed

+384
-94
lines changed

cmd/dump.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"gitea.com/macaron/session"
2525
archiver "github.com/mholt/archiver/v3"
26-
"github.com/unknwon/com"
2726
"github.com/urfave/cli"
2827
)
2928

@@ -306,7 +305,11 @@ func runDump(ctx *cli.Context) error {
306305
log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
307306
}
308307

309-
if com.IsExist(setting.AppDataPath) {
308+
isExist, err := util.IsExist(setting.AppDataPath)
309+
if err != nil {
310+
log.Error("Unable to check if %s exists. Error: %v", setting.AppDataPath, err)
311+
}
312+
if isExist {
310313
log.Info("Packing data directory...%s", setting.AppDataPath)
311314

312315
var excludes []string
@@ -349,9 +352,15 @@ func runDump(ctx *cli.Context) error {
349352
// yet or not.
350353
if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
351354
log.Info("Skip dumping log files")
352-
} else if com.IsExist(setting.LogRootPath) {
353-
if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil {
354-
fatal("Failed to include log: %v", err)
355+
} else {
356+
isExist, err := util.IsExist(setting.LogRootPath)
357+
if err != nil {
358+
log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err)
359+
}
360+
if isExist {
361+
if err := addRecursive(w, "log", setting.LogRootPath, verbose); err != nil {
362+
fatal("Failed to include log: %v", err)
363+
}
355364
}
356365
}
357366

cmd/web.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import (
1616
"code.gitea.io/gitea/modules/graceful"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/setting"
19+
"code.gitea.io/gitea/modules/util"
1920
"code.gitea.io/gitea/routers"
2021
"code.gitea.io/gitea/routers/routes"
2122

2223
context2 "github.com/gorilla/context"
23-
"github.com/unknwon/com"
2424
"github.com/urfave/cli"
2525
"golang.org/x/crypto/acme/autocert"
2626
ini "gopkg.in/ini.v1"
@@ -188,7 +188,11 @@ func setPort(port string) error {
188188
default:
189189
// Save LOCAL_ROOT_URL if port changed
190190
cfg := ini.Empty()
191-
if com.IsFile(setting.CustomConf) {
191+
isFile, err := util.IsFile(setting.CustomConf)
192+
if err != nil {
193+
log.Fatal("Unable to check if %s is a file", err)
194+
}
195+
if isFile {
192196
// Keeps custom settings if there is already something.
193197
if err := cfg.Append(setting.CustomConf); err != nil {
194198
return fmt.Errorf("Failed to load custom conf '%s': %v", setting.CustomConf, err)

contrib/environment-to-ini/environment-to-ini.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212

1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/modules/util"
1516

16-
"github.com/unknwon/com"
1717
"github.com/urfave/cli"
1818
ini "gopkg.in/ini.v1"
1919
)
@@ -97,7 +97,11 @@ func runEnvironmentToIni(c *cli.Context) error {
9797
setting.SetCustomPathAndConf(providedCustom, providedConf, providedWorkPath)
9898

9999
cfg := ini.Empty()
100-
if com.IsFile(setting.CustomConf) {
100+
isFile, err := util.IsFile(setting.CustomConf)
101+
if err != nil {
102+
log.Fatal("Unable to check if %s is a file. Error: %v", setting.CustomConf, err)
103+
}
104+
if isFile {
101105
if err := cfg.Append(setting.CustomConf); err != nil {
102106
log.Fatal("Failed to load custom conf '%s': %v", setting.CustomConf, err)
103107
}
@@ -145,7 +149,7 @@ func runEnvironmentToIni(c *cli.Context) error {
145149
if len(destination) == 0 {
146150
destination = setting.CustomConf
147151
}
148-
err := cfg.SaveTo(destination)
152+
err = cfg.SaveTo(destination)
149153
if err != nil {
150154
return err
151155
}

integrations/api_repo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func TestAPIRepoMigrate(t *testing.T) {
325325
if resp.Code == http.StatusUnprocessableEntity {
326326
respJSON := map[string]string{}
327327
DecodeJSON(t, resp, &respJSON)
328-
if assert.Equal(t, respJSON["message"], "Remote visit addressed rate limitation.") {
328+
if assert.Equal(t, "Remote visit addressed rate limitation.", respJSON["message"]) {
329329
t.Log("test hit github rate limitation")
330330
}
331331
} else {

models/repo.go

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ func loadRepoConfig() {
7474
log.Fatal("Failed to get %s files: %v", t, err)
7575
}
7676
customPath := path.Join(setting.CustomPath, "options", t)
77-
if com.IsDir(customPath) {
77+
isDir, err := util.IsDir(customPath)
78+
if err != nil {
79+
log.Fatal("Failed to get custom %s files: %v", t, err)
80+
}
81+
if isDir {
7882
customFiles, err := com.StatDir(customPath)
7983
if err != nil {
8084
log.Fatal("Failed to get custom %s files: %v", t, err)
@@ -1004,7 +1008,11 @@ func isRepositoryExist(e Engine, u *User, repoName string) (bool, error) {
10041008
OwnerID: u.ID,
10051009
LowerName: strings.ToLower(repoName),
10061010
})
1007-
return has && com.IsDir(RepoPath(u.Name, repoName)), err
1011+
if err != nil {
1012+
return false, err
1013+
}
1014+
isDir, err := util.IsDir(RepoPath(u.Name, repoName))
1015+
return has && isDir, err
10081016
}
10091017

10101018
// IsRepositoryExist returns true if the repository with given name under user has already existed.
@@ -1078,7 +1086,12 @@ func CheckCreateRepository(doer, u *User, name string, overwriteOrAdopt bool) er
10781086
return ErrRepoAlreadyExist{u.Name, name}
10791087
}
10801088

1081-
if !overwriteOrAdopt && com.IsExist(RepoPath(u.Name, name)) {
1089+
isExist, err := util.IsExist(RepoPath(u.Name, name))
1090+
if err != nil {
1091+
log.Error("Unable to check if %s exists. Error: %v", RepoPath(u.Name, name), err)
1092+
return err
1093+
}
1094+
if !overwriteOrAdopt && isExist {
10821095
return ErrRepoFilesAlreadyExist{u.Name, name}
10831096
}
10841097
return nil
@@ -1110,7 +1123,11 @@ func GetRepoInitFile(tp, name string) ([]byte, error) {
11101123

11111124
// Use custom file when available.
11121125
customPath := path.Join(setting.CustomPath, relPath)
1113-
if com.IsFile(customPath) {
1126+
isFile, err := util.IsFile(customPath)
1127+
if err != nil {
1128+
log.Error("Unable to check if %s is a file. Error: %v", customPath, err)
1129+
}
1130+
if isFile {
11141131
return ioutil.ReadFile(customPath)
11151132
}
11161133

@@ -1156,7 +1173,12 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO
11561173
}
11571174

11581175
repoPath := RepoPath(u.Name, repo.Name)
1159-
if !overwriteOrAdopt && com.IsExist(repoPath) {
1176+
isExist, err := util.IsExist(repoPath)
1177+
if err != nil {
1178+
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
1179+
return err
1180+
}
1181+
if !overwriteOrAdopt && isExist {
11601182
log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
11611183
return ErrRepoFilesAlreadyExist{
11621184
Uname: u.Name,
@@ -1408,7 +1430,12 @@ func TransferOwnership(doer *User, newOwnerName string, repo *Repository) error
14081430

14091431
// Rename remote wiki repository to new path and delete local copy.
14101432
wikiPath := UncycloPath(oldOwner.Name, repo.Name)
1411-
if com.IsExist(wikiPath) {
1433+
isExist, err := util.IsExist(wikiPath)
1434+
if err != nil {
1435+
log.Error("Unable to check if %s exists. Error: %v", wikiPath, err)
1436+
return err
1437+
}
1438+
if isExist {
14121439
if err = os.Rename(wikiPath, UncycloPath(newOwner.Name, repo.Name)); err != nil {
14131440
return fmt.Errorf("rename repository wiki: %v", err)
14141441
}
@@ -1451,7 +1478,12 @@ func ChangeRepositoryName(doer *User, repo *Repository, newRepoName string) (err
14511478
}
14521479

14531480
wikiPath := repo.UncycloPath()
1454-
if com.IsExist(wikiPath) {
1481+
isExist, err := util.IsExist(wikiPath)
1482+
if err != nil {
1483+
log.Error("Unable to check if %s exists. Error: %v", wikiPath, err)
1484+
return err
1485+
}
1486+
if isExist {
14551487
if err = os.Rename(wikiPath, UncycloPath(repo.Owner.Name, newRepoName)); err != nil {
14561488
return fmt.Errorf("rename repository wiki: %v", err)
14571489
}
@@ -1528,11 +1560,16 @@ func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err e
15281560

15291561
// Create/Remove git-daemon-export-ok for git-daemon...
15301562
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
1531-
if repo.IsPrivate && com.IsExist(daemonExportFile) {
1563+
isExist, err := util.IsExist(daemonExportFile)
1564+
if err != nil {
1565+
log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
1566+
return err
1567+
}
1568+
if repo.IsPrivate && isExist {
15321569
if err = util.Remove(daemonExportFile); err != nil {
15331570
log.Error("Failed to remove %s: %v", daemonExportFile, err)
15341571
}
1535-
} else if !repo.IsPrivate && !com.IsExist(daemonExportFile) {
1572+
} else if !repo.IsPrivate && !isExist {
15361573
if f, err := os.Create(daemonExportFile); err != nil {
15371574
log.Error("Failed to create %s: %v", daemonExportFile, err)
15381575
} else {

models/ssh_key.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -736,11 +736,18 @@ func rewriteAllPublicKeys(e Engine) error {
736736
}
737737
}()
738738

739-
if setting.SSH.AuthorizedKeysBackup && com.IsExist(fPath) {
740-
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
741-
if err = com.Copy(fPath, bakPath); err != nil {
739+
if setting.SSH.AuthorizedKeysBackup {
740+
isExist, err := util.IsExist(fPath)
741+
if err != nil {
742+
log.Error("Unable to check if %s exists. Error: %v", fPath, err)
742743
return err
743744
}
745+
if isExist {
746+
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
747+
if err = com.Copy(fPath, bakPath); err != nil {
748+
return err
749+
}
750+
}
744751
}
745752

746753
if err := regeneratePublicKeys(e, t); err != nil {
@@ -765,7 +772,12 @@ func regeneratePublicKeys(e Engine, t io.StringWriter) error {
765772
}
766773

767774
fPath := filepath.Join(setting.SSH.RootPath, "authorized_keys")
768-
if com.IsExist(fPath) {
775+
isExist, err := util.IsExist(fPath)
776+
if err != nil {
777+
log.Error("Unable to check if %s exists. Error: %v", fPath, err)
778+
return err
779+
}
780+
if isExist {
769781
f, err := os.Open(fPath)
770782
if err != nil {
771783
return err
@@ -1206,11 +1218,18 @@ func rewriteAllPrincipalKeys(e Engine) error {
12061218
os.Remove(tmpPath)
12071219
}()
12081220

1209-
if setting.SSH.AuthorizedPrincipalsBackup && com.IsExist(fPath) {
1210-
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
1211-
if err = com.Copy(fPath, bakPath); err != nil {
1221+
if setting.SSH.AuthorizedPrincipalsBackup {
1222+
isExist, err := util.IsExist(fPath)
1223+
if err != nil {
1224+
log.Error("Unable to check if %s exists. Error: %v", fPath, err)
12121225
return err
12131226
}
1227+
if isExist {
1228+
bakPath := fmt.Sprintf("%s_%d.gitea_bak", fPath, time.Now().Unix())
1229+
if err = com.Copy(fPath, bakPath); err != nil {
1230+
return err
1231+
}
1232+
}
12141233
}
12151234

12161235
if err := regeneratePrincipalKeys(e, t); err != nil {
@@ -1249,7 +1268,12 @@ func regeneratePrincipalKeys(e Engine, t io.StringWriter) error {
12491268
}
12501269

12511270
fPath := filepath.Join(setting.SSH.RootPath, authorizedPrincipalsFile)
1252-
if com.IsExist(fPath) {
1271+
isExist, err := util.IsExist(fPath)
1272+
if err != nil {
1273+
log.Error("Unable to check if %s exists. Error: %v", fPath, err)
1274+
return err
1275+
}
1276+
if isExist {
12531277
f, err := os.Open(fPath)
12541278
if err != nil {
12551279
return err

models/upload.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"os"
1313
"path"
1414

15+
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/setting"
1617
"code.gitea.io/gitea/modules/util"
1718

1819
gouuid "github.com/google/uuid"
19-
"github.com/unknwon/com"
2020
)
2121

2222
// ____ ___ .__ .___ ___________.___.__
@@ -126,7 +126,11 @@ func DeleteUploads(uploads ...*Upload) (err error) {
126126

127127
for _, upload := range uploads {
128128
localPath := upload.LocalPath()
129-
if !com.IsFile(localPath) {
129+
isFile, err := util.IsFile(localPath)
130+
if err != nil {
131+
log.Error("Unable to check if %s is a file. Error: %v", localPath, err)
132+
}
133+
if !isFile {
130134
continue
131135
}
132136

models/wiki.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
"github.com/unknwon/com"
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/util"
1314
)
1415

1516
// UncycloCloneLink returns clone URLs of repository wiki.
@@ -29,5 +30,9 @@ func (repo *Repository) UncycloPath() string {
2930

3031
// HasUncyclo returns true if repository has wiki.
3132
func (repo *Repository) HasUncyclo() bool {
32-
return com.IsDir(repo.UncycloPath())
33+
isDir, err := util.IsDir(repo.UncycloPath())
34+
if err != nil {
35+
log.Error("Unable to check if %s is a directory: %v", repo.UncycloPath(), err)
36+
}
37+
return isDir
3338
}

modules/auth/repo_form.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010
"strings"
1111

1212
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/setting"
1415
"code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/modules/util"
1517
"code.gitea.io/gitea/routers/utils"
1618

1719
"gitea.com/macaron/binding"
1820
"gitea.com/macaron/macaron"
19-
"github.com/unknwon/com"
2021
)
2122

2223
// _______________________________________ _________.______________________ _______________.___.
@@ -107,8 +108,15 @@ func ParseRemoteAddr(remoteAddr, authUsername, authPassword string, user *models
107108
}
108109
} else if !user.CanImportLocal() {
109110
return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
110-
} else if !com.IsDir(remoteAddr) {
111-
return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
111+
} else {
112+
isDir, err := util.IsDir(remoteAddr)
113+
if err != nil {
114+
log.Error("Unable to check if %s is a directory: %v", remoteAddr, err)
115+
return "", err
116+
}
117+
if !isDir {
118+
return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
119+
}
112120
}
113121

114122
return remoteAddr, nil

modules/git/hook.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/modules/util"
16-
"github.com/unknwon/com"
1716
)
1817

1918
// hookNames is a list of Git server hooks' name that are supported.
@@ -129,7 +128,12 @@ const (
129128
func SetUpdateHook(repoPath, content string) (err error) {
130129
log("Setting update hook: %s", repoPath)
131130
hookPath := path.Join(repoPath, HookPathUpdate)
132-
if com.IsExist(hookPath) {
131+
isExist, err := util.IsExist(hookPath)
132+
if err != nil {
133+
log("Unable to check if %s exists. Error: %v", hookPath, err)
134+
return err
135+
}
136+
if isExist {
133137
err = util.Remove(hookPath)
134138
} else {
135139
err = os.MkdirAll(path.Dir(hookPath), os.ModePerm)

0 commit comments

Comments
 (0)