From 46b30f0623dbdf8058eba4c82770a119046f2153 Mon Sep 17 00:00:00 2001 From: Greg Hormann Date: Sun, 27 Jun 2021 14:11:47 -0400 Subject: [PATCH] Add validation to proxy names Related to https://www.huntr.dev/bounties/31-other-FalconChristmas/fpp/ --- www/js/fpp.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/www/js/fpp.js b/www/js/fpp.js index e52e4820e..de32143c2 100644 --- a/www/js/fpp.js +++ b/www/js/fpp.js @@ -6030,3 +6030,48 @@ function PreviewStatistics() { $('#statsPreviewPopup').fppDialog( "moveToTop" ); $('#statsPreviewDiv').load('api/statistics/usage'); } + +function isValidIpAddress(ip) +{ + if (ip == "") { + return false; + } + return /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ip); +} + + +// Source: https://github.com/miguelmota/is-valid-hostname/blob/master/index.js +// License MIT: https://raw.githubusercontent.com/miguelmota/is-valid-hostname/master/LICENSE +function isValidHostname(value) { + if (typeof value !== 'string') return false + + const validHostnameChars = /^[a-zA-Z0-9-.]{1,253}\.?$/g + if (!validHostnameChars.test(value)) { + return false + } + + if (value.endsWith('.')) { + value = value.slice(0, value.length - 1) + } + + if (value.length > 253) { + return false + } + + const labels = value.split('.') + + const isValid = labels.every(function (label) { + const validLabelChars = /^([a-zA-Z0-9-]+)$/g + + const validLabel = ( + validLabelChars.test(label) && + label.length < 64 && + !label.startsWith('-') && + !label.endsWith('-') + ) + + return validLabel + }) + + return isValid + } \ No newline at end of file