Files
renovate/lib/modules/manager/nix/artifacts.ts
Jamie Tanna 521d325afd fix(manager/nix): quote GitHub token in flake update command
A malicious Host Rule could be crafted to inject shell metacharacters
which could lead to command execution, we can make sure they are quoted
appropriately.

Co-authored-by: Claude Sonnet 5 <jamie.tanna+claude-code@mend.io>
2026-08-07 14:22:55 +01:00

88 lines
2.4 KiB
TypeScript

import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is';
import { quote } from 'shlex';
import { logger } from '../../../logger/index.ts';
import { findGithubToken } from '../../../util/check-token.ts';
import { exec } from '../../../util/exec/index.ts';
import type { ExecOptions } from '../../../util/exec/types.ts';
import { readLocalFile } from '../../../util/fs/index.ts';
import { getRepoStatus } from '../../../util/git/index.ts';
import * as hostRules from '../../../util/host-rules.ts';
import { regEx } from '../../../util/regex.ts';
import type { UpdateArtifact, UpdateArtifactsResult } from '../types.ts';
export async function updateArtifacts({
packageFileName,
config,
updatedDeps,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
const lockFileName = packageFileName.replace(regEx(/\.nix$/), '.lock');
const existingLockFileContent = await readLocalFile(lockFileName, 'utf8');
if (!existingLockFileContent) {
logger.debug('No flake.lock found');
return null;
}
let cmd = `nix --extra-experimental-features 'nix-command flakes' `;
const token = findGithubToken(
hostRules.find({
hostType: 'github',
url: 'https://api.github.com/',
}),
);
if (token) {
cmd += `--extra-access-tokens github.com=${quote(token)} `;
}
if (config.isLockFileMaintenance) {
cmd += 'flake update';
} else {
const inputs = updatedDeps
.map(({ depName }) => depName)
.filter(isNonEmptyStringAndNotWhitespace)
.map((depName) => quote(depName))
.join(' ');
cmd += `flake update ${inputs}`;
}
const execOptions: ExecOptions = {
cwdFile: packageFileName,
toolConstraints: [
{
toolName: 'nix',
constraint: config.constraints?.nix,
},
],
docker: {},
};
try {
await exec(cmd, execOptions);
const status = await getRepoStatus();
if (!status.modified.includes(lockFileName)) {
return null;
}
logger.debug('Returning updated flake.lock');
return [
{
file: {
type: 'addition',
path: lockFileName,
contents: await readLocalFile(lockFileName),
},
},
];
} catch (err) {
logger.warn({ err }, 'Error updating flake.lock');
return [
{
artifactError: {
fileName: lockFileName,
stderr: err.message,
},
},
];
}
}