AI Security
Zero‑Trust Walkthrough: Giving a Coding AI Agent Private GitHub Access
TL;DR: Use a GitHub App with fine‑grained permissions, generate short‑lived installation tokens, store them in a secret manager, and enforce audit logging. Rotate tokens weekly and restrict the agent to a single repository or branch to keep the risk surface minimal.
What does “private GitHub access” actually entail for an AI coding agent?
An AI coding agent needs two things to work with a private repository:
- Authentication: A credential that GitHub accepts (OAuth app, personal access token, or GitHub App installation token).
- Authorization: The set of permissions (read, write, pull‑request, workflow) that the credential grants.
For small teams the safest default is a GitHub App that is scoped to the exact repo and actions the agent needs. This avoids the broad, long‑lived privileges of a personal access token (PAT).
How to create a minimal‑privilege GitHub App for an AI agent
- Log in to GitHub as an organization owner and go to Settings → Developer settings → GitHub Apps.
- Click New GitHub App and fill in a descriptive name (e.g.,
ai‑code‑assistant). - Under Permissions enable only what the agent truly needs:
Contents – Read & Write(to clone, edit, and push files).Pull requests – Read & Write(if the agent creates PRs).- Disable all other permissions such as
Issues,Deployments,Workflows, unless explicitly required.
- Under Repository access select Only select repositories and choose the single private repo the agent will touch.
- Generate a private key for the app. Store this key in a secret manager (e.g., Cloudflare Workers KV, HashiCorp Vault) – never hard‑code it.
- Save the app and note the
App IDandClient ID/Secretfor later token exchange.
How to obtain short‑lived installation tokens at runtime
The AI agent should never hold the private key or a long‑lived token. Instead, your automation layer (e.g., a Cloudflare Worker) performs the OAuth flow:
POST https://api.github.com/app/installations/:installation_id/access_tokens
Authorization: Bearer <JWT generated from App private key>
This request returns a JSON payload with an token that is valid for up to 1 hour. The token is then passed to the AI agent as an environment variable (e.g., GITHUB_TOKEN).
Key points:
- Generate a new JWT for each token request (expires in 10 minutes).
- Cache the token only for the duration of the current AI task.
- Never log the token value; mask it in observability pipelines.
How to embed the token securely into the AI agent’s runtime
Most AI coding agents (OpenAI Codex, OpenAI Agents) accept environment variables. Example for a Python‑based agent:
import os, subprocess
github_token = os.getenv("GITHUB_TOKEN")
if not github_token:
raise RuntimeError("Missing GITHUB_TOKEN")
# Clone the repo using the token (HTTPS URL)
repo_url = f"https://{github_token}@github.com/yourorg/your‑private‑repo.git"
subprocess.run(["git", "clone", repo_url], check=True)
Because the token is part of the HTTPS URL, it never touches the filesystem – it lives only in memory.
What audit logs should you collect?
To retain visibility and satisfy compliance, log the following events:
| Event | Details to Capture |
|---|---|
| Token Generation | timestamp, JWT ID, requesting service, IP address (masked) |
| Git Operations | repo name, branch, commit SHA, operation type (clone, push, PR), user‑agent identifier |
| Token Revocation / Rotation | timestamp, reason, new token ID |
Store logs in an immutable store (e.g., Cloudflare R2 with object lock) and retain for at least 90 days.
How often should you rotate the GitHub App’s private key and installation tokens?
Follow a simple schedule:
- Private key: rotate every 30 days. GitHub allows multiple active keys, so you can add a new key, switch the automation to use it, then delete the old one.
- Installation tokens: they are short‑lived by design; generate a fresh token for each AI task.
Automate rotation with a CI job that updates the secret manager and notifies the team via Slack.
What fallback mechanisms keep the workflow running if the token request fails?
Implement a retry with exponential back‑off (max 3 attempts). If all attempts fail, the automation should:
- Notify a human operator (e.g., via email or webhook).
- Pause further AI runs for that repo until the issue is resolved.
This prevents the agent from attempting unauthenticated Git commands, which would leak credentials in error messages.
When should you involve AISecAll?
If you need help designing the GitHub App, integrating the token flow into a custom AI agent, or setting up immutable audit logging, AISecAll offers hands‑on security engineering services tailored for small teams.
Need a practical AI security review?
AISecAll reviews prompts, tool permissions, document flows, and agent behavior so small teams can use AI without guessing where the risk sits.