Skip to content

fix: Sanitized the path to avoid excluding the files #1725

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

Draft
wants to merge 8 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
583 changes: 355 additions & 228 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import path from 'path';
import { Command } from '@contentstack/cli-command';
import {
cliux,
messageHandler,
printFlagDeprecation,
managementSDKClient,
flags,
ContentstackClient,
FlagInput,
pathValidator,
sanitizePath,
} from '@contentstack/cli-utilities';
import { ModuleExporter } from '../../../export';
import { setupExportConfig, log, formatError, writeExportMetaFile } from '../../../utils';
Expand Down Expand Up @@ -108,7 +108,7 @@ export default class ExportCommand extends Command {
exportConfig.region = this.region;
exportConfig.developerHubBaseUrl = this.developerHubUrl;
if (this.personalizeUrl) exportConfig.modules.personalize.baseURL[exportConfig.region.name] = this.personalizeUrl;
exportDir = exportConfig.cliLogsPath || exportConfig.data || exportConfig.exportDir;
exportDir = path.resolve(sanitizePath(exportConfig.cliLogsPath || exportConfig.data || exportConfig.exportDir));
const managementAPIClient: ContentstackClient = await managementSDKClient(exportConfig);
const moduleExporter = new ModuleExporter(managementAPIClient, exportConfig);
await moduleExporter.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import merge from 'merge';
import * as path from 'path';
import { configHandler, isAuthenticated, FlagInput, cliux } from '@contentstack/cli-utilities';
import { configHandler, isAuthenticated, FlagInput, cliux, sanitizePath } from '@contentstack/cli-utilities';
import defaultConfig from '../config';
import { readFile } from './file-helper';
import { askExportDir, askAPIKey } from './interactive';
Expand All @@ -15,8 +15,7 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
const externalConfig = await readFile(exportCmdFlags['config']);
config = merge.recursive(config, externalConfig);
}
config.exportDir = exportCmdFlags['data'] || exportCmdFlags['data-dir'] || config.data || (await askExportDir());

config.exportDir = sanitizePath(exportCmdFlags['data'] || exportCmdFlags['data-dir'] || config.data || (await askExportDir()));
const pattern = /[*$%#<>{}!&?]/g;
if (pattern.test(config.exportDir)) {
cliux.print(`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`, {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-export/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function init(_logPath: string) {
}

export const log = async (config: ExportConfig, message: any, type: string) => {
const logsPath = config.cliLogsPath || config.data;
const logsPath = path.resolve(config.cliLogsPath || config.data);
// ignoring the type argument, as we are not using it to create a logfile anymore
if (type !== 'error') {
// removed type argument from init method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
// config = merge.recursive(config, externalConfig);
// }

config.contentDir = importCmdFlags['data'] || importCmdFlags['data-dir'] || config.data || (await askContentDir());
config.contentDir = sanitizePath(importCmdFlags['data'] || importCmdFlags['data-dir'] || config.data || (await askContentDir()));
const pattern = /[*$%#<>{}!&?]/g;
if (pattern.test(config.contentDir)) {
cliux.print(`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`, {
color: 'yellow',
});
config.contentDir = await askContentDir();
config.contentDir = sanitizePath(await askContentDir());
}
config.contentDir = config.contentDir.replace(/['"]/g, '');
config.contentDir = path.resolve(config.contentDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
config = merge.recursive(config, externalConfig);
}

config.contentDir = importCmdFlags['data'] || importCmdFlags['data-dir'] || config.data || (await askContentDir());
config.contentDir = sanitizePath(importCmdFlags['data'] || importCmdFlags['data-dir'] || config.data || (await askContentDir()));
const pattern = /[*$%#<>{}!&?]/g;
if (pattern.test(config.contentDir)) {
cliux.print(`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`, {
color: 'yellow',
});
config.contentDir = await askContentDir();
config.contentDir = sanitizePath(await askContentDir());
}
config.contentDir = config.contentDir.replace(/['"]/g, '');
config.contentDir = path.resolve(config.contentDir);
Expand Down
3 changes: 1 addition & 2 deletions packages/contentstack-import/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import * as winston from 'winston';
import * as path from 'path';
import mkdirp from 'mkdirp';
import { ImportConfig } from '../types';
import { sanitizePath, redactObject } from '@contentstack/cli-utilities';

Expand Down Expand Up @@ -140,7 +139,7 @@ function init(_logPath: string) {
}

export const log = async (config: ImportConfig, message: any, type: string) => {
config.cliLogsPath = config.cliLogsPath || config.data || path.join(__dirname, 'logs');
config.cliLogsPath = sanitizePath(config.cliLogsPath || config.data || path.join(__dirname, 'logs'));
// ignoring the type argument, as we are not using it to create a logfile anymore
if (type !== 'error') {
// removed type argument from init method
Expand Down
6 changes: 3 additions & 3 deletions packages/contentstack-migrate-rte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"dependencies": {
"@contentstack/cli-command": "~1.3.3",
"@contentstack/cli-utilities": "~1.9.0",
"@contentstack/json-rte-serializer": "~2.0.11",
"@contentstack/json-rte-serializer": "~2.0.12",
"collapse-whitespace": "^1.1.7",
"chalk": "^4.1.2",
"jsdom": "^20.0.3",
"jsonschema": "^1.4.1",
"jsonschema": "^1.5.0",
"lodash": "^4.17.21",
"nock": "^13.5.5",
"nock": "^13.5.6",
"omit-deep-lodash": "^1.1.7",
"sinon": "^19.0.2"
},
Expand Down
15 changes: 14 additions & 1 deletion packages/contentstack-utilities/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { checkSync } from 'recheck';
import traverse from 'traverse';
import authHandler from './auth-handler';
import { HttpClient, cliux, configHandler } from '.';
import { normalize } from 'path';
export const isAuthenticated = () => authHandler.isAuthenticated();
export const doesBranchExist = async (stack, branchName) => {
return stack
Expand Down Expand Up @@ -53,8 +54,20 @@ export const validatePath = (input: string) => {
// To escape special characters in a string
export const escapeRegExp = (str: string) => str?.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

const sanitizePathRegex = (str: string) =>
str
?.replace(/\\/g, '/') // Convert Windows-style backslashes to forward slashes
.replace(/\/+/g, '/') // Collapse multiple slashes into one
.replace(/(?:^|\/)\.+(\/|$)/g, '/'); // Remove any `.` or `..` path segments

// To remove the relative path
export const sanitizePath = (str: string) => str?.replace(/^(\.\.(\/|\\|$))+/, '');
export const sanitizePath = (str: string) => {
const decodedStr = decodeURIComponent(str);
return decodedStr
?.replace(/^([\/\\]){2,}/, "./") // Normalize leading slashes/backslashes to ''
.replace(/[\/\\]+/g, "/") // Replace multiple slashes/backslashes with a single '/'
.replace(/(\.\.(\/|\\|$))+/g, ""); // Remove directory traversal (../ or ..\)
};

// To validate the UIDs of assets
export const validateUids = (uid) => /^[a-zA-Z0-9]+$/.test(uid);
Expand Down
Loading