Skip to content

ci: use pull_request_target in project-sync so fork PRs get board updates - #483

Merged
nockawa merged 1 commit into
mainfrom
fix/project-sync-fork-pr-secrets
Jul 13, 2026
Merged

nockawa merged 1 commit into
mainfrom
fix/project-sync-fork-pr-secrets

Conversation

@nockawa

@nockawa nockawa commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Problem

Project Sync fails on every PR opened from a fork. First observed on #482 (failing run):

GH_TOKEN:                     ← empty
Found linked issue: #481
gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable.
##[error]Process completed with exit code 4.

GitHub deliberately withholds repository secrets from pull_request runs that originate in a fork, so secrets.PROJECT_TOKEN resolved to "" and gh exited 4 on the first API call.

This is not caused by anything in #482 — it affects any external contributor's PR. Prior Project Sync runs were all green only because they were issues events from the same repo, so the link-pr-to-issue job had never run from an untrusted context before.

Fix

Switch the trigger to pull_request_target, which runs in the base repo's context where the secret is available.

  • Updated both PR job guards — link-pr-to-issue (opened) and update-on-merge (closed). Both authenticate with the PAT, so both were broken on forks; changing only the first would have left the merge job silently dead.
  • Added workflow-level permissions: {} — every job uses PROJECT_TOKEN, so GITHUB_TOKEN needs no scopes at all.

Why pull_request_target is safe here

It's normally a repository-takeover footgun, so the constraints are documented in the trigger comment. It's sound in this workflow only because it never trusts fork content:

  1. No actions/checkout — attacker-supplied code is never fetched, let alone executed.
  2. The only fork-controlled values it reads (PR title, head ref) already flow through env: and are never inlined into a run: body — the existing script-injection guard, which the original author had already got right.
  3. permissions: {} strips GITHUB_TOKEN.

Adding a checkout of the PR head, or inlining ${{ … }} of any user-controlled field into a shell body, would break this. The comment says so explicitly.

Verification

  • Workflow YAML parses; triggers and all five job guards resolve as intended.
  • grep: no actions/checkout anywhere in the file; no stale bare pull_request triggers left behind.
  • Surviving github.event.pull_request.* references are correct — that payload object exists under pull_request_target too, and both are already routed via env:.

Note

pull_request_target loads the workflow from the base branch, so this only takes effect once merged to main. It does not retroactively fix #482's red check — that run stays in the history.

🤖 Generated with Claude Code

…ates

GitHub withholds repository secrets from `pull_request` runs originating in a
fork, so PROJECT_TOKEN resolved to "" and gh exited 4 ("set the GH_TOKEN
environment variable") on the first API call. Board sync therefore failed for
every external contributor's PR (first seen on PR #482, from a fork).

pull_request_target runs in the base repo's context, where the secret is
available. Switch the trigger and update both PR job guards — link-pr-to-issue
(opened) and update-on-merge (closed) — since both authenticate with the PAT
and both were broken on forks.

pull_request_target is safe here only because the workflow never trusts fork
content: it does not check out PR code, and the fork-controlled values it reads
(PR title, head ref) already go through `env:` rather than being inlined into
`run:`. Also add workflow-level `permissions: {}` — every job uses the PAT, so
GITHUB_TOKEN needs no scopes. Documented the invariant in the trigger comment.

Note: pull_request_target loads the workflow from the base branch, so this takes
effect only once merged to main; it does not retroactively fix PR #482's run.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nockawa
nockawa merged commit 447adf4 into main Jul 13, 2026
5 checks passed
@nockawa
nockawa deleted the fix/project-sync-fork-pr-secrets branch July 13, 2026 17:54
nockawa added a commit that referenced this pull request Jul 14, 2026
* ci: block closing keywords that would auto-close a pull request

GitHub scans a PR's description and its commit messages for a bare KEYWORD-then-
reference adjacency (close/closes/closed, fix/fixes/fixed, resolve/resolves/
resolved, immediately followed by #N) and closes item N on merge into the default
branch. Two properties make this a trap:

  1. The parser has NO notion of negation. The phrase "does not retroactively
     fix", immediately followed by "#N", links and closes item N exactly as if it
     had read "fixes #N".
  2. Pull requests and issues share one numbering space, so an N that happens to
     be a PR closes that PR.

Both fired together: the body of PR #483 contained that negated phrasing followed
by a bare reference to the then-open PR #482, silently closing that unrelated
in-flight PR on merge. GitHub attributes such closes to whoever merged, so the
audit trail blamed a human who never clicked anything.

There is no repo/org/enterprise setting to disable closing keywords, so a check is
the only systemic defense. New `closing-keywords` job scans the same two surfaces
GitHub does and fails only when a keyword-linked number resolves to a pull request.
The everyday intended use -- a closing keyword naming a real issue -- stays green.

Surface handling mirrors GitHub's own parser:
  - PR description is Markdown: fenced blocks and inline code spans are stripped
    first, since GitHub does not parse references inside them. Without this the
    guard flags any doc that merely quotes a keyword -- including the backticked
    form the guard's own error message recommends.
  - Commit messages are plain text: backticks do NOT defuse a keyword there, so
    they are scanned raw. This matters because squash-merge concatenates every
    commit message into the squash body.

Regex notes: the leading (^|[^[:alnum:]_]) is a word boundary -- without it
"postfix #N" trips on the trailing "fix". Spelled out rather than \b so it does
not rely on GNU-grep extensions. An intervening word (as in "fix PR #N") breaks
GitHub's adjacency rule and is correctly not matched here either.

Wired into the aggregate `gate` verdict; skipped on push/workflow_dispatch, where
there is no PR body to scan.

Verified: replays the incident (PR #483 -> RED, PR #482 -> GREEN), plus 15
positive/negative regex cases including the defusing forms the error suggests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* ci: don't fail clean PRs when no closing keyword is present

The step's shell is `bash -e` and the script adds pipefail, so the keyword grep
exiting 1 on NO match -- the healthy, common case -- propagated and killed the
script before it printed anything. Every clean PR went red; caught when this
branch's own PR failed its own check with zero output.

Guard the assignment with `|| true` and note why it must stay. Verified under
`bash -e` + pipefail: no-keyword -> exit 0, keyword-present -> detected.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant