settings: Validate candidate linkifier before sending to server.

Before submitting a candidate custom linkifier, the web app now
validates it locally using `python_to_js_linkifier`. This prevents
invalid linkifiers from reaching the server, as the server may
support some linkifiers that the web app does not.

To achieve this, `python_to_js_linkifier` is exported from
`linkifiers.ts`, and a try-catch block is added in
`settings_linkifiers.ts`. If the web-app does not support
the linkifier, the app displays an error message
and prevents submission.
Fixes:#33238
This commit is contained in:
Abhay Upadhyay
2025-02-07 08:28:38 +05:30
committed by Tim Abbott
parent a8fbf6d5d5
commit 51a4ed61ee
2 changed files with 17 additions and 1 deletions

View File

@@ -16,7 +16,7 @@ export function get_linkifier_map(): LinkifierMap {
return linkifier_map;
}
function python_to_js_linkifier(
export function python_to_js_linkifier(
pattern: string,
url: string,
): [RegExp | null, url_template_lib.Template, Record<number, string>] {

View File

@@ -11,6 +11,7 @@ import * as channel from "./channel.ts";
import * as confirm_dialog from "./confirm_dialog.ts";
import * as dialog_widget from "./dialog_widget.ts";
import {$t_html} from "./i18n.ts";
import * as linkifiers from "./linkifiers.ts";
import * as ListWidget from "./list_widget.ts";
import * as scroll_util from "./scroll_util.ts";
import * as settings_ui from "./settings_ui.ts";
@@ -255,6 +256,21 @@ export function build_page(): void {
$pattern_status.hide();
$template_status.hide();
const pattern = String($("#linkifier_pattern").val()).trim();
const url_template = String($("#linkifier_template").val()).trim();
try {
linkifiers.python_to_js_linkifier(pattern, url_template);
} catch {
$add_linkifier_button.prop("disabled", false);
ui_report.error(
$t_html({defaultMessage: "Failed: Invalid Pattern"}),
undefined,
$pattern_status,
);
return;
}
void channel.post({
url: "/json/realm/filters",
data: $(this).serialize(),