DEV Community

Sandscale
Sandscale

Posted on Originally published at sandscale.dev

GitHub Actions and agent pull requests: triggers, tokens and where the tests run

When a coding agent opens a pull request, your CI runs code nobody has reviewed: the agent's changes, the tests it wrote, and every install script in every dependency it added. On GitHub Actions that code runs on a runner with whatever the workflow gives it: a GITHUB_TOKEN, repository secrets, a cache shared with other runs, and a network connection.

This post walks through the settings that decide what an agent's PR can reach, the trigger that turns a test run into a credential leak, and a workflow that keeps the runner as a coordinator and runs the PR's code in a sandbox.

All the GitHub behavior below is from GitHub's own docs: events that trigger workflows and workflow syntax.

Agent PRs are not fork PRs

Much of the GitHub Actions security advice is about pull requests from forks, where GitHub protects you by default: the docs say that, with the exception of GITHUB_TOKEN, secrets are not passed to the runner when a workflow is triggered from a forked repository.

Agent PRs usually do not come from forks. The agent pushes a branch to your repository and opens a PR from it, so the run is a same-repository pull_request, and repository secrets are available to it. That puts an agent's PR in a more trusted position than a stranger's, even though its content deserves less trust than a colleague's.

The trigger trap: pull_request_target

pull_request_target runs in the context of the base repository, with access to secrets and a token that can write, so that workflows can label or comment on PRs from forks. GitHub's docs are direct about the consequence: avoid this event if you need to build or run code from the pull request. They warn that running untrusted code on this trigger can lead to cache poisoning and unintended access to write privileges or secrets, and link to GitHub Security Lab's write-up on preventing pwn requests.

The dangerous pattern is a pull_request_target workflow that checks out the PR's head and runs its tests. If you have one, for agent PRs or any others, fix it first.

The settings that matter for agent PRs

The workflow file is part of the PR

For pull_request, the workflow that runs comes from the PR's merge commit. An agent that can edit .github/workflows/ can change what its own CI run does: add a step, remove a check, print a secret. Two controls help:

  • Do not give the agent's token permission to change workflows. GitHub rejects pushes that modify workflow files unless the credential has that specific permission, so leave it out of the agent's token.
  • Add a CODEOWNERS rule for .github/ so any change there needs a named reviewer.

Least-privilege GITHUB_TOKEN

Set permissions at the top of the workflow. A test job needs contents: read and nothing else. Without it, the token's permissions follow your repository or organization default, which may allow writes.

Do not leave credentials in the checkout

actions/checkout saves the token in the local git config by default so later steps can push. A test job never needs to push, so set persist-credentials: false, and the PR's code cannot read the token from .git/config.

Runs triggered by agents may need approval

GitHub's docs note that when a pull request is created or updated by a workflow using GITHUB_TOKEN, the resulting pull_request runs require approval from someone with write access. If your agent runs inside GitHub Actions and opens PRs with that token, expect that approval step; it is a feature, not a bug to work around.

Where should the tests run?

Even with all of the above, the PR's code runs on the runner, next to whatever secrets the job has and with the runner's network. For agent PRs, a stronger pattern is to keep the runner as a coordinator that never executes the PR's code:

  1. Check out the code without credentials.
  2. Fetch dependencies with npm ci --ignore-scripts, which downloads packages without running their install scripts.
  3. Pack the workspace into a tarball.
  4. Start a sandbox, upload the tarball, and run the tests there, with no network.
  5. Report the exit code and output back to the job, and stop the sandbox.

The PR's code and its dependencies' code only execute inside the sandbox, which holds no repository secrets and cannot reach the network.

The workflow

This uses the Sandscale sandbox API with curl and jq. SANDSCALE_CI_KEY is a repository secret holding a personal API key scoped to sandboxes. The API reference lists sandboxes:create for starting a sandbox, sandboxes:use for uploading and running, and sandboxes:manage for stopping one. Our post on scoping API keys covers why a CI key should hold nothing else.

name: agent-pr-tests

on:
  pull_request:
    branches: [main]

permissions:
  contents: read

# An explicit shell makes GitHub run steps with -eo pipefail, so a failed curl in a pipe fails the step.
defaults:
  run:
    shell: bash

