Skip to content

Commit

Permalink
Update to latest refs and add Web docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Apr 23, 2022
1 parent 32bb931 commit 744dd10
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
55 changes: 54 additions & 1 deletion articles/imagesharp.web/gettingstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,58 @@ services.AddImageSharp()
options.CacheFolder = "different-cache";
});
```


>[!IMPORTANT]
>ImageSharp.Web v2.0.0 contains breaking changes to caching which require additional configuration for v1.x installs.
With ImageSharp.Web v2.0.0 a new concept @SixLabors.ImageSharp.Web.Caching.ICacheKey was introduced to allow greater flexibility when generating cached file names. To preserve the v1.x cache format users must configure two settings:

1. @SixLabors.ImageSharp.Web.Caching.ICacheKey should be configured to use @SixLabors.ImageSharp.Web.Caching.LegacyV1CacheKey
2. @SixLabors.ImageSharp.Web.Caching.PhysicalFileSystemCacheOptions.CacheFolderDepth should be configured to use the same value as @SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddlewareOptions.CacheHashLength - Default `12`.

A complete configuration sample allowing the replication of legacy v1.x behavior can be found below:

```c#
services.AddImageSharp(options =>
{
// Set to previous default value of CachedNameLength
options.CacheHashLength = 12;

// Use the same command parsing as v1.x
options.OnParseCommandsAsync = c =>
{
if (c.Commands.Count == 0)
{
return Task.CompletedTask;
}

// It's a good idea to have this to provide very basic security.
// We can safely use the static resize processor properties.
uint width = c.Parser.ParseValue<uint>(
c.Commands.GetValueOrDefault(ResizeWebProcessor.Width),
c.Culture);

uint height = c.Parser.ParseValue<uint>(
c.Commands.GetValueOrDefault(ResizeWebProcessor.Height),
c.Culture);

if (width > 4000 && height > 4000)
{
c.Commands.Remove(ResizeWebProcessor.Width);
c.Commands.Remove(ResizeWebProcessor.Height);
}

return Task.CompletedTask;
});
})
.Configure<PhysicalFileSystemCacheOptions>(options =>
{
// Ensure this value is the same as CacheHashLength to generate a backwards-compatible cache folder structure
options.CacheFolderDepth = 12;
})
.SetCacheKey<LegacyV1CacheKey>()
.ClearProviders()
.AddProvider<WebRootImageProvider>();
```

Full Configuration API options are available [here](xref:SixLabors.ImageSharp.Web.DependencyInjection.ImageSharpBuilderExtensions).
1 change: 0 additions & 1 deletion articles/imagesharp.web/imagecaches.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ The @"SixLabors.ImageSharp.Web.Caching.PhysicalFileSystemCache", by default, sto

Images are cached in separate folders based upon a hash of the request URL. this allows the caching of millions of image files without slowing down the file system.


### AzureBlobStorageImageCache

This cache allows the caching of image files using [Azure Blob Storage](https://docs.microsoft.com/en-us/azure/storage/blobs/) and is available as an external package installable via [NuGet](https://www.nuget.org/packages/SixLabors.ImageSharp.Web.Providers.Azure)
Expand Down
31 changes: 31 additions & 0 deletions articles/imagesharp.web/processingcommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,34 @@ Allows the changing of the background color of transparent images.
{PATH_TO_YOUR_IMAGE}?bgcolor=128,64,32
{PATH_TO_YOUR_IMAGE}?bgcolor=128,64,32,16
```

## Securing Processing Commands

With ImageSharp.Web it is possible to configure an action to generate an HMAC by setting the @SixLabors.ImageSharp.Web.Middleware.ImageSharpMiddlewareOptions.HMACSecretKey property to any byte array value. This triggers checks in the middleware to look for and compare a HMAC hash of the request URL with the hash that is passed alongside the commands.

In cryptography, an HMAC (sometimes expanded as either keyed-hash message authentication code or hash-based message authentication code) is a specific type of message authentication code (MAC) involving a cryptographic hash function and a secret cryptographic key. As with any MAC, it may be used to simultaneously verify both the data integrity and authenticity of a message.

HMAC can provide authentication using a shared secret instead of using digital signatures with asymmetric cryptography. It trades off the need for a complex public key infrastructure by delegating the key exchange to the communicating parties, who are responsible for establishing and using a trusted channel to agree on the key prior to communication.

Any cryptographic hash function, such as SHA-2 or SHA-3, may be used in the calculation of an HMAC; the resulting MAC algorithm is termed HMAC-X, where X is the hash function used (e.g. HMAC-SHA256 or HMAC-SHA3-512). The cryptographic strength of the HMAC depends upon the cryptographic strength of the underlying hash function, the size of its hash output, and the size and quality of the key.

HMAC does not encrypt the message. Instead, the message (encrypted or not) must be sent alongside the HMAC hash. Parties with the secret key will hash the message again themselves, and if it is authentic, the received and computed hashes will match.

By default ImageSharp.Web will use a HMAC-SHA256 algorithm.

```c#
private Func<ImageCommandContext, byte[], Task<string>> onComputeHMACAsync = (context, secret) =>
{
string uri = CaseHandlingUriBuilder.BuildRelative(
CaseHandlingUriBuilder.CaseHandling.LowerInvariant,
context.Context.Request.PathBase,
context.Context.Request.Path,
QueryString.Create(context.Commands));

return Task.FromResult(HMACUtilities.ComputeHMACSHA256(uri, secret));
};
```

Users can replicate that key using the same @SixLabors.ImageSharp.Web.CaseHandlingUriBuilder and @SixLabors.ImageSharp.Web.HMACUtilities APIs to generate the HMAC hash on the client. The hash must be passed via a command using the @SixLabors.ImageSharp.Web.HMACUtilities.TokenCommand constant.

