From 33d3cdc62661d4c15d445784cbcf3649ceae9eba Mon Sep 17 00:00:00 2001 From: Scott Semtner Date: Fri, 13 Sep 2024 14:48:24 -0700 Subject: [PATCH] limit ap ssid length --- radio/configuration_request_ap.go | 13 ++++++++++++- radio/configuration_request_ap_test.go | 9 ++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/radio/configuration_request_ap.go b/radio/configuration_request_ap.go index ceceb8c..ea33331 100644 --- a/radio/configuration_request_ap.go +++ b/radio/configuration_request_ap.go @@ -9,7 +9,10 @@ import ( "regexp" ) -const stationSsidRegex = "^[a-zA-Z0-9-]*$" +const ( + maxStationSsidLength = 14 + stationSsidRegex = "^[a-zA-Z0-9-]*$" +) // ConfigurationRequest represents a JSON request to configure the radio. type ConfigurationRequest struct { @@ -112,6 +115,14 @@ func (request ConfigurationRequest) Validate(radio *Radio) error { if stationConfiguration.Ssid == "" { return fmt.Errorf("SSID for station %s cannot be blank", stationName) } + if len(stationConfiguration.Ssid) > maxStationSsidLength { + return fmt.Errorf( + "invalid SSID length for station %s: %d (expecting 1-%d)", + stationName, + len(stationConfiguration.Ssid), + maxStationSsidLength, + ) + } if !regexp.MustCompile(stationSsidRegex).MatchString(stationConfiguration.Ssid) { return fmt.Errorf("invalid SSID for station %s (expecting alphanumeric with hyphens)", stationName) } diff --git a/radio/configuration_request_ap_test.go b/radio/configuration_request_ap_test.go index 26cca5d..a650036 100644 --- a/radio/configuration_request_ap_test.go +++ b/radio/configuration_request_ap_test.go @@ -68,6 +68,13 @@ func TestConfigurationRequest_Validate(t *testing.T) { err = request.Validate(linksysRadio) assert.EqualError(t, err, "SSID for station blue1 cannot be blank") + // Too-long SSID. + request = ConfigurationRequest{ + StationConfigurations: map[string]StationConfiguration{"blue1": {Ssid: "12345-longsuffix", WpaKey: "12345678"}}, + } + err = request.Validate(linksysRadio) + assert.EqualError(t, err, "invalid SSID length for station blue1: 16 (expecting 1-14)") + // Invalid characters in SSID. request = ConfigurationRequest{ StationConfigurations: map[string]StationConfiguration{"blue1": {Ssid: "abc_XYZ", WpaKey: "12345678"}}, @@ -77,7 +84,7 @@ func TestConfigurationRequest_Validate(t *testing.T) { // Too-short WPA key. request = ConfigurationRequest{ - StationConfigurations: map[string]StationConfiguration{"blue1": {Ssid: "254-suffix", WpaKey: "1234567"}}, + StationConfigurations: map[string]StationConfiguration{"blue1": {Ssid: "12345-suffix", WpaKey: "1234567"}}, } err = request.Validate(linksysRadio) assert.EqualError(t, err, "invalid WPA key length for station blue1: 7 (expecting 8-16)")