Skip to content

Commit faa5bd3

Browse files
Fixed Semgrep issues (#22)
1 parent 3a69e35 commit faa5bd3

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change log
22

3+
### Version: 4.0.4
4+
#### Date: July-09-2024
5+
Fixed semgrep issues
6+
37
### Version: 4.0.3
48
#### Date: June-11-2024
59
Fixed region issue

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.0.3",
3+
"version": "4.0.4",
44
"type": "commonjs",
55
"main": "./dist/cjs/src/index.js",
66
"types": "./dist/types/src/index.d.ts",

src/lib/query.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { BaseQuery } from './base-query';
33
import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation } from './types';
44
import { params, queryParams } from './internal-types';
55

6+
const safePatterns: RegExp[] = [
7+
/^[a-zA-Z0-9_.-]+$/, // Alphanumeric with underscores, periods, and dashes
8+
];
9+
610
export class Query extends BaseQuery {
711
private _contentTypeUid?: string;
812

@@ -23,19 +27,15 @@ export class Query extends BaseQuery {
2327
const alphanumericRegex = /^[a-zA-Z0-9_.-]+$/;
2428
return alphanumericRegex.test(input);
2529
}
26-
// Validate if input is a valid regex pattern
30+
31+
// Validate if input matches any of the safe, pre-approved patterns
2732
private isValidRegexPattern(input: string): boolean {
28-
try {
29-
RegExp(input)
30-
return true;
31-
}
32-
catch {
33+
if (!this.isValidAlphanumeric(input)) {
3334
return false;
3435
}
35-
36+
return safePatterns.some(pattern => pattern.test(input));
3637
}
3738

38-
// Validate if value is an array of strings, numbers, or booleans
3939
private isValidValue(value: any[]): boolean {
4040
return Array.isArray(value) && value.every(item => typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean');
4141
}

tools/cleanup.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
const fs = require('fs');
22
const path = require('path');
3-
/* eslint-enable */
3+
4+
function validateAndSanitize(input) {
5+
// Allow only alphanumeric characters, dashes, underscores, and dots for file extensions
6+
return input.replace(/[^a-zA-Z0-9-_\.]/g, '');
7+
}
8+
9+
function ensureSafePath(basePath, targetPath) {
10+
const resolvedBase = path.resolve(basePath);
11+
const resolvedTarget = path.resolve(basePath, targetPath);
12+
13+
// console.log('Base Path:', resolvedBase);
14+
// console.log('Target Path:', resolvedTarget);
15+
16+
if (resolvedTarget.indexOf(resolvedBase) !== 0) {
17+
throw new Error(`Unsafe path detected: ${resolvedTarget} is not within ${resolvedBase}`);
18+
}
19+
20+
return resolvedTarget;
21+
}
422

523
const deleteFolderRecursive = (_path) => {
24+
// console.log('Attempting to delete:', _path);
25+
626
if (fs.existsSync(_path)) {
727
fs.readdirSync(_path).forEach((file) => {
8-
const curPath = path.join(_path, file);
28+
const sanitizedFile = validateAndSanitize(file);
29+
const curPath = ensureSafePath(_path, sanitizedFile);
30+
31+
// console.log('Deleting:', curPath);
32+
933
if (fs.lstatSync(curPath).isDirectory()) {
1034
deleteFolderRecursive(curPath);
1135
} else {
1236
fs.unlinkSync(curPath);
1337
}
1438
});
1539
fs.rmdirSync(_path);
40+
} else {
41+
console.log('Path does not exist:', _path);
1642
}
1743
};
1844

45+
const rootDir = path.resolve(__dirname, '..'); // Set the base path to the root of the project
1946
const folder = process.argv.slice(2)[0];
47+
const sanitizedFolder = folder ? validateAndSanitize(folder) : null;
2048

21-
if (folder) {
22-
deleteFolderRecursive(path.join(__dirname, '../dist', folder));
49+
if (sanitizedFolder) {
50+
// console.log('Sanitized folder:', sanitizedFolder);
51+
deleteFolderRecursive(ensureSafePath(rootDir, path.join('dist', sanitizedFolder)));
2352
} else {
24-
deleteFolderRecursive(path.join(__dirname, '../dist/cjs'));
25-
deleteFolderRecursive(path.join(__dirname, '../dist/esm'));
26-
deleteFolderRecursive(path.join(__dirname, '../dist/umd'));
27-
deleteFolderRecursive(path.join(__dirname, '../dist/types'));
28-
}
53+
// console.log('No folder specified, deleting default directories...');
54+
deleteFolderRecursive(ensureSafePath(rootDir, 'dist/cjs'));
55+
deleteFolderRecursive(ensureSafePath(rootDir, 'dist/esm'));
56+
deleteFolderRecursive(ensureSafePath(rootDir, 'dist/umd'));
57+
deleteFolderRecursive(ensureSafePath(rootDir, 'dist/types'));
58+
}

0 commit comments

Comments
 (0)