Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ function accessFailure(request) {
const caller = string(access.caller_repo)
if (!allowed.includes(target) || !tokenRepos.includes(target)) return "Target repository is not explicitly authorized by allowed_repos and access_token_repos."
if (!caller) return "Caller repository is required for publication authorization."
if (target !== caller && process.env.EXPLICIT_ACCESS_TOKEN_CONFIGURED !== "true") return "An explicit ACCESS_TOKEN is required for cross-repository publication."
if (target !== caller && process.env.EXPLICIT_ACCESS_TOKEN_CONFIGURED !== "true") return "An explicit ACCESS_TOKEN or minted GitHub App token is required for cross-repository publication."
if (!string(process.env.ACCESS_TOKEN || process.env.GITHUB_TOKEN)) return "No effective GitHub token is available for runner workspace tools."
return ""
}
Expand Down
67 changes: 62 additions & 5 deletions .github/workflows/run-agent-task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ name: Run Agent Task (reusable)
MODEL_PROVIDER_SECRET_5:
required: false
ACCESS_TOKEN:
description: Optional explicit GitHub token for an authorized cross-repository target. Same-repository publication uses the caller token.
description: Optional explicit GitHub token; takes precedence over app credentials and the caller token.
required: false
ACCESS_APP_ID:
description: Optional GitHub App ID for publication; requires ACCESS_APP_PRIVATE_KEY.
required: false
ACCESS_APP_PRIVATE_KEY:
description: Optional GitHub App private key for publication; requires ACCESS_APP_ID.
required: false
EXTERNAL_PACKAGE_SOURCE_POLICY:
description: Trusted versioned JSON policy authorizing external native package repositories and exact standalone .agent.json paths.
Expand Down Expand Up @@ -223,12 +229,63 @@ jobs:
working-directory: .wp-codebox-workflow
run: npm ci

- name: Prepare GitHub App token configuration
id: access-app-config
env:
ACCESS_APP_ID: ${{ secrets.ACCESS_APP_ID }}
ACCESS_APP_PRIVATE_KEY: ${{ secrets.ACCESS_APP_PRIVATE_KEY }}
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
TARGET_REPO: ${{ inputs.target_repo }}
ACCESS_TOKEN_REPOS: ${{ inputs.access_token_repos }}
run: |
node --input-type=module <<'NODE'
import { appendFileSync } from 'node:fs';
const { ACCESS_APP_ID, ACCESS_APP_PRIVATE_KEY, ACCESS_TOKEN, TARGET_REPO, ACCESS_TOKEN_REPOS } = process.env;
if (Boolean(ACCESS_APP_ID) !== Boolean(ACCESS_APP_PRIVATE_KEY)) {
console.error('::error::ACCESS_APP_ID and ACCESS_APP_PRIVATE_KEY must both be configured, or both omitted.');
process.exit(1);
}
const [owner, repository, extra] = TARGET_REPO.split('/');
if (!owner || !repository || extra || !/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(TARGET_REPO)) {
console.error('::error::target_repo must be in OWNER/REPO format.');
process.exit(1);
}
const repositories = new Set([repository]);
for (const entry of (ACCESS_TOKEN_REPOS || '').split(',')) {
const [entryOwner, name, remainder] = entry.trim().split('/');
if (entryOwner.toLowerCase() === owner.toLowerCase() && name && !remainder && /^[A-Za-z0-9_.-]+$/.test(name)) {
repositories.add(name);
}
}
appendFileSync(process.env.GITHUB_OUTPUT, `mint=${Boolean(ACCESS_APP_ID && ACCESS_APP_PRIVATE_KEY && !ACCESS_TOKEN)}\nowner=${owner}\nrepositories=${[...repositories].join(',')}\n`);
NODE

- name: Mint GitHub App installation token
id: access-app-token
if: steps.access-app-config.outputs.mint == 'true'
uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1
with:
app-id: ${{ secrets.ACCESS_APP_ID }}
private-key: ${{ secrets.ACCESS_APP_PRIVATE_KEY }}
owner: ${{ steps.access-app-config.outputs.owner }}
repositories: ${{ steps.access-app-config.outputs.repositories }}

