Skip to content

Commit

Permalink
🐛 Better URL validation, Fixed hoppscotch#471
Browse files Browse the repository at this point in the history
  • Loading branch information
Liyas Thomas committed Jan 3, 2020
1 parent 35df7c6 commit 0930b1a
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions pages/realtime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -209,28 +209,28 @@ export default {
},
computed: {
urlValid() {
const pattern = new RegExp(
"^(wss?:\\/\\/)?" +
"((([a-z\\d]([a-z\\d-]*[a-z:@\\d])*)\\.)+[a-z]{2,}|" +
"((\\d{1,3}\\.){3}\\d{1,3}))" +
"(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*" +
"(\\?[:\\;&a-z\\d%_.~+=-]*)?" +
"(\\#[-a-z\\d_]*)?$",
"i"
const protocol = "^(wss?:\\/\\/)?";
const validIP = new RegExp(
protocol +
"(([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])$"
);
return pattern.test(this.url);
const validHostname = new RegExp(
protocol +
"(([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/])$"
);
return validIP.test(this.url) || validHostname.test(this.url);
},
serverValid() {
const pattern = new RegExp(
"^(http(s)?:\\/\\/)?" +
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" +
"((\\d{1,3}\\.){3}\\d{1,3}))" +
"(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*" +
"(\\?[:\\;&a-z\\d%_.~+=-]*)?" +
"(\\#[-a-z\\d_]*)?$",
"i"
const protocol = "^(https?:\\/\\/)?";
const validIP = new RegExp(
protocol +
"(([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])$"
);
const validHostname = new RegExp(
protocol +
"(([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/])$"
);
return pattern.test(this.server);
return validIP.test(this.server) || validHostname.test(this.server);
}
},
methods: {
Expand Down

0 comments on commit 0930b1a

Please sign in to comment.