Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allow admins to rename default/protected branches #33276

Merged
merged 8 commits into from
Jan 15, 2025

Conversation

kemzeb
Copy link
Contributor

@kemzeb kemzeb commented Jan 14, 2025

Currently, anyone with write permissions to a repo are able to rename default or protected branches.

This change follows GitHub's design by only allowing repo/site admins to change these branches. However, it also follows are current design for protected branches and only allows admins to modify branch names == branch protection rule names. Glob-based rules cannot be renamed by anyone (as was already the case, but we now catch ErrBranchIsProtected which we previously did not catch, throwing a 500).

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 14, 2025
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 14, 2025
@github-actions github-actions bot added modifies/translation modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code labels Jan 14, 2025
@@ -351,6 +352,9 @@ func RenameBranchPost(ctx *context.Context) {
msg, err := repository.RenameBranch(ctx, ctx.Repo.Repository, ctx.Doer, ctx.Repo.GitRepo, form.From, form.To)
if err != nil {
switch {
case repo_model.IsErrUserDoesNotHaveAccessToRepo(err):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm iffy about using ErrUserDoesNotHaveAccessToRepo as an error here. Technically the user does have access to the repo, just not the access required to rename default/protected branches. Open to ideas

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 14, 2025

An example of changing default/protected branches in the web UI:

Screenshot 2025-01-14 at 3 38 43 PM

@wxiaoguang
Copy link
Contributor

FYI: Make admins adhere to branch protection rules #32248 : there is a feature to block admins from bypassing the branch protection, maybe it should also be considered (or has it been considered already) ?

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 15, 2025

The current change doesn't consider this. I found a GitHub changelog that seem to match my current implementation. I not familiar with setting up branch protection rules, but from messing around with GitHub's rulesets I don't see a way to disable the ability to rename protected branches

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 15, 2025

Then again I can see how it could be potentially dangerous. An example:
(I have a ruleset of protected/**)

Screenshot 2025-01-14 at 5 06 03 PM

I now renamed the protected branch. GitHub now no longer sees this as a protected branch:

Screenshot 2025-01-14 at 5 06 54 PM

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 15, 2025

Hmm I think we already (in a buggy way) handle this??

Using my changes, when I define a rule without special characters (i.e. protected) I am able to rename it:

Screenshot 2025-01-14 at 5 15 35 PM

But if I introduce wildcard characters (i.e. protected/**) it will attempt and fail to rename because it throws a ErrBranchIsProtected error that we currently don't catch:

Screenshot 2025-01-14 at 5 20 34 PM

Not sure why we don't throw this error for the previous case? In the code we seem to honor the update and reflect it in the protected_branch table, but when I gave wildcard characters to the rule it would be considered a ErrBranchIsProtected error.

@kemzeb
Copy link
Contributor Author

kemzeb commented Jan 15, 2025

I think I might understand this design. It looks like if the new branch name == rule name, we treat it as "oh since the user wants to rename the branch I assume they also want to rename the rule". But if is a glob-based rule we don't make this assumption and instead throw this error.

I'll make the necessary changes to follow this design closely.

@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jan 15, 2025
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jan 15, 2025
@lunny lunny added this to the 1.24.0 milestone Jan 15, 2025
@lunny lunny added the type/enhancement An improvement of existing functionality label Jan 15, 2025
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jan 15, 2025
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jan 15, 2025
@lunny lunny enabled auto-merge (squash) January 15, 2025 20:13
@lunny lunny merged commit 2483a93 into go-gitea:main Jan 15, 2025
26 checks passed
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jan 15, 2025
@kemzeb kemzeb deleted the feat/only-allow-admins-to-rename-branches branch January 16, 2025 02:03
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jan 16, 2025
* giteaofficial/main:
  Only allow admins to rename default/protected branches (go-gitea#33276)
  Enable Typescript `noImplicitThis` (go-gitea#33250)
  Prepare for support performance trace (go-gitea#33286)
  Fix closed dependency title (go-gitea#33285)
  Move some Actions related functions from `routers` to `services` (go-gitea#33280)
  Fix incorrect TagName/BranchName usages (go-gitea#33279)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code modifies/translation size/L Denotes a PR that changes 100-499 lines, ignoring generated files. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants