From 3046c9f3faf9260005d34f6e0cd5bb344e91012b Mon Sep 17 00:00:00 2001 From: Simon Malesys <simon.malesys@pasteur.fr> Date: Thu, 13 Feb 2025 16:57:26 +0100 Subject: [PATCH] Test for escapeString --- src/server/utils/escapeString.js | 2 +- src/server/utils/escapeString.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/server/utils/escapeString.test.js diff --git a/src/server/utils/escapeString.js b/src/server/utils/escapeString.js index 2606aec6..2d06104b 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 00000000..f9eb19af --- /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) +}) -- GitLab