Sub topic 9
App permissions and webhook events
This chapter covers how a GitHub App declares what it can access (permissions) and what it hears about (webhook events), and how the two settings depend on each other. It assumes you can read a JSON payload, can run a small HTTP server, and know what a GitHub App installation is from the previous chapter.
Two separate settings
A GitHub App has two configuration areas that are often confused:
- Permissions control what the app's tokens can do through the API. They are an access control.
- Webhook events control which notifications GitHub sends to your app's webhook URL. They are a delivery subscription.
Permissions and events are linked, but they are not the same thing. You can have a permission without subscribing to any event, and you cannot subscribe to an event unless the app has the permission that event requires.
| Setting | Question it answers | Where it is enforced |
|---|---|---|
| Permission | What may this app read or change? | On every API request made with the app's tokens |
| Webhook event | What should GitHub tell this app about? | When GitHub decides whether to deliver a payload |
Permission levels and categories
Each permission is set to one of three levels: No access, Read-only, or Read and write. Write includes read. A few permissions offer only a subset of these levels.
Permissions fall into three groups:
- Repository permissions apply to repositories the app is installed on. Examples are
Contents,Issues,Pull requests,Metadata, andChecks. - Organization permissions apply to the organization that installed the app. Examples are
MembersandAdministration. - Account permissions apply to the user who authorizes the app, and they matter when the app acts on behalf of a user. An example is
Email addresses.
Metadata is read-only and is granted to every app. It exposes basic repository information, such as names and collaborators, and several other permissions depend on it.
The permission names in the app settings are not the same as the strings in the API. For example, the setting shown as Pull requests appears as pull_requests in API payloads. Check the exact key names when you work with the JSON.
Least privilege for apps
Choose the smallest set of permissions that lets the app do its job. Three reasons make this more important for apps than for personal tokens:
- Installation owners see the list of requested permissions when they install the app. A long list lowers trust and the number of installs.
- Any token the app creates can use every permission the installation has. A leaked token is as powerful as the permission list.
- Raising permissions later has a cost, as described below.
An installation token can also be narrowed further when you create it. The create-token endpoint accepts a repositories list and a permissions object. This produces a token that is a strict subset of the installation's access.
curl -L -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $APP_JWT" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/app/installations/$INSTALLATION_ID/access_tokens \
-d '{"repositories":["my-repo"],"permissions":{"issues":"write"}}'You can only request permissions the installation already has. Asking for more returns an error.
Changing permissions after install
Permission changes behave differently depending on direction.
- Reducing permissions takes effect for existing installations without any action from the owners.
- Adding or raising permissions does not take effect automatically. GitHub emails the installation owner, who must review and accept the new permissions. Until then, the installation keeps the old permission set.
While an upgrade is pending, requests that need the new permission fail with a 403 or 404 response. The app should handle that without crashing.
When a new permission is accepted, GitHub sends a new_permissions_accepted action in the installation event. Apps that need to know when the upgrade has completed can listen for it.
Webhook events
A webhook is an HTTP POST that GitHub sends to a URL you configure. In the app settings you give a webhook URL and a secret, then tick the events to subscribe to.
Events are grouped by the permission that gates them. You will only see Issues in the subscribe list if the app has at least read access to Issues. Some examples:
| Event | Required permission | Typical use |
|---|---|---|
| `issues` | Issues (read) | React to opened or labeled issues |
| `pull_request` | Pull requests (read) | Run checks on new pull requests |
| `push` | Contents (read) | Trigger builds on new commits |
| `check_suite` | Checks (read) | Rerun or create checks |
| `installation` | None (always sent) | Track installs and uninstalls |
Every event payload has an action field that narrows it down. For example, issues can have the actions opened, closed, labeled, and others. Your handler should branch on both the event name and the action, and ignore combinations it does not care about.
Some events are always delivered to a GitHub App, whether or not you subscribed. The installation and installation_repositories events are the main ones. They tell you when the app is installed, removed, suspended, or when its repository selection changes.
Anatomy of a delivery
Each delivery includes headers that identify it:
X-GitHub-Eventis the event name, such asissues.X-GitHub-Deliveryis a unique ID for the delivery.X-Hub-Signature-256is an HMAC SHA-256 signature of the raw body, made with your webhook secret.
The payload for app events includes an installation object with an id. This is the value you pass to the create-token endpoint, so you do not need to look it up separately.
Verifying the signature
Anyone who learns your webhook URL can send fake requests to it. Always verify X-Hub-Signature-256 before you trust a payload. Compute the HMAC over the raw request bytes, not over a parsed and re-serialized JSON object, because whitespace changes alter the signature. Compare using a constant-time function.
The following Node.js server verifies the signature and responds to issues events with the opened action. It needs express installed and the environment variable WEBHOOK_SECRET set.
import crypto from "node:crypto";
import express from "express";
const app = express();
const secret = process.env.WEBHOOK_SECRET;
app.post("/webhook", express.raw({ type: "application/json" }), (req, res) => {
const signature = req.get("X-Hub-Signature-256") || "";
const expected =
"sha256=" + crypto.createHmac("sha256", secret).update(req.body).digest("hex");
const a = Buffer.from(signature);
const b = Buffer.from(expected);
if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) {
return res.status(401).send("Invalid signature");
}
const event = req.get("X-GitHub-Event");
const payload = JSON.parse(req.body.toString("utf8"));
if (event === "issues" && payload.action === "opened") {
console.log(
`Issue #${payload.issue.number} opened in ${payload.repository.full_name}`,
`(installation ${payload.installation.id})`
);
}
res.status(202).send("Accepted");
});
app.listen(3000, () => console.log("Listening on port 3000"));The handler returns quickly with 202. GitHub expects a response within 10 seconds. If your work takes longer, put it on a queue and respond first.
Delivery behavior and failure modes
Webhooks are best-effort. Plan for these cases:
- No automatic retry. GitHub does not redeliver a failed delivery by itself. In the app's Advanced tab you can inspect recent deliveries and redeliver them by hand. The REST API also has endpoints for listing and redelivering app deliveries.
- Duplicates. A manual redelivery reuses the same payload, and your handler can see the same event more than once. Use
X-GitHub-Deliveryto deduplicate. - Ordering. Events can arrive out of order. Do not assume a
labeledevent arrives afteropened. When state matters, fetch the current object from the API instead of trusting the payload alone. - Local development. GitHub cannot reach
localhost. Use a tunnel or a proxy service to expose your local server while you develop.
Common errors
- Event not in the list. The app lacks the permission that gates that event. Add the permission, then subscribe.
- Signature always fails. The server parsed the body as JSON before hashing, or the secret differs from the one in the app settings. Hash the raw bytes.
403 Resource not accessible by integration. The token lacks a permission for that endpoint, or the installation has not accepted a permission upgrade. Check the endpoint documentation for the permission it needs.- Events arrive but the API returns
404. The installation does not include that repository. Check the repository selection from the installations chapter. - No
installationevents seen. These are sent to the webhook URL of the app itself. Confirm the webhook is active and its URL is reachable.
Summary of the relationship
Permissions decide what the app may do. Events decide what the app is told. A permission gates both the API calls and the events related to it, and installation owners control whether a permission change takes effect. Keep both lists as short as the app's behavior allows.
Exercise
An app has Issues: read permission and subscribes to the issues event. The developer changes the permission to Issues: read and write so the app can add labels. After the change, label requests from an existing installation still return 403. What is the most likely cause, and what should the developer do?
Show answer
Raising a permission does not apply to existing installations until the installation owner accepts it. GitHub emails the owner, and the installation keeps Issues: read until they approve. The developer should ask the owner to review and accept the request, and the app can listen for the installation event with the new_permissions_accepted action to learn when it takes effect.
Exercise
A webhook handler parses the request body with express.json() and then computes the HMAC from JSON.stringify(req.body). Valid GitHub deliveries are rejected with a signature mismatch. Why, and how do you fix it?
Show answer
GitHub signs the exact bytes it sent. Re-serializing the parsed object can change whitespace, key formatting, or character escaping, so the hash differs. Read the raw body (for example with express.raw({ type: "application/json" })), compute the HMAC over those bytes, compare with crypto.timingSafeEqual, and parse the JSON afterward.
Exercise
You create an installation token for an installation that has Contents: write and Issues: write. You need a token for a script that only comments on issues in one repository. How do you limit the token?
Show answer
Send a request body to the create-token endpoint with "repositories": ["repo-name"] and "permissions": {"issues": "write"}. The resulting token covers only that repository and only the issues permission, which is a subset of what the installation holds.