From 42f33a146367b2e39d6f7c3bf9a9a466084b371d Mon Sep 17 00:00:00 2001 From: int0h Date: Mon, 16 Sep 2019 20:15:56 +0200 Subject: [PATCH] used code from PR: https://github.com/mklabs/tabtab/pull/49 by sorleone --- lib/installer.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/installer.js b/lib/installer.js index 8401aba..3a32b55 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -45,6 +45,16 @@ const scriptFromShell = (shell = systemShell()) => { return path.join(__dirname, 'scripts/bash.sh'); }; +/** + * Helper to escape regular expression chars + * + * @param {String} str - String to escape the chars from + * @returns The escaped text + */ +const escapeRegExp = (str) => { + return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&'); +} + /** * Helper to return the expected location for SHELL config file, based on the * provided shell value. @@ -122,7 +132,7 @@ const checkFilenameForLine = async (filename, line) => { } } - return !!filecontent.match(`${line}`); + return !!filecontent.match(escapeRegExp(`${line}`)); }; /** @@ -329,7 +339,7 @@ const removeLinesFromFilename = async (filename, name) => { ? `# tabtab source for packages` : `# tabtab source for ${name} package`; - const hasLine = !!filecontent.match(`${sourceLine}`); + const hasLine = !!filecontent.match(escapeRegExp(`${sourceLine}`)); if (!hasLine) { return debug('File %s does not include the line: %s', filename, sourceLine); } @@ -338,7 +348,7 @@ const removeLinesFromFilename = async (filename, name) => { const buffer = lines // Build up the new buffer, removing the 3 lines following the sourceline .map((line, index) => { - const match = line.match(sourceLine); + const match = line.match(escapeRegExp(sourceLine)); if (match) { lineIndex = index; } else if (lineIndex + 3 <= index) {