forked from starifly/NekoBoxForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Require explicit release publication opt-in #144
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
Merged
+168
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
|
|
||
| WORKFLOW=".github/workflows/release.yml" | ||
|
|
||
| has_safe_publish_input() { | ||
| local file=$1 | ||
| local block line | ||
| local has_type=false | ||
| local has_default=false | ||
|
|
||
| block=$( | ||
| awk ' | ||
| /^ workflow_dispatch:[[:space:]]*$/ { in_dispatch = 1; next } | ||
| in_dispatch && /^ inputs:[[:space:]]*$/ { in_inputs = 1; next } | ||
| in_inputs && /^ publish:[[:space:]]*$/ { in_publish = 1; next } | ||
| in_publish && $0 !~ /^ / && $0 !~ /^[[:space:]]*$/ { exit } | ||
| in_publish { print } | ||
| ' "$file" | ||
| ) | ||
|
|
||
| while IFS= read -r line; do | ||
| [[ "$line" =~ ^[[:space:]]+type:[[:space:]]*boolean[[:space:]]*$ ]] && has_type=true | ||
| [[ "$line" =~ ^[[:space:]]+default:[[:space:]]*false[[:space:]]*$ ]] && has_default=true | ||
| done <<< "$block" | ||
|
|
||
| $has_type && $has_default | ||
| } | ||
|
|
||
| publish_job_block() { | ||
| local file=$1 | ||
| awk ' | ||
| /^ publish:[[:space:]]*$/ { in_publish = 1; next } | ||
| in_publish && $0 !~ /^ / && $0 !~ /^[[:space:]]*$/ { exit } | ||
| in_publish { print } | ||
| ' "$file" | ||
| } | ||
|
|
||
| has_positive_publish_condition() { | ||
| local block=$1 | ||
| local line | ||
| while IFS= read -r line; do | ||
| if [[ "$line" =~ ^[[:space:]]+if:[[:space:]]*\$\{\{[[:space:]]*inputs\.publish[[:space:]]*==[[:space:]]*true[[:space:]]*\}\}[[:space:]]*$ ]]; then | ||
| return 0 | ||
| fi | ||
| done <<< "$block" | ||
| return 1 | ||
| } | ||
|
|
||
| has_publish_write_permission() { | ||
| local block=$1 | ||
| local line | ||
| local in_permissions=false | ||
| while IFS= read -r line; do | ||
| if [[ "$line" =~ ^[[:space:]]{4}permissions:[[:space:]]*$ ]]; then | ||
| in_permissions=true | ||
| continue | ||
| fi | ||
| if $in_permissions && [[ "$line" =~ ^[[:space:]]{4}[^[:space:]] ]]; then | ||
| break | ||
| fi | ||
| if $in_permissions && [[ "$line" =~ ^[[:space:]]+contents:[[:space:]]*write[[:space:]]*$ ]]; then | ||
| return 0 | ||
| fi | ||
| done <<< "$block" | ||
| return 1 | ||
| } | ||
|
|
||
| has_workflow_read_permission() { | ||
| local file=$1 | ||
| local line | ||
| local in_permissions=false | ||
| while IFS= read -r line; do | ||
| if [[ "$line" =~ ^permissions:[[:space:]]*$ ]]; then | ||
| in_permissions=true | ||
| continue | ||
| fi | ||
| if $in_permissions && [[ "$line" =~ ^[^[:space:]] ]]; then | ||
| break | ||
| fi | ||
| if $in_permissions && [[ "$line" =~ ^[[:space:]]+contents:[[:space:]]*read[[:space:]]*$ ]]; then | ||
| return 0 | ||
| fi | ||
| done < "$file" | ||
| return 1 | ||
| } | ||
|
|
||
| check_file() { | ||
| local file=$1 | ||
| local failed=0 | ||
| local content | ||
| local publish_block | ||
| content=$(<"$file") | ||
| publish_block=$(publish_job_block "$file") | ||
|
|
||
| if ! has_safe_publish_input "$file"; then | ||
| echo "Error: release publish input must be a boolean that defaults to false: $file" >&2 | ||
| failed=1 | ||
| fi | ||
|
|
||
| if ! has_positive_publish_condition "$publish_block"; then | ||
| echo "Error: publish job must use the positive inputs.publish == true condition: $file" >&2 | ||
| failed=1 | ||
| fi | ||
|
|
||
| if [[ "$content" == *"github.event.inputs.publish != 'y'"* ]]; then | ||
| echo "Error: legacy fail-open release publish condition is present: $file" >&2 | ||
| failed=1 | ||
| fi | ||
|
|
||
| if ! has_workflow_read_permission "$file"; then | ||
| echo "Error: workflow-level contents permission must remain read-only: $file" >&2 | ||
| failed=1 | ||
| fi | ||
|
|
||
| if ! has_publish_write_permission "$publish_block"; then | ||
| echo "Error: publish job must retain contents write permission: $file" >&2 | ||
| failed=1 | ||
| fi | ||
|
|
||
| return "$failed" | ||
| } | ||
|
|
||
| self_test() { | ||
| check_file "$WORKFLOW" | ||
|
|
||
| local tmp_dir | ||
| local unsafe_workflow | ||
| local quoted_tmp_dir | ||
| tmp_dir=$(mktemp -d) | ||
| unsafe_workflow="$tmp_dir/release.yml" | ||
| printf -v quoted_tmp_dir '%q' "$tmp_dir" | ||
| trap "rm -rf -- $quoted_tmp_dir" EXIT | ||
|
|
||
| cp "$WORKFLOW" "$unsafe_workflow" | ||
| sed 's#if: \${{ inputs\.publish == true }}#if: github.event.inputs.publish != '"'"'y'"'"'#' \ | ||
| "$unsafe_workflow" > "$unsafe_workflow.tmp" | ||
| mv "$unsafe_workflow.tmp" "$unsafe_workflow" | ||
|
|
||
| if check_file "$unsafe_workflow"; then | ||
| echo "Error: release publish guard accepted the prior unsafe condition" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| rm -rf -- "$tmp_dir" | ||
| trap - EXIT | ||
| echo "OK: release publish gate is explicit and rejects the prior unsafe condition." | ||
| } | ||
|
|
||
| case "${1:-}" in | ||
| "") | ||
| check_file "$WORKFLOW" | ||
| echo "OK: release publish gate is explicit and safe by default." | ||
| ;; | ||
| --self-test) | ||
| self_test | ||
| ;; | ||
| *) | ||
| echo "Usage: $0 [--self-test]" >&2 | ||
| exit 2 | ||
| ;; | ||
| esac | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This guard still requires one exact spelling of the publish condition. A workflow with the same boolean input defaulting to
falseand an equivalent positive gate such asif: ${{ inputs.publish }}remains explicitly opt-in, but this regex rejects it and blocks CI. The guard should check the publish job's safety property instead of requiring this single expression form.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.
No change. The guard intentionally requires the explicit typed comparison from the release policy:
inputs.publish == true. Shorthand truthiness is excluded so future edits cannot weaken the positive boolean opt-in. Field order and unrelated job settings remain flexible.