Skip to content

Commit e3e06d1

Browse files
authored
fix permission check for delete tag (#19985)
fix #19970 by the way, fix some error response about protected tags. Signed-off-by: a1012112796 <[email protected]>
1 parent 89b0aac commit e3e06d1

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

routers/api/v1/repo/release.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ func DeleteRelease(ctx *context.APIContext) {
345345
// "$ref": "#/responses/empty"
346346
// "404":
347347
// "$ref": "#/responses/notFound"
348+
// "405":
349+
// "$ref": "#/responses/empty"
348350

349351
id := ctx.ParamsInt64(":id")
350352
rel, err := models.GetReleaseByID(ctx, id)
@@ -358,6 +360,10 @@ func DeleteRelease(ctx *context.APIContext) {
358360
return
359361
}
360362
if err := release_service.DeleteReleaseByID(ctx, id, ctx.Doer, false); err != nil {
363+
if models.IsErrProtectedTagName(err) {
364+
ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
365+
return
366+
}
361367
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
362368
return
363369
}

routers/api/v1/repo/release_tags.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func DeleteReleaseByTag(ctx *context.APIContext) {
9292
// "$ref": "#/responses/empty"
9393
// "404":
9494
// "$ref": "#/responses/notFound"
95+
// "405":
96+
// "$ref": "#/responses/empty"
9597

9698
tag := ctx.Params(":tag")
9799

@@ -111,7 +113,12 @@ func DeleteReleaseByTag(ctx *context.APIContext) {
111113
}
112114

113115
if err = releaseservice.DeleteReleaseByID(ctx, release.ID, ctx.Doer, false); err != nil {
116+
if models.IsErrProtectedTagName(err) {
117+
ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
118+
return
119+
}
114120
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
121+
return
115122
}
116123

117124
ctx.Status(http.StatusNoContent)

routers/api/v1/repo/tag.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ func CreateTag(ctx *context.APIContext) {
176176
// "$ref": "#/responses/Tag"
177177
// "404":
178178
// "$ref": "#/responses/notFound"
179+
// "405":
180+
// "$ref": "#/responses/empty"
179181
// "409":
180182
// "$ref": "#/responses/conflict"
181183
form := web.GetForm(ctx).(*api.CreateTagOption)
@@ -196,6 +198,11 @@ func CreateTag(ctx *context.APIContext) {
196198
ctx.Error(http.StatusConflict, "tag exist", err)
197199
return
198200
}
201+
if models.IsErrProtectedTagName(err) {
202+
ctx.Error(http.StatusMethodNotAllowed, "CreateNewTag", "user not allowed to create protected tag")
203+
return
204+
}
205+
199206
ctx.InternalServerError(err)
200207
return
201208
}
@@ -236,6 +243,8 @@ func DeleteTag(ctx *context.APIContext) {
236243
// "$ref": "#/responses/empty"
237244
// "404":
238245
// "$ref": "#/responses/notFound"
246+
// "405":
247+
// "$ref": "#/responses/empty"
239248
// "409":
240249
// "$ref": "#/responses/conflict"
241250
tagName := ctx.Params("*")
@@ -256,7 +265,12 @@ func DeleteTag(ctx *context.APIContext) {
256265
}
257266

258267
if err = releaseservice.DeleteReleaseByID(ctx, tag.ID, ctx.Doer, true); err != nil {
268+
if models.IsErrProtectedTagName(err) {
269+
ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
270+
return
271+
}
259272
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
273+
return
260274
}
261275

262276
ctx.Status(http.StatusNoContent)

routers/web/repo/branch.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ func CreateBranch(ctx *context.Context) {
373373
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
374374
}
375375
if err != nil {
376+
if models.IsErrProtectedTagName(err) {
377+
ctx.Flash.Error(ctx.Tr("repo.release.tag_name_protected"))
378+
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
379+
return
380+
}
381+
376382
if models.IsErrTagAlreadyExists(err) {
377383
e := err.(models.ErrTagAlreadyExists)
378384
ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))

routers/web/repo/release.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,11 @@ func DeleteTag(ctx *context.Context) {
519519

520520
func deleteReleaseOrTag(ctx *context.Context, isDelTag bool) {
521521
if err := releaseservice.DeleteReleaseByID(ctx, ctx.FormInt64("id"), ctx.Doer, isDelTag); err != nil {
522-
ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
522+
if models.IsErrProtectedTagName(err) {
523+
ctx.Flash.Error(ctx.Tr("repo.release.tag_name_protected"))
524+
} else {
525+
ctx.Flash.Error("DeleteReleaseByID: " + err.Error())
526+
}
523527
} else {
524528
if isDelTag {
525529
ctx.Flash.Success(ctx.Tr("repo.release.deletion_tag_success"))

services/release/release.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ func DeleteReleaseByID(ctx context.Context, id int64, doer *user_model.User, del
294294
}
295295

296296
if delTag {
297+
protectedTags, err := git_model.GetProtectedTags(rel.RepoID)
298+
if err != nil {
299+
return fmt.Errorf("GetProtectedTags: %v", err)
300+
}
301+
isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, rel.TagName, rel.PublisherID)
302+
if err != nil {
303+
return err
304+
}
305+
if !isAllowed {
306+
return models.ErrProtectedTagName{
307+
TagName: rel.TagName,
308+
}
309+
}
310+
297311
if stdout, _, err := git.NewCommand(ctx, "tag", "-d", rel.TagName).
298312
SetDescription(fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID)).
299313
RunStdString(&git.RunOpts{Dir: repo.RepoPath()}); err != nil && !strings.Contains(err.Error(), "not found") {

templates/swagger/v1_json.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8960,6 +8960,9 @@
89608960
},
89618961
"404": {
89628962
"$ref": "#/responses/notFound"
8963+
},
8964+
"405": {
8965+
"$ref": "#/responses/empty"
89638966
}
89648967
}
89658968
}
@@ -9043,6 +9046,9 @@
90439046
},
90449047
"404": {
90459048
"$ref": "#/responses/notFound"
9049+
},
9050+
"405": {
9051+
"$ref": "#/responses/empty"
90469052
}
90479053
}
90489054
},
@@ -9811,6 +9817,9 @@
98119817
"404": {
98129818
"$ref": "#/responses/notFound"
98139819
},
9820+
"405": {
9821+
"$ref": "#/responses/empty"
9822+
},
98149823
"409": {
98159824
"$ref": "#/responses/conflict"
98169825
}
@@ -9898,6 +9907,9 @@
98989907
"404": {
98999908
"$ref": "#/responses/notFound"
99009909
},
9910+
"405": {
9911+
"$ref": "#/responses/empty"
9912+
},
99019913
"409": {
99029914
"$ref": "#/responses/conflict"
99039915
}

0 commit comments

Comments
 (0)