Skip to content
84 changes: 84 additions & 0 deletions .github/workflows/recipe-security-scanner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,91 @@ permissions:
statuses: write

jobs:
# Forks are evaluated from GitHub file/review metadata only; recipe content
# never enters this job or the secret-bearing scanner.
fork-review-boundary:
name: Fork recipe review boundary
if: ${{ github.event.pull_request.head.repo.full_name != github.repository }}
permissions:
# getCollaboratorPermissionLevel requires push access. This job runs only
# pinned base-branch JavaScript against GitHub metadata and never checks
# out or reads fork recipe content.
contents: write
Comment thread
artiehinz marked this conversation as resolved.
Resolved
pull-requests: read
runs-on: ubuntu-latest
steps:
- name: Require write-access approval
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const recipePrefix = 'documentation/src/pages/recipes/data/recipes/';
const pullNumber = context.payload.pull_request.number;
const request = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullNumber,
per_page: 100,
};

const files = await github.paginate(github.rest.pulls.listFiles, request);
const hasReviewableRecipe = files.some(
(file) => file.filename.startsWith(recipePrefix) && file.status !== 'removed'
);

if (!hasReviewableRecipe) {
core.notice('No added or modified recipe requires the fork review boundary.');
return;
}

const currentHead = context.payload.pull_request.head.sha;
const reviews = await github.paginate(github.rest.pulls.listReviews, request);
const latestByReviewer = new Map();
for (const review of reviews) {
if (!review.user?.login || !review.submitted_at) continue;
const state = review.state?.toUpperCase();
if (!['APPROVED', 'CHANGES_REQUESTED', 'DISMISSED'].includes(state)) continue;
const current = latestByReviewer.get(review.user.login);
if (!current || new Date(review.submitted_at) > new Date(current.submitted_at)) {
latestByReviewer.set(review.user.login, review);
}
}

const allowedPermissions = new Set(['admin', 'maintain', 'write']);
const approvers = [];
for (const review of latestByReviewer.values()) {
if (review.state?.toUpperCase() !== 'APPROVED') continue;
if (review.commit_id !== currentHead) continue;
Comment thread
artiehinz marked this conversation as resolved.
Resolved
const response = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: review.user.login,
});
if (allowedPermissions.has(response.data.permission)) {
approvers.push(review.user.login);
}
}

if (approvers.length === 0) {
core.setFailed(
'Fork recipe instructions cannot enter the secret-bearing AI scanner. ' +
'The current head requires an approving review from a user with write access. ' +
'After approval, rerun this failed job.'
);
return;
}

core.notice(`Current fork head approved by @${approvers.join(', @')}.`);
await core.summary
.addHeading('Fork recipe review boundary')
.addRaw(
'The privileged AI scan was not run because this recipe comes from a fork. ' +
`Write-access approval of the current head was verified from @${approvers.join(', @')}.`
)
.write();

security-scan:
# Never load fork-controlled recipe instructions into the privileged scanner.
if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
Comment thread
artiehinz marked this conversation as resolved.
Resolved
Comment thread
artiehinz marked this conversation as resolved.
Resolved
runs-on: ubuntu-latest
steps:
- name: Harden Runner
Expand Down