Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Refactor workflow security checks

Manav Rathi6dff39dd13df
diff --git a/.github/scripts/check_workflow_security.rb b/.github/scripts/check_workflow_security.rb
new file mode 100644
index 0000000000..605cd88ab3
--- /dev/null
+++ b/.github/scripts/check_workflow_security.rb
@@ -0,0 +1,143 @@
+#!/usr/bin/env ruby
+
+require "open3"
+require "set"
+require "yaml"
+
+SENSITIVE_TRIGGERS = %w[
+ pull_request_target
+ issue_comment
+ pull_request_review_comment
+ discussion_comment
+ workflow_run
+].to_set.freeze
+
+WORKFLOW_PATH = %r{\A\.github/workflows/.*\.ya?ml\z}
+USES_REF = %r{\A([A-Za-z0-9._-]+/[A-Za-z0-9._-]+(?:/[A-Za-z0-9._/-]+)?)@(\S+)\z}
+FULL_SHA = /\A[0-9a-fA-F]{40}\z/
+
+def git(*args)
+ stdout, stderr, status = Open3.capture3("git", *args)
+ return stdout if status.success?
+
+ abort(stderr.empty? ? "git #{args.join(' ')} failed" : stderr)
+end
+
+def blob(revision, path)
+ stdout, _stderr, status = Open3.capture3("git", "show", "#{revision}:#{path}")
+ status.success? ? stdout : ""
+end
+
+def workflow_changes(base, head)
+ git("diff", "--name-status", "--find-renames", "--diff-filter=AMR", "#{base}...#{head}")
+ .lines
+ .each_with_object([]) do |line, changes|
+ status, path, new_path = line.chomp.split("\t")
+ head_path = status.start_with?("R") ? new_path : path
+ next unless head_path.match?(WORKFLOW_PATH)
+
+ base_path = status.start_with?("R") && path.match?(WORKFLOW_PATH) ? path : head_path
+ changes << { base: base_path, head: head_path }
+ end
+end
+
+def workflow_yaml(revision, path)
+ YAML.safe_load(
+ blob(revision, path),
+ aliases: true,
+ ) || {}
+rescue Psych::Exception => e
+ abort("Failed to parse workflow YAML in #{revision}:#{path}: #{e.message}")
+end
+
+def trigger_names(workflow)
+ events = workflow["on"] || workflow[true]
+ return [events] if events.is_a?(String)
+ return events.grep(String) if events.is_a?(Array)
+ return events.keys.map(&:to_s) if events.is_a?(Hash)
+
+ []
+end
+
+def uses_values(node)
+ case node
+ when Hash
+ node.flat_map do |key, value|
+ [key.to_s == "uses" && value.is_a?(String) ? value : nil, *uses_values(value)]
+ end.compact
+ when Array
+ node.flat_map { |value| uses_values(value) }
+ else
+ []
+ end
+end
+
+def workflow_facts(revision, path)
+ workflow = workflow_yaml(revision, path)
+ triggers = trigger_names(workflow).to_set & SENSITIVE_TRIGGERS
+ unpinned_actions = uses_values(workflow).each_with_object(Set.new) do |uses, actions|
+ action, ref = uses.match(USES_REF)&.captures
+ next unless action
+ next if action.start_with?("actions/") || ref.match?(FULL_SHA)
+
+ actions.add("#{action}@#{ref}")
+ end
+
+ { triggers: triggers, unpinned_actions: unpinned_actions }
+end
+
+base, head = ARGV
+abort("Usage: #{$PROGRAM_NAME} <base-revision> <head-revision>") unless base && head
+
+changes = workflow_changes(base, head)
+if changes.empty?
+ puts "Workflow Security Checks: Passed"
+ puts "No workflow files changed."
+ exit 0
+end
+
+trigger_violations = []
+unpinned_violations = []
+
+changes.each do |change|
+ path = change[:head]
+ before = workflow_facts(base, change[:base])
+ after = workflow_facts(head, path)
+
+ (after[:triggers] - before[:triggers]).each do |trigger|
+ trigger_violations << "#{path}: #{trigger}"
+ end
+
+ (after[:unpinned_actions] - before[:unpinned_actions]).each do |action|
+ unpinned_violations << "#{path}: #{action}"
+ end
+end
+
+failed = trigger_violations.any? || unpinned_violations.any?
+puts "Workflow Security Checks: #{failed ? "Failed" : "Passed"}"
+puts
+puts "Checked workflow files:"
+changes.each do |change|
+ rename = change[:base] == change[:head] ? "" : " (renamed from #{change[:base]})"
+ puts "- #{change[:head]}#{rename}"
+end
+
+exit 0 unless failed
+
+unless trigger_violations.empty?
+ puts
+ puts "New privileged triggers:"
+ trigger_violations.each { |violation| puts "- #{violation}" }
+end
+
+unless unpinned_violations.empty?
+ puts
+ puts "New unpinned third-party actions:"
+ unpinned_violations.each { |violation| puts "- #{violation}" }
+end
+
+puts
+puts "Fix:"
+puts "- Remove newly added privileged triggers." if trigger_violations.any?
+puts "- Pin third-party actions to a full 40-character commit SHA." if unpinned_violations.any?
+exit 1
diff --git a/.github/workflows/security-check-workflows.yml b/.github/workflows/security-check-workflows.yml
deleted file mode 100644
index 4c1b945b35..0000000000
--- a/.github/workflows/security-check-workflows.yml
+++ /dev/null
@@ -1,180 +0,0 @@
-name: Security - Check Workflows
-
-on:
- pull_request:
- paths:
- - '.github/workflows/**'
-
-permissions:
- contents: read
-
-jobs:
- check-privileged-workflow-patterns:
- runs-on: ubuntu-latest
- environment:
- name: workflow-security-review
- # This requires admin approval in repository settings
- steps:
- - name: Checkout PR
- uses: actions/checkout@v6
- with:
- fetch-depth: 0
-
- - name: Check workflow security guardrails
- shell: bash
- run: |
- extract_sensitive_triggers() {
- local revision=$1
- local file=$2
-
- if ! git cat-file -e "${revision}:${file}" 2>/dev/null; then
- return 0
- fi
-
- git show "${revision}:${file}" | ruby -e '
- require "date"
- require "yaml"
-
- sensitive = %w[
- pull_request_target
- issue_comment
- pull_request_review_comment
- discussion_comment
- workflow_run
- ]
-
- workflow = YAML.safe_load(
- STDIN.read,
- aliases: true,
- permitted_classes: [Date, Time, DateTime]
- ) || {}
- events = case workflow["on"] || workflow[true]
- when String
- [workflow["on"] || workflow[true]]
- when Array
- (workflow["on"] || workflow[true]).grep(String)
- when Hash
- (workflow["on"] || workflow[true]).keys.map(&:to_s)
- else
- []
- end
-
- puts events.select { |event| sensitive.include?(event) }.uniq.sort
- '
- }
-
- extract_unpinned_third_party_actions() {
- local revision=$1
- local file=$2
-
- if ! git cat-file -e "${revision}:${file}" 2>/dev/null; then
- return 0
- fi
-
- git show "${revision}:${file}" | ruby -e '
- FULL_SHA = /\A[0-9a-f]{40}\z/
-
- refs = STDIN.read.scan(
- /^\s*-?\s*uses:\s*([A-Za-z0-9._-]+\/[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._\/-]+)?)@([^\s#]+)/
- )
- refs.each do |action, ref|
- next if action.start_with?("actions/")
- next if ref.match?(FULL_SHA)
-
- puts "#{action}@#{ref}"
- end
- ' | sort -u
- }
-
- mapfile -t modified_workflows < <(
- git diff --name-only --diff-filter=AMR "origin/${{ github.base_ref }}...HEAD" |
- grep -E '^\.github/workflows/.*\.ya?ml$' || true
- )
-
- if [ "${#modified_workflows[@]}" -eq 0 ]; then
- echo "No workflow files modified"
- exit 0
- fi
-
- printf 'Checking modified workflows:\n'
- printf ' - %s\n' "${modified_workflows[@]}"
-
- sensitive_triggers=(
- "pull_request_target"
- "issue_comment"
- "pull_request_review_comment"
- "discussion_comment"
- "workflow_run"
- )
- violations=()
- unpinned_violations=()
-
- for file in "${modified_workflows[@]}"; do
- if ! current_triggers=$(extract_sensitive_triggers HEAD "$file"); then
- echo "❌ ERROR: Failed to parse workflow triggers in HEAD:$file"
- exit 1
- fi
-
- if ! base_triggers=$(extract_sensitive_triggers "origin/${{ github.base_ref }}" "$file"); then
- echo "❌ ERROR: Failed to parse workflow triggers in origin/${{ github.base_ref }}:$file"
- exit 1
- fi
-
- for trigger in "${sensitive_triggers[@]}"; do
- if grep -Fxq "$trigger" <<< "$current_triggers"; then
- if grep -Fxq "$trigger" <<< "$base_triggers"; then
- echo "βœ“ $file: ${trigger} already existed (allowed)"
- else
- echo "βœ— $file: NEW ${trigger} trigger detected!"
- violations+=("$file:${trigger}")
- fi
- fi
- done
-
- if ! current_unpinned=$(extract_unpinned_third_party_actions HEAD "$file"); then
- echo "❌ ERROR: Failed to parse action refs in HEAD:$file"
- exit 1
- fi
-
- if ! base_unpinned=$(extract_unpinned_third_party_actions "origin/${{ github.base_ref }}" "$file"); then
- echo "❌ ERROR: Failed to parse action refs in origin/${{ github.base_ref }}:$file"
- exit 1
- fi
-
- while IFS= read -r action_ref; do
- [ -z "$action_ref" ] && continue
-
- if grep -Fxq "$action_ref" <<< "$base_unpinned"; then
- echo "βœ“ $file: ${action_ref} already existed unpinned (allowed for now)"
- else
- echo "βœ— $file: NEW unpinned third-party action ${action_ref} detected!"
- unpinned_violations+=("$file:${action_ref}")
- fi
- done <<< "$current_unpinned"
- done
-
- if [ "${#violations[@]}" -gt 0 ]; then
- echo ""
- echo "❌ ERROR: New privileged workflow triggers detected:"
- printf ' - %s\n' "${violations[@]}"
- echo ""
- echo "These triggers can execute with repository trust or be chained"
- echo "from untrusted pull requests into privileged automation."
- echo ""
- echo "If you need to add one of these triggers, please:"
- echo "1. Discuss with maintainers first"
- echo "2. Ensure the workflow never executes untrusted PR code with write tokens or secrets"
- echo "3. Get explicit approval before merging"
- exit 1
- fi
-
- if [ "${#unpinned_violations[@]}" -gt 0 ]; then
- echo ""
- echo "❌ ERROR: New unpinned third-party workflow actions detected:"
- printf ' - %s\n' "${unpinned_violations[@]}"
- echo ""
- echo "Pin third-party actions to a full commit SHA before merging."
- exit 1
- fi
-
- echo "βœ“ No new privileged triggers or unpinned third-party actions detected"
diff --git a/.github/workflows/workflow-security-checks.yml b/.github/workflows/workflow-security-checks.yml
new file mode 100644
index 0000000000..cc5c0feebb
--- /dev/null
+++ b/.github/workflows/workflow-security-checks.yml
@@ -0,0 +1,25 @@
+name: Workflow Security Checks
+
+on:
+ pull_request:
+ paths:
+ - '.github/workflows/**'
+ - '.github/scripts/check_workflow_security.rb'
+
+permissions:
+ contents: read
+
+jobs:
+ workflow-security-checks:
+ runs-on: ubuntu-latest
+ environment:
+ # Require admin approval for workflow changes.
+ name: workflow-change-approval
+ steps:
+ - name: Checkout PR
+ uses: actions/checkout@v6
+ with:
+ fetch-depth: 0
+
+ - name: Run workflow security checks
+ run: ruby .github/scripts/check_workflow_security.rb "origin/${{ github.base_ref }}" HEAD