- name: Resolve GitHub access token
id: access-token
env:
RESOLVED_TOKEN: ${{ secrets.ACCESS_TOKEN || steps.access-app-token.outputs.token || github.token }}
EXPLICIT_TOKEN: ${{ secrets.ACCESS_TOKEN != '' || steps.access-app-token.outputs.token != '' }}
run: |
echo "::add-mask::${RESOLVED_TOKEN}"
echo "token=${RESOLVED_TOKEN}" >> "$GITHUB_OUTPUT"
echo "explicit=${EXPLICIT_TOKEN}" >> "$GITHUB_OUTPUT"

- name: Checkout target workspace
uses: actions/checkout@v4
with:
repository: ${{ inputs.target_repo }}
path: workspace
token: ${{ secrets.ACCESS_TOKEN || github.token }}
token: ${{ steps.access-token.outputs.token }}
persist-credentials: false

- name: Build Codebox task request
Expand Down Expand Up @@ -283,8 +340,8 @@ jobs:
MODEL_PROVIDER_SECRET_5: ${{ secrets.MODEL_PROVIDER_SECRET_5 }}
# Runner workspace tools resolve GitHub credentials from this standard
# environment variable. It is never written to task inputs or artifacts.
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN || github.token }}
EXPLICIT_ACCESS_TOKEN_CONFIGURED: ${{ secrets.ACCESS_TOKEN != '' }}
GITHUB_TOKEN: ${{ steps.access-token.outputs.token }}
EXPLICIT_ACCESS_TOKEN_CONFIGURED: ${{ steps.access-token.outputs.explicit }}
EXTERNAL_PACKAGE_SOURCE_POLICY: ${{ secrets.EXTERNAL_PACKAGE_SOURCE_POLICY }}
run: node .wp-codebox-workflow/.github/scripts/run-agent-task/execute-native-agent-task.mjs

Expand All @@ -302,7 +359,7 @@ jobs:
MODEL_PROVIDER_SECRET_3: ${{ secrets.MODEL_PROVIDER_SECRET_3 }}
MODEL_PROVIDER_SECRET_4: ${{ secrets.MODEL_PROVIDER_SECRET_4 }}
MODEL_PROVIDER_SECRET_5: ${{ secrets.MODEL_PROVIDER_SECRET_5 }}
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN || github.token }}
GITHUB_TOKEN: ${{ steps.access-token.outputs.token }}
EXTERNAL_PACKAGE_SOURCE_POLICY: ${{ secrets.EXTERNAL_PACKAGE_SOURCE_POLICY }}

