Skip to content

Commit

Permalink
Merge pull request #1328 from contentstack/fix/CS-43472
Browse files Browse the repository at this point in the history
fix: semgrep reported issues
  • Loading branch information
aman19K authored Mar 5, 2024
2 parents 587268f + 83d6625 commit 3b3ee39
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import memoize from 'lodash/memoize';
import { escapeRegExp } from '@contentstack/cli-utilities';

const errors = {};

Expand Down Expand Up @@ -65,8 +66,8 @@ function $t(msg: string, args: Record<string, string>): string {
if (!msg) return '';

for (const key of Object.keys(args)) {
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
msg = msg.replace(new RegExp(`{${escapedKey}}`, 'g'), args[key] || escapedKey);
const escapedKey = escapeRegExp(key);
msg = msg.replace(new RegExp(`{${escapedKey}}`, 'g'), escapeRegExp(args[key]) || escapedKey);
}

return msg;
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-bulk-publish/src/consumer/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async function publishEntry(data, _config, queue) {
.publish({
publishDetails: { environments: entryObj.environments, locales: lang },
locale: entryObj.locale || 'en-us',
version: entryObj.version
})
.then((publishEntryResponse) => {
if (!publishEntryResponse.error_message) {
Expand Down Expand Up @@ -246,9 +245,10 @@ async function performBulkPublish(data, _config, queue) {
.publish(payload)
.then((bulkPublishEntriesResponse) => {
if (!bulkPublishEntriesResponse.error_message) {
const sanitizedData = JSON.stringify(removePublishDetails(bulkPublishObj.entries));
console.log(
chalk.green(
`Bulk entries sent for publish ${JSON.stringify(removePublishDetails(bulkPublishObj.entries))}`,
`Bulk entries sent for publish ${sanitizedData}`,
),
(bulkPublishEntriesResponse.job_id) ? chalk.yellow(`job_id: ${bulkPublishEntriesResponse.job_id}`) : ''
);
Expand Down
7 changes: 4 additions & 3 deletions packages/contentstack-import/src/utils/asset-helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Bluebird from 'bluebird';
import * as url from 'url';
import * as path from 'path';
import { ContentstackClient, managementSDKClient } from '@contentstack/cli-utilities';
import { ContentstackClient, managementSDKClient, escapeRegExp } from '@contentstack/cli-utilities';
import { ImportConfig } from '../types';
const debug = require('debug')('util:requests');
let _ = require('lodash');
Expand Down Expand Up @@ -249,8 +249,9 @@ export const lookupAssets = function (
assetUrls.forEach(function (assetUrl: any) {
let mappedAssetUrl = mappedAssetUrls[assetUrl];
if (typeof mappedAssetUrl !== 'undefined') {
const escapedAssetUrl = assetUrl.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
entry = entry.replace(new RegExp(escapedAssetUrl, 'img'), mappedAssetUrl);
const sanitizedUrl = escapeRegExp(assetUrl);
const escapedMappedUrl = escapeRegExp(mappedAssetUrl);
entry = entry.replace(new RegExp(sanitizedUrl, 'img'), escapedMappedUrl);
matchedUrls.push(mappedAssetUrl);
} else {
unmatchedUrls.push(assetUrl);
Expand Down
6 changes: 5 additions & 1 deletion packages/contentstack-import/src/utils/common-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ export const field_rules_update = (importConfig: ImportConfig, ctPath: string) =
management_token: importConfig.management_token,
});
let ctObj = stackAPIClient.contentType(schema.uid);
Object.assign(ctObj, _.cloneDeep(schema));
//NOTE:- Remove this code Object.assign(ctObj, _.cloneDeep(schema)); -> security vulnerabilities due to mass assignment
const schemaKeys = Object.keys(schema);
for (const key of schemaKeys) {
ctObj[key] = _.cloneDeep(schema[key]);
}
ctObj
.update()
.then(() => {
Expand Down
6 changes: 4 additions & 2 deletions packages/contentstack-import/src/utils/entries-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as path from 'path';
import * as _ from 'lodash';
import config from '../config';
import * as fileHelper from './file-helper';
import { escapeRegExp } from '@contentstack/cli-utilities';

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

Expand Down Expand Up @@ -199,8 +200,9 @@ export const lookupEntries = function (
let entry = JSON.stringify(data.entry);
uids.forEach(function (uid: any) {
if (mappedUids.hasOwnProperty(uid)) {
const escapedUid = uid.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
entry = entry.replace(new RegExp(escapedUid, 'img'), mappedUids[uid]);
const sanitizedUid = escapeRegExp(uid);
const escapedMappedUid = escapeRegExp(mappedUids[uid]);
entry = entry.replace(new RegExp(sanitizedUid, 'img'), escapedMappedUid);
mapped.push(uid);
} else {
unmapped.push(uid);
Expand Down
3 changes: 3 additions & 0 deletions packages/contentstack-utilities/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ export const createDeveloperHubUrl = (developerHubBaseUrl: string): string => {
: developerHubBaseUrl;
return developerHubBaseUrl.startsWith('http') ? developerHubBaseUrl : `https://${developerHubBaseUrl}`;
};

// To escape special characters in a string
export const escapeRegExp = (str: string) => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

0 comments on commit 3b3ee39

Please sign in to comment.