-
Notifications
You must be signed in to change notification settings - Fork 383
[Sampler.AWS] Limit the max size read for response body getting the sampling rules to 1MB. #4100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| namespace OpenTelemetry.Sampler.AWS; | ||
|
|
||
| /// <summary> | ||
| /// A read-only stream wrapper that throws <see cref="InvalidOperationException"/> | ||
| /// if the underlying stream exceeds a configured maximum number of bytes. | ||
| /// This protects against denial-of-service when reading from untrusted HTTP responses. | ||
| /// </summary> | ||
| internal sealed class LimitedStream : Stream | ||
| { | ||
| private readonly Stream innerStream; | ||
| private readonly long maxBytes; | ||
| private long totalBytesRead; | ||
|
|
||
| public LimitedStream(Stream innerStream, long maxBytes) | ||
| { | ||
| this.innerStream = innerStream ?? throw new ArgumentNullException(nameof(innerStream)); | ||
|
normj marked this conversation as resolved.
|
||
|
|
||
| if (maxBytes <= 0) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(maxBytes), maxBytes, "Value must be greater than zero."); | ||
| } | ||
|
|
||
| this.maxBytes = maxBytes; | ||
| } | ||
|
|
||
| public override bool CanRead => this.innerStream.CanRead; | ||
|
|
||
| public override bool CanSeek => false; | ||
|
|
||
| public override bool CanWrite => false; | ||
|
|
||
| public override long Length => throw new NotSupportedException(); | ||
|
|
||
| public override long Position | ||
| { | ||
| get => throw new NotSupportedException(); | ||
| set => throw new NotSupportedException(); | ||
| } | ||
|
|
||
| public override int Read(byte[] buffer, int offset, int count) | ||
| { | ||
| var remaining = this.maxBytes - this.totalBytesRead; | ||
| if (remaining <= 0) | ||
| { | ||
| // Allowance exhausted - signal EOF so callers stop reading. | ||
| return 0; | ||
| } | ||
|
normj marked this conversation as resolved.
|
||
|
|
||
| var clampedCount = (int)Math.Min(count, remaining); | ||
| var bytesRead = this.innerStream.Read(buffer, offset, clampedCount); | ||
| this.totalBytesRead += bytesRead; | ||
| return bytesRead; | ||
| } | ||
|
|
||
| public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
| { | ||
| #if NET | ||
| return await this.ReadAsync(buffer.AsMemory(offset, count), cancellationToken).ConfigureAwait(false); | ||
| #else | ||
| var remaining = this.maxBytes - this.totalBytesRead; | ||
| if (remaining <= 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| var clampedCount = (int)Math.Min(count, remaining); | ||
| var bytesRead = await this.innerStream.ReadAsync(buffer, offset, clampedCount, cancellationToken).ConfigureAwait(false); | ||
| this.totalBytesRead += bytesRead; | ||
| return bytesRead; | ||
| #endif | ||
| } | ||
|
|
||
| #if NET | ||
| public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
| { | ||
| var remaining = this.maxBytes - this.totalBytesRead; | ||
| if (remaining <= 0) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| var clampedLength = (int)Math.Min(buffer.Length, remaining); | ||
| var bytesRead = await this.innerStream.ReadAsync(buffer[..clampedLength], cancellationToken).ConfigureAwait(false); | ||
| this.totalBytesRead += bytesRead; | ||
| return bytesRead; | ||
| } | ||
| #endif | ||
|
|
||
| public override void Flush() => this.innerStream.Flush(); | ||
|
|
||
| public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); | ||
|
|
||
| public override void SetLength(long value) => throw new NotSupportedException(); | ||
|
|
||
| public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| this.innerStream.Dispose(); | ||
| } | ||
|
|
||
| base.Dispose(disposing); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
test/OpenTelemetry.Sampler.AWS.Tests/TestLimitedStreamReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Text; | ||
| using Xunit; | ||
|
|
||
| namespace OpenTelemetry.Sampler.AWS.Tests; | ||
|
|
||
| public class TestLimitedStreamReader | ||
| { | ||
| [Fact] | ||
| public async Task ReadWithinLimitSucceeds() | ||
| { | ||
| var data = Encoding.UTF8.GetBytes("hello"); | ||
| using var inner = new MemoryStream(data); | ||
| using var limited = new LimitedStream(inner, maxBytes: 1024); | ||
| using var reader = new StreamReader(limited); | ||
|
|
||
| var result = await reader.ReadToEndAsync(); | ||
|
|
||
| Assert.Equal("hello", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadExactlyAtLimitSucceeds() | ||
| { | ||
| var data = Encoding.UTF8.GetBytes("12345"); | ||
| using var inner = new MemoryStream(data); | ||
| using var limited = new LimitedStream(inner, maxBytes: 5); | ||
| using var reader = new StreamReader(limited); | ||
|
|
||
| var result = await reader.ReadToEndAsync(); | ||
|
|
||
| Assert.Equal("12345", result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadExceedingLimitTruncates() | ||
| { | ||
| var data = Encoding.UTF8.GetBytes(new string('x', 2048)); | ||
| using var inner = new MemoryStream(data); | ||
| using var limited = new LimitedStream(inner, maxBytes: 1024); | ||
| using var reader = new StreamReader(limited); | ||
|
|
||
| var result = await reader.ReadToEndAsync(); | ||
|
|
||
| Assert.Equal(1024, result.Length); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SyncReadClampsThenReturnsZero() | ||
| { | ||
| var data = Encoding.UTF8.GetBytes(new string('x', 2048)); | ||
| using var inner = new MemoryStream(data); | ||
| using var limited = new LimitedStream(inner, maxBytes: 1024); | ||
|
|
||
| var buffer = new byte[2048]; | ||
|
|
||
| // First read is clamped to 1024 bytes. | ||
| var bytesRead = limited.Read(buffer, 0, buffer.Length); | ||
| Assert.Equal(1024, bytesRead); | ||
|
|
||
| // Second read returns 0 (EOF) because the allowance is exhausted. | ||
| bytesRead = limited.Read(buffer, 0, buffer.Length); | ||
| Assert.Equal(0, bytesRead); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SyncReadClampsToRemainingAllowance() | ||
| { | ||
| var data = Encoding.UTF8.GetBytes(new string('x', 200)); | ||
| using var inner = new MemoryStream(data); | ||
| using var limited = new LimitedStream(inner, maxBytes: 100); | ||
|
|
||
| var buffer = new byte[200]; | ||
| var bytesRead = limited.Read(buffer, 0, buffer.Length); | ||
|
|
||
| Assert.Equal(100, bytesRead); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CannotWrite() | ||
| { | ||
| using var inner = new MemoryStream(); | ||
| using var limited = new LimitedStream(inner, maxBytes: 1024); | ||
|
|
||
| Assert.False(limited.CanWrite); | ||
| Assert.Throws<NotSupportedException>( | ||
| () => limited.Write(new byte[1], 0, 1)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CannotSeek() | ||
| { | ||
| using var inner = new MemoryStream(); | ||
| using var limited = new LimitedStream(inner, maxBytes: 1024); | ||
|
|
||
| Assert.False(limited.CanSeek); | ||
| Assert.Throws<NotSupportedException>( | ||
| () => limited.Seek(0, SeekOrigin.Begin)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ThrowsOnNullInnerStream() | ||
| { | ||
| Assert.Throws<ArgumentNullException>(() => new LimitedStream(null!, maxBytes: 1024)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(0)] | ||
| [InlineData(-1)] | ||
| [InlineData(-100)] | ||
| public void ThrowsOnInvalidMaxBytes(long maxBytes) | ||
| { | ||
| using var inner = new MemoryStream(); | ||
| Assert.Throws<ArgumentOutOfRangeException>(() => new LimitedStream(inner, maxBytes)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.