- name: Upload Codebox task request
Expand Down
2 changes: 2 additions & 0 deletions contracts/run-agent-task-reusable-workflow-interface.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"MODEL_PROVIDER_SECRET_4": { "required": false },
"MODEL_PROVIDER_SECRET_5": { "required": false },
"ACCESS_TOKEN": { "required": false },
"ACCESS_APP_ID": { "required": false },
"ACCESS_APP_PRIVATE_KEY": { "required": false },
"EXTERNAL_PACKAGE_SOURCE_POLICY": { "required": true }
},
"outputs": {
Expand Down
26 changes: 20 additions & 6 deletions docs/agent-task-reusable-workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,26 @@ This release-coherence contract fixes [#1759](https://github.com/Automattic/wp-c

## Access And Publication

For a target equal to the caller repository, the workflow passes the caller's
built-in `github.token` as `GITHUB_TOKEN` to native agent execution. An
`ACCESS_TOKEN` secret is not required in that case. For a target in another
repository, `ACCESS_TOKEN` is required explicitly and must be scoped by both
`access_token_repos` and `allowed_repos`. The caller token is never inferred to
have cross-repository access. The effective token is not serialized into task
The workflow resolves one token for target checkout, native execution, and upload
redaction, in this order: optional `ACCESS_TOKEN`, a minted GitHub App installation
token, then the caller's built-in `github.token` as `GITHUB_TOKEN`. To mint inside
this job, pass both optional secrets `ACCESS_APP_ID` (GitHub App ID) and
`ACCESS_APP_PRIVATE_KEY` (its private key) in the reusable workflow caller's
`secrets:` mapping. Exactly one app secret is an error, even with `ACCESS_TOKEN`.
When `ACCESS_TOKEN` is set, it wins and no app token is minted.

Install the app on the target repositories with contents and pull-request write
permissions. The mint step scopes the token to the owner of `target_repo`, the
target repository name, and any `access_token_repos` entries under that same owner
(case-insensitive); entries under other owners are not requested. App installation
tokens last 1 hour, longer than the default `time_budget_ms` of 600000 (10 minutes);
they are not refreshed during a run. App-authored PRs can trigger `pull_request`
workflows, unlike PRs opened with the caller token.

For a target equal to the caller repository, neither explicit credential is
required. For a target in another repository, `ACCESS_TOKEN` or both app secrets
are required, and the target must be scoped by both `access_token_repos` and
`allowed_repos`. The caller token is never inferred to have cross-repository access. The effective token is not serialized into task
input, result, or artifact data. GitHub runner tools
receive the normalized `allowed_repos` policy explicitly through the runtime and
fail closed for every PR, issue, and comment operation outside that set. The
Expand Down
51 changes: 47 additions & 4 deletions tests/agent-task-reusable-workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,49 @@ assert.match(workflow, /access_token_repos:/)
assert.doesNotMatch(publicWorkflowSurface, /require_access_token:/)
assert.match(workflow, /projected_outputs_json:/)
assert.match(workflow, /ACCESS_TOKEN:/)
assert.match(workflow, /GITHUB_TOKEN: \$\{\{ secrets\.ACCESS_TOKEN \|\| github\.token \}\}/)
assert.match(publicWorkflowSurface, /ACCESS_APP_ID:\n description: .+\n required: false/)
assert.match(publicWorkflowSurface, /ACCESS_APP_PRIVATE_KEY:\n description: .+\n required: false/)
assert.match(workflow, /RESOLVED_TOKEN: \$\{\{ secrets\.ACCESS_TOKEN \|\| steps\.access-app-token\.outputs\.token \|\| github\.token \}\}/)
assert.equal(workflow.match(/GITHUB_TOKEN: \$\{\{ steps\.access-token\.outputs\.token \}\}/g)?.length, 2)
assert.match(workflow, /token: \$\{\{ steps\.access-token\.outputs\.token \}\}/)
assert.doesNotMatch(workflow, /secrets\.ACCESS_TOKEN \|\| github\.token/)
assert.match(workflow, /EXPLICIT_TOKEN: \$\{\{ secrets\.ACCESS_TOKEN != '' \|\| steps\.access-app-token\.outputs\.token != '' \}\}/)
assert.match(workflow, /if: steps\.access-app-config\.outputs\.mint == 'true'\n uses: actions\/create-github-app-token@[a-f0-9]{40} # v2\.2\.1/)
assert.match(workflow, /app-id: \$\{\{ secrets\.ACCESS_APP_ID \}\}/)
assert.match(workflow, /private-key: \$\{\{ secrets\.ACCESS_APP_PRIVATE_KEY \}\}/)
assert.match(workflow, /owner: \$\{\{ steps\.access-app-config\.outputs\.owner \}\}/)
assert.match(workflow, /repositories: \$\{\{ steps\.access-app-config\.outputs\.repositories \}\}/)
assert.ok(workflow.indexOf('name: Mint GitHub App installation token') < workflow.indexOf('name: Checkout target workspace'))

// Exercise the actual inline configuration code without GitHub or app credentials.
const appConfigScript = workflow.match(/node --input-type=module <<'NODE'\n([\s\S]*?) NODE/)?.[1].replace(/^ /gm, '')
assert.ok(appConfigScript)
const appConfigTmp = await mkdtemp(join(tmpdir(), 'wp-codebox-app-config-'))
try {
for (const [id, key, token, mint] of [
['', '', '', false], ['123', 'private-key', '', true],
['123', 'private-key', 'explicit-token', false], ['', '', 'explicit-token', false],
] as const) {
const output = join(appConfigTmp, 'output')
await writeFile(output, '')
await execFileAsync(process.execPath, ['--input-type=module', '-e', appConfigScript], { env: {
...process.env, ACCESS_APP_ID: id, ACCESS_APP_PRIVATE_KEY: key, ACCESS_TOKEN: token,
TARGET_REPO: 'Automattic/target', ACCESS_TOKEN_REPOS: 'Automattic/extra,other/ignored,automattic/extra,Automattic/target', GITHUB_OUTPUT: output,
} })
assert.equal(await readFile(output, 'utf8'), `mint=${mint}\nowner=Automattic\nrepositories=target,extra\n`)
}
for (const [id, key] of [['123', ''], ['', 'private-key']]) {
for (const token of ['', 'explicit-token']) {
await assert.rejects(execFileAsync(process.execPath, ['--input-type=module', '-e', appConfigScript], { env: {
...process.env, ACCESS_APP_ID: id, ACCESS_APP_PRIVATE_KEY: key, ACCESS_TOKEN: token,
} }), /::error::ACCESS_APP_ID and ACCESS_APP_PRIVATE_KEY must both be configured/)
}
}
} finally {
await rm(appConfigTmp, { recursive: true, force: true })
}
assert.match(workflow, /CALLER_REPO: \$\{\{ github\.repository \}\}/)
assert.match(workflow, /EXPLICIT_ACCESS_TOKEN_CONFIGURED: \$\{\{ secrets\.ACCESS_TOKEN != '' \}\}/)
assert.match(workflow, /EXPLICIT_ACCESS_TOKEN_CONFIGURED: \$\{\{ steps\.access-token\.outputs\.explicit \}\}/)
assert.doesNotMatch(workflow, /homeboy|require_app_token|require_homeboy_app_token|REQUIRE_HOMEBOY_APP_TOKEN|Extra-Chill\/homeboy-action|agent-task run-plan/i)
assert.doesNotMatch(workflow, /docs-agent|wp-codebox\/docs-agent-runner-recipe\/v1|recipe_path|recipe_json|wp_codebox_ref/i)
assert.doesNotMatch(workflow, /datamachine-agent-ci|runtime-agent-full-run|Extra-Chill\/homeboy-extensions/)
Expand Down Expand Up @@ -95,7 +135,10 @@ assert.match(docs, /external_package_source/)
assert.match(docs, /runner_workspace/)
assert.match(docs, /access_token_repos/)
assert.match(docs, /built-in `github\.token` as `GITHUB_TOKEN`/)
assert.match(docs, /target in another\nrepository, `ACCESS_TOKEN` is required explicitly/)
assert.match(docs, /`ACCESS_APP_ID`/)
assert.match(docs, /`ACCESS_APP_PRIVATE_KEY`/)
assert.match(docs, /tokens last 1 hour/)
assert.match(docs, /`ACCESS_TOKEN` or both app secrets/)
assert.match(docs, /success_requires_pr/)
assert.match(docs, /intentional exposed-workflow breaking change/)
assert.match(docs, /wp-codebox\/reusable-workflow-interface\/v1/)
Expand Down Expand Up @@ -293,7 +336,7 @@ const crossRepositoryRequest = {
await assert.rejects(
executeAccessCase(crossRepositoryRequest, { GITHUB_TOKEN: callerToken, EXPLICIT_ACCESS_TOKEN_CONFIGURED: "false" }),
)
assert.match(JSON.parse(await readFile(resultPath, "utf8")).access.error, /explicit ACCESS_TOKEN is required for cross-repository publication/)
assert.match(JSON.parse(await readFile(resultPath, "utf8")).access.error, /explicit ACCESS_TOKEN or minted GitHub App token is required for cross-repository publication/)

const targetMismatchRequest = {
...request,
Expand Down
Loading