Skip to content

Commit 21efd0c

Browse files
Merge pull request #390 from SixLabors/js/fix-233
Ensure invalid path extensions are skipped when validating input URLs
2 parents e38bc79 + 24a2782 commit 21efd0c

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

samples/ImageSharp.Web.Sample/Pages/Index.cshtml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
</div>
5353
<div>
5454
<p>
55-
<code>sixlabors.imagesharp.web.svg?width=300</code>
55+
<code>sixlabors.imagesharp.web.svg?width=300&format=jpg</code>
5656
</p>
5757
<p>
58-
<img src="sixlabors.imagesharp.web.svg" imagesharp-width="300" />
58+
<img src="sixlabors.imagesharp.web.svg" imagesharp-width="300" imagesharp-format="Format.Jpg" />
5959
</p>
6060
</div>
6161
</section>

src/ImageSharp.Web/FormatUtilities.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ public FormatUtilities(IOptions<ImageSharpMiddlewareOptions> options)
5353
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5454
public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? extension)
5555
{
56+
// Attempts to extract a valid image file extension from the URI.
57+
// If the path contains a recognized extension, it is used.
58+
// If the path lacks an extension and a query string is present,
59+
// the method checks for a valid 'format' parameter as a fallback.
60+
// Returns true if a supported extension is found in either location.
5661
extension = null;
5762
int query = uri.IndexOf('?');
5863
ReadOnlySpan<char> path;
5964

6065
if (query > -1)
6166
{
67+
path = uri.AsSpan(0, query);
68+
6269
if (uri.Contains(FormatWebProcessor.Format, StringComparison.OrdinalIgnoreCase)
6370
&& QueryHelpers.ParseQuery(uri[query..]).TryGetValue(FormatWebProcessor.Format, out StringValues ext))
6471
{
@@ -68,15 +75,13 @@ public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? e
6875
{
6976
if (extSpan.Equals(e, StringComparison.OrdinalIgnoreCase))
7077
{
78+
// We've found a valid extension in the query.
79+
// Now we need to check the path to see if there is a file extension and validate that.
7180
extension = e;
72-
return true;
81+
break;
7382
}
7483
}
75-
76-
return false;
7784
}
78-
79-
path = uri.AsSpan(0, query);
8085
}
8186
else
8287
{
@@ -92,13 +97,17 @@ public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? e
9297
{
9398
if (pathExtension.Equals(e, StringComparison.OrdinalIgnoreCase))
9499
{
95-
extension = e;
100+
// We've found a valid extension in the path, however we do not
101+
// want to overwrite an existing extension.
102+
extension ??= e;
96103
return true;
97104
}
98105
}
106+
107+
return false;
99108
}
100109

101-
return false;
110+
return extension != null;
102111
}
103112

104113
/// <summary>

src/ImageSharp.Web/ImageSharp.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<ItemGroup>
4747
<FrameworkReference Include="Microsoft.AspNetCore.App" />
4848
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.1" />
49-
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.8" />
49+
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.11" />
5050
</ItemGroup>
5151

5252
<Import Project="..\..\shared-infrastructure\src\SharedInfrastructure\SharedInfrastructure.projitems" Label="Shared" />

tests/ImageSharp.Web.Tests/Helpers/FormatUtilitiesTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,16 @@ public void GetExtensionShouldAcknowledgeQueryStringFormatParameter()
4646
}
4747

4848
[Fact]
49-
public void GetExtensionShouldRejectInvalidQueryStringFormatParameter()
49+
public void GetExtensionShouldAllowInvalidQueryStringFormatParameterWithValidExtension()
5050
{
5151
const string uri = "http://www.example.org/some/path/to/image.bmp?width=300&format=invalid";
52+
Assert.True(FormatUtilities.TryGetExtensionFromUri(uri, out _));
53+
}
54+
55+
[Fact]
56+
public void GetExtensionShouldRejectInvalidPathWithValidQueryStringFormatParameter()
57+
{
58+
const string uri = "http://www.example.org/some/path/to/image.svg?width=300&format=jpg";
5259
Assert.False(FormatUtilities.TryGetExtensionFromUri(uri, out _));
5360
}
5461
}

0 commit comments

Comments
 (0)