Skip to content

Commit 2842b11

Browse files
Ensure invalid path extensions are skipped. Fix #233
1 parent e38bc79 commit 2842b11

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
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: 13 additions & 6 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
{
@@ -96,9 +101,11 @@ public bool TryGetExtensionFromUri(string uri, [NotNullWhen(true)] out string? e
96101
return true;
97102
}
98103
}
104+
105+
return false;
99106
}
100107

101-
return false;
108+
return extension != null;
102109
}
103110

104111
/// <summary>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ public void GetExtensionShouldRejectInvalidQueryStringFormatParameter()
5151
const string uri = "http://www.example.org/some/path/to/image.bmp?width=300&format=invalid";
5252
Assert.False(FormatUtilities.TryGetExtensionFromUri(uri, out _));
5353
}
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";
59+
Assert.False(FormatUtilities.TryGetExtensionFromUri(uri, out _));
60+
}
5461
}

0 commit comments

Comments
 (0)