mirror of
https://github.com/zulip/zulip.git
synced 2025-11-17 20:41:46 +00:00
markdown: Handle SyntaxError in python_to_js_filter.
We swallow the error if our python_to_js_filter code is unable to parse some python regex properly. This ensures that the web app stays responsive. We would fail to show an accurate local echo for these regexes, however, the backend would act as the final authority for handling the realm pattern conversion.
This commit is contained in:
committed by
Tim Abbott
parent
d936fcab3b
commit
15e29e209c
@@ -524,6 +524,14 @@ run_test('python_to_js_filter', () => {
|
||||
actual_value = marked.InlineLexer.rules.zulip.realm_filters;
|
||||
expected_value = [/#cf([0-9]+)([A-Z][0-9A-Z]*)(?![\w])/g];
|
||||
assert.deepEqual(actual_value, expected_value);
|
||||
// Test incorrect syntax.
|
||||
blueslip.set_test_data('error', 'python_to_js_filter: Invalid regular expression: /!@#@(!#&((!&(@#((?![\\w])/: Unterminated group');
|
||||
markdown.set_realm_filters([['!@#@(!#&((!&(@#(', 'http://google.com']]);
|
||||
actual_value = marked.InlineLexer.rules.zulip.realm_filters;
|
||||
expected_value = [];
|
||||
assert.deepEqual(actual_value, expected_value);
|
||||
assert(blueslip.get_test_logs('error').length, 1);
|
||||
blueslip.clear_test_data();
|
||||
});
|
||||
|
||||
run_test('katex_throws_unexpected_exceptions', () => {
|
||||
|
||||
@@ -277,7 +277,16 @@ function python_to_js_filter(pattern, url) {
|
||||
// message is rendered on the backend which has proper support
|
||||
// for negative lookbehind.
|
||||
pattern = pattern + /(?![\w])/.source;
|
||||
return [new RegExp(pattern, js_flags), url];
|
||||
var final_regex = null;
|
||||
try {
|
||||
final_regex = new RegExp(pattern, js_flags);
|
||||
} catch (ex) {
|
||||
// We have an error computing the generated regex syntax.
|
||||
// We'll ignore this realm filter for now, but log this
|
||||
// failure for debugging later.
|
||||
blueslip.error('python_to_js_filter: ' + ex.message);
|
||||
}
|
||||
return [final_regex, url];
|
||||
}
|
||||
|
||||
exports.set_realm_filters = function (realm_filters) {
|
||||
@@ -290,6 +299,10 @@ exports.set_realm_filters = function (realm_filters) {
|
||||
var pattern = realm_filter[0];
|
||||
var url = realm_filter[1];
|
||||
var js_filters = python_to_js_filter(pattern, url);
|
||||
if (!js_filters[0]) {
|
||||
// Skip any realm filters that could not be converted
|
||||
return;
|
||||
}
|
||||
|
||||
realm_filter_map[js_filters[0]] = js_filters[1];
|
||||
realm_filter_list.push([js_filters[0], js_filters[1]]);
|
||||
|
||||
Reference in New Issue
Block a user