From 9616d996408d079edddfdcb8de29bf38da5e0cbe Mon Sep 17 00:00:00 2001 From: advplyr Date: Mon, 30 Oct 2023 16:35:41 -0500 Subject: [PATCH] Fix:Crash when matching with author names ending in ??? by escaping regex strings #2265 --- server/finders/BookFinder.js | 4 ++-- server/utils/index.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/server/finders/BookFinder.js b/server/finders/BookFinder.js index a0b64f55..75e5a5f1 100644 --- a/server/finders/BookFinder.js +++ b/server/finders/BookFinder.js @@ -6,7 +6,7 @@ const Audnexus = require('../providers/Audnexus') const FantLab = require('../providers/FantLab') const AudiobookCovers = require('../providers/AudiobookCovers') const Logger = require('../Logger') -const { levenshteinDistance } = require('../utils/index') +const { levenshteinDistance, escapeRegExp } = require('../utils/index') class BookFinder { constructor() { @@ -201,7 +201,7 @@ class BookFinder { add(title, position = 0) { // if title contains the author, remove it if (this.cleanAuthor) { - const authorRe = new RegExp(`(^| | by |)${this.cleanAuthor}(?= |$)`, "g") + const authorRe = new RegExp(`(^| | by |)${escapeRegExp(this.cleanAuthor)}(?= |$)`, "g") title = this.bookFinder.cleanAuthorForCompares(title).replace(authorRe, '').trim() } diff --git a/server/utils/index.js b/server/utils/index.js index 84167229..0377b173 100644 --- a/server/utils/index.js +++ b/server/utils/index.js @@ -192,4 +192,16 @@ module.exports.asciiOnlyToLowerCase = (str) => { } } return temp +} + +/** + * Escape string used in RegExp + * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping + * + * @param {string} str + * @returns {string} + */ +module.exports.escapeRegExp = (str) => { + if (typeof str !== 'string') return '' + return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') } \ No newline at end of file