Fix Bug #3175: Update 'sync_list' line parsing to correctly escape characters for regex parsing (#3184)

* Fix createRegexCompatiblePath to escape characters that cause issues for regex parsing
* Update 'matchSegment' to reuse 'createRegexCompatiblePath' to be consistent in regex creation
This commit is contained in:
abraunegg 2025-03-30 07:39:21 +11:00 committed by GitHub
commit 519e9ffb3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -599,21 +599,27 @@ class ClientSideFiltering {
}
return depth; // No wildcard found should be '0'
}
// Create a wildcard regex compatible string based on the sync list rule
string createRegexCompatiblePath(string regexCompatiblePath) {
regexCompatiblePath = regexCompatiblePath.replace(".", "\\."); // Escape the dot (.) if present
regexCompatiblePath = regexCompatiblePath.replace(" ", "\\s"); // Escape spaces if present
regexCompatiblePath = regexCompatiblePath.replace("*", ".*"); // Replace * with '.*' to be compatible with function and to match any characters
// Escape all special regex characters that could break regex parsing
regexCompatiblePath = escaper(regexCompatiblePath).text;
// Restore wildcard (*) support with '.*' to be compatible with function and to match any characters
regexCompatiblePath = regexCompatiblePath.replace("\\*", ".*");
// Ensure space matches only literal space, not \s (tabs, etc.)
regexCompatiblePath = regexCompatiblePath.replace(" ", "\\ ");
// Return the regex compatible path
return regexCompatiblePath;
}
// Create a regex compatible string to match a relevant segment
bool matchSegment(string ruleSegment, string pathSegment) {
ruleSegment = ruleSegment.replace("*", ".*"); // Replace * with '.*' to be compatible with function and to match any characters
ruleSegment = ruleSegment.replace(" ", "\\s"); // Escape spaces if present
auto pattern = regex("^" ~ ruleSegment ~ "$");
// Check if there's a match
// Create the required pattern
auto pattern = regex("^" ~ createRegexCompatiblePath(ruleSegment) ~ "$");
// Check if there's a match and return result
return !match(pathSegment, pattern).empty;
}