TMAegis
More rules for github

TMA-GITHUB-1.1.17

Ensure branch deletions are denied

MediumLevel 1Automatedgithub

Description

Ensure that users with only push access are incapable of deleting a protected branch. When enabling deletion of a protected branch, any user with at least push access to the repository can delete a branch. This can be potentially dangerous, as a simple human mistake or a hacked account can lead to data ... Checks the default branch: branch deletion is blocked. Satisfied by classic branch protection or an active ruleset (rules/branches). Uses the GitHub REST/GraphQL API through gh api (read-only). The repository comes from GH_REPO or the current git checkout (gh's {owner}/{repo} resolution, GH_HOST for GitHub Enterprise Server); the organization comes from GH_ORG (default: the repository owner). Fails when gh is missing or unauthenticated (gh auth login or GH_TOKEN); organization settings and branch protection need an owner/admin token.

How it is checked

Audit method: Shell command

shell
command -v gh >/dev/null 2>&1 || exit 1
gh auth status >/dev/null 2>&1 || exit 1
info=$(gh api 'repos/{owner}/{repo}' --jq '.full_name + " " + .default_branch' 2>/dev/null) || exit 1
R=${info% *}; B=${info#* }
[ -n "$R" ] && [ -n "$B" ] || exit 1
p=$(gh api "repos/$R/branches/$B/protection" --jq '.allow_deletions.enabled == false' 2>/dev/null) || p=false
s=$(gh api "repos/$R/rules/branches/$B" --jq 'any(.[]; .type == "deletion")' 2>/dev/null) || s=false
[ "$p" = true ] || [ "$s" = true ] && echo compliant
Expected output
compliant

How it is fixed

Fix method: Guidance or infrastructure patch

Settings > Branches (or Rules > Rulesets) for the default branch: leave Allow deletions unchecked (ruleset: Restrict deletions).

Properties

Operating systems
Any
Tags
githubsource-code
Reference
CIS GitHub Benchmark v1.2.0 - 1.1.17 Ensure branch deletions are denied

Check this host

Run this on a machine you are allowed to audit. The scan is read-only.

shell
tmaegis scan --target github