Skip to content

Commit

Permalink
reject sqids with many numbers (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
rktjump authored Nov 12, 2024
1 parent 1360ba3 commit a8aa1d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django_sqids/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def get_prep_value(self, value):
decoded_values = self.sqids_instance.decode(value)
if not decoded_values:
return None
if len(decoded_values) > 1:
return None
return decoded_values[0]

def from_db_value(self, value, expression, connection, *args):
Expand Down
10 changes: 10 additions & 0 deletions tests/test_django_sqids.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,13 @@ def test_url_manually_with_prefix(client):
assert response.status_code == 200
assert response.context["object"] == instance

def test_sqid_with_many_numbers():
from tests.test_app.models import TestModelWithDifferentConfig
instance = TestModelWithDifferentConfig.objects.create()
sqids_instance = TestModelWithDifferentConfig.sqid.get_sqid_instance()
sqid_single_number = sqids_instance.encode([instance.pk])
sqid_two_numbers = sqids_instance.encode([instance.pk, 42])

assert TestModelWithDifferentConfig.objects.get(sqid=sqid_single_number) == instance
with pytest.raises(TestModelWithDifferentConfig.DoesNotExist):
TestModelWithDifferentConfig.objects.get(sqid=sqid_two_numbers)

0 comments on commit a8aa1d4

Please sign in to comment.