fix: escape multi-character separators before building the cleanup regex#146
Open
spokodev wants to merge 1 commit into
Open
fix: escape multi-character separators before building the cleanup regex#146spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
A multi-character `separator` (a documented string option) was concatenated
straight into a `RegExp`, escaping only its first character:
getSlug('foo bar baz', { separator: '..' }) // "foo" (bar, baz dropped)
getSlug('foo bar baz', { separator: '++' }) // threw "Invalid regular expression"
getSlug('foo bar baz', { separator: '()' }) // threw "Invalid regular expression"
For '..' the second dot became an unescaped wildcard that swallowed the rest
of the string (data loss); for '++'/'()' the result was an invalid pattern
that threw. Escape the whole separator with the existing `escapeChars` helper
and group it so the full sequence is matched when collapsing duplicates and
trimming the ends. Single-character separators are unaffected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A multi-character
separator(documented as a{string}option) is concatenated straight into aRegExpwith only its first character escaped, so it either silently drops data or throws:For
'..'the cleanup regex becomes/\..+/— the second.is an unescaped wildcard that greedily eats the rest of the string (data loss). For'++'/'()'/'**'the result is an invalid pattern that throws an unhandled exception. Single-character separators, and multi-char separators made only of non-metacharacters ('::','__'), happen to work, which is why the issue went unnoticed.Cause
'\\' + separatorescapes only the first character of the separator.Fix
Escape the whole separator with the file's existing
escapeCharshelper (already used elsewhere) and group it so the full sequence is matched when collapsing duplicates and trimming the ends.Verification
test/test-separator.jsfor'..'/'++'/'()'; fails before, passes after.-,_,*,.) verified unchanged.