Merge pull request #12827 from snipe/fixes/tighter_handling_of_offset_and_limit

Better handling for weird data passed to the API via limit and offset
This commit is contained in:
snipe 2023-04-15 16:52:33 -07:00 committed by GitHub
commit a97b2d141d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 66 additions and 110 deletions

View file

@ -80,12 +80,9 @@ class AccessoriesController extends Controller
$accessories->where('notes','=',$request->input('notes')); $accessories->where('notes','=',$request->input('notes'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $accessories->count()) ? $accessories->count() : intval(request('offset'));
$offset = (($accessories) && ($request->get('offset') > $accessories->count())) ? $accessories->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort_override = $request->input('sort'); $sort_override = $request->input('sort');

View file

@ -55,12 +55,9 @@ class AssetMaintenancesController extends Controller
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $maintenances->count()) ? $maintenances->count() : intval(request('offset'));
$offset = (($maintenances) && ($request->get('offset') > $maintenances->count())) ? $maintenances->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$allowed_columns = [ $allowed_columns = [
'id', 'id',

View file

@ -78,12 +78,9 @@ class AssetModelsController extends Controller
$assetmodels->TextSearch($request->input('search')); $assetmodels->TextSearch($request->input('search'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $assetmodels->count()) ? $assetmodels->count() : intval(request('offset'));
$offset = (($assetmodels) && ($request->get('offset') > $assetmodels->count())) ? $assetmodels->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'models.created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'models.created_at';

View file

@ -199,13 +199,9 @@ class AssetsController extends Controller
$request->filled('order_number') ? $assets = $assets->where('assets.order_number', '=', e($request->get('order_number'))) : ''; $request->filled('order_number') ? $assets = $assets->where('assets.order_number', '=', e($request->get('order_number'))) : '';
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $assets->count()) ? $assets->count() : intval(request('offset'));
$offset = (($assets) && ($request->get('offset') > $assets->count())) ? $assets->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';

View file

@ -95,12 +95,9 @@ class CategoriesController extends Controller
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $categories->count()) ? $categories->count() : intval(request('offset'));
$offset = (($categories) && ($request->get('offset') > $categories->count())) ? $categories->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'assets_count'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'assets_count';

View file

@ -48,12 +48,9 @@ class CompaniesController extends Controller
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $companies->count()) ? $companies->count() : intval(request('offset'));
$offset = (($companies) && ($request->get('offset') > $companies->count())) ? $companies->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -72,12 +72,9 @@ class ComponentsController extends Controller
$components->where('notes','=',$request->input('notes')); $components->where('notes','=',$request->input('notes'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $components->count()) ? $components->count() : intval(request('offset'));
$offset = (($components) && ($request->get('offset') > $components->count())) ? $components->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';

View file

@ -84,12 +84,9 @@ class ConsumablesController extends Controller
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $consumables->count()) ? $consumables->count() : intval(request('offset'));
$offset = (($consumables) && ($request->get('offset') > $consumables->count())) ? $consumables->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$allowed_columns = ['id', 'name', 'order_number', 'min_amt', 'purchase_date', 'purchase_cost', 'company', 'category', 'model_number', 'item_no', 'manufacturer', 'location', 'qty', 'image']; $allowed_columns = ['id', 'name', 'order_number', 'min_amt', 'purchase_date', 'purchase_cost', 'company', 'category', 'model_number', 'item_no', 'manufacturer', 'location', 'qty', 'image'];
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';

View file

@ -58,12 +58,9 @@ class DepartmentsController extends Controller
$departments->where('location_id', '=', $request->input('location_id')); $departments->where('location_id', '=', $request->input('location_id'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $departments->count()) ? $departments->count() : intval(request('offset'));
$offset = (($departments) && ($request->get('offset') > $departments->count())) ? $departments->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -28,12 +28,9 @@ class DepreciationsController extends Controller
$depreciations = $depreciations->TextSearch($request->input('search')); $depreciations = $depreciations->TextSearch($request->input('search'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $depreciations->count()) ? $depreciations->count() : intval(request('offset'));
$offset = (($depreciations) && ($request->get('offset') > $depreciations->count())) ? $depreciations->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -35,12 +35,9 @@ class GroupsController extends Controller
$groups->where('name', '=', $request->input('name')); $groups->where('name', '=', $request->input('name'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $groups->count()) ? $groups->count() : intval(request('offset'));
$offset = (($groups) && ($request->get('offset') > $groups->count())) ? $groups->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -39,8 +39,11 @@ class LicenseSeatsController extends Controller
} }
$total = $seats->count(); $total = $seats->count();
$offset = (($seats) && (request('offset') >= $total)) ? 0 : request('offset', 0);
$limit = request('limit', 50); // Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $seats->count()) ? $seats->count() : intval(request('offset'));
$limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
$seats = $seats->skip($offset)->take($limit)->get(); $seats = $seats->skip($offset)->take($limit)->get();

View file

@ -94,12 +94,9 @@ class LicensesController extends Controller
$licenses->onlyTrashed(); $licenses->onlyTrashed();
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $licenses->count()) ? $licenses->count() : intval(request('offset'));
$offset = (($licenses) && ($request->get('offset') > $licenses->count())) ? $licenses->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';