jobs:
  test-in-sandbox:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    env:
      API: https://api.sandscale.dev/api/v1/sandbox
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      # Fetch dependencies on the runner without running any package's scripts.
      - name: Install dependencies (no scripts)
        run: npm ci --ignore-scripts

      - name: Pack the workspace
        run: tar -czf "$RUNNER_TEMP/project.tgz" --exclude=.git .

      - name: Start a sandbox
        env:
          SANDSCALE_API_KEY: ${{ secrets.SANDSCALE_CI_KEY }}
        run: |
          id=$(curl -sS --fail-with-body --max-time 60 -X POST "$API/instances" \
            -H "Authorization: Bearer $SANDSCALE_API_KEY" -H "Content-Type: application/json" \
            -d '{"template_id": "builtin:node", "ttl_seconds": 1800, "labels": {"ci": "agent-pr"}}' | jq -r .id)
          echo "SANDBOX_ID=$id" >> "$GITHUB_ENV"

      - name: Upload the project
        env:
          SANDSCALE_API_KEY: ${{ secrets.SANDSCALE_CI_KEY }}
        run: |
          curl -sS --fail-with-body --max-time 300 -X POST "$API/instances/$SANDBOX_ID/project" \
            -H "Authorization: Bearer $SANDSCALE_API_KEY" -H "Content-Type: application/gzip" \
            --data-binary "@$RUNNER_TEMP/project.tgz"

      - name: Run the tests in the sandbox
        env:
          SANDSCALE_API_KEY: ${{ secrets.SANDSCALE_CI_KEY }}
        run: |
          curl -sS --fail-with-body --max-time 960 -X POST "$API/instances/$SANDBOX_ID/exec" \
            -H "Authorization: Bearer $SANDSCALE_API_KEY" -H "Content-Type: application/json" \
            -d '{"command": ["npm", "test"], "timeout_sec": 900}' > "$RUNNER_TEMP/run.json"
          jq -r .stdout "$RUNNER_TEMP/run.json"
          jq -r .stderr "$RUNNER_TEMP/run.json" >&2
          if [ "$(jq -r .timed_out "$RUNNER_TEMP/run.json")" = "true" ]; then echo "::error::tests timed out"; exit 1; fi
          exit "$(jq -r .exit_code "$RUNNER_TEMP/run.json")"

      - name: Stop the sandbox
        if: always() && env.SANDBOX_ID != ''
        env:
          SANDSCALE_API_KEY: ${{ secrets.SANDSCALE_CI_KEY }}
        run: |
          curl -sS --max-time 60 -X DELETE "$API/instances/$SANDBOX_ID" \
            -H "Authorization: Bearer $SANDSCALE_API_KEY" || true
Enter fullscreen mode Exit fullscreen mode

Notes on the workflow

shell: bash matters. GitHub's docs say steps run with set -e by default, and that -o pipefail is added only when shell: bash is specified. Without it, a failed curl piped into jq would not fail the step.

The sandbox has no network. The create request does not ask for one, and the reference documents an empty or none network mode as no network. That is why dependencies are fetched on the runner first. Packages that must compile native code during install will not work with --ignore-scripts; build those in a template instead.

Two lifetimes. ttl_seconds: 1800 stops the sandbox by itself if the job is cancelled before the stop step. The stop step runs with if: always() so the normal path cleans up at once.

The exit code is the test result. The job exits with the command's exit code and fails on a timeout, so branch protection works as usual.

The runner still holds a credential. The Sandscale key is in the job's environment while it runs. The PR's code never runs on the runner, which is what keeps that key out of its reach. Keep it that way: no npm test, no build scripts and no PR-provided actions on the runner.

Checklist

  • No pull_request_target workflows that run PR code.
  • permissions: contents: read on test workflows.
  • persist-credentials: false on checkout.
  • The agent's token cannot modify workflow files, and .github/ has a code owner.
  • Dependencies fetched without running scripts on the runner.
  • PR code runs in an isolated environment with no secrets and no network.

Our post on running untrusted PR code in sandboxes covers the same idea outside GitHub Actions, and the review checklist for agent PRs covers what a person should still read.

Originally published on the Sandscale blog.

Top comments (0)