From a37622ccb2eadec187cc1dfdeaae8f4f7450825f Mon Sep 17 00:00:00 2001 From: vadiminc Date: Sun, 28 Jan 2024 16:28:48 +0100 Subject: [PATCH] test win --- .../src/commands/actions/validators/file.d.ts | 5 +++ .../src/commands/actions/validators/file.js | 35 ++++++++++--------- .../commands/actions/validators/file.js.map | 2 +- src/commands/actions/validators/file.ts | 34 +++++++++--------- 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/dist/tsc/src/commands/actions/validators/file.d.ts b/dist/tsc/src/commands/actions/validators/file.d.ts index 854fdaca..759a01a0 100644 --- a/dist/tsc/src/commands/actions/validators/file.d.ts +++ b/dist/tsc/src/commands/actions/validators/file.d.ts @@ -1,3 +1,8 @@ export declare const fileExistsValidator: (filePath: string, message?: string) => boolean | string; export declare const jsonFileValidator: (filePath: string, message?: string) => boolean | string; +/** + * Make sure the path contains + * @param path + * @param regex + */ export declare const sanitizePath: (inputPath: string) => string; diff --git a/dist/tsc/src/commands/actions/validators/file.js b/dist/tsc/src/commands/actions/validators/file.js index 8b0112c8..f19248ac 100644 --- a/dist/tsc/src/commands/actions/validators/file.js +++ b/dist/tsc/src/commands/actions/validators/file.js @@ -2,7 +2,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.sanitizePath = exports.jsonFileValidator = exports.fileExistsValidator = void 0; const tslib_1 = require("tslib"); -/* eslint-disable no-useless-escape */ const fs_1 = tslib_1.__importDefault(require("fs")); const path_1 = tslib_1.__importDefault(require("path")); const fileExistsValidator = (filePath, message = '') => { @@ -35,26 +34,30 @@ const jsonFileValidator = (filePath, message = '') => { return true; }; exports.jsonFileValidator = jsonFileValidator; +/** + * Make sure the path contains + * @param path + * @param regex + */ const sanitizePath = (inputPath) => { - // Strip quotes from the beginning or end of the path. + // Strip quotes from the beginning or end. const strippedPath = inputPath.replace(/^["']|["']$/g, ''); - // Normalize the path to handle different OS path formats and resolve '..' and '.' segments. + // Normalize the path to resolve '..' and '.' segments. let sanitizedPath = path_1.default.normalize(strippedPath); - // Optionally, expand the regex to allow additional valid characters as needed. - // The current regex allows alphanumeric, spaces, hyphens, underscores, periods, and path separators. - // Modify this regex based on your specific requirements. - sanitizedPath = sanitizedPath.replace(/[^a-zA-Z0-9 _\-.\\\/]/g, ''); - console.log(`Current platform: ${process.platform}`); - // Handle Windows-specific path formatting (like drive letters). + // Remove any characters that are not typically allowed or are problematic in file paths. + // Here, we're allowing alphanumeric characters, spaces, hyphens, underscores, and periods. + // You can adjust the regex as needed. + sanitizedPath = sanitizedPath.replace(/[^a-zA-Z0-9_\-./\\ ]/g, ''); + // On Windows, paths might start with a drive letter. We can check and ensure it's a valid drive letter. + /* if (process.platform === 'win32') { - console.log('[debug] windows'); - const driveLetterMatch = sanitizedPath.match(/^([a-zA-Z]:)/); - if (driveLetterMatch) { - // Normalize the drive letter to uppercase. - sanitizedPath = driveLetterMatch[1].toUpperCase() + sanitizedPath.substring(driveLetterMatch[1].length); - } - console.log('[debug] windows sanitizedPath:', sanitizedPath); + const match = sanitizedPath.match(/^([a-zA-Z]:)/); + if (match) { + // Ensure the drive letter is uppercase (just a normalization step; not strictly necessary). + sanitizedPath = match[1].toUpperCase() + sanitizedPath.substring(match[1].length); + } } + */ return sanitizedPath; }; exports.sanitizePath = sanitizePath; diff --git a/dist/tsc/src/commands/actions/validators/file.js.map b/dist/tsc/src/commands/actions/validators/file.js.map index b9f6fe0c..fea60422 100644 --- a/dist/tsc/src/commands/actions/validators/file.js.map +++ b/dist/tsc/src/commands/actions/validators/file.js.map @@ -1 +1 @@ -{"version":3,"file":"file.js","sourceRoot":"","sources":["../../../../../../src/commands/actions/validators/file.ts"],"names":[],"mappings":";;;;AAAA,sCAAsC;AACtC,oDAAoB;AACpB,wDAAwB;AAEjB,MAAM,mBAAmB,GAAG,CAAC,QAAgB,EAAE,OAAO,GAAG,EAAE,EAAoB,EAAE;IACtF,QAAQ,GAAG,IAAA,oBAAY,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,C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AAgE;IAChE,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE;QAChC,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;QAC/B,MAAM,gBAAgB,GAAG,aAAa,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;QAC7D,IAAI,gBAAgB,EAAE;YACpB,2CAA2C;YAC3C,aAAa,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,aAAa,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;SACzG;QACD,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAE,aAAa,CAAC,CAAC;KAC9D;IAED,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC;AA1BW,QAAA,YAAY,gBA0BvB"} \ No newline at end of file +{"version":3,"file":"file.js","sourceRoot":"","sources":["../../../../../../src/commands/actions/validators/file.ts"],"names":[],"mappings":";;;;AAAA,oDAAoB;AACpB,wDAAwB;AAEjB,MAAM,mBAAmB,GAAG,CAAC,QAAgB,EAAE,OAAO,GAAG,EAAE,EAAoB,EAAE;IACtF,QAAQ,GAAG,IAAA,oBAAY,EAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;IACjD,IAAI;QACF,YAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACtB,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,KAAU,EAAE;QACnB,gDAAgD;QAChD,OAAO,OAAO,IAAI,KAAK,CAAC,OAAO,IAAI,oCAAoC,CAAC;KACzE;AACH,CAAC,CAAC;AATW,QAAA,mBAAmB,uBAS9B;AAEK,MAAM,iBAAiB,GAAG,CAAC,QAAgB,EAAE,OAAO,GAAG,EAAE,EAAoB,EAAE;IACpF,IAAI,YAAY,CAAC;IACjB,QAAQ,GAAG,IAAA,oBAAY,EAAC,QAAQ,CAAC,CAAC;IAElC,IAAI;QACF,YAAY,GAAG,YAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC;KACjE;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,OAAO,IAAI,yCAAyC,CAAC;KAC7D;IACD,IAAI;QACF,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;KAC1B;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,kBAAkB,QAAQ,wBAAwB,CAAC;KAC3D;IACD,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAfW,QAAA,iBAAiB,qBAe5B;AAEF;;;;GAIG;AACI,MAAM,YAAY,GAAG,CAAC,SAAiB,EAAU,EAAE;IACxD,0CAA0C;IAC1C,MAAM,YAAY,GAAG,SAAS,CAAC,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC,CAAC;IAE3D,uDAAuD;IACvD,IAAI,aAAa,GAAG,cAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC;IAEjD,yFAAyF;IACzF,2FAA2F;IAC3F,sCAAsC;IACtC,aAAa,GAAG,aAAa,CAAC,OAAO,CAAC,uBAAuB,EAAE,EAAE,CAAC,CAAC;IAEnE,wGAAwG;IACxG;;;;;;;;MAQE;IAEF,OAAO,aAAa,CAAC;AACvB,CAAC,CAAC;AAxBW,QAAA,YAAY,gBAwBvB"} \ No newline at end of file diff --git a/src/commands/actions/validators/file.ts b/src/commands/actions/validators/file.ts index fab29e1c..628de5e8 100644 --- a/src/commands/actions/validators/file.ts +++ b/src/commands/actions/validators/file.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-useless-escape */ import fs from 'fs'; import path from 'path'; @@ -30,30 +29,33 @@ export const jsonFileValidator = (filePath: string, message = ''): boolean | str return true; }; +/** + * Make sure the path contains + * @param path + * @param regex + */ export const sanitizePath = (inputPath: string): string => { - // Strip quotes from the beginning or end of the path. + // Strip quotes from the beginning or end. const strippedPath = inputPath.replace(/^["']|["']$/g, ''); - // Normalize the path to handle different OS path formats and resolve '..' and '.' segments. + // Normalize the path to resolve '..' and '.' segments. let sanitizedPath = path.normalize(strippedPath); - // Optionally, expand the regex to allow additional valid characters as needed. - // The current regex allows alphanumeric, spaces, hyphens, underscores, periods, and path separators. - // Modify this regex based on your specific requirements. - sanitizedPath = sanitizedPath.replace(/[^a-zA-Z0-9 _\-.\\\/]/g, ''); + // Remove any characters that are not typically allowed or are problematic in file paths. + // Here, we're allowing alphanumeric characters, spaces, hyphens, underscores, and periods. + // You can adjust the regex as needed. + sanitizedPath = sanitizedPath.replace(/[^a-zA-Z0-9_\-./\\ ]/g, ''); - console.log(`Current platform: ${process.platform}`); - - // Handle Windows-specific path formatting (like drive letters). + // On Windows, paths might start with a drive letter. We can check and ensure it's a valid drive letter. + /* if (process.platform === 'win32') { - console.log('[debug] windows'); - const driveLetterMatch = sanitizedPath.match(/^([a-zA-Z]:)/); - if (driveLetterMatch) { - // Normalize the drive letter to uppercase. - sanitizedPath = driveLetterMatch[1].toUpperCase() + sanitizedPath.substring(driveLetterMatch[1].length); + const match = sanitizedPath.match(/^([a-zA-Z]:)/); + if (match) { + // Ensure the drive letter is uppercase (just a normalization step; not strictly necessary). + sanitizedPath = match[1].toUpperCase() + sanitizedPath.substring(match[1].length); } - console.log('[debug] windows sanitizedPath:', sanitizedPath); } + */ return sanitizedPath; };