Skip to content

Commit fdf345e

Browse files
committed
Merge pull request #115 from AltSchool/fix/protectfilters
protect against weird and broken query params, e.g. "?" where they sh…
2 parents 0513f09 + 265cca4 commit fdf345e

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

dynamic_rest/filters.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ def _filter_queryset(
527527
raise ValidationError(
528528
dict(e) if hasattr(e, 'error_dict') else list(e)
529529
)
530+
except Exception as e:
531+
# Some other Django error in parsing the filter. Very likely
532+
# a bad query, so throw a ValidationError.
533+
err_msg = getattr(e, 'message', '')
534+
raise ValidationError(err_msg)
530535

531536
# A serializer can have this optional function
532537
# to dynamically apply additional filters on

tests/test_api.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,3 +1491,15 @@ def test_immutable_field(self):
14911491
# ... and it should not have changed:
14921492
self.assertEqual(data['cat']['parent'], parent_id)
14931493
self.assertEqual(data['cat']['name'], kitten_name)
1494+
1495+
1496+
class TestFilters(APITestCase):
1497+
1498+
"""
1499+
Tests for filters.
1500+
"""
1501+
1502+
def testUnparseableInt(self):
1503+
url = '/users/?filter{pk}=123x'
1504+
response = self.client.get(url)
1505+
self.assertEqual(400, response.status_code)

0 commit comments

Comments
 (0)