Skip to content

Commit

Permalink
limit ap ssid length
Browse files Browse the repository at this point in the history
  • Loading branch information
ssemtner authored and patfair committed Sep 14, 2024
1 parent 2a31356 commit 33d3cdc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
13 changes: 12 additions & 1 deletion radio/configuration_request_ap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion radio/configuration_request_ap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand All @@ -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)")
Expand Down

0 comments on commit 33d3cdc

Please sign in to comment.