Skip to content

armgit / ente

Public Imported from GitHub
0

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

Prepare checks for trusted PR approvals

Manav Rathi12d0dd583e02
diff --git a/.github/checks/change-approval/rust.mjs b/.github/checks/change-approval/rust.mjs
index 7d6db20a90..072b38fd17 100644
--- a/.github/checks/change-approval/rust.mjs
+++ b/.github/checks/change-approval/rust.mjs
@@ -5,6 +5,7 @@ const cargoLints = (source) =>
execFileSync(
"python3",
[
+ "-I",
"-c",
`
import json, sys, tomllib
diff --git a/.github/checks/change-approval/test.mjs b/.github/checks/change-approval/test.mjs
index 3e8620cff8..5f455ab918 100644
--- a/.github/checks/change-approval/test.mjs
+++ b/.github/checks/change-approval/test.mjs
@@ -1061,6 +1061,7 @@ test("workflow scans PR changes with the complete checker from main", (t) => {
const { output, summary } = scan(
t,
{
+ "rust/Cargo.toml": '[lints.rust]\nunsafe_code = "deny"\n',
[`${checkerDir}/rust.mjs`]:
'export { checkRust } from "./additional-rule.mjs";',
[`${checkerDir}/additional-rule.mjs`]: readFileSync(
@@ -1069,6 +1070,8 @@ test("workflow scans PR changes with the complete checker from main", (t) => {
),
},
{
+ "rust/Cargo.toml": '[lints.rust]\nunsafe_code = "allow"\n',
+ "tomllib.py": "def loads(source):\n return {}\n",
[`${checkerDir}/additional-rule.mjs`]:
"export function checkRust() { return []; }",
[`${checkerDir}/web.mjs`]:
@@ -1085,4 +1088,5 @@ test("workflow scans PR changes with the complete checker from main", (t) => {
);
assert.match(summary, /- `src\/lib.rs`/);
assert.match(summary, /- `web\/example.ts`/);
+ assert.match(summary, /- `rust\/Cargo.toml`/);
});
diff --git a/.github/checks/run.sh b/.github/checks/run.sh
index f578694780..28efdbb78a 100755
--- a/.github/checks/run.sh
+++ b/.github/checks/run.sh
@@ -9,6 +9,7 @@ node .github/checks/documentation-comments/check.mjs
node .github/checks/documentation-comments/test.mjs
ruby .github/checks/workflow-paths/check.rb
ruby .github/checks/workflow-paths/test.rb
+ruby .github/checks/workflow-security/test.rb
node .github/checks/wasm-ci-paths/check.mjs
node --test .github/checks/wasm-ci-paths/test.mjs
node --test .github/scripts/ci/test.mjs
diff --git a/.github/checks/workflow-security/check.rb b/.github/checks/workflow-security/check.rb
index 91863f694b..c9ba6d9bc1 100644
--- a/.github/checks/workflow-security/check.rb
+++ b/.github/checks/workflow-security/check.rb
@@ -73,6 +73,8 @@ unpinned_violations = []
checked_files.each do |path|
facts = workflow_facts(path)
facts[:triggers].each do |trigger|
+ next if path == ".github/workflows/pr-approval.yml" && trigger == "pull_request_target"
+
trigger_violations << "#{path}: #{trigger}"
end
diff --git a/.github/checks/workflow-security/test.rb b/.github/checks/workflow-security/test.rb
new file mode 100644
index 0000000000..38e656c3d0
--- /dev/null
+++ b/.github/checks/workflow-security/test.rb
@@ -0,0 +1,42 @@
+require "fileutils"
+require "open3"
+require "rbconfig"
+require "tmpdir"
+
+CHECK = File.expand_path("check.rb", __dir__)
+
+def run(files)
+ Dir.mktmpdir("ente-workflow-security-") do |root|
+ files.each do |path, source|
+ destination = "#{root}/#{path}"
+ FileUtils.mkdir_p(File.dirname(destination))
+ File.write(destination, source)
+ end
+ Open3.capture3(RbConfig.ruby, CHECK, chdir: root)
+ end
+end
+
+approval = ".github/workflows/pr-approval.yml"
+stdout, stderr, status = run(approval => "on: pull_request_target\n")
+raise "#{stdout}#{stderr}" unless status.success?
+
+{
+ ".github/workflows/other.yml" => "on: [pull_request_target]\n",
+ ".github/workflows/pr-approval.yaml" => "on:\n pull_request_target:\n",
+}.each do |path, source|
+ stdout, stderr, status = run(approval => "on: pull_request_target\n", path => source)
+ raise "#{stdout}#{stderr}" unless status.exitstatus == 1 && stdout.include?("#{path}: pull_request_target")
+end
+
+stdout, stderr, status = run(approval => <<~YAML)
+ on: [pull_request_target, workflow_run]
+ jobs:
+ approve:
+ steps:
+ - uses: actions/github-script@v9
+YAML
+raise "#{stdout}#{stderr}" unless status.exitstatus == 1
+["workflow_run", "actions/github-script@v9"].each do |violation|
+ raise stdout unless stdout.include?("#{approval}: #{violation}")
+end
+raise stdout if stdout.include?("#{approval}: pull_request_target")
diff --git a/.github/workflows/change-approval.yml b/.github/workflows/change-approval.yml
index 3fb077759e..7f9ce902f6 100644
--- a/.github/workflows/change-approval.yml
+++ b/.github/workflows/change-approval.yml
@@ -1,5 +1,6 @@
name: Change approval
+# Guardrail for accidental policy changes; this workflow remains PR-editable.
on:
pull_request: