Skip to content

Commit 4b7f0c6

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

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

routers/api/v1/repo/release.go

+6
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ func DeleteRelease(ctx *context.APIContext) {
344344
// "$ref": "#/responses/empty"
345345
// "404":
346346
// "$ref": "#/responses/notFound"
347+
// "405":
348+
// "$ref": "#/responses/empty"
347349

348350
id := ctx.ParamsInt64(":id")
349351
rel, err := models.GetReleaseByID(id)
@@ -357,6 +359,10 @@ func DeleteRelease(ctx *context.APIContext) {
357359
return
358360
}
359361
if err := releaseservice.DeleteReleaseByID(id, ctx.User, false); err != nil {
362+
if models.IsErrProtectedTagName(err) {
363+
ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
364+
return
365+
}
360366
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
361367
return
362368
}

routers/api/v1/repo/release_tags.go

+7
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(release.ID, ctx.User, 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

+14
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(tag.ID, ctx.User, 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

+6
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ func CreateBranch(ctx *context.Context) {
370370
err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
371371
}
372372
if err != nil {
373+
if models.IsErrProtectedTagName(err) {
374+
ctx.Flash.Error(ctx.Tr("repo.release.tag_name_protected"))
375+
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
376+
return
377+
}
378+
373379
if models.IsErrTagAlreadyExists(err) {
374380
e := err.(models.ErrTagAlreadyExists)
375381
ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))

routers/web/repo/release.go

+5-1
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.FormInt64("id"), ctx.User, 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

+14
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ func DeleteReleaseByID(id int64, doer *user_model.User, delTag bool) error {
295295
}
296296

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

templates/swagger/v1_json.tmpl

+12
Original file line numberDiff line numberDiff line change
@@ -8515,6 +8515,9 @@
85158515
},
85168516
"404": {
85178517
"$ref": "#/responses/notFound"
8518+
},
8519+
"405": {
8520+
"$ref": "#/responses/empty"
85188521
}
85198522
}
85208523
}
@@ -8598,6 +8601,9 @@
85988601
},
85998602
"404": {
86008603
"$ref": "#/responses/notFound"
8604+
},
8605+
"405": {
8606+
"$ref": "#/responses/empty"
86018607
}
86028608
}
86038609
},
@@ -9366,6 +9372,9 @@
93669372
"404": {
93679373
"$ref": "#/responses/notFound"
93689374
},
9375+
"405": {
9376+
"$ref": "#/responses/empty"
9377+
},
93699378
"409": {
93709379
"$ref": "#/responses/conflict"
93719380
}
@@ -9453,6 +9462,9 @@
94539462
"404": {
94549463
"$ref": "#/responses/notFound"
94559464
},
9465+
"405": {
9466+
"$ref": "#/responses/empty"
9467+
},
94569468
"409": {
94579469
"$ref": "#/responses/conflict"
94589470
}

0 commit comments

Comments
 (0)