luci-mod-network: validate DHCP leasetime input master openwrt-24.10
authorLyall Beveridge <lyall.beveridge@morsemicro.com>
Thu, 5 Dec 2024 23:30:36 +0000 (10:30 +1100)
committerPaul Donald <newtwen+github@gmail.com>
Mon, 16 Dec 2024 20:55:31 +0000 (21:55 +0100)
Without validation, `dnsmasq` can fail silently if users enter
invalid leasetime input. This change adds input validation to
align user input with the backend parsing logic. Whilst it does
not enforce the >120 seconds requirement, this is documented
in the field description and handled by `dnsmasq`'s `option.c`,
which adjusts values <120 to meet this minimum.

Signed-off-by: Lyall Beveridge <lyall.beveridge@morsemicro.com>
modules/luci-mod-network/htdocs/luci-static/resources/view/network/interfaces.js

index f262aed39cbae184ef61d7f835cd3917948ff249..8f45fcf51488554386ba70efd48263893e2f5e1c 100644 (file)
@@ -674,6 +674,17 @@ return view.extend({
                                                so = ss.taboption('general', form.Value, 'leasetime', _('Lease time'), _('Expiry time of leased addresses, minimum is 2 minutes (<code>2m</code>).'));
                                                so.optional = true;
                                                so.default = '12h';
+                                               so.validate = function (section_id, value) {
+                                                       if (value === "infinite" || value === "deprecated") {
+                                                               return true;
+                                                       }
+
+                                                       const regex = new RegExp("^[0-9]+[smhdw]?$", "i");
+                                                       if (regex.test(value)) {
+                                                               return true;
+                                                       }
+                                                       return _("Invalid DHCP lease time format. Use integer values optionally followed by s, m, h, d, or w.");
+                                               }
 
                                                so = ss.taboption('advanced', form.Flag, 'dynamicdhcp', _('Dynamic <abbr title="Dynamic Host Configuration Protocol">DHCP</abbr>'), _('Dynamically allocate DHCP addresses for clients. If disabled, only clients having static leases will be served.'));
                                                so.default = so.enabled;