Skip to content

Commit b48a213

Browse files
authored
fix: parse Settings editor regular expressions more thoroughly (microsoft#201248)
1 parent b740055 commit b48a213

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/vs/workbench/services/preferences/common/preferencesValidation.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,30 @@ function valueValidatesAsType(value: any, type: string): boolean {
112112
return true;
113113
}
114114

115+
function toRegExp(pattern: string): RegExp {
116+
try {
117+
// The u flag allows support for better Unicode matching,
118+
// but deprecates some patterns such as [\s-9]
119+
// Ref https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Character_class#description
120+
return new RegExp(pattern, 'u');
121+
} catch (e) {
122+
try {
123+
return new RegExp(pattern);
124+
} catch (e) {
125+
// If the pattern can't be parsed even without the 'u' flag,
126+
// just log the error to avoid rendering the entire Settings editor blank.
127+
// Ref https://github.com/microsoft/vscode/issues/195054
128+
console.error(nls.localize('regexParsingError', "Error parsing the following regex both with and without the u flag:"), pattern);
129+
return /.*/;
130+
}
131+
}
132+
}
133+
115134
function getStringValidators(prop: IConfigurationPropertySchema) {
116135
const uriRegex = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
117136
let patternRegex: RegExp | undefined;
118137
if (typeof prop.pattern === 'string') {
119-
patternRegex = new RegExp(prop.pattern, 'u');
138+
patternRegex = toRegExp(prop.pattern);
120139
}
121140

122141
return [
@@ -272,7 +291,7 @@ function getArrayValidator(prop: IConfigurationPropertySchema): ((value: any) =>
272291
}
273292

274293
if (typeof propItems.pattern === 'string') {
275-
const patternRegex = new RegExp(propItems.pattern, 'u');
294+
const patternRegex = toRegExp(propItems.pattern);
276295
arrayValue.forEach(v => {
277296
if (!patternRegex.test(v)) {
278297
message +=

0 commit comments

Comments
 (0)