dotnet / dotnet/aspnetcore

FormPipeReader modifies the readonly input buffer

Open
#48,251 8 comments 3 reactions 0 assignees View on GitHub
area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

`FormPipeReader` uses unsafe code to convert a `ReadOnlySpan` to a writeable one and then modifies its contents.
The `readOnlySpan` in this case is a buffer from a user-supplied `ReadOnlySequence`.

https://github.com/dotnet/aspnetcore/blob/142e078e3b524cf9f41f694bc7b3b8b5d25b3dda/src/Http/WebUtilities/src/FormPipeReader.cs#L396-L402

This should likely be stackallocating/renting a buffer to write into instead.

### Expected Behavior

The readonly input isn't modified.

### Steps To Reproduce

```c#
var input = "a=c%20d"u8;

var pipe = new Pipe();
pipe.Writer.Write(input);
pipe.Writer.Complete();
pipe.Reader.TryRead(out var result);
var sequence = result.Buffer;

Console.WriteLine($"Input before parsing: {Encoding.UTF8.GetString(sequence)}");

var reader = new FormPipeReader(PipeReader.Create(sequence));
reader.ReadFormAsync().GetAwaiter().GetResult();

Console.WriteLine($"Input after parsing: {Encoding.UTF8.GetString(sequence)}");
```

```log
Input before parsing: a=c%20d
Input after parsing: a=c d0d
```

### Exceptions (if any)

_No response_

### .NET Version

_No response_

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.