From 408a58f6f6b0d419bc6b81eb93f32c701e19118a Mon Sep 17 00:00:00 2001 From: Manuel Munz Date: Sat, 12 Feb 2011 11:09:13 +0000 Subject: [PATCH] web: Improve hostname validation --- libs/web/htdocs/luci-static/resources/cbi.js | 4 ++-- libs/web/luasrc/cbi/datatypes.lua | 5 ++--- modules/admin-full/luasrc/model/cbi/admin_system/system.lua | 2 +- modules/freifunk/luasrc/model/cbi/freifunk/basics.lua | 10 +--------- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/libs/web/htdocs/luci-static/resources/cbi.js b/libs/web/htdocs/luci-static/resources/cbi.js index 44a5b2aec..d12d050a8 100644 --- a/libs/web/htdocs/luci-static/resources/cbi.js +++ b/libs/web/htdocs/luci-static/resources/cbi.js @@ -141,8 +141,8 @@ var cbi_validators = { }, 'hostname': function(v) - { - return (v.match(/^[a-zA-Z_][a-zA-Z0-9_\-.]*$/) != null); + { if ( v.length <= 24 ) + return (v.match(/^[a-zA-Z0-9][a-zA-Z0-9\-.]*[a-zA-Z0-9]$/) != null); }, 'wpakey': function(v) diff --git a/libs/web/luasrc/cbi/datatypes.lua b/libs/web/luasrc/cbi/datatypes.lua index 67cb63daf..d1ac8f405 100644 --- a/libs/web/luasrc/cbi/datatypes.lua +++ b/libs/web/luasrc/cbi/datatypes.lua @@ -127,10 +127,9 @@ function macaddr(val) end function hostname(val) - if val and val:match("[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*") then - return true -- XXX: ToDo: need better solution + if val and (#val < 25) and val.match(val, "^[a-zA-Z0-9][a-zA-Z0-9%-%.]*[a-zA-Z0-9]$") then + return true end - return false end diff --git a/modules/admin-full/luasrc/model/cbi/admin_system/system.lua b/modules/admin-full/luasrc/model/cbi/admin_system/system.lua index dcd2fb018..a0b8844f3 100644 --- a/modules/admin-full/luasrc/model/cbi/admin_system/system.lua +++ b/modules/admin-full/luasrc/model/cbi/admin_system/system.lua @@ -75,7 +75,7 @@ s:taboption("general", DummyValue, "_uptime", translate("Uptime")).value = luci.tools.webadmin.date_format(tonumber(uptime)) hn = s:taboption("general", Value, "hostname", translate("Hostname")) - +hn.datatype = "hostname" function hn.write(self, section, value) Value.write(self, section, value) luci.sys.hostname(value) diff --git a/modules/freifunk/luasrc/model/cbi/freifunk/basics.lua b/modules/freifunk/luasrc/model/cbi/freifunk/basics.lua index b404181dc..31ab9bb67 100644 --- a/modules/freifunk/luasrc/model/cbi/freifunk/basics.lua +++ b/modules/freifunk/luasrc/model/cbi/freifunk/basics.lua @@ -38,15 +38,7 @@ b.anonymous = true hn = b:option(Value, "hostname", translate("Hostname")) hn.rmempty = false -function hn.validate(self, value) - if value == nil then - return - elseif (#value > 24) or string.match(value, "[^%w%.%-]") or string.match(value, "^[%-%.]") or string.match(value, "[%-%.]$") then - return nil, translate("Hostname may contain up to 24 alphanumeric characters. Minus and period are also allowed, but not in the beginning or the end of the hostname.") - else - return value - end -end +hn.datatype = "hostname" loc = b:option(Value, "location", translate("Location")) loc.rmempty = false -- 2.11.0