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

ci(github): sanitize title input in pr-check action #12252

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,39 @@ jobs:

[1]: https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword
- name: Check PR title
# This job checks the PR title using
# https://github.com/conventional-changelog/commitlint
# for the conventional commit format at
# https://www.conventionalcommits.org/en/v1.0.0/
# See also /.github/commitlint.config.js for more details
# We only need to check the PR title because it will end up being the
# (default) commit title when doing squash merges with Github.
# See
# https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/about-pull-request-merges#merge-message-for-a-squash-merge
# for more info. We have "Default to PR title for squash merge commits" enabled.
# Check PR title against the Conventional Commits format using commitlint.
# For more details, see: https://www.conventionalcommits.org/en/v1.0.0/
# This ensures the PR title matches the conventonal commit title format
# as it will be usead as a commit name after squashing.
# See: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/incorporating-changes-from-a-pull-request/about-pull-request-merges#merge-message-for-a-squash-merge.
if: github.event.action != 'synchronize'
# Inject as env variable to escape properly
env:
# Use an intermediate environment variable to safely handle the PR title
# and avoid potential injection risks. See:
# https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TITLE: ${{ github.event.pull_request.title }}
run: |
echo '
module.exports = {
extends: ["@commitlint/config-conventional"],
helpUrl:
"https://github.com/kumahq/kuma/blob/master/CONTRIBUTING.md#commit-message-format",
rules: {
"body-max-line-length": [0],
"footer-max-line-length": [0],
"footer-leading-blank": [0],
"header-max-length": [0],
// Disable some common mistyped scopes and some that should be used
"scope-enum": [2, "never", [
"kumacp", "kumadp", "kumacni", "kumainit", "*", "madr", "test", "ci", "perf", "policies", "tests"
]],
"scope-empty": [2, "never"]
},
};
' > commitlint.config.js
# Create a temporary commitlint configuration file
cat <<EOF > commitlint.config.js
module.exports = {
extends: ["@commitlint/config-conventional"],
helpUrl: "https://github.com/kumahq/kuma/blob/master/CONTRIBUTING.md#commit-message-format",
rules: {
"body-max-line-length": [0],
"footer-max-line-length": [0],
"footer-leading-blank": [0],
"header-max-length": [0],
"scope-enum": [2, "never", [
"kumacp", "kumadp", "kumacni", "kumainit", "*", "madr", "test", "ci", "perf", "policies", "tests"
]],
"scope-empty": [2, "never"]
},
};
EOF

# Install commitlint CLI and configuration
npm install -g @commitlint/[email protected] @commitlint/[email protected]
echo "${{ env.TITLE }}" | commitlint --config commitlint.config.js

# Validate the PR title. Use the intermediate variable to safely handle the title.
# '${{ env.TITLE }}' doesn't protect against injection, so "$TITLE" must be used instead.
echo "$TITLE" | commitlint --config commitlint.config.js
Loading