Skip to content

Commit 445d36b

Browse files
committed
fixed Regex
1 parent bd52b7f commit 445d36b

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

packages/contentstack-audit/src/messages/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import memoize from 'lodash/memoize';
2-
import { escapeRegExp } from '@contentstack/cli-utilities';
2+
import { escapeRegExp, replaceNonAlphanumericWithEmpty } from '@contentstack/cli-utilities';
33

44
const errors = {};
55

@@ -76,7 +76,8 @@ function $t(msg: string, args: Record<string, string>): string {
7676
if (!msg) return '';
7777

7878
for (const key of Object.keys(args)) {
79-
const escapedKey = escapeRegExp(key);
79+
let escapedKey = escapeRegExp(key);
80+
escapedKey = replaceNonAlphanumericWithEmpty(escapedKey)
8081
const escapedKeyRegex = new RegExp(`{${escapedKey}}`, 'g');
8182
const sanitizedValue = args[key] ? escapeRegExp(args[key]) : '';
8283
msg = msg.replace(escapedKeyRegex, sanitizedValue || escapedKey);

packages/contentstack-export/src/export/modules/content-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default class ContentTypesExport extends BaseClass {
9999
async writeContentTypes(contentTypes: Record<string, unknown>[]) {
100100
function write(contentType: Record<string, unknown>) {
101101
return fsUtil.writeFile(
102-
path.join(sanitizePath(this.contentTypesDirPath), `${contentType.uid === 'schema' ? 'schema|1' : contentType.uid}.json`),
102+
path.join(sanitizePath(this.contentTypesDirPath), sanitizePath(`${contentType.uid === 'schema' ? 'schema|1' : contentType.uid}.json`)),
103103
contentType,
104104
);
105105
}

packages/contentstack-export/src/export/modules/entries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export default class EntriesExport extends BaseClass {
153153
options: { locale: string; contentType: string; versionedEntryPath: string },
154154
): Promise<void> {
155155
const onSuccess = ({ response, apiData: entry }: any) => {
156-
fsUtil.writeFile(path.join(sanitizePath(options.versionedEntryPath), `${entry.uid}.json`), response);
156+
fsUtil.writeFile(path.join(sanitizePath(options.versionedEntryPath), sanitizePath(`${entry.uid}.json`)), response);
157157
log(
158158
this.exportConfig,
159159
`Exported versioned entries of type '${options.contentType}' locale '${options.locale}'`,

packages/contentstack-import/src/utils/asset-helper.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Bluebird from 'bluebird';
22
import * as url from 'url';
33
import * as path from 'path';
4-
import { ContentstackClient, managementSDKClient } from '@contentstack/cli-utilities';
4+
import { ContentstackClient, managementSDKClient, replaceNonAlphanumericWithEmpty, isValidURL } from '@contentstack/cli-utilities';
55
import { ImportConfig } from '../types';
66
const debug = require('debug')('util:requests');
77
let _ = require('lodash');
@@ -255,8 +255,10 @@ export const lookupAssets = function (
255255
// const sanitizedUrl = escapeRegExp(assetUrl).replace(/\.\./g, '\\$&');
256256
// const escapedMappedUrl = escapeRegExp(mappedAssetUrl).replace(/\.\./g, '\\$&');
257257
// entry = entry.replace(new RegExp(sanitizedUrl, 'img'), escapedMappedUrl);
258-
entry = entry.replace(new RegExp(assetUrl, 'img'), mappedAssetUrl);
259-
matchedUrls.push(mappedAssetUrl);
258+
if(isValidURL(mappedAssetUrl)){
259+
entry = entry.replace(new RegExp(assetUrl, 'img'), mappedAssetUrl);
260+
matchedUrls.push(mappedAssetUrl);
261+
}
260262
} else {
261263
unmatchedUrls.push(assetUrl);
262264
}
@@ -265,7 +267,8 @@ export const lookupAssets = function (
265267
assetUids.forEach(function (assetUid: any) {
266268
let uid = mappedAssetUids[assetUid];
267269
if (typeof uid !== 'undefined') {
268-
const escapedAssetUid = assetUid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
270+
let escapedAssetUid = assetUid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
271+
escapedAssetUid = replaceNonAlphanumericWithEmpty(escapedAssetUid)
269272
const regex = new RegExp(`\\b${escapedAssetUid}\\b`, 'img');
270273
entry = entry.replace(regex, uid);
271274
matchedUids.push(assetUid);

packages/contentstack-import/src/utils/entries-helper.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path';
77
import * as _ from 'lodash';
88
import config from '../config';
99
import * as fileHelper from './file-helper';
10-
import { escapeRegExp } from '@contentstack/cli-utilities';
10+
import { escapeRegExp, replaceNonAlphanumericWithEmpty } from '@contentstack/cli-utilities';
1111

1212
import { EntryJsonRTEFieldDataType } from '../types/entries';
1313

@@ -200,7 +200,8 @@ export const lookupEntries = function (
200200
let entry = JSON.stringify(data.entry);
201201
uids.forEach(function (uid: any) {
202202
if (mappedUids.hasOwnProperty(uid)) {
203-
const sanitizedUid = escapeRegExp(uid);
203+
let sanitizedUid = escapeRegExp(uid);
204+
sanitizedUid = replaceNonAlphanumericWithEmpty(sanitizedUid)
204205
const escapedMappedUid = escapeRegExp(mappedUids[uid]);
205206
const uidRegex = new RegExp(`\\b${sanitizedUid}\\b`, 'img');
206207
entry = entry.replace(uidRegex, escapedMappedUid);
@@ -573,7 +574,8 @@ export const restoreJsonRteEntryRefs = (
573574
};
574575

575576
function updateUids(str: string, match: string, uidMapper: Record<string, string>) {
576-
const sanitizedMatch = escapeRegExp(match);
577+
let sanitizedMatch = escapeRegExp(match);
578+
sanitizedMatch = replaceNonAlphanumericWithEmpty(sanitizedMatch)
577579
const regex = new RegExp(`\\b${sanitizedMatch}\\b`, 'g');
578580
return str.replace(regex, (matchedString) => uidMapper[matchedString]);
579581
}

packages/contentstack-migration/examples/05-Update-reference-entry-from-mapper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs');
22
const chalk = require('chalk');
33
const path = require('path');
4+
const { replaceNonAlphanumericWithEmpty } = require('@contentstack/cli-utilities');
45
module.exports = async ({ migration, stackSDKInstance, managementAPIClient, config }) => {
56
const modules = ['entries', 'assets', 'extensions', 'marketplace_apps'];
67

@@ -61,6 +62,7 @@ module.exports = async ({ migration, stackSDKInstance, managementAPIClient, conf
6162
let oldUids = Object.keys(uidMapping);
6263
matches.forEach((m) => {
6364
if (oldUids.includes(m)) {
65+
m = replaceNonAlphanumericWithEmpty(m);
6466
let regex = new RegExp(m, 'g');
6567
stringifiedEntry = stringifiedEntry.replace(regex, uidMapping[m]);
6668
console.log(chalk.green(`Replacing the UID '${m}' with '${uidMapping[m]}'...`));

packages/contentstack-migration/examples/change-master-locale/02-change-master-locale-new-file-structure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports = async ({ migration, config }) => {
7272
let sourceMasterLocaleEntries, targetMasterLocaleEntries;
7373

7474
sourceMasterLocaleEntries = await fs.readFile(
75-
pathValidator(path.resolve(sanitizePath(config.data_dir), `entries/${contentType}/${masterLocale}/index.json`)),
75+
pathValidator(path.resolve(sanitizePath(config.data_dir), sanitizePath(`entries/${contentType}/${masterLocale}/index.json`))),
7676
{ encoding: 'utf8' },
7777
);
7878

packages/contentstack-utilities/src/helpers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ export const sanitizePath = (str: string) => str.replace(/^(\.\.(\/|\\|$))+/, '
6262
export const validateUids = (uid) => /^[a-zA-Z0-9]+$/.test(uid);
6363

6464
// Validate File name
65-
export const validateFileName = (fileName) => /^[a-zA-Z0-9-_\.]+$/.test(fileName);
65+
export const validateFileName = (fileName) => /^[a-zA-Z0-9-_\.]+$/.test(fileName);
66+
67+
// Validate Regex
68+
export const replaceNonAlphanumericWithEmpty = input => input.replace(/[^a-zA-Z0-9]/g, '');
69+
70+
export const isValidURL = url => /^(https?:\/\/)?(www\.)?[a-zA-Z0-9-]+(\.[a-zA-Z]{2,})([\/\w .-]*)*\/?$/.test(url);

0 commit comments

Comments
 (0)