Skip to content

Commit

Permalink
fix: modified regex to use RE2 (renovatebot#12025)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
RahulGautamSingh and Rhys Arkins authored Oct 19, 2021
1 parent 28779c6 commit 4b16903
Show file tree
Hide file tree
Showing 36 changed files with 168 additions and 114 deletions.
11 changes: 6 additions & 5 deletions lib/config/decrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import is from '@sindresorhus/is';
import * as openpgp from 'openpgp';
import { logger } from '../logger';
import { maskToken } from '../util/mask';
import { regEx } from '../util/regex';
import { add } from '../util/sanitize';
import { getGlobalConfig } from './global';
import type { RenovateConfig } from './types';
Expand All @@ -18,7 +19,7 @@ export async function tryDecryptPgp(
try {
const pk = await openpgp.readPrivateKey({
// prettier-ignore
armoredKey: privateKey.replace(/\n[ \t]+/g, '\n'), // little massage to help a common problem
armoredKey: privateKey.replace(regEx(/\n[ \t]+/g), '\n'), // little massage to help a common problem
});
const startBlock = '-----BEGIN PGP MESSAGE-----\n\n';
const endBlock = '\n-----END PGP MESSAGE-----';
Expand Down Expand Up @@ -95,7 +96,7 @@ export async function tryDecrypt(
const { o: org, r: repo, v: value } = decryptedObj;
if (is.nonEmptyString(value)) {
if (is.nonEmptyString(org)) {
const orgName = org.replace(/\/$/, ''); // Strip trailing slash
const orgName = org.replace(regEx(/\/$/), ''); // Strip trailing slash
if (is.nonEmptyString(repo)) {
const scopedRepository = `${orgName}/${repo}`;
if (scopedRepository === repository) {
Expand Down Expand Up @@ -171,7 +172,7 @@ export async function decryptConfig(
}
logger.debug(`Decrypted ${eKey}`);
if (eKey === 'npmToken') {
const token = decryptedStr.replace(/\n$/, '');
const token = decryptedStr.replace(regEx(/\n$/), ''); // TODO #12071
add(token);
logger.debug(
{ decryptedToken: maskToken(token) },
Expand All @@ -182,13 +183,13 @@ export async function decryptConfig(
if (decryptedConfig.npmrc.includes('${NPM_TOKEN}')) {
logger.debug('Replacing ${NPM_TOKEN} with decrypted token');
decryptedConfig.npmrc = decryptedConfig.npmrc.replace(
/\${NPM_TOKEN}/g,
regEx(/\${NPM_TOKEN}/g),
token
);
} else {
logger.debug('Appending _authToken= to end of existing npmrc');
decryptedConfig.npmrc = decryptedConfig.npmrc.replace(
/\n?$/,
regEx(/\n?$/), // TODO #12071
`\n_authToken=${token}\n`
);
}
Expand Down
32 changes: 24 additions & 8 deletions lib/config/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import is from '@sindresorhus/is';
import { dequal } from 'dequal';
import { logger } from '../logger';
import { clone } from '../util/clone';
import { regEx } from '../util/regex';
import { getGlobalConfig } from './global';
import { applyMigrations } from './migrations';
import { getOptions } from './options';
Expand Down Expand Up @@ -176,9 +177,15 @@ export function migrateConfig(
migratedConfig.rangeStrategy = 'replace';
}
} else if (is.string(val) && val.includes('{{baseDir}}')) {
migratedConfig[key] = val.replace(/{{baseDir}}/g, '{{packageFileDir}}');
migratedConfig[key] = val.replace(
regEx(/{{baseDir}}/g), // TODO #12071
'{{packageFileDir}}'
);
} else if (is.string(val) && val.includes('{{depNameShort}}')) {
migratedConfig[key] = val.replace(/{{depNameShort}}/g, '{{depName}}');
migratedConfig[key] = val.replace(
regEx(/{{depNameShort}}/g), // TODO #12071
'{{depName}}'
);
} else if (key === 'gitFs') {
delete migratedConfig.gitFs;
} else if (key === 'rebaseStalePrs') {
Expand Down Expand Up @@ -387,21 +394,25 @@ export function migrateConfig(
) {
const parsedSchedule = later.parse.text(
// We need to massage short hours first before we can parse it
schedules[i].replace(/( \d?\d)((a|p)m)/g, '$1:00$2')
schedules[i].replace(regEx(/( \d?\d)((a|p)m)/g), '$1:00$2') // TODO #12071
).schedules[0];
// Only migrate if the after time is greater than before, e.g. "after 10pm and before 5am"
if (parsedSchedule?.t_a?.[0] > parsedSchedule?.t_b?.[0]) {
const toSplit = schedules[i];
schedules[i] = toSplit
.replace(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/,
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
), // TODO #12071
'$1$2 $3 $7'
)
.trim();
schedules.push(
toSplit
.replace(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/,
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
), // TODO #12071
'$1$4 $5 $7'
)
.trim()
Expand All @@ -426,9 +437,14 @@ export function migrateConfig(
schedules[i] = schedules[i].replace(' every day', '');
}
if (
/every (mon|tues|wednes|thurs|fri|satur|sun)day$/.test(schedules[i])
regEx(/every (mon|tues|wednes|thurs|fri|satur|sun)day$/).test(
schedules[i]
) // TODO #12071
) {
schedules[i] = schedules[i].replace(/every ([a-z]*day)$/, 'on $1');
schedules[i] = schedules[i].replace(
regEx(/every ([a-z]*day)$/), // TODO #12071
'on $1'
);
}
if (schedules[i].endsWith('days')) {
schedules[i] = schedules[i].replace('days', 'day');
Expand Down Expand Up @@ -545,7 +561,7 @@ export function migrateConfig(
if (is.string(migratedConfig[key])) {
for (const [from, to] of Object.entries(migratedTemplates)) {
migratedConfig[key] = (migratedConfig[key] as string).replace(
new RegExp(from, 'g'),
regEx(from, 'g'), // TODO #12071
to
);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/config/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 = regEx(`{{${arg}}}`, 'g'); // TODO #12071
returnStr = returnStr.replace(re, argVal);
}
return returnStr;
Expand Down Expand Up @@ -90,7 +90,7 @@ export function parsePreset(input: string): ParsedPreset {
) {
presetSource = 'local';
}
str = str.replace(/^npm>/, '');
str = str.replace(regEx(/^npm>/), '');
presetSource = presetSource || 'npm';
if (str.includes('(')) {
params = str
Expand Down Expand Up @@ -126,7 +126,7 @@ export function parsePreset(input: string): ParsedPreset {
presetName = str.slice(1);
} else if (str.startsWith('@')) {
// scoped namespace
[, packageName] = /(@.*?)(:|$)/.exec(str);
[, packageName] = regEx(/(@.*?)(:|$)/).exec(str);
str = str.slice(packageName.length);
if (!packageName.includes('/')) {
packageName += '/renovate-config';
Expand All @@ -138,7 +138,7 @@ export function parsePreset(input: string): ParsedPreset {
}
} else if (str.includes('//')) {
// non-scoped namespace with a subdirectory preset
const re = /^([\w\-./]+?)\/\/(?:([\w\-./]+)\/)?([\w\-.]+)$/;
const re = regEx(/^([\w\-./]+?)\/\/(?:([\w\-./]+)\/)?([\w\-.]+)$/);

// Validation
if (str.includes(':')) {
Expand All @@ -150,7 +150,7 @@ export function parsePreset(input: string): ParsedPreset {
[, packageName, presetPath, presetName] = re.exec(str);
} else {
// non-scoped namespace
[, packageName] = /(.*?)(:|$)/.exec(str);
[, packageName] = regEx(/(.*?)(:|$)/).exec(str);
presetName = str.slice(packageName.length + 1);
if (presetSource === 'npm' && !packageName.startsWith('renovate-config-')) {
packageName = `renovate-config-${packageName}`;
Expand Down
10 changes: 5 additions & 5 deletions lib/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const ignoredNodes = [

function isManagerPath(parentPath: string): boolean {
return (
/^regexManagers\[[0-9]+]$/.test(parentPath) ||
regEx(/^regexManagers\[[0-9]+]$/).test(parentPath) ||
managerList.includes(parentPath)
);
}
Expand Down Expand Up @@ -85,8 +85,8 @@ function getDeprecationMessage(option: string): string {
export function getParentName(parentPath: string): string {
return parentPath
? parentPath
.replace(/\.?encrypted$/, '')
.replace(/\[\d+\]$/, '')
.replace(regEx(/\.?encrypted$/), '')
.replace(regEx(/\[\d+\]$/), '')
.split('.')
.pop()
: '.';
Expand Down Expand Up @@ -253,7 +253,7 @@ export async function validateConfig(
}
}
if (key === 'extends') {
const tzRe = /^:timezone\((.+)\)$/;
const tzRe = regEx(/^:timezone\((.+)\)$/); // TODO #12071
for (const subval of val) {
if (is.string(subval)) {
if (
Expand Down Expand Up @@ -480,7 +480,7 @@ export async function validateConfig(
}
if (
(selectors.includes(key) || key === 'matchCurrentVersion') &&
!/p.*Rules\[\d+\]$/.test(parentPath) && // Inside a packageRule
!regEx(/p.*Rules\[\d+\]$/).test(parentPath) && // Inside a packageRule // TODO #12071
(parentPath || !isPreset) // top level in a preset
) {
errors.push({
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/artifactory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { logger } from '../../logger';
import { cache } from '../../util/cache/package/decorator';
import { parse } from '../../util/html';
import { HttpError } from '../../util/http/types';
import { regEx } from '../../util/regex';
import { joinUrlParts } from '../../util/url';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
Expand Down Expand Up @@ -108,6 +109,6 @@ export class ArtifactoryDatasource extends Datasource {
}

private static parseReleaseTimestamp(rawText: string): string {
return rawText.trim().replace(/ ?-$/, '');
return rawText.trim().replace(regEx(/ ?-$/), ''); // TODO #12071
}
}
3 changes: 1 addition & 2 deletions lib/datasource/crate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export class CrateDatasource extends Datasource {
this.handleGenericErrors(err);
}
}

throw new Error(`unsupported crate registry flavor: ${info.flavor}`);
}

Expand Down Expand Up @@ -144,7 +143,7 @@ export class CrateDatasource extends Datasource {
* clone the repository.
*/
private static cacheDirFromUrl(url: URL): string {
const proto = url.protocol.replace(/:$/, '');
const proto = url.protocol.replace(/:$/, ''); // TODO #12070
const host = url.hostname;
const hash = hasha(url.pathname, {
algorithm: 'sha256',
Expand Down
9 changes: 5 additions & 4 deletions lib/datasource/docker/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as packageCache from '../../util/cache/package';
import * as hostRules from '../../util/host-rules';
import { Http, HttpOptions, HttpResponse } from '../../util/http';
import type { OutgoingHttpHeaders } from '../../util/http/types';
import { regEx } from '../../util/regex';
import {
ensureTrailingSlash,
parseUrl,
Expand All @@ -20,7 +21,7 @@ import { MediaType, RegistryRepository } from './types';
export const id = 'docker';
export const http = new Http(id);

export const ecrRegex = /\d+\.dkr\.ecr\.([-a-z0-9]+)\.amazonaws\.com/;
export const ecrRegex = regEx(/\d+\.dkr\.ecr\.([-a-z0-9]+)\.amazonaws\.com/);
const DOCKER_HUB = 'https://index.docker.io';
export const defaultRegistryUrls = [DOCKER_HUB];

Expand Down Expand Up @@ -210,11 +211,11 @@ export function getRegistryRepository(
): RegistryRepository {
if (registryUrl !== DOCKER_HUB) {
const registryEndingWithSlash = ensureTrailingSlash(
registryUrl.replace(/^https?:\/\//, '')
registryUrl.replace(regEx(/^https?:\/\//), '')
);
if (lookupName.startsWith(registryEndingWithSlash)) {
let registryHost = trimTrailingSlash(registryUrl);
if (!/^https?:\/\//.test(registryHost)) {
if (!regEx(/^https?:\/\//).test(registryHost)) {
registryHost = `https://${registryHost}`;
}
let dockerRepository = lookupName.replace(registryEndingWithSlash, '');
Expand Down Expand Up @@ -244,7 +245,7 @@ export function getRegistryRepository(
if (registryHost === 'docker.io') {
registryHost = 'index.docker.io';
}
if (!/^https?:\/\//.exec(registryHost)) {
if (!regEx(/^https?:\/\//).exec(registryHost)) {
registryHost = `https://${registryHost}`;
}
const opts = hostRules.find({ hostType: id, url: registryHost });
Expand Down
3 changes: 2 additions & 1 deletion lib/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as packageCache from '../../util/cache/package';
import { hasKey } from '../../util/object';
import { regEx } from '../../util/regex';
import { ensurePathPrefix } from '../../util/url';
import {
api as dockerVersioning,
Expand Down Expand Up @@ -103,7 +104,7 @@ async function getTags(
return cachedResult;
}

const isQuay = /^https:\/\/quay\.io(?::[1-9][0-9]{0,4})?$/i.test(
const isQuay = regEx(/^https:\/\/quay\.io(?::[1-9][0-9]{0,4})?$/i).test(
registryHost
);
let tags: string[] | null;
Expand Down
9 changes: 6 additions & 3 deletions lib/datasource/git-refs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import simpleGit from 'simple-git';
import * as packageCache from '../../util/cache/package';
import { simpleGitConfig } from '../../util/git/config';
import { getRemoteUrlWithToken } from '../../util/git/url';
import { regEx } from '../../util/regex';
import * as semver from '../../versioning/semver';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
import type { RawRefs } from './types';
Expand Down Expand Up @@ -38,8 +39,8 @@ export async function getRawRefs(
return null;
}

const refMatch = /(?<hash>.*?)\s+refs\/(?<type>.*?)\/(?<value>.*)/;
const headMatch = /(?<hash>.*?)\s+HEAD/;
const refMatch = regEx(/(?<hash>.*?)\s+refs\/(?<type>.*?)\/(?<value>.*)/);
const headMatch = regEx(/(?<hash>.*?)\s+HEAD/);

const refs = lsRemote
.trim()
Expand Down Expand Up @@ -83,7 +84,9 @@ export async function getReleases({

const uniqueRefs = [...new Set(refs)];

const sourceUrl = lookupName.replace(/\.git$/, '').replace(/\/$/, '');
const sourceUrl = lookupName
.replace(regEx(/\.git$/), '')
.replace(regEx(/\/$/), '');

const result: ReleaseResult = {
sourceUrl,
Expand Down
5 changes: 4 additions & 1 deletion lib/datasource/git-tags/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { regEx } from '../../util/regex';
import * as semver from '../../versioning/semver';
import * as gitRefs from '../git-refs';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
Expand All @@ -22,7 +23,9 @@ export async function getReleases({
newDigest: ref.hash,
}));

const sourceUrl = lookupName.replace(/\.git$/, '').replace(/\/$/, '');
const sourceUrl = lookupName
.replace(regEx(/\.git$/), '')
.replace(regEx(/\/$/), '');

const result: ReleaseResult = {
sourceUrl,
Expand Down
9 changes: 5 additions & 4 deletions lib/datasource/github-releases/digest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hasha from 'hasha';
import * as packageCache from '../../util/cache/package';
import { regEx } from '../../util/regex';
import { cacheNamespace, http } from './common';
import type { DigestAsset, GithubRelease, GithubReleaseAsset } from './types';

Expand All @@ -13,7 +14,7 @@ async function findDigestFile(
for (const asset of smallAssets) {
const res = await http.get(asset.browser_download_url);
for (const line of res.body.split('\n')) {
const [lineDigest, lineFn] = line.split(/\s+/, 2);
const [lineDigest, lineFn] = line.split(regEx(/\s+/), 2); // TODO #12071
if (lineDigest === digest) {
return {
assetName: asset.name,
Expand Down Expand Up @@ -114,8 +115,8 @@ export async function mapDigestAssetToRelease(
digestAsset: DigestAsset,
release: GithubRelease
): Promise<string | null> {
const current = digestAsset.currentVersion.replace(/^v/, '');
const next = release.tag_name.replace(/^v/, '');
const current = digestAsset.currentVersion.replace(regEx(/^v/), '');
const next = release.tag_name.replace(regEx(/^v/), '');
const releaseChecksumAssetName = digestAsset.assetName.replace(current, next);
const releaseAsset = release.assets.find(
(a: GithubReleaseAsset) => a.name === releaseChecksumAssetName
Expand All @@ -127,7 +128,7 @@ export async function mapDigestAssetToRelease(
const releaseFilename = digestAsset.digestedFileName.replace(current, next);
const res = await http.get(releaseAsset.browser_download_url);
for (const line of res.body.split('\n')) {
const [lineDigest, lineFn] = line.split(/\s+/, 2);
const [lineDigest, lineFn] = line.split(regEx(/\s+/), 2);
if (lineFn === releaseFilename) {
return lineDigest;
}
Expand Down
Loading

0 comments on commit 4b16903

Please sign in to comment.