Skip to content

Commit 199f099

Browse files
[release8/0] AppContext for HttpSys CBT hardening (#64296)
1 parent eadea25 commit 199f099

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Servers/HttpSys/src/NativeInterop/UrlGroup.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Runtime.InteropServices;
66
using Microsoft.AspNetCore.HttpSys.Internal;
77
using Microsoft.Extensions.Logging;
8+
using static Microsoft.AspNetCore.HttpSys.Internal.HttpApiTypes;
89

910
namespace Microsoft.AspNetCore.Server.HttpSys;
1011

@@ -16,6 +17,8 @@ internal sealed partial class UrlGroup : IDisposable
1617
Marshal.SizeOf<HttpApiTypes.HTTP_QOS_SETTING_INFO>();
1718
private static readonly int RequestPropertyInfoSize =
1819
Marshal.SizeOf<HttpApiTypes.HTTP_BINDING_INFO>();
20+
private static readonly int ChannelBindInfoSize =
21+
Marshal.SizeOf<HttpApiTypes.HTTP_CHANNEL_BIND_INFO>();
1922

2023
private readonly ILogger _logger;
2124

@@ -42,6 +45,17 @@ internal unsafe UrlGroup(ServerSession serverSession, RequestQueue requestQueue,
4245

4346
Debug.Assert(urlGroupId != 0, "Invalid id returned by HttpCreateUrlGroup");
4447
Id = urlGroupId;
48+
49+
if (AppContext.TryGetSwitch("Microsoft.AspNetCore.Server.HttpSys.EnableCBTHardening", out var enabled) && enabled)
50+
{
51+
var channelBindingSettings = new HTTP_CHANNEL_BIND_INFO
52+
{
53+
Hardening = HTTP_AUTHENTICATION_HARDENING_LEVELS.HttpAuthenticationHardeningMedium,
54+
ServiceNames = IntPtr.Zero,
55+
NumberOfServiceNames = 0,
56+
};
57+
SetProperty(HTTP_SERVER_PROPERTY.HttpServerChannelBindProperty, new(&channelBindingSettings), (uint)ChannelBindInfoSize);
58+
}
4559
}
4660

4761
internal ulong Id { get; private set; }

src/Shared/HttpSys/NativeInterop/HttpApiTypes.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Licensed to the .NET Foundation under one or more agreements.
33
// The .NET Foundation licenses this file to you under the MIT license.
44

5-
using System;
6-
using System.Collections.Generic;
75
using System.Runtime.InteropServices;
86
using System.Security.Authentication;
97
using Microsoft.AspNetCore.Http;
@@ -654,6 +652,22 @@ internal struct HTTP_BINDING_INFO
654652
internal IntPtr RequestQueueHandle;
655653
}
656654

655+
internal enum HTTP_AUTHENTICATION_HARDENING_LEVELS
656+
{
657+
HttpAuthenticationHardeningLegacy = 0,
658+
HttpAuthenticationHardeningMedium,
659+
HttpAuthenticationHardeningStrict
660+
}
661+
662+
[StructLayout(LayoutKind.Sequential)]
663+
internal struct HTTP_CHANNEL_BIND_INFO
664+
{
665+
internal HTTP_AUTHENTICATION_HARDENING_LEVELS Hardening;
666+
internal uint Flags;
667+
internal /*PHTTP_SERVICE_BINDING_BASE**/ IntPtr ServiceNames;
668+
internal uint NumberOfServiceNames;
669+
}
670+
657671
[StructLayout(LayoutKind.Sequential)]
658672
internal struct HTTP_CONNECTION_LIMIT_INFO
659673
{

0 commit comments

Comments
 (0)