diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-03-22 09:49:52 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-04-04 23:21:53 +0200 |
commit | 70ffbe65a0d9e8a77c9d3bafc34ddd260490c541 (patch) | |
tree | 8f44dc5d7fb0f9c5caa361e2a5af94e6da20b97d /modules/luci-base | |
parent | 4024d4f2242a88a5e9fd18ed437b1c9fd13693f9 (diff) |
luci-base: add a strict flag to the hostname validator
Some applications, e.g. dnsmasq, do not allow hostnames starting with an
underscore, therefor extend the existing hostname datatype validator with
a `strict` which disallows a leading underscore.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-base')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/cbi.js | 7 | ||||
-rw-r--r-- | modules/luci-base/luasrc/cbi/datatypes.lua | 4 |
2 files changed, 6 insertions, 5 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index d40ec34bc..6c35372cd 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -218,12 +218,13 @@ var cbi_validators = { ((ipv4only == 1) && cbi_validators.ip4addr.apply(this)); }, - 'hostname': function() + 'hostname': function(strict) { if (this.length <= 253) - return (this.match(/^[a-zA-Z0-9]+$/) != null || + return (this.match(/^[a-zA-Z0-9_]+$/) != null || (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) && - this.match(/[^0-9.]/))); + this.match(/[^0-9.]/))) && + (!strict || !this.match(/^_/)); return false; }, diff --git a/modules/luci-base/luasrc/cbi/datatypes.lua b/modules/luci-base/luasrc/cbi/datatypes.lua index 55cdf8a74..99113e0b7 100644 --- a/modules/luci-base/luasrc/cbi/datatypes.lua +++ b/modules/luci-base/luasrc/cbi/datatypes.lua @@ -199,13 +199,13 @@ function macaddr(val) return ip.checkmac(val) and true or false end -function hostname(val) +function hostname(val, strict) if val and (#val < 254) and ( val:match("^[a-zA-Z_]+$") or (val:match("^[a-zA-Z0-9_][a-zA-Z0-9_%-%.]*[a-zA-Z0-9]$") and val:match("[^0-9%.]")) ) then - return true + return (not strict or not val:match("^_")) end return false end |