Skip to content

Commit

Permalink
Merge pull request #78 from Hexastack/77-issue-regular-expression-inj…
Browse files Browse the repository at this point in the history
…ection

fix: escape regular expressions
  • Loading branch information
marrouchi authored Sep 24, 2024
2 parents 3110c6a + 73d9020 commit e06dfd6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/migrations/config/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import fs from 'fs';
import path from 'path';

import escapeRegExp from 'lodash/escapeRegExp';

// Get the argument passed (e.g., "all-users-fr")
const arg: string | undefined = process.argv[2];

Expand All @@ -25,7 +27,7 @@ const templatePath: string = path.join(__dirname, '../config/template.ts');

// Check if a migration with the same name (excluding timestamp) already exists
const migrationExists: boolean = fs.readdirSync(migrationsDir).some((file) => {
const regex = new RegExp(`^[0-9]+-${arg}\.ts$`);
const regex = new RegExp(`^[0-9]+-${escapeRegExp(arg)}\\.ts$`);
return regex.test(file);
});

Expand Down
7 changes: 7 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"@types/express": "^4.17.17",
"@types/express-session": "^1.17.10",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.17.9",
"@types/module-alias": "^2.0.4",
"@types/multer": "^1.4.11",
"@types/node": "^20.3.1",
Expand Down
6 changes: 3 additions & 3 deletions api/src/utils/pipes/search-filter.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ArgumentMetadata,
Logger,
} from '@nestjs/common';
import escapeRegExp from 'lodash/escapeRegExp';
import { TFilterQuery, Types } from 'mongoose';

import {
Expand All @@ -36,9 +37,8 @@ export class SearchFilterPipe<T>
}

private getRegexValue(val: string) {
const quote = (str: string) =>
str.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
return new RegExp(quote(val), 'i');
const escapedRegExp = escapeRegExp(val);
return new RegExp(escapedRegExp, 'i');
}

private isAllowedField(field: string) {
Expand Down

0 comments on commit e06dfd6

Please sign in to comment.