Fix codeql issues

This commit is contained in:
Zaid-maker 2024-11-20 19:06:15 +05:00
parent 74decf6d5a
commit 1ca776df78

View file

@ -7,6 +7,81 @@ const successMessage = "Sent Successfully.";
class FlashDuty extends NotificationProvider { class FlashDuty extends NotificationProvider {
name = "FlashDuty"; name = "FlashDuty";
/**
* Sanitize and validate a URL string
* @param {string} urlStr URL to validate
* @returns {string|null} Sanitized URL or null if invalid
*/
validateURL(urlStr) {
try {
const url = new URL(urlStr);
// Only allow http and https protocols
if (![ "http:", "https:" ].includes(url.protocol)) {
return null;
}
return url.toString();
} catch {
return null;
}
}
/**
* Generate a monitor url from the monitors information
* @param {object} monitorInfo Monitor details
* @returns {string|undefined} Monitor URL
*/
genMonitorUrl(monitorInfo) {
if (!monitorInfo) {
return undefined;
}
// For port type monitors
if (monitorInfo.type === "port" && monitorInfo.port) {
// Validate port number
const port = parseInt(monitorInfo.port, 10);
if (isNaN(port) || port < 1 || port > 65535) {
return undefined;
}
// Try to construct a valid URL
try {
// If hostname already includes protocol, use it
const hasProtocol = /^[a-zA-Z]+:\/\//.test(monitorInfo.hostname);
const urlStr = hasProtocol ?
monitorInfo.hostname + ":" + port :
"http://" + monitorInfo.hostname + ":" + port;
const url = new URL(urlStr);
return url.toString();
} catch {
return undefined;
}
}
// For hostname-based monitors
if (monitorInfo.hostname != null) {
try {
// If hostname already includes protocol, use it
const hasProtocol = /^[a-zA-Z]+:\/\//.test(monitorInfo.hostname);
const urlStr = hasProtocol ?
monitorInfo.hostname :
"http://" + monitorInfo.hostname;
const url = new URL(urlStr);
return url.toString();
} catch {
return undefined;
}
}
// For URL-based monitors
if (monitorInfo.url) {
return this.validateURL(monitorInfo.url);
}
return undefined;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
@ -37,21 +112,6 @@ class FlashDuty extends NotificationProvider {
} }
} }
/**
* Generate a monitor url from the monitors infomation
* @param {object} monitorInfo Monitor details
* @returns {string|undefined} Monitor URL
*/
genMonitorUrl(monitorInfo) {
if (monitorInfo.type === "port" && monitorInfo.port) {
return monitorInfo.hostname + ":" + monitorInfo.port;
}
if (monitorInfo.hostname != null) {
return monitorInfo.hostname;
}
return monitorInfo.url;
}
/** /**
* Send the message * Send the message
* @param {BeanModel} notification Message title * @param {BeanModel} notification Message title