From 35b97ec0fe57c3bbb9c58f56452548ee6252c352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sa=C5=A1ek?= Date: Wed, 9 Sep 2020 14:42:18 +0200 Subject: [PATCH 1/4] Add validation for SSID with diacritic --- src/common/WiFiSettings/WiFiSettings.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/common/WiFiSettings/WiFiSettings.js b/src/common/WiFiSettings/WiFiSettings.js index a16d8d7..8b55fa5 100644 --- a/src/common/WiFiSettings/WiFiSettings.js +++ b/src/common/WiFiSettings/WiFiSettings.js @@ -63,6 +63,16 @@ 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 validator(formData) { const formErrors = formData.devices.map((device) => { if (!device.enabled) return {}; @@ -71,6 +81,10 @@ 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 (device.password.length < 8) errors.password = _("Password must contain at least 8 symbols"); 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 2/4] 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. `), From 002786d073640d94b7d92996e810406350f1608e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sa=C5=A1ek?= Date: Fri, 11 Sep 2020 17:13:24 +0200 Subject: [PATCH 3/4] Add test --- src/common/WiFiSettings/__tests__/WiFiSettings.test.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/common/WiFiSettings/__tests__/WiFiSettings.test.js b/src/common/WiFiSettings/__tests__/WiFiSettings.test.js index 4923c67..7f533c2 100644 --- a/src/common/WiFiSettings/__tests__/WiFiSettings.test.js +++ b/src/common/WiFiSettings/__tests__/WiFiSettings.test.js @@ -19,7 +19,7 @@ import { twoDevices, threeDevices, } from "./__fixtures__/wifiSettings"; -import { WiFiSettings, validator } from "../WiFiSettings"; +import { WiFiSettings, validator, byteCount } from "../WiFiSettings"; describe("", () => { let firstRender; @@ -213,4 +213,8 @@ describe("", () => { ]; expect(validator(threeDevices)).toEqual(threeDevicesFormErrors); }); + + it("ByteCount function", () => { + expect(byteCount("abc")).toEqual(3); + }); }); From 5186385b9f844b1fa23235c52565e01fb109eaf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Sa=C5=A1ek?= Date: Fri, 11 Sep 2020 17:13:50 +0200 Subject: [PATCH 4/4] Update snapshots --- .../__snapshots__/WiFiSettings.test.js.snap | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/common/WiFiSettings/__tests__/__snapshots__/WiFiSettings.test.js.snap b/src/common/WiFiSettings/__tests__/__snapshots__/WiFiSettings.test.js.snap index 9d45f10..8a24692 100644 --- a/src/common/WiFiSettings/__tests__/__snapshots__/WiFiSettings.test.js.snap +++ b/src/common/WiFiSettings/__tests__/__snapshots__/WiFiSettings.test.js.snap @@ -5,7 +5,7 @@ exports[` Snapshot 2.4 GHz 1`] = ` - First value + Second value -@@ -245,207 +245,95 @@ +@@ -250,207 +250,95 @@ value=\\"0\\" > auto @@ -336,7 +336,7 @@ exports[` Snapshot guest network. 1`] = ` - First value + Second value -@@ -474,10 +474,89 @@ +@@ -479,10 +479,94 @@ Parameters of the guest network can be set in the Guest network tab. @@ -375,6 +375,11 @@ exports[` Snapshot guest network. 1`] = ` + + + ++ ++ SSID which contains non-standard characters could cause problems on some devices. ++ + +
Snapshot guest network. 1`] = ` class=\\"form-group switch\\" >
@@ -445,7 +450,7 @@ exports[` Snapshot one module enabled. 1`] = ` - First value + Second value -@@ -22,10 +22,462 @@ +@@ -22,10 +22,467 @@ Wi-Fi 1 @@ -485,6 +490,11 @@ exports[` Snapshot one module enabled. 1`] = ` + +
+
++ ++ SSID which contains non-standard characters could cause problems on some devices. ++ + +