View file

@ -78,14 +78,14 @@ class LocationsController extends Controller
$locations->where('locations.country', '=', $request->input('country')); $locations->where('locations.country', '=', $request->input('country'));
} }
$offset = (($locations) && (request('offset') > $locations->count())) ? $locations->count() : request('offset', 0); // Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $locations->count()) ? $locations->count() : intval(request('offset'));
// Check to make sure the limit is not higher than the max allowed $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
switch ($request->input('sort')) { switch ($request->input('sort')) {
case 'parent': case 'parent':
$locations->OrderParent($order); $locations->OrderParent($order);

View file

@ -57,12 +57,9 @@ class ManufacturersController extends Controller
$manufacturers->where('support_email', '=', $request->input('support_email')); $manufacturers->where('support_email', '=', $request->input('support_email'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $manufacturers->count()) ? $manufacturers->count() : intval(request('offset'));
$offset = (($manufacturers) && ($request->get('offset') > $manufacturers->count())) ? $manufacturers->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -29,8 +29,10 @@ class PredefinedKitsController extends Controller
$kits = $kits->TextSearch($request->input('search')); $kits = $kits->TextSearch($request->input('search'));
} }
$offset = $request->input('offset', 0); // Make sure the offset and limit are actually integers and do not exceed system limits
$limit = $request->input('limit', 50); $offset = ($request->input('offset') > $kits->count()) ? $kits->count() : intval(request('offset'));
$limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
$order = $request->input('order') === 'desc' ? 'desc' : 'asc'; $order = $request->input('order') === 'desc' ? 'desc' : 'asc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'name'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'name';
$kits->orderBy($sort, $order); $kits->orderBy($sort, $order);

View file

@ -54,15 +54,15 @@ class ReportsController extends Controller
'note', 'note',
]; ];
// Make sure the offset and limit are actually integers and do not exceed system limits
$offset = ($request->input('offset') > $actionlogs->count()) ? $actionlogs->count() : intval(request('offset'));
$limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'created_at';
$order = ($request->input('order') == 'asc') ? 'asc' : 'desc'; $order = ($request->input('order') == 'asc') ? 'asc' : 'desc';
$offset = request('offset', 0);
$total = $actionlogs->count(); $total = $actionlogs->count();
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$actionlogs = $actionlogs->orderBy($sort, $order)->skip($offset)->take($limit)->get(); $actionlogs = $actionlogs->orderBy($sort, $order)->skip($offset)->take($limit)->get();
return response()->json((new ActionlogsTransformer)->transformActionlogs($actionlogs, $total), 200, ['Content-Type' => 'application/json;charset=utf8'], JSON_UNESCAPED_UNICODE); return response()->json((new ActionlogsTransformer)->transformActionlogs($actionlogs, $total), 200, ['Content-Type' => 'application/json;charset=utf8'], JSON_UNESCAPED_UNICODE);

View file

@ -50,12 +50,9 @@ class StatuslabelsController extends Controller
} }
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $statuslabels->count()) ? $statuslabels->count() : intval(request('offset'));
$offset = (($statuslabels) && ($request->get('offset') > $statuslabels->count())) ? $statuslabels->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -74,12 +74,9 @@ class SuppliersController extends Controller
$suppliers->where('notes', '=', $request->input('notes')); $suppliers->where('notes', '=', $request->input('notes'));
} }
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $suppliers->count()) ? $suppliers->count() : intval(request('offset'));
$offset = (($suppliers) && ($request->get('offset') > $suppliers->count())) ? $suppliers->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';

View file

@ -193,12 +193,9 @@ class UsersController extends Controller
$order = $request->input('order') === 'asc' ? 'asc' : 'desc'; $order = $request->input('order') === 'asc' ? 'asc' : 'desc';
// Set the offset to the API call's offset, unless the offset is higher than the actual count of items in which // Make sure the offset and limit are actually integers and do not exceed system limits
// case we override with the actual count, so we should return 0 items. $offset = ($request->input('offset') > $users->count()) ? $users->count() : intval(request('offset'));
$offset = (($users) && ($request->get('offset') > $users->count())) ? $users->count() : $request->get('offset', 0); $limit = ($request->input('limit') > config('app.max_results')) ? config('app.max_results') : max(intval(request('offset')), config('app.max_results'));
// Check to make sure the limit is not higher than the max allowed
((config('app.max_results') >= $request->input('limit')) && ($request->filled('limit'))) ? $limit = $request->input('limit') : $limit = config('app.max_results');
switch ($request->input('sort')) { switch ($request->input('sort')) {