mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-11-28 01:04:05 +00:00
Merge pull request #2647 from chakflying/fix/ip-regex
Fix: [Regex] Do not allow white space around IP
This commit is contained in:
commit
806eb799c1
3 changed files with 20 additions and 4 deletions
|
@ -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) => {
|
||||||
|
|
||||||
|
|
|
@ -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,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]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((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}))|:)))(%.+)?$))`;
|
||||||
// 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_])$`;
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,10 @@ 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;
|
||||||
|
@ -23,6 +27,10 @@ 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;
|
||||||
|
|
Loading…
Reference in a new issue