diff --git a/src/server/utils/escapeString.js b/src/server/utils/escapeString.js index 2606aec6d02669af6ed19a6ce3ec5465be10050c..2d06104b1da8fbdfed68b48c3927849737ada7dc 100644 --- a/src/server/utils/escapeString.js +++ b/src/server/utils/escapeString.js @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. /** - * Escape all special charcaters in a string to make it safe + * Escape all special characters in a string to make it safe * to use in a regex. * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping * @param {string} string - The string containing characters to escape diff --git a/src/server/utils/escapeString.test.js b/src/server/utils/escapeString.test.js new file mode 100644 index 0000000000000000000000000000000000000000..f9eb19af3e025dbc6ea198d3ac5f5b893fef6aa0 --- /dev/null +++ b/src/server/utils/escapeString.test.js @@ -0,0 +1,25 @@ +// ABSD +// Copyright (C) 2023 Institut Pasteur +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +import { expect, test } from "vitest" +import escapeString from "./escapeString" + +test('Should escape all dangerous characters in a regex', () => { + const dangerousString = "abc.de*fgh+ijk?lmn^op$qr{st}u(vw)xyzABCDE|FG\HIJK[LM]NOPQRSTUVWXYZ123456789-_%&#@" + const safeString = "abc\\.de\\*fgh\\+ijk\\?lmn\\^op\\$qr\\{st\\}u\\(vw\\)xyzABCDE\\|FGHIJK\\[LM\\]NOPQRSTUVWXYZ123456789-_%&#@" + + expect(escapeString(dangerousString)).toBe(safeString) +})