An open source design system that's fully customizable and agent ready
TypeScript
fix(ci): load review helper from the trusted workflow SHA (#6512)
Cindy Zhangc946b3b386e7
diff --git a/.github/scripts/review-signal-workflow.test.mjs b/.github/scripts/review-signal-workflow.test.mjsindex ff27d64bce..970dc7e39c 100644--- a/.github/scripts/review-signal-workflow.test.mjs+++ b/.github/scripts/review-signal-workflow.test.mjs@@ -1,8 +1,16 @@// Copyright (c) Meta Platforms, Inc. and affiliates.+/**+ * @file Review signal workflow contracts, including executable helper loading.+ * @input The actual privileged script, trusted helper bytes, and stub GitHub APIs.+ * @output Ref-provenance, fail-closed, and unchanged review-routing assertions.+ * @position Node regression coverage for review-signal.yml.+ */++import {Buffer} from 'node:buffer';import fs from 'node:fs';import path from 'node:path';-import {describe, expect, it} from 'vitest';+import {describe, expect, it, vi} from 'vitest';import YAML from 'yaml';const root = path.resolve(import.meta.dirname, '../..');@@ -10,6 +18,221 @@ const workflow = fs.readFileSync(path.join(root, '.github/workflows/review-signal.yml'),'utf8',);+const parsed = YAML.parse(workflow);+const script = parsed.jobs.flag.steps.find(+ step => step.name === 'Detect signals and route',+).with.script;+const HELPER_PATH = '.github/scripts/review-signal-decision.cjs';+const WORKFLOW_SHA = '1'.repeat(40);+const OLD_BASE = '2'.repeat(40);+const HEAD = '3'.repeat(40);++async function runSignal({+ eventName = 'workflow_dispatch',+ baseSha = OLD_BASE,+ backfill = false,+ helperMissing = false,+ reviews = [],+} = {}) {+ const pr = {+ number: 42,+ user: {login: 'contributor', type: 'User'},+ base: {sha: baseSha, ref: 'main'},+ head: {sha: HEAD, ref: 'feature'},+ changed_files: 1,+ labels: [{name: 'needs:code-review'}, {name: 'community'}],+ };+ const mutations = {+ createCommitStatus: vi.fn(),+ addLabels: vi.fn(),+ removeLabel: vi.fn(),+ requestReviewers: vi.fn(),+ updateCheck: vi.fn(),+ graphql: vi.fn(),+ };+ const getContent = vi.fn(async ({path: filePath, ref}) => {+ if (filePath === HELPER_PATH) {+ if (ref !== WORKFLOW_SHA || helperMissing) {+ throw Object.assign(new Error('Not Found'), {status: 404});+ }+ } else if (+ filePath !== '.github/scripts/lib/classify-visual.js' ||+ ref !== baseSha+ ) {+ throw new Error(`Unexpected content read: ${ref}:${filePath}`);+ }+ return {+ data: {+ type: 'file',+ content: Buffer.from(+ fs.readFileSync(path.join(root, filePath)),+ ).toString('base64'),+ },+ };+ });+ const github = {+ paginate: async (method, options) => (await method(options)).data,+ request: async () => ({data: ''}),+ graphql: mutations.graphql,+ rest: {+ pulls: {+ get: vi.fn(async () => ({data: pr})),+ list: vi.fn(async () => ({data: [pr]})),+ listFiles: vi.fn(async () => ({+ data: [{filename: 'README.md', status: 'modified'}],+ })),+ listReviews: async () => ({data: reviews}),+ requestReviewers: mutations.requestReviewers,+ },+ repos: {getContent, createCommitStatus: mutations.createCommitStatus},+ issues: {+ addLabels: mutations.addLabels,+ removeLabel: mutations.removeLabel,+ },+ checks: {+ listForRef: async () => ({data: {check_runs: []}}),+ update: mutations.updateCheck,+ },+ },+ };+ const core = {info: vi.fn(), warning: vi.fn(), setFailed: vi.fn()};+ const execute = new Function(+ 'github',+ 'context',+ 'core',+ 'process',+ 'Buffer',+ `return (async () => {\n${script}\n})();`,+ );+ await execute(+ github,+ {+ repo: {owner: 'facebook', repo: 'astryx'},+ sha: WORKFLOW_SHA,+ eventName,+ payload:+ eventName === 'workflow_dispatch'+ ? {inputs: {pr: backfill ? '' : '42'}}+ : {pull_request: pr},+ },+ core,+ {+ env: {+ ...parsed.env,+ ENG_OWNERS: '@engineer',+ DESIGN_OWNERS: '@designer',+ },+ },+ Buffer,+ );+ return {github, core, mutations, getContent};+}++function expectTrustedHelperRead(getContent) {+ expect(+ getContent.mock.calls+ .map(([input]) => input)+ .filter(input => input.path === HELPER_PATH),+ ).toEqual([+ {+ owner: 'facebook',+ repo: 'astryx',+ ref: WORKFLOW_SHA,+ path: HELPER_PATH,+ },+ ]);+}++describe('review-signal trusted helper ref', () => {+ it('only runs the helper-loading job in trusted execution contexts', () => {+ expect(parsed.jobs.flag.if.trim()).toBe(+ "github.event_name == 'pull_request_target' || github.event_name == 'workflow_dispatch'",+ );+ expect(+ parsed.jobs.flag.steps.some(step =>+ step.uses?.startsWith('actions/checkout@'),+ ),+ ).toBe(false);+ });++ it.each([+ ['old-base dispatch', {eventName: 'workflow_dispatch'}],+ ['old-base backfill', {eventName: 'workflow_dispatch', backfill: true}],+ [+ 'current-base dispatch',+ {eventName: 'workflow_dispatch', baseSha: WORKFLOW_SHA},+ ],+ ['old-base PR event', {eventName: 'pull_request_target'}],+ [+ 'current-base PR event',+ {eventName: 'pull_request_target', baseSha: WORKFLOW_SHA},+ ],+ ])(+ 'loads the helper from the workflow SHA for %s',+ async (_name, options) => {+ const h = await runSignal(options);+ expectTrustedHelperRead(h.getContent);+ expect(h.core.setFailed).not.toHaveBeenCalled();+ expect(h.core.warning).not.toHaveBeenCalled();+ expect(h.mutations.createCommitStatus).toHaveBeenCalledExactlyOnceWith({+ owner: 'facebook',+ repo: 'astryx',+ sha: HEAD,+ context: 'review-required',+ state: 'pending',+ description: 'Waiting on code review: community contribution',+ });+ if (options.eventName === 'workflow_dispatch') {+ expect(+ h.github.rest.pulls[options.backfill ? 'list' : 'get'],+ ).toHaveBeenCalledOnce();+ } else {+ expect(h.github.rest.pulls.get).not.toHaveBeenCalled();+ }+ },+ );++ it.each(['workflow_dispatch', 'pull_request_target'])(+ 'fails closed if the helper is missing at the workflow SHA for %s',+ async eventName => {+ const h = await runSignal({eventName, helperMissing: true});+ expectTrustedHelperRead(h.getContent);+ expect(h.core.warning).toHaveBeenCalledWith(+ 'Failed to flag PR #42: Not Found',+ );+ expect(h.core.setFailed).toHaveBeenCalledExactlyOnceWith(+ 'One or more PRs failed to flag; see warnings.',+ );+ expect(h.github.rest.pulls.listFiles).not.toHaveBeenCalled();+ for (const mutation of Object.values(h.mutations)) {+ expect(mutation).not.toHaveBeenCalled();+ }+ },+ );++ it.each([+ [OLD_BASE, 'pending'],+ [HEAD, 'success'],+ ])(+ 'keeps approval bound to the current PR head, not the helper ref (%s)',+ async (reviewedSha, state) => {+ const h = await runSignal({+ reviews: [+ {+ user: {login: 'engineer'},+ state: 'APPROVED',+ commit_id: reviewedSha,+ },+ ],+ });+ expectTrustedHelperRead(h.getContent);+ expect(h.core.setFailed).not.toHaveBeenCalled();+ expect(h.mutations.createCommitStatus).toHaveBeenCalledWith(+ expect.objectContaining({sha: HEAD, state}),+ );+ },+ );+});describe('review-signal appearance-only contract', () => {it('keeps the embedded privileged script syntactically valid', () => {diff --git a/.github/workflows/review-signal.yml b/.github/workflows/review-signal.ymlindex 42bf6704b7..4f697cecbc 100644--- a/.github/workflows/review-signal.yml+++ b/.github/workflows/review-signal.yml@@ -50,10 +50,11 @@## Uses pull_request_target so it can label / request reviewers / disable# auto-merge on fork PRs. It reads the PR file list, diff text, and changed Core-# base/head source bytes as inert API data. The classifiers and approval helper-# come from the trusted base ref. It reads eng/design owner lists from repository-# VARIABLES (vars.ENGOWNERS / vars.DESIGNOWNERS) — no checkout and no execution-# of PR-controlled code. Do not add checkout or build steps here.+# base/head source bytes as inert API data. The approval helper comes from the+# trusted workflow SHA; the visual classifier comes from the PR base ref. It reads+# eng/design owner lists from repository VARIABLES (vars.ENGOWNERS /+# vars.DESIGNOWNERS) — no checkout and no execution of PR-controlled code.+# Do not add checkout or build steps here.name: Review signal@@ -214,8 +215,10 @@ jobs:return;}+ // Match the trusted workflow version: an older PR base can predate+ // this helper even though the running workflow requires it.const { data: reviewDecisionFile } = await github.rest.repos.getContent({- owner, repo, ref: pr.base.sha,+ owner, repo, ref: context.sha,path: '.github/scripts/review-signal-decision.cjs',});const reviewDecisionSource = Buffer.from(