From 488e542ad844e14803a093f2d3b7ca15cfbc9997 Mon Sep 17 00:00:00 2001 From: GJS Date: Sun, 29 Dec 2024 07:30:40 +0100 Subject: [PATCH 1/2] Refactored: Improved search functionality in `MonitorList.vue` - Enhanced the search logic to support regular expressions with case-insensitive matching. - Escaped special characters in the search text to prevent invalid regex patterns. - Expanded search coverage to include `monitor.url`, `monitor.hostname`, and `monitor.dns_resolve_server`. - Added error handling to catch and log invalid regex patterns, ensuring robustness. - Maintained compatibility with existing search fields, such as monitor name and tags. modified: src/components/MonitorList.vue Co-authored-by: Ionys --- src/components/MonitorList.vue | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/components/MonitorList.vue b/src/components/MonitorList.vue index a579316b3..793b9bd00 100644 --- a/src/components/MonitorList.vue +++ b/src/components/MonitorList.vue @@ -320,15 +320,35 @@ export default { } } - // filter by search text - // finds monitor name, tag name or tag value + // Filter monitors based on the search text + // Matches monitor name, URL, hostname, DNS resolve server, or tag names and values let searchTextMatch = true; if (this.searchText !== "") { - const loweredSearchText = this.searchText.toLowerCase(); - searchTextMatch = - monitor.name.toLowerCase().includes(loweredSearchText) - || monitor.tags.find(tag => tag.name.toLowerCase().includes(loweredSearchText) - || tag.value?.toLowerCase().includes(loweredSearchText)); + try { + // Escape special characters for use in the regular expression + const escapeRegExp = (string) => { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + }; + + // Create a case-insensitive regex for the search text + const escapedSearchText = escapeRegExp(this.searchText); + const regex = new RegExp(escapedSearchText, "i"); + + // Test the regex against strings safely + const safeRegexTest = (str) => str && regex.test(str); + + // Check if any relevant fields match the search text + searchTextMatch = + regex.test(monitor.name) || + safeRegexTest(monitor.url) || + safeRegexTest(monitor.hostname) || + safeRegexTest(monitor.dns_resolve_server) || + monitor.tags.some(tag => regex.test(tag.name) || safeRegexTest(tag.value)); + } catch (e) { + // Log error if the regex pattern is invalid + console.error("Invalid regex pattern:", e); + searchTextMatch = false; + } } // filter by status From 33ebfefc5892a9cabd632cc075d9125fb2a9fa1c Mon Sep 17 00:00:00 2001 From: GJS Date: Sun, 29 Dec 2024 07:31:50 +0100 Subject: [PATCH 2/2] Updated: Enhance null safety and set default URL in `EditMonitor.vue` - Added optional chaining to safely access `this.monitor.hostname`. - Introduced default URL (`https://`) assignment to `this.monitor.url`. - Ensured `hostname` is trimmed after the URL is set. modified: src/pages/EditMonitor.vue --- src/pages/EditMonitor.vue | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index a83f91cab..f70145ebd 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -1719,7 +1719,12 @@ message HealthCheckResponse { this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4); } - if (this.monitor.hostname) { + // Check if the 'monitor' object exists and contains a 'hostname' property. + if (this.monitor?.hostname) { + // Initialize the 'url' property of 'monitor' to the base URL scheme. + this.monitor.url = "https://"; + + // Remove any leading or trailing spaces from the 'hostname' value. this.monitor.hostname = this.monitor.hostname.trim(); }