Fixes NullRef exception in IIS in-proc (#22806)

This commit is contained in:
Brennan 2020-06-11 10:57:36 -07:00 committed by GitHub
parent 0ae7f1a701
commit 812f2f8145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 61 deletions

View File

@ -112,7 +112,7 @@ IN_PROCESS_HANDLER::NotifyDisconnect()
if (pManagedHttpContext != nullptr) if (pManagedHttpContext != nullptr)
{ {
m_pDisconnectHandler(m_pManagedHttpContext); m_pDisconnectHandler(pManagedHttpContext);
} }
} }
@ -139,14 +139,17 @@ IN_PROCESS_HANDLER::SetManagedHttpContext(
PVOID pManagedHttpContext PVOID pManagedHttpContext
) )
{ {
bool disconnectFired = false;
{ {
SRWExclusiveLock lock(m_srwDisconnectLock); SRWExclusiveLock lock(m_srwDisconnectLock);
m_pManagedHttpContext = pManagedHttpContext; m_pManagedHttpContext = pManagedHttpContext;
disconnectFired = m_disconnectFired;
} }
if (m_disconnectFired && m_pManagedHttpContext != nullptr) if (disconnectFired && pManagedHttpContext != nullptr)
{ {
m_pDisconnectHandler(m_pManagedHttpContext); m_pDisconnectHandler(pManagedHttpContext);
} }
} }

View File

@ -119,70 +119,76 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
protected void InitializeContext() protected void InitializeContext()
{ {
_thisHandle = GCHandle.Alloc(this); // create a memory barrier between initialize and disconnect to prevent a possible
// NullRef with disconnect being called before these fields have been written
Method = GetVerb(); // disconnect aquires this lock as well
lock (_abortLock)
RawTarget = GetRawUrl();
// TODO version is slow.
HttpVersion = GetVersion();
Scheme = SslStatus != SslStatus.Insecure ? Constants.HttpsScheme : Constants.HttpScheme;
KnownMethod = VerbId;
StatusCode = 200;
var originalPath = GetOriginalPath();
if (KnownMethod == HttpApiTypes.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawTarget, "*", StringComparison.Ordinal))
{ {
PathBase = string.Empty; _thisHandle = GCHandle.Alloc(this);
Path = string.Empty;
}
else
{
// Path and pathbase are unescaped by RequestUriBuilder
// The UsePathBase middleware will modify the pathbase and path correctly
PathBase = string.Empty;
Path = originalPath;
}
var cookedUrl = GetCookedUrl(); Method = GetVerb();
QueryString = cookedUrl.GetQueryString() ?? string.Empty;
RequestHeaders = new RequestHeaders(this); RawTarget = GetRawUrl();
HttpResponseHeaders = new HeaderCollection(); // TODO version is slow.
ResponseHeaders = HttpResponseHeaders; HttpVersion = GetVersion();
Scheme = SslStatus != SslStatus.Insecure ? Constants.HttpsScheme : Constants.HttpScheme;
KnownMethod = VerbId;
StatusCode = 200;
if (_options.ForwardWindowsAuthentication) var originalPath = GetOriginalPath();
{
WindowsUser = GetWindowsPrincipal(); if (KnownMethod == HttpApiTypes.HTTP_VERB.HttpVerbOPTIONS && string.Equals(RawTarget, "*", StringComparison.Ordinal))
if (_options.AutomaticAuthentication)
{ {
User = WindowsUser; PathBase = string.Empty;
Path = string.Empty;
} }
else
{
// Path and pathbase are unescaped by RequestUriBuilder
// The UsePathBase middleware will modify the pathbase and path correctly
PathBase = string.Empty;
Path = originalPath;
}
var cookedUrl = GetCookedUrl();
QueryString = cookedUrl.GetQueryString() ?? string.Empty;
RequestHeaders = new RequestHeaders(this);
HttpResponseHeaders = new HeaderCollection();
ResponseHeaders = HttpResponseHeaders;
if (_options.ForwardWindowsAuthentication)
{
WindowsUser = GetWindowsPrincipal();
if (_options.AutomaticAuthentication)
{
User = WindowsUser;
}
}
MaxRequestBodySize = _options.MaxRequestBodySize;
ResetFeatureCollection();
if (!_server.IsWebSocketAvailable(_pInProcessHandler))
{
_currentIHttpUpgradeFeature = null;
}
_streams = new Streams(this);
(RequestBody, ResponseBody) = _streams.Start();
var pipe = new Pipe(
new PipeOptions(
_memoryPool,
readerScheduler: PipeScheduler.ThreadPool,
pauseWriterThreshold: PauseWriterThreshold,
resumeWriterThreshold: ResumeWriterTheshold,
minimumSegmentSize: MinAllocBufferSize));
_bodyOutput = new OutputProducer(pipe);
} }
MaxRequestBodySize = _options.MaxRequestBodySize;
ResetFeatureCollection();
if (!_server.IsWebSocketAvailable(_pInProcessHandler))
{
_currentIHttpUpgradeFeature = null;
}
_streams = new Streams(this);
(RequestBody, ResponseBody) = _streams.Start();
var pipe = new Pipe(
new PipeOptions(
_memoryPool,
readerScheduler: PipeScheduler.ThreadPool,
pauseWriterThreshold: PauseWriterThreshold,
resumeWriterThreshold: ResumeWriterTheshold,
minimumSegmentSize: MinAllocBufferSize));
_bodyOutput = new OutputProducer(pipe);
NativeMethods.HttpSetManagedContext(_pInProcessHandler, (IntPtr)_thisHandle); NativeMethods.HttpSetManagedContext(_pInProcessHandler, (IntPtr)_thisHandle);
} }

View File

@ -170,7 +170,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
try try
{ {
context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target; context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target;
context.AbortIO(clientDisconnect: true); context?.AbortIO(clientDisconnect: true);
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -184,7 +184,7 @@ namespace Microsoft.AspNetCore.Server.IIS.Core
try try
{ {
context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target; context = (IISHttpContext)GCHandle.FromIntPtr(pvManagedHttpContext).Target;
context.OnAsyncCompletion(hr, bytes); context?.OnAsyncCompletion(hr, bytes);
return NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_PENDING; return NativeMethods.REQUEST_NOTIFICATION_STATUS.RQ_NOTIFICATION_PENDING;
} }
catch (Exception ex) catch (Exception ex)