Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Harden `pull_request_target` workflows against untrusted PR input (#5644)

Dhruv Bhanushali6ddcf9e8c653
diff --git a/.github/workflows/pr_limit_reminders.yml b/.github/workflows/pr_limit_reminders.yml
index 68197d8e1..f0460ed68 100644
--- a/.github/workflows/pr_limit_reminders.yml
+++ b/.github/workflows/pr_limit_reminders.yml
@@ -8,19 +8,28 @@ jobs:
analyze-user-prs:
name: Count user's open pull requests
runs-on: ubuntu-latest
+ permissions:
+ contents: read
+ pull-requests: read
outputs:
pr_count: ${{ steps.count-prs.outputs.pr_count }}
slack_id: ${{ steps.count-prs.outputs.slack_id }}
should_alert: ${{ steps.count-prs.outputs.should_alert }}
steps:
- - uses: actions/checkout@v4
+ # WARNING: This workflow runs on `pull_request_target`, with access to
+ # secrets. Never check out the PR head (e.g. via `ref`) here, or the PR
+ # author's code will run with those secrets.
+ - name: Checkout repository
+ uses: actions/checkout@v4
+ with:
+ persist-credentials: false
+ sparse-checkout: automations/js/src
- name: Count user PRs
uses: actions/github-script@v7
id: count-prs
env:
GH_SLACK_USERNAME_MAP: ${{ secrets.GH_SLACK_USERNAME_MAP }}
with:
- github-token: ${{ secrets.ACCESS_TOKEN }}
script: |
const script = require('./automations/js/src/count_user_reviewable_prs.js')
await script({github, context, core})
@@ -30,6 +39,7 @@ jobs:
name: Send Slack message
if: needs.analyze-user-prs.outputs.pr_count >= 5
runs-on: ubuntu-latest
+ permissions: {}
env:
pr_count: ${{ needs.analyze-user-prs.outputs.pr_count }}
slack_id: ${{ needs.analyze-user-prs.outputs.slack_id }}
diff --git a/.github/workflows/pr_ping.yml b/.github/workflows/pr_ping.yml
index 55ba44091..bbd3fdd20 100644
--- a/.github/workflows/pr_ping.yml
+++ b/.github/workflows/pr_ping.yml
@@ -17,6 +17,7 @@ jobs:
!startsWith(github.event.pull_request.title, '🔄') &&
github.repository_owner == 'WordPress'
runs-on: ubuntu-latest
+ permissions: {}
env:
EVENT_ACTION: ${{ github.event.action }}
steps:
@@ -26,21 +27,29 @@ jobs:
script: |
let { writeFileSync } = await import('fs')
+ // Slack parses `<...>` in mrkdwn as mentions and links, so
+ // escape user-controlled text to prevent pings and spoofed links.
+ // Ref: https://api.slack.com/reference/surfaces/formatting#escaping
+ const escape = (text) =>
+ text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
+
const repoName = `${context.repo.owner}/${context.repo.repo}`
const { pull_request: pr } = context.payload
+ const title = escape(pr.title)
+ const author = escape(pr.user.login)
const isNew = process.env.EVENT_ACTION === 'opened'
const emoji = pr.draft ? ':pr-draft:' : ':pull-request:'
const adjective = isNew ? pr.draft ? 'New draft ' : 'New ' : ''
const verb = isNew ? 'opened' : 'marked ready for review'
const payload = {
- text: `${adjective}PR ${verb} by ${pr.user.login} in ${repoName}: #${pr.number} - ${pr.title}`,
+ text: `${adjective}PR ${verb} by ${author} in ${repoName}: #${pr.number} - ${title}`,
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
- text: `${emoji} ${adjective}PR ${verb} by *${pr.user.login}* in \`${repoName}\`:\n<${pr.html_url}|#${pr.number} - ${pr.title}>`
+ text: `${emoji} ${adjective}PR ${verb} by *${author}* in \`${repoName}\`:\n<${pr.html_url}|#${pr.number} - ${title}>`
}
}
]