Browse Source

Handled DecompressionMethods.All

pull/2569/head
Manuel Menegazzo 3 weeks ago
parent
commit
52813fce47
  1. 15
      shared/OpenIddict.Extensions/OpenIddictPolyfills.cs
  2. 3
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs
  3. 1
      src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpOptions.cs
  4. 3
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs
  5. 1
      src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpOptions.cs
  6. 22
      test/OpenIddict.Client.SystemNetHttp.Tests/OpenIddictClientSystemNetHttpHandlersTests.cs
  7. 22
      test/OpenIddict.Validation.SystemNetHttp.Tests/OpenIddictValidationSystemNetHttpHandlersTests.cs

15
shared/OpenIddict.Extensions/OpenIddictPolyfills.cs

@ -4,6 +4,7 @@
* the license and the contributors participating to this project.
*/
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@ -71,6 +72,20 @@ internal static class OpenIddictPolyfills
#endif
}
extension(DecompressionMethods)
{
#if !NET
/// <summary>
/// Use all compression-decompression algorithms.
/// </summary>
/// <remarks>
/// Note: the backing value (-1) is identical to the one used by the
/// <c>DecompressionMethods.All</c> member exposed by .NET Core 2.1 and higher.
/// </remarks>
public static DecompressionMethods All => (DecompressionMethods) (-1);
#endif
}
extension(HMACSHA256)
{
#if !NET

3
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs

@ -614,6 +614,9 @@ public static partial class OpenIddictClientSystemNetHttpHandlers
// Note: the algorithms that were not explicitly allowed are rejected even if they are natively
// supported by the runtime (e.g to prevent using a decoder affected by a known vulnerability).
//
// DecompressionMethods.All (whose backing value is -1) has all the bits set and can be
// used to bypass the individual checks and allow all the algorithms supported by OpenIddict.
var methods = _options.CurrentValue.AllowedDecompressionMethods;
// Iterate the returned encodings and wrap the response stream using the specified algorithm.

1
src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpOptions.cs

@ -87,6 +87,7 @@ public sealed class OpenIddictClientSystemNetHttpOptions
/// Note: while HTTP compression is never opted-in by default, encoded responses returned by servers
/// that enforce content compression are still decoded. This property can be used to exclude specific
/// algorithms (e.g to mitigate a vulnerability affecting one of the decoders used by the runtime).
/// <c>DecompressionMethods.All</c> can be used to allow all the algorithms supported by OpenIddict.
/// </remarks>
public DecompressionMethods AllowedDecompressionMethods { get; set; }
#if NET

3
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs

@ -610,6 +610,9 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers
// Note: the algorithms that were not explicitly allowed are rejected even if they are natively
// supported by the runtime (e.g to prevent using a decoder affected by a known vulnerability).
//
// DecompressionMethods.All (whose backing value is -1) has all the bits set and can be
// used to bypass the individual checks and allow all the algorithms supported by OpenIddict.
var methods = _options.CurrentValue.AllowedDecompressionMethods;
// Iterate the returned encodings and wrap the response stream using the specified algorithm.

1
src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpOptions.cs

@ -87,6 +87,7 @@ public sealed class OpenIddictValidationSystemNetHttpOptions
/// Note: while HTTP compression is never opted-in by default, encoded responses returned by servers
/// that enforce content compression are still decoded. This property can be used to exclude specific
/// algorithms (e.g to mitigate a vulnerability affecting one of the decoders used by the runtime).
/// <c>DecompressionMethods.All</c> can be used to allow all the algorithms supported by OpenIddict.
/// </remarks>
public DecompressionMethods AllowedDecompressionMethods { get; set; }
#if NET

22
test/OpenIddict.Client.SystemNetHttp.Tests/OpenIddictClientSystemNetHttpHandlersTests.cs

@ -75,6 +75,28 @@ public class OpenIddictClientSystemNetHttpHandlersTests
Assert.Equal(SR.GetResourceString(SR.ID2211), context.ErrorDescription);
}
[Theory]
[MemberData(nameof(SupportedEncodings))]
public async Task DecompressResponseContent_DecompressesContentWhenAllMethodsAreAllowed(string encoding, DecompressionMethods method)
{
// Arrange
using var response = CreateCompressedResponse(encoding);
var context = new PrepareDeviceAuthorizationRequestContext(
CreateTransaction(Mock.Of<ILogger<OpenIddictClientDispatcher>>(), response));
var handler = new DecompressResponseContent<PrepareDeviceAuthorizationRequestContext>(
CreateOptions(DecompressionMethods.All));
// Act
await handler.HandleAsync(context);
// Assert
Assert.True(DecompressionMethods.All.HasFlag(method));
Assert.False(context.IsRejected);
Assert.Empty(response.Content.Headers.ContentEncoding);
Assert.Equal(Payload, await response.Content.ReadAsStringAsync());
}
[Theory]
[InlineData(false)]
[InlineData(true)]

22
test/OpenIddict.Validation.SystemNetHttp.Tests/OpenIddictValidationSystemNetHttpHandlersTests.cs

@ -75,6 +75,28 @@ public class OpenIddictValidationSystemNetHttpHandlersTests
Assert.Equal(SR.GetResourceString(SR.ID2211), context.ErrorDescription);
}
[Theory]
[MemberData(nameof(SupportedEncodings))]
public async Task DecompressResponseContent_DecompressesContentWhenAllMethodsAreAllowed(string encoding, DecompressionMethods method)
{
// Arrange
using var response = CreateCompressedResponse(encoding);
var context = new PrepareConfigurationRequestContext(
CreateTransaction(Mock.Of<ILogger<OpenIddictValidationDispatcher>>(), response));
var handler = new DecompressResponseContent<PrepareConfigurationRequestContext>(
CreateOptions(DecompressionMethods.All));
// Act
await handler.HandleAsync(context);
// Assert
Assert.True(DecompressionMethods.All.HasFlag(method));
Assert.False(context.IsRejected);
Assert.Empty(response.Content.Headers.ContentEncoding);
Assert.Equal(Payload, await response.Content.ReadAsStringAsync());
}
[Theory]
[InlineData(false)]
[InlineData(true)]

Loading…
Cancel
Save