feat: validate user regex (#1766)

Validate that user-inputted regex for package patterns are valid RegExp().

Closes #1450
This commit is contained in:
ikisialeu 2018-04-04 10:23:59 +03:00 committed by Rhys Arkins
parent 90d3a2d92d
commit 484ef0cbcd
3 changed files with 26 additions and 1 deletions

View file

@ -158,6 +158,19 @@ async function validateConfig(config) {
}
}
}
if (
(key === 'packagePatterns' || key === 'excludePackagePatterns') &&
!(val && val.length === 1 && val[0] === '*')
) {
try {
RegExp(val);
} catch (e) {
errors.push({
depName: 'Configuration Error',
message: `Invalid regExp for ${key}: \`${val}\``,
});
}
}
}
} else if (type === 'string') {
if (!isString(val)) {

View file

@ -18,6 +18,14 @@ Array [
"depName": "Configuration Error",
"message": "Invalid timezone: Asia",
},
Object {
"depName": "Configuration Error",
"message": "Configuration option \`packagePatterns\` should be a list (Array)",
},
Object {
"depName": "Configuration Error",
"message": "Invalid regExp for excludePackagePatterns: \`abc ([a-z]+) ([a-z]+))\`",
},
Object {
"depName": "Configuration Error",
"message": "Configuration option \`labels\` should be a list (Array)",

View file

@ -7,6 +7,8 @@ describe('config/validation', () => {
foo: 1,
schedule: ['after 5pm'],
timezone: 'Asia/Singapore',
packagePatterns: ['*'],
excludePackagePatterns: ['[a-z]'],
prBody: 'some-body',
lockFileMaintenance: {
bar: 2,
@ -25,6 +27,8 @@ describe('config/validation', () => {
enabled: 1,
schedule: ['every 15 mins every weekday'],
timezone: 'Asia',
packagePatterns: 'abc ([a-z]+) ([a-z]+))',
excludePackagePatterns: ['abc ([a-z]+) ([a-z]+))'],
labels: 5,
semanticCommitType: 7,
lockFileMaintenance: false,
@ -45,7 +49,7 @@ describe('config/validation', () => {
);
expect(warnings).toHaveLength(0);
expect(errors).toMatchSnapshot();
expect(errors).toHaveLength(11);
expect(errors).toHaveLength(13);
});
});
});