Skip to content

Commit bd4b15d

Browse files
committed
Sanitize stackId and validate file paths in contentMapper service
- Implemented sanitization of stackId to prevent path traversal vulnerabilities. - Added validation to ensure resolved file paths remain within the allowed directory. - Updated file reading logic to use the sanitized and validated path.
1 parent 84c66b8 commit bd4b15d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

api/src/services/contentMapper.service.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,17 +1410,32 @@ const getExistingTaxonomies = async (req: Request) => {
14101410
);
14111411

14121412
// Path 1: Check api/migration-data (processed taxonomies)
1413+
// Sanitize stackId to prevent path traversal
1414+
const sanitizedStackId = path.basename(stackId);
1415+
14131416
const apiMigrationDataPath = path.join(
14141417
MIGRATION_DATA_CONFIG.DATA,
1415-
stackId,
1418+
sanitizedStackId,
14161419
MIGRATION_DATA_CONFIG.TAXONOMIES_DIR_NAME,
14171420
MIGRATION_DATA_CONFIG.TAXONOMIES_FILE_NAME
14181421
);
14191422

1423+
// Resolve to absolute path and validate it's within allowed directory
1424+
const baseDirectory = path.resolve(MIGRATION_DATA_CONFIG.DATA);
1425+
const resolvedPath = path.resolve(apiMigrationDataPath);
1426+
1427+
// Ensure the resolved path is within the base directory
1428+
if (!resolvedPath.startsWith(baseDirectory)) {
1429+
logger.error(
1430+
`Path traversal attempt detected: ${resolvedPath} is outside ${baseDirectory}`
1431+
);
1432+
throw new BadRequestError('Invalid file path');
1433+
}
1434+
14201435
try {
1421-
if (fs.existsSync(apiMigrationDataPath)) {
1436+
if (fs.existsSync(resolvedPath)) {
14221437
const taxonomiesData = await fs.promises.readFile(
1423-
apiMigrationDataPath,
1438+
resolvedPath,
14241439
'utf8'
14251440
);
14261441
const taxonomiesObject = JSON.parse(taxonomiesData);

0 commit comments

Comments
 (0)