Prevent HTTP artifact limit overflow
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -136,7 +137,11 @@ func readArtifactFile(path string, maxBytes int64) (*promptkit.Artifact, error)
|
||||
|
||||
var reader io.Reader = file
|
||||
if maxBytes > 0 {
|
||||
reader = io.LimitReader(file, maxBytes+1)
|
||||
readLimit := maxBytes
|
||||
if readLimit < math.MaxInt64 {
|
||||
readLimit++ // Read one extra byte to detect an oversized file.
|
||||
}
|
||||
reader = io.LimitReader(file, readLimit)
|
||||
}
|
||||
body, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package httpadapter
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math"
|
||||
"mime"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -180,6 +181,15 @@ func TestRestrictedArtifactReaderEnforcesLimits(t *testing.T) {
|
||||
t.Fatalf("expected unlimited artifact, got %#v and %v", artifact, err)
|
||||
}
|
||||
|
||||
maxLimited, err := NewRestrictedArtifactReader(root, math.MaxInt64)
|
||||
if err != nil {
|
||||
t.Fatalf("construct reader at maximum limit: %v", err)
|
||||
}
|
||||
artifact, err = maxLimited.Read(context.Background(), promptkit.File("large.txt"))
|
||||
if err != nil || string(artifact.Body) != "123456" || artifact.Size != 6 {
|
||||
t.Fatalf("expected full artifact at maximum limit, got %#v and %v", artifact, err)
|
||||
}
|
||||
|
||||
if _, err := NewRestrictedArtifactReader(root, -1); err == nil {
|
||||
t.Fatal("expected negative limit to fail")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user