Skip to content

Commit 4b4884c

Browse files
authored
Return nicer error if trying to pull from non-existent user (#18288)
* Return nicer error if trying to pull from non-existent user Gitea serv will currently return an 500 if we try to pull from a repository where the owner does not exist. This PR checks for the UserNotExist Error when checking for the user and will return a NotFound error instead. Fix #18225
1 parent a15353d commit 4b4884c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Diff for: routers/private/serv.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,17 @@ func ServCommand(ctx *context.PrivateContext) {
111111

112112
owner, err := user_model.GetUserByName(results.OwnerName)
113113
if err != nil {
114+
if user_model.IsErrUserNotExist(err) {
115+
// User is fetching/cloning a non-existent repository
116+
log.Warn("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
117+
ctx.JSON(http.StatusNotFound, private.ErrServCommand{
118+
Results: results,
119+
Err: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
120+
})
121+
return
122+
}
114123
log.Error("Unable to get repository owner: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
115-
ctx.JSON(http.StatusInternalServerError, private.ErrServCommand{
124+
ctx.JSON(http.StatusForbidden, private.ErrServCommand{
116125
Results: results,
117126
Err: fmt.Sprintf("Unable to get repository owner: %s/%s %v", results.OwnerName, results.RepoName, err),
118127
})
@@ -135,7 +144,7 @@ func ServCommand(ctx *context.PrivateContext) {
135144
for _, verb := range ctx.FormStrings("verb") {
136145
if "git-upload-pack" == verb {
137146
// User is fetching/cloning a non-existent repository
138-
log.Error("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
147+
log.Warn("Failed authentication attempt (cannot find repository: %s/%s) from %s", results.OwnerName, results.RepoName, ctx.RemoteAddr())
139148
ctx.JSON(http.StatusNotFound, private.ErrServCommand{
140149
Results: results,
141150
Err: fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
@@ -325,7 +334,7 @@ func ServCommand(ctx *context.PrivateContext) {
325334
userMode := perm.UnitAccessMode(unitType)
326335

327336
if userMode < mode {
328-
log.Error("Failed authentication attempt for %s with key %s (not authorized to %s %s/%s) from %s", user.Name, key.Name, modeString, ownerName, repoName, ctx.RemoteAddr())
337+
log.Warn("Failed authentication attempt for %s with key %s (not authorized to %s %s/%s) from %s", user.Name, key.Name, modeString, ownerName, repoName, ctx.RemoteAddr())
329338
ctx.JSON(http.StatusUnauthorized, private.ErrServCommand{
330339
Results: results,
331340
Err: fmt.Sprintf("User: %d:%s with Key: %d:%s is not authorized to %s %s/%s.", user.ID, user.Name, key.ID, key.Name, modeString, ownerName, repoName),

0 commit comments

Comments
 (0)