Skip to content

Commit

Permalink
*: Change default chunk payload string length
Browse files Browse the repository at this point in the history
Closes #1032.

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Dec 4, 2024
1 parent 29be3d6 commit 327cf91
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions api/auth/center_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func TestAwsEncodedChunkReader(t *testing.T) {

t.Run("err chunk header too long", func(t *testing.T) {
streamSigner := v4.NewChunkSigner("us-east-1", "s3", seedSignature, ts, awsCreds)
chunkThreeBody := make([]byte, 4097)
chunkThreeBody := make([]byte, v4.MaxChunkedPayloadLineLength+1)
for i := range chunkThreeBody {
chunkThreeBody[i] = 'a'
}
Expand All @@ -341,7 +341,7 @@ func TestAwsEncodedChunkReader(t *testing.T) {
_ = chunkedReader.Close()
}()

chunk := make([]byte, 4096)
chunk := make([]byte, v4.MaxChunkedPayloadLineLength)
payload := bytes.NewBuffer(nil)
_, err = io.CopyBuffer(payload, chunkedReader, chunk)

Expand Down
13 changes: 8 additions & 5 deletions api/auth/signer/v4/chunked_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import (
"io"
)

const maxLineLength = 4096 // assumed <= bufio.defaultBufSize
// MaxChunkedPayloadLineLength defines the maximum chunk payload length.
// Considering https://github.com/aws/aws-sdk-go-v2/blob/9a211f9a8eb9923a47740503892928d702a2ac63/service/internal/checksum/aws_chunked_encoding.go#L15
// as default value.
const MaxChunkedPayloadLineLength = 1024 * 64

var (
// ErrLineTooLong appears if chunk header exceeds maxLineLength.
// ErrLineTooLong appears if chunk header exceeds MaxChunkedPayloadLineLength.
ErrLineTooLong = errors.New("header line too long")

// ErrInvalidChunkSignature appears if passed chunk signature differs from calculated.
Expand All @@ -37,7 +40,7 @@ var (
// The chunkedReader returns io.EOF when the final 0-length chunk is read.
func NewChunkedReader(r io.ReadCloser, streamSigner *ChunkSigner) io.ReadCloser {
return &chunkedReader{
r: bufio.NewReader(r),
r: bufio.NewReaderSize(r, MaxChunkedPayloadLineLength),
// bufio.Reader can't be closed, thus left link to the original reader to close it later.
origReader: r,
streamSigner: streamSigner,
Expand Down Expand Up @@ -184,7 +187,7 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
}

// Read a line of bytes (up to \n) from b.
// Give up if the line exceeds maxLineLength.
// Give up if the line exceeds MaxChunkedPayloadLineLength.
// The returned bytes are owned by the bufio.Reader
// so they are only valid until the next bufio read.
func readChunkLine(b *bufio.Reader) ([]byte, []byte, error) {
Expand All @@ -199,7 +202,7 @@ func readChunkLine(b *bufio.Reader) ([]byte, []byte, error) {
}
return nil, nil, err
}
if len(p) >= maxLineLength {
if len(p) >= MaxChunkedPayloadLineLength {
return nil, nil, ErrLineTooLong
}

Expand Down

0 comments on commit 327cf91

Please sign in to comment.