-
Notifications
You must be signed in to change notification settings - Fork 80
chore(ci-cd): refactor the branch enforcement workflow #163
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,14 +2,28 @@ name: Check Source Branch | |||||
| on: | ||||||
| pull_request: | ||||||
| branches: | ||||||
| - main | ||||||
| - prod | ||||||
| types: | ||||||
| - opened | ||||||
| - synchronize | ||||||
| - reopened | ||||||
| - edited | ||||||
| jobs: | ||||||
| check-branch: | ||||||
| check-source-branch: | ||||||
| name: check-source-branch | ||||||
| runs-on: ubuntu-latest | ||||||
| steps: | ||||||
| - name: Verify source branch is 'develop' | ||||||
| - name: Check source branch | ||||||
| run: | | ||||||
| if [[ "${{ github.head_ref }}" != "develop" ]]; then | ||||||
| echo "Error: Pull request must come from the 'develop' branch" | ||||||
| HEAD_BRANCH="${{ github.head_ref }}" | ||||||
| BASE_BRANCH="${{ github.base_ref }}" | ||||||
|
|
||||||
| echo "PR: ${HEAD_BRANCH} → ${BASE_BRANCH}" | ||||||
|
|
||||||
| if [ "$BASE_BRANCH" = "prod" ] && [ "$HEAD_BRANCH" != "develop" ]; then | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant Since the workflow trigger already filters to only run on PRs targeting While this is harmless and could be seen as defensive coding, removing it would simplify the logic and make it easier for future maintainers to understand:
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||
| echo "::error::Merging to 'prod' is only allowed from the 'develop' branch." | ||||||
| echo "::error::Source branch '${HEAD_BRANCH}' is not permitted." | ||||||
| exit 1 | ||||||
| fi | ||||||
| fi | ||||||
|
|
||||||
| echo "✅ Source branch '${HEAD_BRANCH}' is allowed to merge into '${BASE_BRANCH}'." | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline at end of file The file is missing a trailing newline (indicated by
Suggested change
(Add a newline after this final line.) |
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script injection vulnerability via direct context interpolation
${{ github.head_ref }}and${{ github.base_ref }}are GitHub Actions template expressions that are resolved before the shell script executes. This means a branch name likedevelop"; malicious_command; echo "would be injected verbatim into the shell, potentially allowing a contributor (with the ability to open a PR from a fork) to execute arbitrary commands in the runner.GitHub Security Lab documents this as a known injection vector, and
github.head_refis explicitly listed as an untrusted input.The fix is to assign the context values to environment variables and reference those in the script instead:
This ensures the branch name is treated as data, not as executable shell syntax.