AI Security
Securely Connecting a Coding AI Agent to a Private GitHub Repository with GitHub Apps
TL;DR: Use a GitHub App with fine‑grained permissions (e.g., contents:read and pull_requests:write), generate short‑lived installation tokens for each agent run, store the private key in a secret manager, rotate keys weekly, and log every token issuance and repo operation to an immutable audit store.
Why a GitHub App Beats Deploy Keys for AI Coding Agents
Deploy keys give an SSH key full read/write access to a single repository. An AI coding agent that can execute arbitrary code may misuse that key to clone, push, or delete any branch. GitHub Apps, by contrast, let you define exactly which actions are allowed, limit the scope to a set of repositories, and revoke a single installation without affecting other integrations.
Step‑by‑Step Integration Guide
1. Create a GitHub App
- Navigate to GitHub Settings → Developer settings → GitHub Apps.
- Click New GitHub App and give it a descriptive name (e.g., AI‑Coder‑Agent).
- Under Repository permissions select the minimal set you need:
Contents – Read & Write(allows reading files and committing changes).Pull requests – Read & Write(enables the agent to open PRs for review).Metadata – Read only(required for installation context).
- Enable Webhook events only for
pushandpull_requestif you want the app to receive notifications. - Generate a private key and download the
.pemfile. Store it in a secret manager (e.g., HashiCorp Vault, AWS Secrets Manager).
2. Install the App on the Target Repository
- From the app’s dashboard, click Install App and select the private repository.
- Choose Only select repositories and pick the repo you want the AI agent to access.
- Note the Installation ID – you’ll need it to request tokens.
3. Generate Short‑Lived Installation Tokens
Each time the AI agent starts a coding task, it should request a fresh token (valid for 10‑15 minutes). Use the private key to sign a JWT, then exchange it for an installation token via the GitHub API:
POST https://api.github.com/app/installations/:installation_id/access_tokens
Authorization: Bearer <JWT>
Accept: application/vnd.github+json
Because the token expires quickly, even if the agent is compromised the window for abuse is limited.
4. Pass the Token to the Agent Runtime
Inject the token as an environment variable (e.g., GITHUB_TOKEN) when launching the agent container. Ensure the variable is not logged or persisted beyond the container’s lifetime.
5. Enforce Token Rotation and Key Management
- Rotate the GitHub App private key at least weekly.
- Re‑install the app automatically after key rotation using a CI script that updates the secret manager.
- Revoke any token that is older than its intended lifespan.
Auditing and Monitoring
Small teams need visibility without drowning in noise. Follow the OWASP GenAI recommendations for audit logging (OWASP GenAI) and record the following fields for each agent run:
| Field | Purpose |
|---|---|
| timestamp | When the token was issued. |
| installation_id | Which GitHub App installation was used. |
| token_id | GitHub‑provided token identifier. |
| repo | Target repository name. |
| agent_job_id | Internal identifier for the AI task. |
| actions_performed | List of API calls (e.g., GET /repos/:owner/:repo/contents). |
Store logs in an immutable store (e.g., append‑only S3 bucket with Object Lock) and set up alerts for anomalous patterns such as a token used on a different repository.
Common Pitfalls and How to Avoid Them
- Over‑permissive scopes: Grant only the actions the agent truly needs. If the agent only creates PRs, omit
contents:writeand use thepull_requestsscope withfilesaccess. - Storing the private key in code: Keep the
.pemfile out of the repository; use a secret manager and inject at runtime. - Long‑lived tokens: Never reuse a token across multiple runs. Generate a fresh token per job.
- Missing revocation: If a run crashes, ensure the container cleanup script revokes the token via the
/applications/:client_id/tokenendpoint.
When to Use a Managed Agent Instead
If your team lacks the resources to maintain a GitHub App lifecycle, consider a managed AI agent platform (e.g., Claude Managed Agents) that already handles token scoping and audit logging. However, managed platforms may not expose the fine‑grained GitHub permissions you need for custom CI/CD pipelines.
Putting It All Together
1️⃣ Create a GitHub App with the smallest permission set.
2️⃣ Install it on the private repo.
3️⃣ Store the private key securely.
4️⃣ Generate short‑lived installation tokens for each AI task.
5️⃣ Log every token issuance and API call.
6️⃣ Rotate keys weekly and monitor logs for anomalies.
Following these steps gives small companies the confidence that an AI coding agent can read and write code without exposing the entire repository or secret credentials.
Need help implementing this workflow or integrating it with your existing AI automation stack? AISecAll can design a custom, audit‑ready pipeline that fits your budget and compliance requirements.
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.