Any invalid matches are rejected at the very start of the processing pipeline with a 400 HttpResponse code.
2 changes: 1 addition & 1 deletion ext/ImageSharp
Submodule ImageSharp updated 38 files
+1 −0 src/Directory.Build.props
+19 −2 src/ImageSharp/Diagnostics/MemoryDiagnostics.cs
+8 −1 src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
+15 −3 src/ImageSharp/Formats/Gif/GifDecoderCore.cs
+4 −1 src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
+71 −29 src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
+3 −0 src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs
+12 −0 src/ImageSharp/Formats/Png/PngDecoderCore.cs
+4 −2 src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs
+37 −27 src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
+175 −83 src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
+7 −0 src/ImageSharp/Formats/Webp/WebpThrowHelper.cs
+9 −0 src/ImageSharp/IO/BufferedReadStream.cs
+9 −0 src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs
+1 −0 tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs
+10 −5 tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs
+1 −0 tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
+33 −0 tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs
+1 −1 tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
+1 −0 tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs
+13 −0 tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs
+1 −0 tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs
+2 −0 tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs
+77 −0 tests/ImageSharp.Tests/MemoryAllocatorValidator.cs
+3 −0 tests/ImageSharp.Tests/TestImages.cs
+1 −1 tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs
+7 −1 tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs
+33 −0 tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs
+3 −0 tests/Images/Input/Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg
+3 −0 tests/Images/Input/Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg
+2 −2 tests/Images/Input/Webp/exif_lossy.webp
+3 −0 tests/Images/Input/Webp/exif_lossy_not_enough_data.webp
2 changes: 1 addition & 1 deletion ext/ImageSharp.Web
Submodule ImageSharp.Web updated 85 files
+0 −33 .github/ISSUE_TEMPLATE/commercial-bug-report.md
+56 −0 .github/ISSUE_TEMPLATE/commercial-bug-report.yml
+0 −30 .github/ISSUE_TEMPLATE/oss-bug-report.md
+52 −0 .github/ISSUE_TEMPLATE/oss-bug-report.yml
+29 −6 .github/workflows/build-and-test.yml
+5 −5 README.md
+30 −39 samples/ImageSharp.Web.Sample/Startup.cs
+0 −3 samples/ImageSharp.Web.Sample/wwwroot/imagesharp-logo.png
+230 −230 samples/ImageSharp.Web.Sample/wwwroot/index.html
+3 −0 samples/ImageSharp.Web.Sample/wwwroot/sixlabors.imagesharp.web.png
+1 −5 src/Directory.Build.targets
+13 −4 src/ImageSharp.Web.Providers.AWS/ImageSharp.Web.Providers.AWS.csproj
+16 −6 src/ImageSharp.Web.Providers.Azure/ImageSharp.Web.Providers.Azure.csproj
+23 −26 src/ImageSharp.Web/Caching/PhysicalFileSystemCache.cs
+10 −10 src/ImageSharp.Web/Caching/PhysicalFileSystemCacheOptions.cs
+7 −9 src/ImageSharp.Web/Caching/SHA256CacheHash.cs
+1 −1 src/ImageSharp.Web/Caching/UriAbsoluteCacheKey.cs
+1 −1 src/ImageSharp.Web/Caching/UriAbsoluteLowerInvariantCacheKey.cs
+1 −1 src/ImageSharp.Web/Caching/UriRelativeCacheKey.cs
+1 −1 src/ImageSharp.Web/Caching/UriRelativeLowerInvariantCacheKey.cs
+18 −7 src/ImageSharp.Web/CaseHandlingUriBuilder.cs
+1 −1 src/ImageSharp.Web/Commands/Converters/ArrayConverter{T}.cs
+4 −4 src/ImageSharp.Web/Commands/Converters/ColorConverter.cs
+2 −1 src/ImageSharp.Web/Commands/Converters/EnumConverter.cs
+1 −1 src/ImageSharp.Web/Commands/Converters/IntegralNumberConverter{T}.cs
+2 −1 src/ImageSharp.Web/Commands/Converters/ListConverter{T}.cs
+1 −1 src/ImageSharp.Web/Commands/Converters/SimpleCommandConverter{T}.cs
+97 −59 src/ImageSharp.Web/DependencyInjection/ImageSharpBuilderExtensions.cs
+0 −19 src/ImageSharp.Web/DependencyInjection/ImageSharpConfiguration.cs
+0 −2 src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs
+240 −0 src/ImageSharp.Web/ExifOrientationUtilities.cs
+1 −1 src/ImageSharp.Web/FormatUtilities.cs
+34 −23 src/ImageSharp.Web/FormattedImage.cs
+100 −0 src/ImageSharp.Web/HMACUtilities.cs
+6 −10 src/ImageSharp.Web/ImageCacheMetadata.cs
+15 −7 src/ImageSharp.Web/ImageSharp.Web.csproj
+5 −2 src/ImageSharp.Web/Middleware/ImageContext.cs
+145 −41 src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs
+32 −23 src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs
+40 −0 src/ImageSharp.Web/PathUtilities.cs
+3 −0 src/ImageSharp.Web/Processors/BackgroundColorWebProcessor.cs
+3 −0 src/ImageSharp.Web/Processors/FormatWebProcessor.cs
+15 −0 src/ImageSharp.Web/Processors/IImageWebProcessor.cs
+3 −0 src/ImageSharp.Web/Processors/QualityWebProcessor.cs
+21 −146 src/ImageSharp.Web/Processors/ResizeWebProcessor.cs
+37 −14 src/ImageSharp.Web/Processors/WebProcessingExtensions.cs
+66 −0 src/ImageSharp.Web/Providers/FileProviderImageProvider.cs
+29 −63 src/ImageSharp.Web/Providers/PhysicalFileSystemProvider.cs
+5 −0 src/ImageSharp.Web/Providers/PhysicalFileSystemProviderOptions.cs
+29 −0 src/ImageSharp.Web/Providers/WebRootImageProvider.cs
+29 −0 src/ImageSharp.Web/Resolvers/FileProviderImageResolver.cs
+26 −7 src/ImageSharp.Web/Resolvers/PhysicalFileSystemCacheResolver.cs
+0 −35 src/ImageSharp.Web/Resolvers/PhysicalFileSystemResolver.cs
+1 −5 tests/Directory.Build.targets
+4 −1 tests/ImageSharp.Web.Benchmarks/ImageSharp.Web.Benchmarks.csproj
+29 −13 tests/ImageSharp.Web.Tests/Caching/PhysicialFileSystemCacheTests.cs
+2 −0 tests/ImageSharp.Web.Tests/DependencyInjection/MockWebProcessor.cs
+83 −0 tests/ImageSharp.Web.Tests/DependencyInjection/ServiceRegistrationExtensionsTests.cs
+169 −0 tests/ImageSharp.Web.Tests/Helpers/ExifOrientationUtilitiesTests.cs
+48 −0 tests/ImageSharp.Web.Tests/Helpers/HMACUtilitiesTests.cs
+3 −7 tests/ImageSharp.Web.Tests/ImageSharp.Web.Tests.csproj
+3 −111 tests/ImageSharp.Web.Tests/Processing/AWSS3StorageCacheServerTests.cs
+3 −111 tests/ImageSharp.Web.Tests/Processing/AzureBlobStorageCacheServerTests.cs
+16 −0 tests/ImageSharp.Web.Tests/Processing/PhysicalFileSystemCacheAuthenticatedServerTests.cs
+3 −111 tests/ImageSharp.Web.Tests/Processing/PhysicalFileSystemCacheServerTests.cs
+18 −0 tests/ImageSharp.Web.Tests/Processors/BackgroundColorWebProcessorTests.cs
+30 −0 tests/ImageSharp.Web.Tests/Processors/FormatWebProcessorTests.cs
+22 −0 tests/ImageSharp.Web.Tests/Processors/QualityWebProcessorTests.cs
+32 −103 tests/ImageSharp.Web.Tests/Processors/ResizeWebProcessorTests.cs
+6 −6 tests/ImageSharp.Web.Tests/Processors/WebProcessingExtensionsTests.cs
+34 −19 tests/ImageSharp.Web.Tests/Providers/PhysicalFileSystemProviderTests.cs
+0 −3 tests/ImageSharp.Web.Tests/SubFolder/imagesharp-logo.png
+3 −0 tests/ImageSharp.Web.Tests/SubFolder/sixlabors.imagesharp.web.png
+17 −83 tests/ImageSharp.Web.Tests/TestUtilities/AWSS3StorageCacheTestServerFixture.cs
+1 −1 tests/ImageSharp.Web.Tests/TestUtilities/AWSS3StorageImageProviderFactory.cs
+46 −0 tests/ImageSharp.Web.Tests/TestUtilities/AuthenticatedServerTestBase.cs
+24 −0 tests/ImageSharp.Web.Tests/TestUtilities/AuthenticatedTestServerFixture.cs
+12 −81 tests/ImageSharp.Web.Tests/TestUtilities/AzureBlobStorageCacheTestServerFixture.cs
+2 −0 tests/ImageSharp.Web.Tests/TestUtilities/CacheBusterWebProcessor.cs
+18 −0 tests/ImageSharp.Web.Tests/TestUtilities/PhysicalFileSystemCacheAuthenticatedTestServerFixture.cs
+5 −79 tests/ImageSharp.Web.Tests/TestUtilities/PhysicalFileSystemCacheTestServerFixture.cs
+131 −1 tests/ImageSharp.Web.Tests/TestUtilities/ServerTestBase.cs
+1 −1 tests/ImageSharp.Web.Tests/TestUtilities/TestConstants.cs
+31 −0 tests/ImageSharp.Web.Tests/TestUtilities/TestEnvironment.cs
+83 −3 tests/ImageSharp.Web.Tests/TestUtilities/TestServerFixture.cs

0 comments on commit 744dd10

Please sign in to comment.