-
Notifications
You must be signed in to change notification settings - Fork 0
Feature data sorting in route handler #7
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
Changes from all commits
805df37
a2cdb38
47d3b9c
bf35926
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,12 +109,34 @@ Future<Response> _handleGet( | |
final queryParams = context.request.uri.queryParameters; | ||
final startAfterId = queryParams['startAfterId']; | ||
final limitParam = queryParams['limit']; | ||
final sortBy = queryParams['sortBy']; | ||
final sortOrderRaw = queryParams['sortOrder']?.toLowerCase(); | ||
final limit = limitParam != null ? int.tryParse(limitParam) : null; | ||
|
||
SortOrder? sortOrder; | ||
if (sortOrderRaw != null) { | ||
if (sortOrderRaw == 'asc') { | ||
sortOrder = SortOrder.asc; | ||
} else if (sortOrderRaw == 'desc') { | ||
sortOrder = SortOrder.desc; | ||
} else { | ||
throw const BadRequestException( | ||
'Invalid "sortOrder" parameter. Must be "asc" or "desc".', | ||
); | ||
} | ||
} | ||
Comment on lines
+116
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
||
final specificQueryForClient = <String, String>{}; | ||
final Set<String> allowedKeys; | ||
final receivedKeys = queryParams.keys | ||
.where((k) => k != 'model' && k != 'startAfterId' && k != 'limit') | ||
.where( | ||
(k) => | ||
k != 'model' && | ||
k != 'startAfterId' && | ||
k != 'limit' && | ||
k != 'sortBy' && | ||
k != 'sortOrder', | ||
Comment on lines
+133
to
+138
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The chain of
|
||
) | ||
.toSet(); | ||
|
||
switch (modelName) { | ||
|
@@ -207,6 +229,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
Comment on lines
229
to
234
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This While a full refactor might be beyond the scope of this PR, consider exploring ways to reduce this duplication in the future. One approach could be to enhance the |
||
case 'category': | ||
final repo = context.read<HtDataRepository<Category>>(); | ||
|
@@ -215,6 +239,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
case 'source': | ||
final repo = context.read<HtDataRepository<Source>>(); | ||
|
@@ -223,6 +249,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
case 'country': | ||
final repo = context.read<HtDataRepository<Country>>(); | ||
|
@@ -231,6 +259,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
case 'user': | ||
final repo = context.read<HtDataRepository<User>>(); | ||
|
@@ -239,6 +269,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
case 'user_app_settings': | ||
final repo = context.read<HtDataRepository<UserAppSettings>>(); | ||
|
@@ -247,6 +279,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
case 'user_content_preferences': | ||
final repo = context.read<HtDataRepository<UserContentPreferences>>(); | ||
|
@@ -255,6 +289,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
case 'app_config': | ||
final repo = context.read<HtDataRepository<AppConfig>>(); | ||
|
@@ -263,6 +299,8 @@ Future<Response> _handleGet( | |
userId: userIdForRepoCall, | ||
startAfterId: startAfterId, | ||
limit: limit, | ||
sortBy: sortBy, | ||
sortOrder: sortOrder, | ||
); | ||
default: | ||
throw OperationFailedException( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
sortBy
query parameter is passed directly to the data layer without validation. This is a potential security and performance risk, as clients could request sorting on un-indexed or sensitive fields. You should validate thesortBy
value against an allowlist of sortable fields for each model.This validation should be performed within the
switch (modelName)
block, where the context of the model is known.For example, for the
headline
model: