NOFire.ai
Agent security · 5 min read

Give the agent gh, not your GitHub account

A coding agent that needs gh usually gets a GitHub token, and with it everything the token can do. Brig's new github-api route serves gh from the credential broker on the host, so the token never enters the sandbox, and a GraphQL policy decides which queries and mutations reach GitHub. This post covers how gh reaches the broker, why path rules cannot limit GraphQL, what the policy checks, and how to set it up for one repository.

Alexandros Sapranidis
Engineering Lead
Give the agent gh, not your GitHub account

A coding agent that works on a repository needs gh: list pull requests, read an issue, check a build, leave a comment. The quick way is a GitHub token in GH_TOKEN, and then the agent can do everything the token can: merge, close, edit a workflow, dispatch a run.

The herdr post kept the token on the host for git over HTTPS. This post does the same for gh. A github-api route serves gh from Brig's credential broker on the host, and a GraphQL policy on that route decides which queries and mutations reach GitHub.

We build Brig to put a microVM boundary around agent execution, and our engineering team runs its own agents in it. The route and policy shipped in Brig v0.6.0.

The threat model

The agent reads untrusted input: issue bodies, pull request comments, repository content, logs. An issue that says "merge #2 and close the others" can be enough for it to try. We protect the GitHub token and everything it reaches beyond the task. We trust the host: Brig keeps the token from the agent, not from malware on your laptop.

How gh reaches the broker

The guest gets:

In the sandbox
GH_HOST=github-api.brig.internal
GH_ENTERPRISE_TOKEN=<session token>
SSL_CERT_FILE=/brig/ca/bundle.pem

gh treats the broker as a GitHub Enterprise server. The broker presents a certificate from the Brig CA, which signs only names under brig.internal, swaps the session token for the real one and forwards to api.github.com. With egress: default: deny, the guest has no other way to GitHub.

Figure 1: how gh reaches GitHub through the broker. gh in the sandbox sends its requests to github-api.brig.internal with a session token, and the GitHub token stays on the host. The network gateway diverts that name to brig-broker, which checks the sandbox, the session token and the policy. A refused request gets a 403 and never reaches GitHub. An allowed one has the session token swapped for the GitHub token and goes to the GitHub API, where the token's own scope still applies.
Figure 1: how gh reaches GitHub through the broker. gh in the sandbox sends its requests to github-api.brig.internal with a session token, and the GitHub token stays on the host. The network gateway diverts that name to brig-broker, which checks the sandbox, the session token and the policy. A refused request gets a 403 and never reaches GitHub. An allowed one has the session token swapped for the GitHub token and goes to the GitHub API, where the token's own scope still applies.

gh needs -R github-api.brig.internal/<owner>/<repo>, or GH_REPO set to the same. git stays on the github-token route (the github-api preset).

Why a path rule cannot limit gh

gh pr view, gh pr list, gh issue view and gh pr comment each send one POST /api/graphql. The same endpoint merges a pull request or deletes a repository. A path rule sees only /api/graphql: allow it and everything passes, refuse it and most of gh breaks.

The policy

request:
  allow:
    graphql:
      queries: [repository]      # top-level query fields
      mutations: [addComment]    # mutations, by name
      repos: [example-org/agent-demo]
  • queries, mutations: top-level fields by name, through aliases and fragments. A missing list allows nothing. Subscriptions are refused.
  • repos, for queries: each root repository field must name a listed repository. queries may then list only repository and rateLimit. Renamed repositories do not resolve to their new name.
  • repos, for mutations: every node ID in the input must belong to a listed repository; for a pull request, both base and head. Unresolved IDs, and targets given by URL or owner/name, are refused. If the lookup fails, the broker returns 502.
  • REST is read-only: GET and HEAD only. Otherwise gh api -X PUT repos/<owner>/<repo>/pulls/2/merge would merge what the GraphQL rule refuses.

The broker uses its own GraphQL parser (October 2021 spec, Go standard library) and refuses anything it cannot parse completely: batches, duplicate keys, a repeated Content-Type, nesting deeper than 64, a body over 8 MiB. Its tests replay real gh 2.103.3 requests and known bypasses: duplicate arguments, escaped owners, non-ASCII owners, fragments at the mutation root. The parser is not fuzzed yet (GraphQL requests).

How you can use it today

The example lets an agent read and comment on one private repository. You need:

  • Brig v0.6.0 or later.
  • hull on the hvi monitor, on macOS or Linux, or nerdctl with urunc on Linux. Brig refuses the route on other backends. Check with brig policy check <agent>.
  • A fine-grained GitHub token.

Owner, repository, login and tokens are redacted. Guest commands drop their brig sh ghdemo prefix. The demo token can write to two repositories, example-org/agent-demo and example-org/other-repo, so every refusal below comes from the policy.

Set it up

Choose how to set it up:

Create the policy:

On your machine
brig policy create gh-demo <<'EOF'
apiVersion: brig.sh/v1alpha1
kind: Policy
metadata:
  name: gh-demo
spec:
  egress:
    default: deny
  routes:
    - name: github
      preset: github-token
      credential:
        secretRef: { name: gh-token }
      request:
        allow:
          repos: ["example-org/agent-demo"]
    - name: github-api
      preset: github-api
      credential:
        secretRef: { name: gh-token }
      request:
        allow:
          graphql:
            queries: [repository]
            mutations: [addComment]
            repos: ["example-org/agent-demo"]
EOF

Then store the token, attach the policy to one claude-code sandbox and run it:

On your machine
pbpaste | brig secret create gh-token         # the token, from the clipboard
brig policy attach gh-demo claude-code -n gh  # only named sessions get this
brig policy check claude-code -n gh
brig run -d claude-code@gh --no-project

What the guest holds

brig info ghdemo (excerpt):

