From 95c934e08bf7ea97a85a7107059d804950907953 Mon Sep 17 00:00:00 2001 From: Nelson Chan Date: Fri, 20 Jan 2023 06:33:45 +0800 Subject: [PATCH] Fix: Do not allow white space around IP Feat: Trim input on submit Test: Add test for whitespace regex match --- src/pages/EditMonitor.vue | 8 ++++++++ src/util-frontend.js | 2 +- test/cypress/unit/util-frontend.spec.js | 14 +++++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 00f45d7eb..e9cbd8245 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -949,6 +949,14 @@ message HealthCheckResponse { this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4); } + if (this.monitor.hostname) { + this.monitor.hostname = this.monitor.hostname.trim(); + } + + if (this.monitor.url) { + this.monitor.url = this.monitor.url.trim(); + } + if (this.isAdd) { this.$root.add(this.monitor, async (res) => { diff --git a/src/util-frontend.js b/src/util-frontend.js index 55d0f6349..882ee2914 100644 --- a/src/util-frontend.js +++ b/src/util-frontend.js @@ -88,7 +88,7 @@ export function hostNameRegexPattern(mqtt = false) { // mqtt, mqtts, ws and wss schemes accepted by mqtt.js (https://github.com/mqttjs/MQTT.js/#connect) const mqttSchemeRegexPattern = "((mqtt|ws)s?:\\/\\/)?"; // Source: https://digitalfortress.tech/tips/top-15-commonly-used-regex/ - const ipRegexPattern = `((^\\s*${mqtt ? mqttSchemeRegexPattern : ""}((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\\s*$)|(^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|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0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?$))`; // Source: https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address const hostNameRegexPattern = `^${mqtt ? mqttSchemeRegexPattern : ""}([a-zA-Z0-9])?(([a-zA-Z0-9_]|[a-zA-Z0-9_][a-zA-Z0-9\\-_]*[a-zA-Z0-9_])\\.)*([A-Za-z0-9_]|[A-Za-z0-9_][A-Za-z0-9\\-_]*[A-Za-z0-9_])$`; diff --git a/test/cypress/unit/util-frontend.spec.js b/test/cypress/unit/util-frontend.spec.js index 6abedf821..9bfd4becc 100644 --- a/test/cypress/unit/util-frontend.spec.js +++ b/test/cypress/unit/util-frontend.spec.js @@ -9,7 +9,11 @@ describe("Test util-frontend.js", () => { expect(regex.test("www.test.com")).to.be.true; expect(regex.test("127.0.0.1")).to.be.true; expect(regex.test("192.168.1.156")).to.be.true; - + expect(regex.test(" 192.168.1.145")).to.be.false; + expect(regex.test("192.168.1.145 ")).to.be.false; + expect(regex.test(" fe80::3282:3ff:ae28:592")).to.be.false; + expect(regex.test("fe80::3282:3ff:ae28:592 ")).to.be.false; + ["mqtt", "mqtts", "ws", "wss"].forEach(schema => { expect(regex.test(`${schema}://www.test.com`)).to.be.false; expect(regex.test(`${schema}://127.0.0.1`)).to.be.false; @@ -23,11 +27,15 @@ describe("Test util-frontend.js", () => { expect(regex.test("www.test.com")).to.be.true; expect(regex.test("127.0.0.1")).to.be.true; expect(regex.test("192.168.1.156")).to.be.true; - + expect(regex.test(" 192.168.1.145")).to.be.false; + expect(regex.test("192.168.1.145 ")).to.be.false; + expect(regex.test(" fe80::3282:3ff:ae28:592")).to.be.false; + expect(regex.test("fe80::3282:3ff:ae28:592 ")).to.be.false; + ["mqtt", "mqtts", "ws", "wss"].forEach(schema => { expect(regex.test(`${schema}://www.test.com`)).to.be.true; expect(regex.test(`${schema}://127.0.0.1`)).to.be.true; }); }); }); -}); \ No newline at end of file +});