Fix: Do not allow white space around IP

Feat: Trim input on submit

Test: Add test for whitespace regex match
This commit is contained in:
Nelson Chan 2023-01-20 06:33:45 +08:00
parent ce82ad1c12
commit 95c934e08b
3 changed files with 20 additions and 4 deletions

View File

@ -949,6 +949,14 @@ message HealthCheckResponse {
this.monitor.headers = JSON.stringify(JSON.parse(this.monitor.headers), null, 4); 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) { if (this.isAdd) {
this.$root.add(this.monitor, async (res) => { this.$root.add(this.monitor, async (res) => {

View File

@ -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) // mqtt, mqtts, ws and wss schemes accepted by mqtt.js (https://github.com/mqttjs/MQTT.js/#connect)
const mqttSchemeRegexPattern = "((mqtt|ws)s?:\\/\\/)?"; const mqttSchemeRegexPattern = "((mqtt|ws)s?:\\/\\/)?";
// Source: https://digitalfortress.tech/tips/top-15-commonly-used-regex/ // 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|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2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.+)?$))`;
// Source: https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address // 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_])$`; 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_])$`;

View File

@ -9,7 +9,11 @@ describe("Test util-frontend.js", () => {
expect(regex.test("www.test.com")).to.be.true; expect(regex.test("www.test.com")).to.be.true;
expect(regex.test("127.0.0.1")).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.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 => { ["mqtt", "mqtts", "ws", "wss"].forEach(schema => {
expect(regex.test(`${schema}://www.test.com`)).to.be.false; expect(regex.test(`${schema}://www.test.com`)).to.be.false;
expect(regex.test(`${schema}://127.0.0.1`)).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("www.test.com")).to.be.true;
expect(regex.test("127.0.0.1")).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.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 => { ["mqtt", "mqtts", "ws", "wss"].forEach(schema => {
expect(regex.test(`${schema}://www.test.com`)).to.be.true; expect(regex.test(`${schema}://www.test.com`)).to.be.true;
expect(regex.test(`${schema}://127.0.0.1`)).to.be.true; expect(regex.test(`${schema}://127.0.0.1`)).to.be.true;
}); });
}); });
}); });
}); });