Skip to content

Commit

Permalink
fix: Revert "refactor: use re2 instead of RegExp (#4441)"
Browse files Browse the repository at this point in the history
This reverts commit ab1c5b2.
  • Loading branch information
rarkins committed Oct 15, 2019
1 parent 1242981 commit f524557
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 47 deletions.
3 changes: 1 addition & 2 deletions lib/config/presets.ts
Expand Up @@ -7,7 +7,6 @@ import * as npm from '../datasource/npm';
import * as gitlab from '../datasource/gitlab';
import { RenovateConfig } from './common';
import { mergeChildConfig } from './utils';
import { regEx } from '../util/regex';

const datasources = {
github,
Expand Down Expand Up @@ -127,7 +126,7 @@ export function replaceArgs(
if (is.string(obj)) {
let returnStr = obj;
for (const [arg, argVal] of Object.entries(argMapping)) {
const re = regEx(`{{${arg}}}`, 'g');
const re = new RegExp(`{{${arg}}}`, 'g');
returnStr = returnStr.replace(re, argVal);
}
return returnStr;
Expand Down
32 changes: 22 additions & 10 deletions lib/config/validation.ts
@@ -1,10 +1,10 @@
import is from '@sindresorhus/is';
import safe from 'safe-regex';
import { getOptions, RenovateOptions } from './definitions';
import { resolveConfigPresets } from './presets';
import { hasValidSchedule, hasValidTimezone } from '../workers/branch/schedule';
import * as managerValidator from './validation-helpers/managers';
import { RenovateConfig, ValidationMessage } from './common';
import { regEx } from '../util/regex';

const options = getOptions();

Expand Down Expand Up @@ -183,7 +183,13 @@ export async function validateConfig(
!(val && val.length === 1 && val[0] === '*')
) {
try {
regEx(val as any);
RegExp(val as any);
if (!safe(val as any)) {
errors.push({
depName: 'Configuration Error',
message: `Unsafe regExp for ${currentPath}: \`${val}\``,
});
}
} catch (e) {
errors.push({
depName: 'Configuration Error',
Expand All @@ -192,15 +198,21 @@ export async function validateConfig(
}
}
if (key === 'fileMatch') {
for (const fileMatch of val) {
try {
regEx(fileMatch);
} catch (e) {
errors.push({
depName: 'Configuration Error',
message: `Invalid regExp for ${currentPath}: \`${fileMatch}\``,
});
try {
for (const fileMatch of val) {
RegExp(fileMatch);
if (!safe(fileMatch)) {
errors.push({
depName: 'Configuration Error',
message: `Unsafe regExp for ${currentPath}: \`${fileMatch}\``,
});
}
}
} catch (e) {
errors.push({
depName: 'Configuration Error',
message: `Invalid regExp for ${currentPath}: \`${val}\``,
});
}
}
if (
Expand Down
3 changes: 1 addition & 2 deletions lib/datasource/go/index.ts
Expand Up @@ -2,7 +2,6 @@ import { logger } from '../../logger';
import got from '../../util/got';
import * as github from '../github';
import { DigestConfig, PkgReleaseConfig, ReleaseResult } from '../common';
import { regEx } from '../../util/regex';

interface DataSource {
datasource: string;
Expand Down Expand Up @@ -31,7 +30,7 @@ async function getDatasource(name: string): Promise<DataSource | null> {
hostType: 'go',
})).body;
const sourceMatch = res.match(
regEx(`<meta\\s+name="go-source"\\s+content="${name}\\s+([^\\s]+)`)
new RegExp(`<meta\\s+name="go-source"\\s+content="${name}\\s+([^\\s]+)`)
);
if (sourceMatch) {
const [, goSourceUrl] = sourceMatch;
Expand Down
3 changes: 1 addition & 2 deletions lib/manager/ansible/update.ts
@@ -1,7 +1,6 @@
import { logger } from '../../logger';
import { getNewFrom } from '../dockerfile/update';
import { Upgrade } from '../common';
import { regEx } from '../../util/regex';

export default function updateDependency(
fileContent: string,
Expand All @@ -12,7 +11,7 @@ export default function updateDependency(
logger.debug(`ansible.updateDependency(): ${newFrom}`);
const lines = fileContent.split('\n');
const lineToChange = lines[upgrade.managerData.lineNumber];
const imageLine = regEx(`^(\\s*image:\\s*'?"?)[^\\s'"]+('?"?\\s*)$`);
const imageLine = new RegExp(/^(\s*image:\s*'?"?)[^\s'"]+('?"?\s*)$/);
if (!lineToChange.match(imageLine)) {
logger.debug('No image line found');
return null;
Expand Down
3 changes: 1 addition & 2 deletions lib/manager/bazel/extract.ts
Expand Up @@ -3,7 +3,6 @@ import parse from 'github-url-from-git';
import { parse as _parse } from 'url';
import { logger } from '../../logger';
import { PackageDependency, PackageFile } from '../common';
import { regEx } from '../../util/regex';

interface UrlParsedResult {
repo: string;
Expand Down Expand Up @@ -79,7 +78,7 @@ function parseContent(content: string): string[] {
(acc, prefix) => [
...acc,
...content
.split(regEx(prefix + '\\s*\\(', 'g'))
.split(new RegExp(prefix + '\\s*\\(', 'g'))
.slice(1)
.map(base => {
const ind = findBalancedParenIndex(base);
Expand Down
13 changes: 8 additions & 5 deletions lib/manager/bazel/update.ts
Expand Up @@ -2,7 +2,6 @@ import { fromStream } from 'hasha';
import got from '../../util/got';
import { logger } from '../../logger';
import { Upgrade } from '../common';
import { regEx } from '../../util/regex';

function updateWithNewVersion(
content: string,
Expand Down Expand Up @@ -134,11 +133,13 @@ export async function updateDependency(
newDef = setNewHash(newDef, hash);
} else if (upgrade.depType === 'http_archive' && upgrade.newDigest) {
const [, shortRepo] = upgrade.repo.split('/');
const url = `https://github.com/${upgrade.repo}/archive/${upgrade.newDigest}.tar.gz`;
const url = `https://github.com/${upgrade.repo}/archive/${
upgrade.newDigest
}.tar.gz`;
const hash = await getHashFromUrl(url);
newDef = setNewHash(upgrade.managerData.def, hash);
newDef = newDef.replace(
regEx(`(strip_prefix\\s*=\\s*)"[^"]*"`),
new RegExp(`(strip_prefix\\s*=\\s*)"[^"]*"`),
`$1"${shortRepo}-${upgrade.newDigest}"`
);
const match =
Expand All @@ -148,11 +149,13 @@ export async function updateDependency(
});
}
logger.debug({ oldDef: upgrade.managerData.def, newDef });
let existingRegExStr = `${upgrade.depType}\\([^\\)]+name\\s*=\\s*"${upgrade.depName}"(.*\\n)+?\\s*\\)`;
let existingRegExStr = `${upgrade.depType}\\([^\\)]+name\\s*=\\s*"${
upgrade.depName
}"(.*\\n)+?\\s*\\)`;
if (newDef.endsWith('\n')) {
existingRegExStr += '\n';
}
const existingDef = regEx(existingRegExStr);
const existingDef = new RegExp(existingRegExStr);
// istanbul ignore if
if (!fileContent.match(existingDef)) {
logger.info('Cannot match existing string');
Expand Down
3 changes: 1 addition & 2 deletions lib/manager/buildkite/update.ts
@@ -1,6 +1,5 @@
import { logger } from '../../logger';
import { Upgrade } from '../common';
import { regEx } from '../../util/regex';

export function updateDependency(
currentFileContent: string,
Expand All @@ -11,7 +10,7 @@ export function updateDependency(
logger.debug(`buildkite.updateDependency: ${upgrade.newValue}`);
const lines = currentFileContent.split('\n');
const lineToChange = lines[lineIdx];
const depLine = regEx(`^(\\s+[^#]+#)[^:]+(:.*)$`);
const depLine = new RegExp(/^(\s+[^#]+#)[^:]+(:.*)$/);
if (!lineToChange.match(depLine)) {
logger.debug('No image line found');
return null;
Expand Down
15 changes: 8 additions & 7 deletions lib/manager/bundler/extract.ts
Expand Up @@ -2,7 +2,6 @@ import { logger } from '../../logger';
import { isValid } from '../../versioning/ruby';
import { PackageFile, PackageDependency } from '../common';
import { platform } from '../../platform';
import { regEx } from '../../util/regex';

export { extractPackageFile };

Expand All @@ -23,7 +22,7 @@ async function extractPackageFile(
sourceMatch =
sourceMatch ||
line.match(
regEx(`^source ${delimiter}([^${delimiter}]+)${delimiter}\\s*$`)
new RegExp(`^source ${delimiter}([^${delimiter}]+)${delimiter}\\s*$`)
);
}
if (sourceMatch) {
Expand All @@ -33,7 +32,9 @@ async function extractPackageFile(
for (const delimiter of delimiters) {
rubyMatch =
rubyMatch ||
line.match(regEx(`^ruby ${delimiter}([^${delimiter}]+)${delimiter}`));
line.match(
new RegExp(`^ruby ${delimiter}([^${delimiter}]+)${delimiter}`)
);
}
if (rubyMatch) {
res.compatibility = { ruby: rubyMatch[1] };
Expand All @@ -42,9 +43,9 @@ async function extractPackageFile(
let gemDelimiter: string;
for (const delimiter of delimiters) {
const gemMatchRegex = `^gem ${delimiter}([^${delimiter}]+)${delimiter}(,\\s+${delimiter}([^${delimiter}]+)${delimiter}){0,2}`;
if (line.match(regEx(gemMatchRegex))) {
if (line.match(new RegExp(gemMatchRegex))) {
gemDelimiter = delimiter;
gemMatch = gemMatch || line.match(regEx(gemMatchRegex));
gemMatch = gemMatch || line.match(new RegExp(gemMatchRegex));
}
}
if (gemMatch) {
Expand All @@ -55,7 +56,7 @@ async function extractPackageFile(
if (gemMatch[3]) {
dep.currentValue = gemMatch[0]
.substring(`gem ${gemDelimiter}${dep.depName}${gemDelimiter},`.length)
.replace(regEx(gemDelimiter, 'g'), '')
.replace(new RegExp(gemDelimiter, 'g'), '')
.trim();
if (!isValid(dep.currentValue)) {
dep.skipReason = 'invalid-value';
Expand Down Expand Up @@ -99,7 +100,7 @@ async function extractPackageFile(
}
for (const delimiter of delimiters) {
const sourceBlockMatch = line.match(
regEx(`^source\\s+${delimiter}(.*?)${delimiter}\\s+do`)
new RegExp(`^source\\s+${delimiter}(.*?)${delimiter}\\s+do`)
);
if (sourceBlockMatch) {
const repositoryUrl = sourceBlockMatch[1];
Expand Down
6 changes: 5 additions & 1 deletion test/config/__snapshots__/validation.spec.ts.snap
Expand Up @@ -59,6 +59,10 @@ Array [
"depName": "Configuration Error",
"message": "Invalid regExp for npm.fileMatch: \`abc ([a-z]+) ([a-z]+))\`",
},
Object {
"depName": "Configuration Error",
"message": "Unsafe regExp for docker.fileMatch: \`(x+x+)+y\`",
},
]
`;

Expand Down Expand Up @@ -109,7 +113,7 @@ Array [
},
Object {
"depName": "Configuration Error",
"message": "Invalid regExp for packageRules[0].excludePackagePatterns: \`(x+x+)+y\`",
"message": "Unsafe regExp for packageRules[0].excludePackagePatterns: \`(x+x+)+y\`",
},
]
`;
Expand Down
15 changes: 1 addition & 14 deletions test/config/validation.spec.ts
Expand Up @@ -138,7 +138,6 @@ describe('config/validation', () => {
expect(errors).toMatchSnapshot();
expect(errors).toHaveLength(0);
});

it('errors for unsafe fileMatches', async () => {
const config = {
npm: {
Expand All @@ -152,20 +151,8 @@ describe('config/validation', () => {
config
);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(1);
expect(errors).toHaveLength(2);
expect(errors).toMatchSnapshot();
});

it('validates regEx for each fileMatch', async () => {
const config = {
fileMatch: ['js', '***$}{]]['],
};
const { warnings, errors } = await configValidation.validateConfig(
config,
true
);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(1);
});
});
});

0 comments on commit f524557

Please sign in to comment.