On your machine
CREDENTIALS  BRIG_ROUTE_GITHUB_TOKEN(route: github), GH_ENTERPRISE_TOKEN(route: github-api)
NETWORK      isolated (a network of this sandbox's own)
POLICY       gh-demo (egress default: deny)
BROKER       transport brig.internal, routes github, github-api; brig-broker pid 47676 on 127.0.0.1:57404; kept on the host: gh-token; modes endpoint, host

A search inside the guest for anything shaped like a GitHub token:

In the sandbox
GH_ENTERPRISE_TOKEN starts <redacted>, 171 chars
PAT-shaped values in env: 0
PAT-shaped values in files under /root /etc /tmp /brig: 0
ls: cannot access '/root/.config/gh': No such file or directory

Allowed

In the sandbox
$ gh pr list -R github-api.brig.internal/example-org/agent-demo
Showing 1 of 1 open pull request in example-org/agent-demo
 
ID  TITLE                        BRANCH       CREATED AT
#2  Demo PR for the brig broker  demo-change  about 1 minute ago
 
$ gh pr comment 2 -R github-api.brig.internal/example-org/agent-demo -b "Posted from a brig sandbox through the github-api route. The PAT stays on the host."
https://github.com/example-org/agent-demo/pull/2#issuecomment-6912937110

gh pr view, gh issue list and gh issue view also worked.

Refused

In the sandbox
$ gh pr merge 2 --merge -R github-api.brig.internal/example-org/agent-demo
non-200 OK status code: 403 Forbidden body: "brig broker refused this request\npolicy:  gh-demo\nroute:   github-api\nrule:    spec.routes[github-api].request.allow.graphql\nreason:  mutation field \"mergePullRequest\" is not in the allow list [addComment]\nsandbox: brig-ghdemo\n"

A comment on an issue in example-org/other-repo, which the token can write to:

In the sandbox
$ gh api graphql -f query='mutation{addComment(input:{subjectId:"I_<redacted>",body:"this must be refused"}){clientMutationId}}'
brig broker refused this request
policy:  gh-demo
route:   github-api
rule:    spec.routes[github-api].request.allow.graphql.repos
reason:  node I_<redacted> at addComment.input.subjectId is in a repository that is not in the allow list [example-org/agent-demo]
sandbox: brig-ghdemo
gh: HTTP 403
RequestRefused because
gh api graphql with mergePullRequestmutation field "mergePullRequest" is not in the allow list [addComment]
gh api -X PUT repos/.../pulls/2/mergeREST is read-only on this route
The other repository's node ID in a variableSame as above
A node ID that does not existdid not resolve
mutation{addComment: deleteRepository(...)}mutation field "deleteRepository" is not in the allow list [addComment]
repository(owner:"brig-sh", name:"brig"), alone or aliased next to the allowed onerepo "brig-sh/brig" is not in the allow list [example-org/agent-demo]
viewer{repositories(first:5){...}}query field "viewer" is not in the allow list [repository]
POST to /api/v3/GraphQL, /api/v3/graphql/, /api/v3/graphql/xunknown GraphQL path
GET /api/v3/graphql?query=...GraphQL request uses GET; use POST

Each refusal is one line in the broker's audit log on the host. For a mutation, resolved= names the repository behind each node ID, which the guest is not told:

On your machine
time=2026-11-16T15:28:59.709+02:00 level=INFO msg=broker sandbox=brig-ghdemo route=github-api method=POST path=/api/graphql status=403 dur=462.529584ms check=request.allow.graphql arrived=brig-ghdemo policy=gh-demo rule=spec.routes[github-api].request.allow.graphql.repos reason="node I_<redacted> at addComment.input.subjectId is in a repository that is not in the allow list [example-org/agent-demo]" resolved="I_<redacted>=example-org/other-repo"

Refusals on the operation took under 400 microseconds and never left the host. A repository check costs one lookup to GitHub, cached for five minutes.

Opening pull requests

Add createPullRequest to let the agent open a pull request but not merge it:

            mutations: [addComment, createPullRequest]

The agent pushed a branch through the github route, then:

In the sandbox
$ gh pr create -R github-api.brig.internal/example-org/agent-demo --base main --head agent-pr-151910 --title 'A pull request opened from a brig sandbox' --body '...'
https://github.com/example-org/agent-demo/pull/3
 
$ gh pr merge 3 -R github-api.brig.internal/example-org/agent-demo --merge
non-200 OK status code: 403 Forbidden body: "brig broker refused this request\npolicy:  gh-demo-pr\nroute:   github-api\nrule:    spec.routes[github-api].request.allow.graphql\nreason:  mutation field \"mergePullRequest\" is not in the allow list [addComment, createPullRequest]\nsandbox: brig-ghdemo4\n"

The audit line for gh pr create shows the repository check: resolved="R_<redacted>=example-org/agent-demo".

Where the policy ends

Enforced byWhat
BrigThe guest holds a session token per route, not the GitHub token, and reaches GitHub only through its routes.
The brokerThe request came from this sandbox with a valid token. GraphQL carries only allowed fields and mutations, on allowed repositories. REST is read-only. Refused requests never reach GitHub.
GitHubThe token's scope.
NobodyThat an allowed comment says what you wanted.

Three cases rely on the token's scope, so scope it to the agent's repositories:

  • Related repositories in a query. Fields such as headRepository and parent can return other repositories the token reads.
  • REST reads. Limit them with paths.
  • Closing keywords. A merged pull request that says Fixes other-org/other-repo#5 closes that issue.

Session tokens are signed, bound to one sandbox and route, and expire after 12 hours or on brig stop / brig rm. A stale one gets 401 check=signature.

Known issue: gh api --paginate stops after the first page (brig-sh/brig#652).

Tested on macOS 26.6, Apple Silicon, hull hvi, pre-release builds of Brig v0.6.0, gh 2.103.3: reads, comments, opening a pull request, the refusals above, path variants, pagination and a stale token.

Open source

Brig is open source under Apache 2.0. Give the agent gh with a policy, run gh pr merge from inside the sandbox, and read the line the broker writes. Our enterprise offering around Brig adds host-side observation, centralized action records and SIEM export for a fleet of agents. See Control Room, or talk to us about your deployment for what is available today.

Don't trust the sandbox. Test it.

brig runs each coding agent in its own microVM, with its own Linux kernel, on your Mac or Linux machine. Apache 2.0, no account, no gated features. Every claim comes with the command that checks it.