diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-04-16 20:15:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-16 20:15:24 -0400 |
commit | 36af2cf8ac46b3fce7144efdb6fcc2d3a71593fb (patch) | |
tree | b2bd895befa449ac8fb983755fc9f337e25926e0 /src/SMAPI.Web/wwwroot/Content/js/log-parser.js | |
parent | d486d940bae57f627fbe3210bbfa611a5586c8e8 (diff) | |
parent | 446205c7bd342422f4b4f14d6c8f36dc0b6cf468 (diff) | |
download | SMAPI-36af2cf8ac46b3fce7144efdb6fcc2d3a71593fb.tar.gz SMAPI-36af2cf8ac46b3fce7144efdb6fcc2d3a71593fb.tar.bz2 SMAPI-36af2cf8ac46b3fce7144efdb6fcc2d3a71593fb.zip |
Merge pull request #841 from KhloeLeclair/safe-regex
[Website] Improve regex safety for the log viewer
Diffstat (limited to 'src/SMAPI.Web/wwwroot/Content/js/log-parser.js')
-rw-r--r-- | src/SMAPI.Web/wwwroot/Content/js/log-parser.js | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/src/SMAPI.Web/wwwroot/Content/js/log-parser.js b/src/SMAPI.Web/wwwroot/Content/js/log-parser.js index c730309b..e19e3301 100644 --- a/src/SMAPI.Web/wwwroot/Content/js/log-parser.js +++ b/src/SMAPI.Web/wwwroot/Content/js/log-parser.js @@ -247,7 +247,8 @@ smapi.logParser = function (state) { // add the properties we're passing to Vue state.totalMessages = state.messages.length; state.filterText = ""; - state.filterRegex = ""; + state.filterRegex = null; + state.filterError = null; state.showContentPacks = true; state.useHighlight = true; state.useRegex = false; @@ -506,6 +507,12 @@ smapi.logParser = function (state) { } index = match.index + match[0].length; + + // In the event of a zero-length match, forcibly increment + // the last index of the regular expression to ensure we + // aren't stuck in an infinite loop. + if (match[0].length == 0) + filter.lastIndex++; } // Add any trailing text after the last match was found. @@ -857,14 +864,30 @@ smapi.logParser = function (state) { if (!text || !text.length) { this.filterText = ""; this.filterRegex = null; + this.filterError = null; } else { if (!state.useRegex) text = helpers.escapeRegex(text); - this.filterRegex = new RegExp( - state.useWord ? `\\b${text}\\b` : text, - state.useInsensitive ? "ig" : "g" - ); + + const flags = state.useInsensitive ? "ig" : "g"; + + this.filterError = null; + let regex; + + try { + regex = new RegExp(text, flags); + } catch (err) { + regex = null; + this.filterError = err.message; + } + + if (regex) + this.filterRegex = state.useWord ? + new RegExp(`\\b(?:${text})\\b`, flags) : + regex; + else + this.filterRegex = null; } this.updateUrl(); |