Description
This check verifies that branch restrictions are configured on the default branch of a Bitbucket repository. Branch restrictions help protect critical branches by controlling who can push, merge, delete, or rewrite branch history, ensuring that changes follow the organization's development and review processes.
Note: Only repository-level branch restrictions are detected. Restrictions set only at the project level are not returned by Bitbucket’s API and will result in a FAIL. Configure the restriction at the repository level for accurate results.
Rationale
The default branch typically contains stable or production-ready code. Without branch restrictions, users with repository access may directly push changes, bypass pull request reviews, overwrite commits, or accidentally modify critical code. Enabling branch protection reduces the risk of unauthorized changes and helps maintain code quality and repository integrity.
Impact
Prevent direct commits to the default branch.
Require pull requests before changes can be merged.
Enforce minimum approval requirements.
Restrict branch deletion and history rewrites.
Introduce additional review requirements before code changes are accepted.
Default Value
By default, Bitbucket repositories do not have branch restrictions configured. Repository administrators must manually create branch restriction rules to protect the default branch.
Pre-Requisites
Access to the repository
An existing Bitbucket repository
Test Plan
Sign in to Bitbucket at https://bitbucket.org.
Navigate to the target repository.
Select Repository settings.
Click Branch restrictions under the Workflow section.
Verify whether a branch protection rule exists for the default branch
If a branch protection rule is not created for the default branch(typically main or master), follow the implementation steps.
Expected Result: The default branch has branch restriction rules configured that prevent unauthorized direct changes and enforce the organization's review requirements.
Implementation Steps
Sign in to Bitbucket at https://bitbucket.org.
Navigate to the target repository and Select Repository settings.
Click Branch restrictions under the Workflow section.
Select Add a branch restriction.
Select by branch name or pattern and enter the default branch name (main)
Under Write Access, select : “Only specific people or groups have write access"
Ensure the following options remain unchecked (Allow rewriting branch history & Allow deleting this branch). Under Merge Access via Pull Requests, select (Only specific people or groups have merge access)
Add the required administrator, maintainer, or repository owner group that is authorized to merge pull requests.
Click Merge Settings and configure the required protections depending on the organization's requirements.
Required
☑ Minimum number of approvals = 1
☑ No changes are requested
☑ No unresolved pull request tasks
Optional (if you have CI/CD builds configured)
☑ Minimum number of successful builds for the last commit = 1
Click Save.
Backout Plan
Sign in to Bitbucket at https://bitbucket.org.
If the branch restriction rule was enabled as part of this remediation and needs to be reverted:
Log in to Bitbucket with repository administrator privileges.
Navigate to the target repository.
Select Repository settings.
Click Branch restrictions.
Locate the branch restriction rule that was enforced for the default branch protection
Click on Remove to delete the branch protection rule
Confirm that repository operations function as expected after the rollback.
Reference
https://support.atlassian.com/bitbucket-cloud/docs/use-branch-permissions/
https://support.atlassian.com/bitbucket-cloud/docs/suggest-or-require-checks-before-a-merge/
https://support.atlassian.com/bitbucket-cloud/docs/work-with-pull-requests/