Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: split trustLevel into separate options #9388

Merged
merged 1 commit into from Apr 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 post-upgrade commands.
Expand Down Expand Up @@ -52,6 +54,8 @@ npm ci --ignore-scripts
npx ng update @angular/core --from=9.0.0 --to=10.0.0 --migrateOnly --allowDirty --force
```

## allowScripts

## allowedPostUpgradeCommands

A list of regular expressions that determine which commands in `postUpgradeTasks` are allowed to be executed.
Expand Down Expand Up @@ -178,6 +182,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.
Comment on lines +187 to +188
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
By default, Renovate only passes a limited set of environment variables to package managers.
Confidential data can be leaked if a malicious script enumerates all environment variables.
Set `exposeAllEnv` to `true` only if you have reviewed (and trust) the repositories which Renovate bot runs against.

How about this?


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Setting this to true will also allow for variable substitution in `.npmrc` files.
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 @@ -379,13 +390,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 [
"config:js-app",
"config:js-lib",
Expand All @@ -108,8 +111,8 @@ Object {
"includeForks": true,
"lockFileMaintenance": Object {
"automerge": true,
"exposeAllEnv": false,
"schedule": "before 5am",
"trustLevel": "low",
},
"major": Object {
"automerge": false,
Expand Down Expand Up @@ -233,7 +236,6 @@ Object {
"travis": Object {
"enabled": true,
},
"trustLevel": "high",
}
`;

Expand Down
4 changes: 3 additions & 1 deletion lib/config/admin.ts
Expand Up @@ -4,14 +4,16 @@ 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',
'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 @@ -461,17 +461,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 @@ -46,6 +46,7 @@ describe('config/migration', () => {
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 @@ -189,11 +189,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 @@ -84,14 +84,16 @@ 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>;
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 getReleases refuses to clone if trustLevel is not high 1`] = `null`;
exports[`datasource/crate getReleases refuses to clone if allowCustomCrateRegistries is not true 1`] = `null`;

exports[`datasource/crate getReleases returns null for 404 1`] = `
Array [
Expand Down
12 changes: 6 additions & 6 deletions lib/datasource/crate/index.spec.ts
Expand Up @@ -224,7 +224,7 @@ describe('datasource/crate', () => {
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 @@ -239,7 +239,7 @@ describe('datasource/crate', () => {
});
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 @@ -253,7 +253,7 @@ describe('datasource/crate', () => {
});
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 @@ -267,7 +267,7 @@ describe('datasource/crate', () => {
});
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 @@ -283,7 +283,7 @@ describe('datasource/crate', () => {
});
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 @@ -309,7 +309,7 @@ describe('datasource/crate', () => {
});
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 @@ -375,7 +375,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 @@ -384,7 +384,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 @@ -39,7 +39,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;
execOptions.extraEnv.NPM_TOKEN = env.NPM_TOKEN;
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;
execOptions.extraEnv.NPM_TOKEN = env.NPM_TOKEN;
Expand Down