From 4d246540c137c6dfc164c9baaa1b0d221b9ba143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sa=C5=A1ek?= Date: Fri, 11 Sep 2020 17:12:40 +0200 Subject: [PATCH] Add SSID validation for bytes count --- src/common/WiFiSettings/WiFiForm.js | 1 + src/common/WiFiSettings/WiFiGuestForm.js | 1 + src/common/WiFiSettings/WiFiSettings.js | 20 ++++++++------------ src/common/WiFiSettings/constants.js | 3 +++ 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/common/WiFiSettings/WiFiForm.js b/src/common/WiFiSettings/WiFiForm.js index 6005d6b..63bfd47 100644 --- a/src/common/WiFiSettings/WiFiForm.js +++ b/src/common/WiFiSettings/WiFiForm.js @@ -105,6 +105,7 @@ function DeviceForm({ label="SSID" value={formData.SSID} error={formErrors.SSID || null} + helpText={HELP_TEXTS.ssid} required onChange={setFormValue((value) => ({ devices: { diff --git a/src/common/WiFiSettings/WiFiGuestForm.js b/src/common/WiFiSettings/WiFiGuestForm.js index 93380cd..7ae88c0 100644 --- a/src/common/WiFiSettings/WiFiGuestForm.js +++ b/src/common/WiFiSettings/WiFiGuestForm.js @@ -57,6 +57,7 @@ export default function WifiGuestForm({ label="SSID" value={formData.SSID} error={formErrors.SSID} + helpText={HELP_TEXTS.ssid} onChange={setFormValue((value) => ({ devices: { [formData.id]: { diff --git a/src/common/WiFiSettings/WiFiSettings.js b/src/common/WiFiSettings/WiFiSettings.js index 8b55fa5..be455af 100644 --- a/src/common/WiFiSettings/WiFiSettings.js +++ b/src/common/WiFiSettings/WiFiSettings.js @@ -63,14 +63,10 @@ function prepDataToSubmit(formData) { return formData; } -function diacriticValidation(string) { - for (let i = 0; i < string.length; i++) { - const charCode = string.charCodeAt(i); - if (charCode < 32 || charCode > 127) { - return false; - } - } - return true; +export function byteCount(string) { + const buffer = Buffer.from(string, "utf-8"); + const count = buffer.byteLength; + return count; } export function validator(formData) { @@ -81,10 +77,8 @@ export function validator(formData) { if (device.SSID.length > 32) errors.SSID = _("SSID can't be longer than 32 symbols"); if (device.SSID.length === 0) errors.SSID = _("SSID can't be empty"); - if (!diacriticValidation(device.SSID)) - errors.SSID = _( - "Your SSID contains non-standard characters. These are not forbidden, but could cause problems on some devices." - ); + if (byteCount(device.SSID) > 32) + errors.SSID = _("SSID can't be longer than 32 bytes"); if (device.password.length < 8) errors.password = _("Password must contain at least 8 symbols"); @@ -96,6 +90,8 @@ export function validator(formData) { guest_wifi_errors.SSID = _("SSID can't be longer than 32 symbols"); if (device.guest_wifi.SSID.length === 0) guest_wifi_errors.SSID = _("SSID can't be empty"); + if (byteCount(device.guest_wifi.SSID) > 32) + guest_wifi_errors.SSID = _("SSID can't be longer than 32 bytes"); if (device.guest_wifi.password.length < 8) guest_wifi_errors.password = _( diff --git a/src/common/WiFiSettings/constants.js b/src/common/WiFiSettings/constants.js index 480c505..0018fef 100644 --- a/src/common/WiFiSettings/constants.js +++ b/src/common/WiFiSettings/constants.js @@ -19,6 +19,9 @@ export const HWMODES = { "11a": "5", }; export const HELP_TEXTS = { + ssid: _( + `SSID which contains non-standard characters could cause problems on some devices.` + ), password: _(` WPA2 pre-shared key, that is required to connect to the network. `),