AI Security

Securely Connecting an OpenAI Coding Agent to a Private GitHub Repository: A Practical Playbook for Small Teams

TL;DR: Use a GitHub App with fine‑grained read/write permissions, store its private key in a secret manager, let the OpenAI Agent retrieve a short‑lived JWT at runtime, and enforce sandboxed execution plus audit logging. Rotate the App secret every 30 days and monitor GitHub audit logs for unexpected pushes.

What is the threat model when an AI agent writes code to a private repo?

When you let an AI agent push commits, the main risks are:

Designing a threat model starts by asking: What actions must the agent perform, and nothing more? For most coding assistants the answer is “read the repository, create a feature branch, add or modify files, and open a pull request.” Anything beyond that should be blocked.

Which GitHub permissions are truly needed?

GitHub Apps support fine‑grained permissions. For a coding agent, the minimal set is:

  1. Contents: read & write – to fetch files and push changes.
  2. Pull requests: read & write – to open PRs and comment.
  3. Metadata: read – required for authentication.

Do not enable:

These can be added later if a concrete business case emerges.

How to set up a GitHub App for an AI agent

Follow these steps in the GitHub UI:

  1. Navigate to Settings → Developer settings → GitHub Apps and click New GitHub App.
  2. Give the app a clear name, e.g., ai‑code‑assistant, and set the webhook URL to a dummy endpoint (you can disable webhooks if not needed).
  3. Under Permissions & events, grant the three permissions listed above.
  4. Choose Only select repositories and pick the private repo(s) the agent may touch.
  5. Generate a private key and download the .pem file.
  6. Save the App ID and the private key in a secret manager (e.g., AWS Secrets Manager, HashiCorp Vault).

Do not store the PEM file in source control.

How to configure the OpenAI Agent to use the token securely

OpenAI Agents can call external APIs via function calling. Implement a small wrapper service (e.g., a Cloudflare Worker) that:

  1. Loads the PEM from the secret manager.
  2. Creates a JWT signed with the private key (valid for 10 minutes).
  3. Exchanges the JWT for an installation access token via GitHub’s /app/installations/:installation_id/access_tokens endpoint.
  4. Returns the short‑lived token to the agent as a function result.

Sample pseudo‑code (Python‑like) for the wrapper:

import jwt, time, requests, os
pem = os.getenv('GITHUB_APP_PEM')
app_id = os.getenv('GITHUB_APP_ID')
payload = {"iat": int(time.time()), "exp": int(time.time()) + 600, "iss": app_id}
jwt_token = jwt.encode(payload, pem, algorithm='RS256')
resp = requests.post(
    f"https://api.github.com/app/installations/{installation_id}/access_tokens",
    headers={"Authorization": f"Bearer {jwt_token}", "Accept": "application/vnd.github+json"}
)
print(resp.json()["token"])

Because the token expires quickly, even if the agent logs it, the window for abuse is tiny.

What runtime safeguards should you enforce?

Beyond permission scoping, add these guardrails:

Implement these checks in your CI pipeline or as GitHub branch protection rules.

How to audit and rotate credentials regularly

Maintain a simple audit log:

{
  "timestamp": "2026-07-30T12:34:56Z",
  "event": "github_token_issued",
  "agent_id": "openai‑assistant‑001",
  "repo": "my‑private‑repo",
  "branch": "ai/feature‑xyz"
}

Send the log to a centralized SIEM or a low‑cost log service. Review the logs weekly for anomalies.

Rotate the GitHub App private key at least every 30 days. Automate the rotation by scripting the creation of a new key, updating the secret manager, and revoking the old key via the GitHub API.

Putting it all together – a quick checklist

  1. Define the exact actions the agent needs.
  2. Create a GitHub App with only those permissions and limited to the target repo.
  3. Store the App private key in a secret manager; never commit it.
  4. Build a short‑lived token service that the OpenAI Agent can call.
  5. Enforce branch, file‑type, and PR‑review policies in GitHub.
  6. Log every token issuance and PR creation; monitor for unexpected activity.
  7. Rotate the App key monthly and audit the permission set quarterly.

Following this playbook lets a small team reap the productivity boost of an AI coding assistant while keeping the repository’s integrity under control.

Need a hands‑on security review of your AI‑driven development pipeline? Reach out to AISecAll for a tailored assessment.

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