Skip to content

Commit

Permalink
feat: split trustLevel into separate options (#9388)
Browse files Browse the repository at this point in the history
Removes trustLevel and replaces it with 3 other options instead. No longer necessary to set anything additional is configuring `allowedPostUpgradeCommands`

BREAKING CHANGE: `trustLevel` is no longer supported and instead broken into `allowCustomCrateRegistries` , `allowScripts` , and `exposeAllEnv`.
  • Loading branch information
rarkins committed Apr 22, 2021
1 parent 95ac109 commit 8af905e
Show file tree
Hide file tree
Showing 27 changed files with 88 additions and 62 deletions.
20 changes: 11 additions & 9 deletions docs/usage/self-hosted-configuration.md
Expand Up @@ -9,6 +9,8 @@ The configuration options listed in this document are applicable to self-hosted

Please also see [Self-Hosted Experimental Options](./self-hosted-experimental.md).

## allowCustomCrateRegistries

## allowPostUpgradeCommandTemplating

Set to true to allow templating of dependency level post-upgrade commands.
Expand Down Expand Up @@ -52,6 +54,8 @@ npm ci --ignore-scripts
npx ng update @angular/core --from=10.0.0 --to=11.0.0 --migrate-only --allow-dirty --force
```

## allowScripts

## allowedPostUpgradeCommands

A list of regular expressions that determine which commands in `postUpgradeTasks` are allowed to be executed.
Expand Down Expand Up @@ -186,6 +190,13 @@ e.g.

## endpoint

## exposeAllEnv

By default, Renovate will only pass a limited set of environment variables to package managers.
Potentially, there could be leaks of confidential data if a script you don't trust enumerates all values in env, so set this to true only if you trust the repositories which the bot runs against.

Setting this to true will also allow for variable substitution in `.npmrc` files.

## force

This object is used as a "force override" when you need to make sure certain configuration overrides whatever is configured in the repository.
Expand Down Expand Up @@ -375,13 +386,4 @@ This is currently applicable to `npm` and `lerna`/`npm` only, and only used in c

## token

## trustLevel

Setting trustLevel to `"high"` can make sense in many self-hosted cases where the bot operator trusts the content in each repository.

Setting trustLevel=high means:

- Child processes are run with full access to `env`
- `.npmrc` files can have environment variable substitution performed

## username
6 changes: 4 additions & 2 deletions lib/config/__snapshots__/migration.spec.ts.snap
Expand Up @@ -78,6 +78,8 @@ Array [
exports[`config/migration migrateConfig(config, parentConfig) migrates config 1`] = `
Object {
"additionalBranchPrefix": "{{parentDir}}-",
"allowCustomCrateRegistries": true,
"allowScripts": true,
"autodiscover": true,
"automerge": false,
"automergeType": "branch",
Expand All @@ -94,6 +96,7 @@ Object {
"dependencyDashboard": true,
"dependencyDashboardTitle": "foo",
"enabled": true,
"exposeAllEnv": true,
"extends": Array [
":automergeBranch",
"config:js-app",
Expand All @@ -109,8 +112,8 @@ Object {
"includeForks": true,
"lockFileMaintenance": Object {
"automerge": true,
"exposeAllEnv": false,
"schedule": "before 5am",
"trustLevel": "low",
},
"major": Object {
"automerge": false,
Expand Down Expand Up @@ -243,7 +246,6 @@ Object {
"travis": Object {
"enabled": true,
},
"trustLevel": "high",
}
`;

Expand Down
4 changes: 3 additions & 1 deletion lib/config/admin.ts
Expand Up @@ -4,15 +4,17 @@ let adminConfig: RepoAdminConfig = {};

// TODO: once admin config work is complete, add a test to make sure this list includes all options with admin=true
export const repoAdminOptions = [
'allowCustomCrateRegistries',
'allowPostUpgradeCommandTemplating',
'allowScripts',
'allowedPostUpgradeCommands',
'customEnvVariables',
'dockerChildPrefix',
'dockerImagePrefix',
'dockerUser',
'dryRun',
'exposeAllEnv',
'privateKey',
'trustLevel',
];

export function setAdminConfig(config: RenovateConfig = {}): void {
Expand Down
26 changes: 21 additions & 5 deletions lib/config/definitions.ts
Expand Up @@ -473,17 +473,33 @@ const options: RenovateOptions[] = [
default: false,
},
{
name: 'trustLevel',
name: 'exposeAllEnv',
description:
'Set this to "high" if the bot should trust the repository owners/contents.',
'Configure this to true to allow passing of all env variables to package managers.',
admin: true,
type: 'string',
default: 'low',
type: 'boolean',
default: false,
},
{
name: 'allowScripts',
description:
'Configure this to true if repositories are allowed to run install scripts.',
admin: true,
type: 'boolean',
default: false,
},
{
name: 'allowCustomCrateRegistries',
description:
'Configure this to true if custom crate registries are allowed.',
admin: true,
type: 'boolean',
default: false,
},
{
name: 'ignoreScripts',
description:
'Configure this to true if trustLevel is high but you wish to skip running scripts when updating lock files.',
'Configure this to true if allowScripts=true but you wish to skip running scripts when updating lock files.',
type: 'boolean',
default: false,
},
Expand Down
1 change: 1 addition & 0 deletions lib/config/migration.spec.ts
Expand Up @@ -56,6 +56,7 @@ describe(getName(__filename), () => {
masterIssueTitle: 'foo',
gomodTidy: true,
upgradeInRange: true,
trustLevel: 'high',
automergeType: 'branch-push',
branchName:
'{{{branchPrefix}}}{{{managerBranchPrefix}}}{{{branchTopic}}}{{{baseDir}}}',
Expand Down
11 changes: 7 additions & 4 deletions lib/config/migration.ts
Expand Up @@ -191,11 +191,14 @@ export function migrateConfig(
migratedConfig.rebaseWhen = 'never';
}
} else if (key === 'exposeEnv') {
migratedConfig.exposeAllEnv = val;
delete migratedConfig.exposeEnv;
if (val === true) {
migratedConfig.trustLevel = 'high';
} else if (val === false) {
migratedConfig.trustLevel = 'low';
} else if (key === 'trustLevel') {
delete migratedConfig.trustLevel;
if (val === 'high') {
migratedConfig.allowCustomCrateRegistries ??= true;
migratedConfig.allowScripts ??= true;
migratedConfig.exposeAllEnv ??= true;
}
} else if (
key === 'branchName' &&
Expand Down
4 changes: 3 additions & 1 deletion lib/config/types.ts
Expand Up @@ -83,15 +83,17 @@ export interface GlobalOnlyConfig {
// Config options used within the repository worker, but not user configurable
// The below should contain config options where admin=true
export interface RepoAdminConfig {
allowCustomCrateRegistries?: boolean;
allowPostUpgradeCommandTemplating?: boolean;
allowScripts?: boolean;
allowedPostUpgradeCommands?: string[];
customEnvVariables?: Record<string, string>;
dockerChildPrefix?: string;
dockerImagePrefix?: string;
dockerUser?: string;
dryRun?: boolean;
exposeAllEnv?: boolean;
privateKey?: string | Buffer;
trustLevel?: 'low' | 'high';
}

export interface LegacyAdminConfig {
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/crate/__snapshots__/index.spec.ts.snap
Expand Up @@ -331,7 +331,7 @@ Array [
]
`;

exports[`datasource/crate/index getReleases refuses to clone if trustLevel is not high 1`] = `null`;
exports[`datasource/crate/index getReleases refuses to clone if allowCustomCrateRegistries is not true 1`] = `null`;

exports[`datasource/crate/index getReleases returns null for 404 1`] = `
Array [
Expand Down
12 changes: 6 additions & 6 deletions lib/datasource/crate/index.spec.ts
Expand Up @@ -225,7 +225,7 @@ describe(getName(__filename), () => {
expect(res).toBeDefined();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('refuses to clone if trustLevel is not high', async () => {
it('refuses to clone if allowCustomCrateRegistries is not true', async () => {
const { mockClone } = setupGitMocks();

const url = 'https://dl.cloudsmith.io/basic/myorg/myrepo/cargo/index.git';
Expand All @@ -240,7 +240,7 @@ describe(getName(__filename), () => {
});
it('clones cloudsmith private registry', async () => {
const { mockClone } = setupGitMocks();
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowCustomCrateRegistries: true });
const url = 'https://dl.cloudsmith.io/basic/myorg/myrepo/cargo/index.git';
const res = await getPkgReleases({
datasource,
Expand All @@ -254,7 +254,7 @@ describe(getName(__filename), () => {
});
it('clones other private registry', async () => {
const { mockClone } = setupGitMocks();
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowCustomCrateRegistries: true });
const url = 'https://github.com/mcorbin/testregistry';
const res = await getPkgReleases({
datasource,
Expand All @@ -268,7 +268,7 @@ describe(getName(__filename), () => {
});
it('clones once then reuses the cache', async () => {
const { mockClone } = setupGitMocks();
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowCustomCrateRegistries: true });
const url = 'https://github.com/mcorbin/othertestregistry';
await getPkgReleases({
datasource,
Expand All @@ -284,7 +284,7 @@ describe(getName(__filename), () => {
});
it('guards against race conditions while cloning', async () => {
const { mockClone } = setupGitMocks(250);
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowCustomCrateRegistries: true });
const url = 'https://github.com/mcorbin/othertestregistry';

await Promise.all([
Expand All @@ -310,7 +310,7 @@ describe(getName(__filename), () => {
});
it('returns null when git clone fails', async () => {
setupErrorGitMock();
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowCustomCrateRegistries: true });
const url = 'https://github.com/mcorbin/othertestregistry';

const result = await getPkgReleases({
Expand Down
4 changes: 2 additions & 2 deletions lib/datasource/crate/index.ts
Expand Up @@ -163,9 +163,9 @@ async function fetchRegistryInfo(
};

if (flavor !== RegistryFlavor.CratesIo) {
if (getAdminConfig().trustLevel !== 'high') {
if (!getAdminConfig().allowCustomCrateRegistries) {
logger.warn(
'crate datasource: trustLevel=high is required for registries other than crates.io, bailing out'
'crate datasource: allowCustomCrateRegistries=true is required for registries other than crates.io, bailing out'
);
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/datasource/npm/index.spec.ts
Expand Up @@ -359,7 +359,7 @@ describe(getName(__filename), () => {
.reply(200, npmResponse);
process.env.REGISTRY = 'https://registry.from-env.com';
process.env.RENOVATE_CACHE_NPM_MINUTES = '15';
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ exposeAllEnv: true });
// eslint-disable-next-line no-template-curly-in-string
const npmrc = 'registry=${REGISTRY}';
const res = await getPkgReleases({ datasource, depName: 'foobar', npmrc });
Expand All @@ -368,7 +368,7 @@ describe(getName(__filename), () => {
});

it('should throw error if necessary env var is not present', () => {
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ exposeAllEnv: true });
// eslint-disable-next-line no-template-curly-in-string
expect(() => setNpmrc('registry=${REGISTRY_MISSING}')).toThrow(
Error('env-replace')
Expand Down
2 changes: 1 addition & 1 deletion lib/datasource/npm/npmrc.spec.ts
Expand Up @@ -38,7 +38,7 @@ describe(getName(__filename), () => {
});

it('sanitize _authtoken with high trust', () => {
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ exposeAllEnv: true });
process.env.TEST_TOKEN = 'test';
setNpmrc(
// eslint-disable-next-line no-template-curly-in-string
Expand Down
8 changes: 4 additions & 4 deletions lib/datasource/npm/npmrc.ts
Expand Up @@ -61,13 +61,13 @@ export function setNpmrc(input?: string): void {
npmrcRaw = input;
logger.debug('Setting npmrc');
npmrc = ini.parse(input.replace(/\\n/g, '\n'));
const { trustLevel } = getAdminConfig();
const { exposeAllEnv } = getAdminConfig();
for (const [key, val] of Object.entries(npmrc)) {
if (trustLevel !== 'high') {
if (!exposeAllEnv) {
sanitize(key, val);
}
if (
trustLevel !== 'high' &&
!exposeAllEnv &&
key.endsWith('registry') &&
val &&
val.includes('localhost')
Expand All @@ -80,7 +80,7 @@ export function setNpmrc(input?: string): void {
return;
}
}
if (trustLevel !== 'high') {
if (!exposeAllEnv) {
return;
}
for (const key of Object.keys(npmrc)) {
Expand Down
5 changes: 3 additions & 2 deletions lib/manager/composer/artifacts.spec.ts
Expand Up @@ -30,6 +30,7 @@ const config = {
localDir: join('/tmp/github/some/repo'),
cacheDir: join('/tmp/renovate/cache'),
composerIgnorePlatformReqs: true,
ignoreScripts: false,
};

const repoStatus = partial<StatusResult>({
Expand All @@ -46,7 +47,7 @@ describe('.updateArtifacts()', () => {
await setUtilConfig(config);
docker.resetPrefetchedImages();
hostRules.clear();
setAdminConfig();
setAdminConfig({ allowScripts: false });
});
it('returns if no composer.lock found', async () => {
expect(
Expand All @@ -63,7 +64,7 @@ describe('.updateArtifacts()', () => {
const execSnapshots = mockExecAll(exec);
fs.readLocalFile.mockReturnValueOnce('Current composer.lock' as any);
git.getRepoStatus.mockResolvedValue(repoStatus);
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowScripts: true });
expect(
await composer.updateArtifacts({
packageFileName: 'composer.json',
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/composer/artifacts.ts
Expand Up @@ -151,7 +151,7 @@ export async function updateArtifacts({
args += ' --ignore-platform-reqs';
}
args += ' --no-ansi --no-interaction';
if (getAdminConfig().trustLevel !== 'high' || config.ignoreScripts) {
if (!getAdminConfig().allowScripts || config.ignoreScripts) {
args += ' --no-scripts --no-autoloader';
}
logger.debug({ cmd, args }, 'composer command');
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/extract/index.ts
Expand Up @@ -107,7 +107,7 @@ export async function extractPackageFile(
npmrc = npmrc.replace(/(^|\n)package-lock.*?(\n|$)/g, '\n');
}
if (is.string(npmrc)) {
if (npmrc.includes('=${') && getAdminConfig().trustLevel !== 'high') {
if (npmrc.includes('=${') && !getAdminConfig().exposeAllEnv) {
logger.debug('Discarding .npmrc file with variables');
ignoreNpmrcFile = true;
npmrc = undefined;
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/lerna.spec.ts
Expand Up @@ -109,7 +109,7 @@ describe(getName(__filename), () => {
});
it('allows scripts for trust level high', async () => {
const execSnapshots = mockExecAll(exec);
setAdminConfig({ trustLevel: 'high' });
setAdminConfig({ allowScripts: true });
const res = await lernaHelper.generateLockFiles(
lernaPkgFile('npm'),
'some-dir',
Expand Down
7 changes: 2 additions & 5 deletions lib/manager/npm/post-update/lerna.ts
Expand Up @@ -72,10 +72,7 @@ export async function generateLockFiles(
return { error: false };
}
let lernaCommand = `lerna bootstrap --no-ci --ignore-scripts -- `;
if (
getAdminConfig().trustLevel === 'high' &&
config.ignoreScripts !== false
) {
if (getAdminConfig().allowScripts && config.ignoreScripts !== false) {
cmdOptions = cmdOptions.replace('--ignore-scripts ', '');
lernaCommand = lernaCommand.replace('--ignore-scripts ', '');
}
Expand All @@ -96,7 +93,7 @@ export async function generateLockFiles(
},
};
// istanbul ignore if
if (getAdminConfig().trustLevel === 'high') {
if (getAdminConfig().exposeAllEnv) {
execOptions.extraEnv.NPM_AUTH = env.NPM_AUTH;
execOptions.extraEnv.NPM_EMAIL = env.NPM_EMAIL;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/manager/npm/post-update/npm.ts
Expand Up @@ -71,7 +71,7 @@ export async function generateLockFile(
},
};
// istanbul ignore if
if (getAdminConfig().trustLevel === 'high') {
if (getAdminConfig().exposeAllEnv) {
execOptions.extraEnv.NPM_AUTH = env.NPM_AUTH;
execOptions.extraEnv.NPM_EMAIL = env.NPM_EMAIL;
}
Expand Down

0 comments on commit 8af905e

Please sign in to comment.