From 0ec4c911ddca7c3a8d320aa6b62d34599a6dc712 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 13 Jun 2020 11:13:48 -0400 Subject: [PATCH] Fix improper handling of regex flags in search widget Reported internally. Potential regex flags are passed as is to RegExp contructor, and in case of failure the query is deemed a plain text one. Related commit: - https://github.com/codemirror/CodeMirror/commit/8de67d22bd17453b5c5c09118a5e397421fe86b9#diff-3f4aa453cefa49f6431f1bba3bb53a8e --- src/js/codemirror/search.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/js/codemirror/search.js b/src/js/codemirror/search.js index 248249170..d3420f7bb 100644 --- a/src/js/codemirror/search.js +++ b/src/js/codemirror/search.js @@ -172,16 +172,24 @@ }); } + // FIX: use all potential regex flags as is, and if this throws, treat + // the query string as plain text. function parseQuery(query) { - var isRE = query.match(/^\/(.*)\/([a-z]*)$/); - if (isRE) { - try { query = new RegExp(isRE[1], isRE[2].indexOf("i") === -1 ? "" : "i"); } - catch(e) {} // Not a regular expression after all, do a string search - } else { + let isRE = query.match(/^\/(.*)\/([a-z]*)$/); + if ( isRE ) { + try { + query = new RegExp(isRE[1], isRE[2]); + } + catch (e) { + isRE = false; + } + } + if ( isRE === false ) { query = parseString(query); } - if (typeof query === "string" ? query === "" : query.test("")) + if ( typeof query === 'string' ? query === '' : query.test('') ) { query = /x^/; + } return query; }