-
Notifications
You must be signed in to change notification settings - Fork 354
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
fix: reject invalid order parameter values DHIS2-16935 #16673
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #16673 +/- ##
=========================================
Coverage 66.92% 66.92%
- Complexity 32035 32036 +1
=========================================
Files 3552 3550 -2
Lines 131537 131554 +17
Branches 15315 15321 +6
=========================================
+ Hits 88031 88043 +12
- Misses 36346 36348 +2
- Partials 7160 7163 +3
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 5 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
9283e71
to
26b8b2f
Compare
Rename OrderCriteria factory to valueOf so its used by Springs conversion system. The OrderCriteriaParamEditor is not needed. In fact its even causing the issue that "order=createdAt:desc,event:asc" is not split by Spring and passed as is to the PropertyEditor. Whatever mechansim is used to convert Spring suggests throwing an IllegalArgumentException in case conversion failed. We returned null which lead to successful conversions with null elements
instead of strings. Add tests for passing comma separated values in one order param. Add test for the mixed case of comma separated values in one order param with a repeated param
as it has a contructor from String -> UID its automatically picked up by Spring
we need to update this to the new tracker mechanism. Not sure if these workinglists are also used by old tracker though
Quality Gate passedIssues Measures |
Fixes issue introduced in #15803. Also fixes issues we had before that. https://dhis2.atlassian.net/browse/DHIS2-16935 has a table with pre/post #15803 behaviors.
We now support
order
parameter like inorder=createdAt:desc,event:asc
order
parameters with a value each like inorder=createdAt:desc&order=event:asc
and reject
order=createdAt:desc,event:asc&order=completedAt:asc
Same is true for UID.
Tests
Changes
To OrderCriteria
OrderCriteria
factory tovalueOf
so it's used by Springs conversion systemOrderCriteriaParamEditor
as it's not needed if we have a constructor/factory for String -> target type. In fact its even causing the issue that"order=createdAt:desc,event:asc"
is not split by Spring and passed as is to thePropertyEditor
IllegalArgumentException
in case conversion failed as suggested by Spring. We returnednull
which lead to successful conversions with null elements. Thenull
element lead to the HTTP 500 in the endorder=createdAt,,scheduledAt:desc
to prevent any issues downstreamConversionException
and try to identify the mixed case to report a meaningful errorTo UID
PropertyEditor
for UID as it has a constructor taking a String to UIDError cases
(
GET {{PROTOCOL}}://{{AUTH}}@{{HOST}}/api/tracker/events?fields=event&order=createdAt:desc&order=event:wrong
)(
GET {{PROTOCOL}}://{{AUTH}}@{{HOST}}/api/tracker/events?fields=event&order=createdAt:desc&order=:desc
)order
request parameter with comma-separated values within at least one leads to(
GET {{PROTOCOL}}://{{AUTH}}@{{HOST}}/api/tracker/events?fields=event&order=createdAt:desc,event:asc&order=completedAt:asc
):
separators(
GET {{PROTOCOL}}://{{AUTH}}@{{HOST}}/api/tracker/events?fields=event&order=createdAt:desc&order=event:asc:some
)Spring Type Conversion
This describes how it worked after #15803. The behavior before was different as we used a converter for
String
toList<OrderCriteria>
. This was also not without its flaws as you can see in https://dhis2.atlassian.net/browse/DHIS2-16935.A request parameter like
order=createdAt,event:desc
that has comma separated values and is not repeated will be split byhttps://github.com/spring-projects/spring-framework/blob/d81619addd6d9060c9d8dc454c359054e517ed98/spring-core/src/main/java/org/springframework/core/convert/support/StringToCollectionConverter.java#L68
Each element will then be converted by Springs' conversion service.
Since Spring knows the target class of
OrderCriteria
it looks for a constructor or factory mapping a String to the target typeOrderCriteria
.Here are the factories it is looking for
https://github.com/spring-projects/spring-framework/blob/d81619addd6d9060c9d8dc454c359054e517ed98/spring-core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java#L191-L197
If there is no such method or if instantiation fails it will call a registered
PropertyEditor
for the target type.Since
OrderCriteria
did not have a suitable method for String ->OrderCriteria
Spring calledOrderCriteriaParamEditor
with the entire StringcreatedAt,event:desc
.OrderCriteriaParamEditor
calls a factory for String ->OrderCriteria
which can only handle a single order value. AFAIK aPropertyEditor
is also expected to turn a String into one instance of a type.https://github.com/dhis2/dhis2-core/pull/15803/files#diff-d6b509407da006d686ce32cd61fc18a99968ccb8f248bc3b3413cd9de29b9954L41-R36
Repeated parameters are treated differently by Spring.
order=FV4JCI73wO2,oosW4rSH7CM&order=LKQXmteRtvQ,sxPParLSimC
would be represented as String[] {"FV4JCI73wO2,oosW4rSH7CM", "LKQXmteRtvQ,sxPParLSimC"}. ArrayToCollectionConverter converts theString[]
of into List/Set/... of the target type. Here Strings with comma-separated values will not be split!Related Spring issues
Why escaped comma is the delimiter for
@RequestParam List<String>
? spring-projects/spring-framework#23820Why escaped comma is the delimiter for
@RequestParam List<String>
? spring-projects/spring-framework#23820 (comment)