GitHub Branch Protection Policy Override by Bot
Description
A GitHub App or bot bypassed a branch protection requirement on a repository and branch where no automation is expected to write. Automation is only expected to write to the protected branches listed in the rule; a bypass anywhere else may mean an automation token was obtained by untrusted code, such as a pull request's workflow run.
Query · python
from panther_base_helpers import pattern_match_list
from panther_github_helpers import github_alert_context
APP_TOKEN_TYPE = "GitHub App server-to-server token"
# Protected-branch writes the organisation's own automation is expected to make,
# keyed on the audit event's numeric repo_id so no repository name is published here.
EXPECTED_BOT_OVERRIDES = {
"256069557": ["refs/heads/main"],
"499630320": ["refs/heads/main"],
"243627255": ["refs/heads/release-*"],
}
def is_bot_actor(event):
return (
event.get("actor_is_bot") is True
or str(event.get("actor", "")).endswith("[bot]")
or event.get("programmatic_access_type") == APP_TOKEN_TYPE
)
def is_expected_override(event):
patterns = EXPECTED_BOT_OVERRIDES.get(str(event.get("repo_id", "")), [])
return pattern_match_list(event.get("branch", ""), patterns)
def rule(event):
if event.get("action") != "protected_branch.policy_override":
return False
return is_bot_actor(event) and not is_expected_override(event)
def title(event):
return (
f"Bot [{event.get('actor', '<UNKNOWN_ACTOR>')}] overrode branch protection"
f" on [{event.get('branch', '<UNKNOWN_BRANCH>')}]"
f" in [{event.get('repo', '<UNKNOWN_REPO>')}]"
)
def dedup(event):
return f"{event.get('repo', '')}:{event.get('branch', '')}:{event.get('actor', '')}"
def alert_context(event):
context = github_alert_context(event)
for field in (
"repo_id",
"branch",
"actor_is_bot",
"programmatic_access_type",
"token_id",
"rule_suite_id",
"before",
"after",
"overridden_codes",
):
context[field] = event.get(field)
return context
Analyst notes
- Confirm whether the write or approval came from an approved automation workflow run (the workflow run that minted the token is in the token broker's log).
- If it did not: suspend the GitHub App installation for the organisation, or rotate the App's private key, so no further token is minted.
- Disable the workflow that performed it.
- Revert the commit or dismiss the review, and review every change the token made (same token_id).
- Notify Security.