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:

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

  1. Log in to GitHub as an organization owner and go to Settings → Developer settings → GitHub Apps.
  2. Click New GitHub App and fill in a descriptive name (e.g., ai‑code‑assistant).
  3. 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.
  4. Under Repository access select Only select repositories and choose the single private repo the agent will touch.
  5. 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.
  6. Save the app and note the App ID and Client ID/Secret for 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:

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:

EventDetails to Capture
Token Generationtimestamp, JWT ID, requesting service, IP address (masked)
Git Operationsrepo name, branch, commit SHA, operation type (clone, push, PR), user‑agent identifier
Token Revocation / Rotationtimestamp, 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:

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:

  1. Notify a human operator (e.g., via email or webhook).
  2. 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.

Book a call Discuss a project