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

fix: modified regex to use RE2 #12025

Merged
merged 17 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
});
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$/), '');
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?$/),
`\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),
'{{packageFileDir}}'
);
} else if (is.string(val) && val.includes('{{depNameShort}}')) {
migratedConfig[key] = val.replace(/{{depNameShort}}/g, '{{depName}}');
migratedConfig[key] = val.replace(
regEx(/{{depNameShort}}/g),
'{{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')
).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) (.*?)( |$)(.*)/
),
'$1$2 $3 $7'
)
.trim();
schedules.push(
toSplit
.replace(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/,
regEx(
/^(.*?)(after|before) (.*?) and (after|before) (.*?)( |$)(.*)/
),
'$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(
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
schedules[i]
)
) {
schedules[i] = schedules[i].replace(/every ([a-z]*day)$/, 'on $1');
schedules[i] = schedules[i].replace(
regEx(/every ([a-z]*day)$/),
'on $1'
);
}
if (schedules[i].endsWith('days')) {
schedules[i] = schedules[i].replace('days', 'day');
Expand Down Expand Up @@ -540,7 +556,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'),
to
);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/config/presets/index.ts
Original file line number Diff line number Diff line change
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\((.+)\)$/);
for (const subval of val) {
if (is.string(subval)) {
if (
Expand Down Expand Up @@ -479,7 +479,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
(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(/ ?-$/), '');
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 2 additions & 1 deletion lib/datasource/crate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as memCache from '../../util/cache/memory';
import * as packageCache from '../../util/cache/package';
import { privateCacheDir, readFile } from '../../util/fs';
import { Http } from '../../util/http';
import { regEx } from '../../util/regex';
import * as cargoVersioning from '../../versioning/cargo';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import { CrateRecord, RegistryFlavor, RegistryInfo } from './types';
Expand Down Expand Up @@ -92,7 +93,7 @@ function getDependencyUrl(info: RegistryInfo, lookupName: string): string {
* clone the repository.
*/
function cacheDirFromUrl(url: URL): string {
const proto = url.protocol.replace(/:$/, '');
const proto = url.protocol.replace(regEx(/:$/), '');
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
@@ -1,6 +1,7 @@
import simpleGit from 'simple-git';
import * as packageCache from '../../util/cache/package';
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 @@ -37,8 +38,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 @@ -82,7 +83,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);
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
3 changes: 2 additions & 1 deletion lib/datasource/gitlab-tags/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as packageCache from '../../util/cache/package';
import { GitlabHttp } from '../../util/http/gitlab';
import { regEx } from '../../util/regex';
import { joinUrlParts } from '../../util/url';
import type { GetReleasesConfig, ReleaseResult } from '../types';
import type { GitlabTag } from './types';
Expand All @@ -22,7 +23,7 @@ export async function getReleases({
registryUrl,
lookupName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const depHost = registryUrl.replace(/\/api\/v4$/, '');
const depHost = registryUrl.replace(regEx(/\/api\/v4$/), '');

const cachedResult = await packageCache.get<ReleaseResult>(
cacheNamespace,
Expand Down
Loading