-
Notifications
You must be signed in to change notification settings - Fork 300
query validation filter #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sliverc
merged 30 commits into
django-json-api:master
from
n2ygk:JSONAPIQueryValidationFilter
Sep 19, 2018
+130
−22
Merged
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
7b29f36
initial integration of JSONAPIDjangoFilter
n2ygk dc5ca38
documentation, isort, flake8
n2ygk 6b0dc8c
Forgot to add django_filters to installed_apps
n2ygk d4fbf24
backwards compatibility for py27 + django-filter
n2ygk d86d217
handle optional django-filter package
n2ygk 83c4cc0
fix travis to match new TOXENVs due to django-filter
n2ygk f5792c1
fixed a typo
n2ygk cbc9d55
add a warning if django-filter is missing and JSONAPIDjangoFilter is …
n2ygk 2f6ba1d
JSONAPIQueryValidationFilter implementation
n2ygk 4f2b75b
improve filter_regex
n2ygk 48b4c51
Merge branch 'JSONAPIDjangoFilter' into JSONAPIQueryValidationFilter
n2ygk 2742d60
rename tests from filter to param
n2ygk 6a8d7ae
easy changes recommended by @sliverc review
n2ygk db9e1f9
resolve @sliverc review method of using optional django-filter.
n2ygk 68f5e02
Merge branch 'JSONAPIDjangoFilter' into JSONAPIQueryValidationFilter
n2ygk f0bdbd4
Merge branch 'master' into JSONAPIQueryValidationFilter
n2ygk 64d4af0
remove JSONAPI prefix per #471
n2ygk 23616a2
inadvertently removed when merging master
n2ygk 2c476d9
add QueryValidation filter to NonPaginatedEntryViewset to avoid break…
n2ygk 9b5ab9d
flake8
n2ygk dbd3d32
100% test coverage for QueryParamaterValidationFilter
n2ygk 11aaf06
move QueryValidationFilter earlier and document how to extend query_r…
n2ygk 57e95cc
QueryValidationFilter to README
n2ygk a22ca21
py2.7 fix for a non-ASCII quotation mark
n2ygk 0252096
ugh I added back this junk file by mistake again
n2ygk 9e715fa
Change "invalid filter" to "invalid query parameter" for malformed fi…
n2ygk af10543
renamed to QueryParameterValidationFilter to be clear that this is qu…
n2ygk c928d72
clearer language
n2ygk 6e008ad
flake8 line length after renaming the class
n2ygk eed8133
Merge branch 'master' into JSONAPIQueryValidationFilter
sliverc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .sort import OrderingFilter # noqa: F401 | ||
from .queryvalidation import QueryValidationFilter # noqa: F401 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import re | ||
|
||
from rest_framework.exceptions import ValidationError | ||
from rest_framework.filters import BaseFilterBackend | ||
|
||
|
||
class QueryValidationFilter(BaseFilterBackend): | ||
n2ygk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
A backend filter that performs strict validation of query parameters for | ||
jsonapi spec conformance and raises a 400 error if non-conforming usage is | ||
found. | ||
|
||
If you want to add some additional non-standard query parameters, | ||
simply override `.query_regex` adding the new parameters but, "with the additional | ||
requirement that they MUST contain contain at least one non a-z character (U+0061 to U+007A). | ||
n2ygk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
It is RECOMMENDED that a U+002D HYPHEN-MINUS, "-", U+005F LOW LINE, "_", or capital letter is | ||
used (e.g. camelCasing)." -- http://jsonapi.org/format/#query-parameters | ||
""" | ||
#: compiled regex that matches the allowed http://jsonapi.org/format/#query-parameters | ||
#: `sort` and `include` stand alone; `filter`, `fields`, and `page` have []'s | ||
query_regex = re.compile(r'^(sort|include)$|^(filter|fields|page)(\[[\w\.\-]+\])?$') | ||
|
||
def validate_query_params(self, request): | ||
""" | ||
Validate that query params are in the list of valid query keywords | ||
Raises ValidationError if not. | ||
""" | ||
# TODO: For jsonapi error object conformance, must set jsonapi errors "parameter" for | ||
# the ValidationError. This requires extending DRF/DJA Exceptions. | ||
for qp in request.query_params.keys(): | ||
if not self.query_regex.match(qp): | ||
raise ValidationError('invalid query parameter: {}'.format(qp)) | ||
if len(request.query_params.getlist(qp)) > 1: | ||
raise ValidationError( | ||
'repeated query parameter not allowed: {}'.format(qp)) | ||
|
||
def filter_queryset(self, request, queryset, view): | ||
self.validate_query_params(request) | ||
return queryset |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.