Skip to content

Commit

Permalink
[freeradius] Add max_length validation for called_station_id in PostA…
Browse files Browse the repository at this point in the history
…uthSerializer openwisp#467

Updated the PostAuthSerializer to include a `max_length` attribute of 50 for the `called_station_id` field.
This ensures that requests exceeding the character limit return an HTTP 400 error with an appropriate error message.

Fixes openwisp#467
  • Loading branch information
dee077 committed Dec 15, 2024
1 parent a56b54e commit 71551fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion openwisp_radius/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ class RadiusPostAuthSerializer(serializers.ModelSerializer):
allow_blank=True,
style={'input_type': 'password'},
)
called_station_id = serializers.CharField(required=False, allow_blank=True)
called_station_id = serializers.CharField(
required=False, allow_blank=True, max_length=50
)
calling_station_id = serializers.CharField(required=False, allow_blank=True)

def validate(self, data):
Expand Down
12 changes: 12 additions & 0 deletions openwisp_radius/tests/test_api/test_freeradius_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ def test_postauth_400(self):
self.assertEqual(RadiusPostAuth.objects.all().count(), 0)
self.assertEqual(response.status_code, 400)

def test_postauth_called_station_id_max_length_50_exceed_400(self):
params = {'called_station_id': 'C0-4A-00-EE-D1-0D:' + 'A' * 50}
params = self._get_postauth_params(**params)
response = self.client.post(
reverse('radius:postauth'), params, HTTP_AUTHORIZATION=self.auth_header
)
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data['called_station_id'][0],
'Ensure this field has no more than 50 characters.',
)

@capture_any_output()
def test_postauth_no_token_403(self):
response = self.client.post(reverse('radius:postauth'), {'username': 'tester'})
Expand Down

0 comments on commit 71551fa

Please sign in to comment.