Guard against using invalid sort keys

pull/2229/head v1.24.2.4749
Bogdan 5 months ago
parent ba002a7a4a
commit 97d1384726

@ -110,7 +110,6 @@ export const defaultState = {
{ {
name: 'actions', name: 'actions',
columnLabel: () => translate('Actions'), columnLabel: () => translate('Actions'),
isSortable: true,
isVisible: true, isVisible: true,
isModifiable: false 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) 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 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()) if (eventTypes != null && eventTypes.Any())
{ {

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -29,7 +31,11 @@ namespace Prowlarr.Api.V1.Logs
} }
var pagingResource = new PagingResource<LogResource>(paging); 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") if (pageSpec.SortKey == "time")
{ {

@ -38,7 +38,11 @@ namespace Prowlarr.Http
public static class PagingResourceMapper 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> var pagingSpec = new PagingSpec<TModel>
{ {
@ -48,14 +52,15 @@ namespace Prowlarr.Http
SortDirection = pagingResource.SortDirection, SortDirection = pagingResource.SortDirection,
}; };
if (pagingResource.SortKey == null) pagingSpec.SortKey = pagingResource.SortKey != null &&
{ allowedSortKeys is { Count: > 0 } &&
pagingSpec.SortKey = defaultSortKey; allowedSortKeys.Contains(pagingResource.SortKey)
if (pagingResource.SortDirection == SortDirection.Default) ? pagingResource.SortKey
{ : defaultSortKey;
pagingSpec.SortDirection = defaultSortDirection;
} pagingSpec.SortDirection = pagingResource.SortDirection == SortDirection.Default
} ? defaultSortDirection
: pagingResource.SortDirection;
return pagingSpec; return pagingSpec;
} }

Loading…
Cancel
Save