mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-03-05 13:40:08 -05:00
Guard against using invalid sort keys
This commit is contained in:
@@ -110,7 +110,6 @@ export const defaultState = {
|
||||
{
|
||||
name: 'actions',
|
||||
columnLabel: () => translate('Actions'),
|
||||
isSortable: true,
|
||||
isVisible: true,
|
||||
isModifiable: false
|
||||
}
|
||||
|
||||
@@ -25,7 +25,13 @@ namespace Prowlarr.Api.V1.History
|
||||
public PagingResource<HistoryResource> GetHistory([FromQuery] PagingRequestResource paging, [FromQuery(Name = "eventType")] int[] eventTypes, bool? successful, string downloadId, [FromQuery] int[] indexerIds = null)
|
||||
{
|
||||
var pagingResource = new PagingResource<HistoryResource>(paging);
|
||||
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, NzbDrone.Core.History.History>("date", SortDirection.Descending);
|
||||
var pagingSpec = pagingResource.MapToPagingSpec<HistoryResource, NzbDrone.Core.History.History>(
|
||||
new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"date"
|
||||
},
|
||||
"date",
|
||||
SortDirection.Descending);
|
||||
|
||||
if (eventTypes != null && eventTypes.Any())
|
||||
{
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Configuration;
|
||||
@@ -29,7 +31,11 @@ namespace Prowlarr.Api.V1.Logs
|
||||
}
|
||||
|
||||
var pagingResource = new PagingResource<LogResource>(paging);
|
||||
var pageSpec = pagingResource.MapToPagingSpec<LogResource, Log>();
|
||||
var pageSpec = pagingResource.MapToPagingSpec<LogResource, Log>(new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
"id",
|
||||
"time"
|
||||
});
|
||||
|
||||
if (pageSpec.SortKey == "time")
|
||||
{
|
||||
|
||||
@@ -38,7 +38,11 @@ namespace Prowlarr.Http
|
||||
|
||||
public static class PagingResourceMapper
|
||||
{
|
||||
public static PagingSpec<TModel> MapToPagingSpec<TResource, TModel>(this PagingResource<TResource> pagingResource, string defaultSortKey = "Id", SortDirection defaultSortDirection = SortDirection.Ascending)
|
||||
public static PagingSpec<TModel> MapToPagingSpec<TResource, TModel>(
|
||||
this PagingResource<TResource> pagingResource,
|
||||
HashSet<string> allowedSortKeys,
|
||||
string defaultSortKey = "id",
|
||||
SortDirection defaultSortDirection = SortDirection.Ascending)
|
||||
{
|
||||
var pagingSpec = new PagingSpec<TModel>
|
||||
{
|
||||
@@ -48,14 +52,15 @@ namespace Prowlarr.Http
|
||||
SortDirection = pagingResource.SortDirection,
|
||||
};
|
||||
|
||||
if (pagingResource.SortKey == null)
|
||||
{
|
||||
pagingSpec.SortKey = defaultSortKey;
|
||||
if (pagingResource.SortDirection == SortDirection.Default)
|
||||
{
|
||||
pagingSpec.SortDirection = defaultSortDirection;
|
||||
}
|
||||
}
|
||||
pagingSpec.SortKey = pagingResource.SortKey != null &&
|
||||
allowedSortKeys is { Count: > 0 } &&
|
||||
allowedSortKeys.Contains(pagingResource.SortKey)
|
||||
? pagingResource.SortKey
|
||||
: defaultSortKey;
|
||||
|
||||
pagingSpec.SortDirection = pagingResource.SortDirection == SortDirection.Default
|
||||
? defaultSortDirection
|
||||
: pagingResource.SortDirection;
|
||||
|
||||
return pagingSpec;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user