Renovate ignored my read-only token
We use Renovate at work, a bot that opens pull requests to update dependencies, and I’ve spent a lot of time investigating how it works under the hood. This post is about one of those investigations.
I like it when my tools only get the permissions they need, which sounds like a boring preference until you try to apply it to Renovate and GitHub’s token model at the same time. It’s the story of how I found out that a read-only token was being ignored, how I traced it through the code, and what it took to fix.
Two tokens, because GitHub makes you
Renovate needs to write to the repositories it updates: branches, pull requests, comments. It may also need to read other repositories when fetching dependency versions, such as submodule commits for submodule updates.
GitHub’s fine-grained tokens can’t say “write to repo A, read from repos B and C”. So the least-privilege setup is two tokens:
RENOVATE_TOKEN: write access, scoped to just the repositories Renovate works on.- A second, read-only token with access to everything it needs to look at, registered as a host rule with
readOnly: true.
That is exactly what the docs seem to promise. From the security page:
We apply the Principle of Least Privilege (PoLP) but do need substantial privileges for our apps to work.
and from the readOnly option:
If more than one token matches for a read-only request then the readOnly token will be given preference.
I set that up, and it did not behave the way I expected.
The symptom
My original problem was git-submodules updates: the write token was scoped to one repo, so it couldn’t read the private submodule, and Renovate wasn’t using my read-only token for it. While digging I noticed something worse. Renovate passes git credentials to the commands it runs using GIT_CONFIG_* environment variables, and for postUpgradeTasks that included the write token, even though a read-only one was available.
You can see it with a very small setup. This is the config from my reproducer repo:
module.exports = {
hostRules: [
{
matchHost: "api.github.com",
token: process.env.READONLY_GITHUB_TOKEN,
readOnly: true,
},
],
allowedCommands: [".*"],
};
Then a postUpgradeTasks command that looks at its own environment. In the reproducer it uses what it finds to post a comment on a pull request, which only a write token can do. The result is this comment: proof that the write token reached the task.
Why does that matter if I wrote the commands myself? Because commands you allow often run code you didn’t write: a build script, a package manager that executes lifecycle hooks, a dependency. Whatever they run inherits the environment.
Tracing it
Here is where the token comes from. All links are to the commit the discussion was written against (bc82cff), since main has moved on.
For postUpgradeTasks, the environment is built by calling getGitEnvironmentVariables() (execute-post-upgrade-commands.ts#L140):
const execOpts: ExecOptions = {
shell: GlobalConfig.get('allowShellExecutorForPostUpgradeCommands', false),
cwd: workingDir,
extraEnv: getGitEnvironmentVariables(),
};
That function looks up the GitHub credential with a hard-coded search (auth.ts#L178):
// hard-coded logic to use authentication for github.com based on the githubToken for api.github.com
const gitHubHostRule = find({
hostType: 'github',
url: 'https://api.github.com/',
});
Notice what is missing: no readOnly. So the question becomes what find does with a search that doesn’t mention it. This is the interesting part (host-rules.ts#L165-L172):
if (!isUndefined(rule.readOnly)) {
readOnlyMatch = false;
if (search.readOnly === rule.readOnly) {
readOnlyMatch = true;
hostTypeMatch = true; // When we match `readOnly`, we don't care about `hostType`
}
}
If a rule has readOnly: true, it only matches a search that also says readOnly: true. Our search says nothing (undefined), undefined === true is false, and the read-only rule is skipped entirely. It is not outranked by the write token. It is never considered. That is why the docs’ “read-only token will be given preference” only applies to callers that explicitly ask for a read-only match, and this one doesn’t.
Here is the same thing as a test. This is the one I put in the discussion, and the one I’d want to pass:
it('git env lookup prefers readonly token when overlapping github rules exist', () => {
add({
hostType: 'github',
matchHost: 'https://api.github.com/',
token: 'write-token',
});
add({
hostType: 'github',
matchHost: 'https://api.github.com/',
token: 'readonly-token',
readOnly: true,
});
expect(getGitEnvironmentVariables()).toStrictEqual({
GIT_CONFIG_COUNT: '3',
GIT_CONFIG_KEY_0: 'url.https://ssh:[email protected]/.insteadOf',
GIT_CONFIG_KEY_1: 'url.https://git:[email protected]/.insteadOf',
GIT_CONFIG_KEY_2: 'url.https://[email protected]/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://[email protected]/',
GIT_CONFIG_VALUE_1: '[email protected]:',
GIT_CONFIG_VALUE_2: 'https://github.com/',
});
});
There is a second, smaller issue. When both a rule with a hostType and a read-only rule match, they get the same rank, so the winner comes down to ordering (host-rules.ts#L113-L127):
function hostRuleRank({ hostType, matchHost, readOnly }: HostRule): number {
if ((hostType || readOnly) && matchHost) {
return 3;
}
...
So even if the lookup found both, “read-only wins” would be an accident of sort order and not a rule.
It is not just postUpgradeTasks
Going through the code in the pull request, these are the places I found that pass the token along: postUpgradeTasks, the git-submodules manager and the other git operations Renovate runs itself (git checkout), the git-refs datasource (git ls-remote), and the artifact updaters for gomod, cargo, conan, poetry, copier, vendir, hermit and pep621 (pdm and uv). Most of them go through one helper that builds the GIT_CONFIG_* variables, which is why a single fix covers them. A few others look the GitHub token up on their own, and the pull request changes those too: composer, mise, nix, the GitHub Actions manager and Renovate’s own token check. The list in the discussion is a bit shorter. Some of these run tools whose behaviour I would not want to reason about with a write token in the environment. Most of them are benign today. My argument in the discussion was that it’s hard to anticipate what can be abused later, so these should get a read-only token where possible, or none at all without an explicit opt-in.
The fix
The idea is small: ask for a read-only match where a read-only token is enough, and fall back to the write token when there isn’t one. From the pull request, the core of it in getGitEnvironmentVariables:
const gitHubHostRule = find({
hostType: 'github',
+ readOnly: true,
url: 'https://api.github.com/',
});
and the rank change, so read-only rules sort above typed ones and the preference stops depending on ordering:
function hostRuleRank({ hostType, matchHost, readOnly }: HostRule): number {
- if ((hostType || readOnly) && matchHost) {
+ if (readOnly && matchHost) {
+ return 4;
+ }
+
+ if (hostType && matchHost) {
return 3;
}
If no read-only token is configured, nothing breaks: RENOVATE_TOKEN still matches read-only searches, so it is used as before.
The rest of the change is the same readOnly: true repeated in the other call sites (git-refs through getRemoteUrlWithToken, composer, mise, nix, the GitHub Actions manager and the token check), plus docs and tests. It touches 19 files and adds around 420 lines, almost all of it tests that repeat the same shape in different code paths.
I tried it on the reproducer repo with two fine-grained tokens, a write token for public repos only and a read-only token for the private submodule. The postUpgradeTasks run no longer sees a write token and can’t post a comment, and the submodule update works.
What to do today if you self-host
Keep the write token as narrow as you can. Until the fix lands, the read-only token isn’t used on these paths, so
RENOVATE_TOKENhas to be able to read everything Renovate looks at, which means it gets write access to those repositories as well. Limit it to the repositories Renovate depends on, and no more.Clean the environment for
postUpgradeTasks. Start your commands withenv -iand pass only what they need:env -i PATH="$PATH" HOME="$HOME" MY_VAR="$MY_VAR" ./my-commandThis works for
postUpgradeTasks. It does nothing for the managers and datasources that Renovate runs for you, which is why the pull request matters.After the fix lands, register the read-only token for both hosts. Then scope
RENOVATE_TOKENto a single repo you’re opening pull requests against (you should isolate your Renovate runs for each repository). You’ll need host rules forapi.github.comandgithub.comto cover every read-only use.
A potential better future
I started with “this is a bug”. I ended with a question about design: credentials injected into the environment of every subprocess by default, with no way to opt out, is a very different thing from credentials a user explicitly hands to a command.
This is by no means the only surprising Renovate behaviour I have run into, and I have reported a number of others over the years. In my experience they usually come from if statements that patch one edge case and often introduce another, and those are the real code smell. That is what happens when a big code base keeps adding features without breaking anyone’s config. Renovate and Dependabot are the two big tools in this space, and a tool with that much history has a hard time making sweeping changes. A smaller tool built around security and performance could start clean, at the cost of supporting fewer ecosystems. Running it next to Renovate for everything it doesn’t cover would be a faff, and “supports less” is a hard thing to sell, so I understand why it doesn’t exist.
For now, the pull request makes the default less dangerous. The opt-in version, where a command only gets the credentials you explicitly give it, is the part I would still like to see.