Use URL.canParse for URL validation; add strict regex fallback (maintainer feedback)
This commit is contained in:
1 parent
8ffd9a5e2a
commit
945cff978b
2 files changed
+52
-60
No files matched your search
@@ -128,40 +128,36 @@ const monitorValidation = joi.object({
|
|||||||
.string()
|
.string()
|
||||||
.trim()
|
.trim()
|
||||||
.custom((value, helpers) => {
|
.custom((value, helpers) => {
|
||||||
const noProtocol = value.replace(/^(https?:\/\/)/, "");
|
// 1. Standard URLs: must have protocol and pass canParse()
|
||||||
|
if (/^(https?:\/\/)/.test(value)) {
|
||||||
|
if (
|
||||||
|
typeof URL !== "undefined" &&
|
||||||
|
typeof URL.canParse === "function" &&
|
||||||
|
URL.canParse(value)
|
||||||
|
) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
// else, it's a malformed URL with protocol
|
||||||
|
return helpers.error("string.invalidUrl");
|
||||||
|
}
|
||||||
|
|
||||||
if (/^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?(:\d{1,5})?$/.test(noProtocol)) {
|
// 2. Docker/internal hostnames (no protocol)
|
||||||
return value;
|
if (/^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?(:\d{1,5})?$/.test(value)) {
|
||||||
}
|
if (value.includes("..")) {
|
||||||
|
return helpers.error("string.invalidUrl");
|
||||||
// Accept full domain/IP/URL (as fallback)
|
}
|
||||||
var urlRegex = new RegExp(
|
// Split by dot, check each label
|
||||||
"^" +
|
const labels = value.split(":")[0].split(".");
|
||||||
"(?:(?:https?|ftp):\\/\\/)?" +
|
for (const label of labels) {
|
||||||
"(?:" +
|
if (!label) return helpers.error("string.invalidUrl");
|
||||||
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
|
if (label.startsWith("-") || label.endsWith("-")) {
|
||||||
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
|
return helpers.error("string.invalidUrl");
|
||||||
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
|
}
|
||||||
"|" +
|
}
|
||||||
"(?:" +
|
|
||||||
"(?:" +
|
|
||||||
"[a-z0-9\\u00a1-\\uffff]" +
|
|
||||||
"[a-z0-9\\u00a1-\\uffff_-]{0,62}" +
|
|
||||||
")?" +
|
|
||||||
"[a-z0-9\\u00a1-\\uffff]\\." +
|
|
||||||
")+" +
|
|
||||||
"(?:[a-z\\u00a1-\\uffff]{2,}\\.?)" +
|
|
||||||
")" +
|
|
||||||
"(?::\\d{2,5})?" +
|
|
||||||
"(?:[/?#]\\S*)?" +
|
|
||||||
"$",
|
|
||||||
"i"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (urlRegex.test(value)) {
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3. Everything else is invalid
|
||||||
return helpers.error("string.invalidUrl");
|
return helpers.error("string.invalidUrl");
|
||||||
})
|
})
|
||||||
.messages({
|
.messages({
|
||||||
|
|||||||
+26
-30
@@ -172,40 +172,36 @@ const createMonitorBodyValidation = joi.object({
|
|||||||
.string()
|
.string()
|
||||||
.required()
|
.required()
|
||||||
.custom((value, helpers) => {
|
.custom((value, helpers) => {
|
||||||
const noProtocol = value.replace(/^(https?:\/\/)/, "");
|
// 1. Standard URLs: must have protocol and pass canParse()
|
||||||
|
if (/^(https?:\/\/)/.test(value)) {
|
||||||
|
if (
|
||||||
|
typeof URL !== "undefined" &&
|
||||||
|
typeof URL.canParse === "function" &&
|
||||||
|
URL.canParse(value)
|
||||||
|
) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
// else, it's a malformed URL with protocol
|
||||||
|
return helpers.error("string.invalidUrl");
|
||||||
|
}
|
||||||
|
|
||||||
if (/^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?(:\d{1,5})?$/.test(noProtocol)) {
|
// 2. Docker/internal hostnames (no protocol)
|
||||||
return value;
|
if (/^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])?(:\d{1,5})?$/.test(value)) {
|
||||||
}
|
if (value.includes("..")) {
|
||||||
|
return helpers.error("string.invalidUrl");
|
||||||
// Existing complex URL validation as fallback
|
}
|
||||||
var urlRegex = new RegExp(
|
// Split by dot, check each label
|
||||||
"^" +
|
const labels = value.split(":")[0].split(".");
|
||||||
"(?:(?:https?|ftp):\\/\\/)?" +
|
for (const label of labels) {
|
||||||
"(?:" +
|
if (!label) return helpers.error("string.invalidUrl");
|
||||||
"(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" +
|
if (label.startsWith("-") || label.endsWith("-")) {
|
||||||
"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
|
return helpers.error("string.invalidUrl");
|
||||||
"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" +
|
}
|
||||||
"|" +
|
}
|
||||||
"(?:" +
|
|
||||||
"(?:" +
|
|
||||||
"[a-z0-9\\u00a1-\\uffff]" +
|
|
||||||
"[a-z0-9\\u00a1-\\uffff_-]{0,62}" +
|
|
||||||
")?" +
|
|
||||||
"[a-z0-9\\u00a1-\\uffff]\\." +
|
|
||||||
")+" +
|
|
||||||
"(?:[a-z\\u00a1-\\uffff]{2,}\\.?)" +
|
|
||||||
")" +
|
|
||||||
"(?::\\d{2,5})?" +
|
|
||||||
"(?:[/?#]\\S*)?" +
|
|
||||||
"$",
|
|
||||||
"i"
|
|
||||||
);
|
|
||||||
|
|
||||||
if (urlRegex.test(value)) {
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3. Everything else is invalid
|
||||||
return helpers.error("string.invalidUrl");
|
return helpers.error("string.invalidUrl");
|
||||||
})
|
})
|
||||||
.messages({
|
.messages({
|
||||||
|
|||||||
Reference in new issue
Block a user