Skip to content

Commit

Permalink
Merge pull request #829 from dlcs/feature/prevent_zero_size
Browse files Browse the repository at this point in the history
Prevent 0 size image request
  • Loading branch information
donaldgray committed Apr 25, 2024
2 parents 5e5d943 + 927a018 commit 9b11e82
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text.Json.Nodes;
using System.Threading;
using DLCS.Core.Types;
using DLCS.Model.Assets.CustomHeaders;
Expand All @@ -17,6 +18,7 @@
using Orchestrator.Infrastructure.Auth;
using Orchestrator.Infrastructure.ReverseProxy;
using Orchestrator.Settings;
using Test.Helpers.Data;
using Version = IIIF.ImageApi.Version;

namespace Orchestrator.Tests.Features.Images;
Expand Down Expand Up @@ -126,6 +128,31 @@ public async Task HandleRequest_Returns400_IfAssetPathParserThrowsException()
.Which.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Theory]
[InlineData("0,")]
[InlineData(",0")]
[InlineData("!0,0")]
[InlineData("20,0")]
[InlineData("0,20")]
public async Task HandleRequest_Returns400_IfInvalidSize(string size)
{
// Arrange
var id = AssetIdGenerator.GetAssetId();

// Act
var context = new DefaultHttpContext();
context.Request.Path = $"/iiif-img/{id}/full/{size}/0/default.jpg";

var sut = GetImageRequestHandlerWithMockPathParser();

// Act
var result = await sut.HandleRequest(context);

// Assert
result.Should().BeOfType<StatusCodeResult>()
.Which.StatusCode.Should().Be(HttpStatusCode.BadRequest);
}

[Theory]
[InlineData(AvailableDeliveryChannel.File)]
[InlineData(AvailableDeliveryChannel.Timebased)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1606,9 +1606,9 @@ public async Task Get_Returns500_IfRedirectsImageServer_ButOrchestratorError()
}

[Theory]
[InlineData("/info.json")]
[InlineData("/full/max/0/default.jpg")]
[InlineData("/0,0,1000,1000/200,200/0/default.jpg")]
[InlineData("info.json")]
[InlineData("full/max/0/default.jpg")]
[InlineData("0,0,1000,1000/200,200/0/default.jpg")]
public async Task Get_404_IfNotForDelivery(string path)
{
// Arrange
Expand All @@ -1630,9 +1630,9 @@ public async Task Get_404_IfNotForDelivery(string path)
}

[Theory]
[InlineData("/info.json")]
[InlineData("/full/max/0/default.jpg")]
[InlineData("/0,0,1000,1000/200,200/0/default.jpg")]
[InlineData("info.json")]
[InlineData("full/max/0/default.jpg")]
[InlineData("0,0,1000,1000/200,200/0/default.jpg")]
public async Task Get_404_IfNotForImageDeliveryChannel(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public async Task<IProxyActionResult> HandleRequest(HttpContext httpContext)
{
return new StatusCodeResult(statusCode ?? HttpStatusCode.InternalServerError);
}

if (!IsSizeValid(assetRequest.IIIFImageRequest.Size))
{
logger.LogDebug("Request for {Path}: invalid size", httpContext.Request.Path);
return new StatusCodeResult(HttpStatusCode.BadRequest);
}

var orchestrationImage = await assetRequestProcessor.GetAsset<OrchestrationImage>(httpContext, assetRequest);
if (orchestrationImage == null)
Expand Down Expand Up @@ -105,6 +111,8 @@ public async Task<IProxyActionResult> HandleRequest(HttpContext httpContext)
return proxyActionResult;
}

private bool IsSizeValid(SizeParameter size) => (size.Width ?? 1) > 0 && (size.Height ?? 1) > 0;

private async Task<IProxyActionResult> HandleRequestInternal(HttpContext httpContext,
OrchestrationImage orchestrationImage, ImageAssetDeliveryRequest assetRequest)
{
Expand Down

0 comments on commit 9b11e82

Please sign in to comment.