Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/5.0] Fix NegotiateStream handling of EOF #43741

Merged
merged 2 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private async ValueTask<int> ReadAsync<TAdapter>(TAdapter adapter, Memory<byte>

while (true)
{
int readBytes = await ReadAllAsync(adapter, _readHeader).ConfigureAwait(false);
int readBytes = await ReadAllAsync(adapter, _readHeader, allowZeroRead: true).ConfigureAwait(false);
if (readBytes == 0)
{
return 0;
Expand All @@ -386,12 +386,8 @@ private async ValueTask<int> ReadAsync<TAdapter>(TAdapter adapter, Memory<byte>
{
_readBuffer = new byte[readBytes];
}
readBytes = await ReadAllAsync(adapter, new Memory<byte>(_readBuffer, 0, readBytes)).ConfigureAwait(false);
if (readBytes == 0)
{
// We already checked that the frame body is bigger than 0 bytes. Hence, this is an EOF.
throw new IOException(SR.net_io_eof);
}

readBytes = await ReadAllAsync(adapter, new Memory<byte>(_readBuffer, 0, readBytes), allowZeroRead: false).ConfigureAwait(false);

// Decrypt into internal buffer, change "readBytes" to count now _Decrypted Bytes_
// Decrypted data start from zero offset, the size can be shrunk after decryption.
Expand Down Expand Up @@ -423,27 +419,28 @@ private async ValueTask<int> ReadAsync<TAdapter>(TAdapter adapter, Memory<byte>
_readInProgress = 0;
}

static async ValueTask<int> ReadAllAsync(TAdapter adapter, Memory<byte> buffer)
static async ValueTask<int> ReadAllAsync(TAdapter adapter, Memory<byte> buffer, bool allowZeroRead)
{
int length = buffer.Length;
int read = 0;

do
{
int bytes = await adapter.ReadAsync(buffer).ConfigureAwait(false);
if (bytes == 0)
{
if (!buffer.IsEmpty)
if (read != 0 || !allowZeroRead)
{
throw new IOException(SR.net_io_eof);
}
break;
}

buffer = buffer.Slice(bytes);
read += bytes;
}
while (!buffer.IsEmpty);

return length;
return read;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,29 @@ await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
}
}

[ConditionalFact(nameof(IsNtlmInstalled))]
public async Task NegotiateStream_ReadToEof_Returns0()
{
(Stream stream1, Stream stream2) = TestHelper.GetConnectedStreams();
using (var client = new NegotiateStream(stream1))
using (var server = new NegotiateStream(stream2))
{
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(
AuthenticateAsClientAsync(client, CredentialCache.DefaultNetworkCredentials, string.Empty),
AuthenticateAsServerAsync(server));

client.Write(Encoding.UTF8.GetBytes("hello"));
client.Dispose();

Assert.Equal('h', server.ReadByte());
Assert.Equal('e', server.ReadByte());
Assert.Equal('l', server.ReadByte());
Assert.Equal('l', server.ReadByte());
Assert.Equal('o', server.ReadByte());
Assert.Equal(-1, server.ReadByte());
}
}
}

public sealed class NegotiateStreamStreamToStreamTest_Async_Array : NegotiateStreamStreamToStreamTest
Expand Down