Prevent HTTP artifact limit overflow

This commit is contained in:
2026-11-19 15:19:39 +00:00
parent 6562d63a02
commit 3105f4d62f
3 changed files with 21 additions and 1 deletions

View File

@@ -124,6 +124,11 @@ and stream behavior; no new formatter or replay algorithm is needed.
## R6: Prevent artifact read-limit overflow (Stage 4) ## R6: Prevent artifact read-limit overflow (Stage 4)
**Complete.** The read cap no longer overflows at `math.MaxInt64`.
The new test failed before the fix and now verifies that a small file keeps
its full body and size at that limit. The existing size-limit tests, full
Go suite, race suite, vet, and build passed.
[The restricted HTTP artifact reader](../../internal/adapter/http/artifact_reader.go) [The restricted HTTP artifact reader](../../internal/adapter/http/artifact_reader.go)
allows the same nonnegative `int64` limit as application configuration allows the same nonnegative `int64` limit as application configuration
and the CLI. It computes `maxBytes + 1` for `io.LimitReader` to detect and the CLI. It computes `maxBytes + 1` for `io.LimitReader` to detect

View File

@@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math"
"mime" "mime"
"os" "os"
"path/filepath" "path/filepath"
@@ -136,7 +137,11 @@ func readArtifactFile(path string, maxBytes int64) (*promptkit.Artifact, error)
var reader io.Reader = file var reader io.Reader = file
if maxBytes > 0 { 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) body, err := io.ReadAll(reader)
if err != nil { if err != nil {

View File

@@ -3,6 +3,7 @@ package httpadapter
import ( import (
"context" "context"
"errors" "errors"
"math"
"mime" "mime"
"os" "os"
"path/filepath" "path/filepath"
@@ -180,6 +181,15 @@ func TestRestrictedArtifactReaderEnforcesLimits(t *testing.T) {
t.Fatalf("expected unlimited artifact, got %#v and %v", artifact, err) 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 { if _, err := NewRestrictedArtifactReader(root, -1); err == nil {
t.Fatal("expected negative limit to fail") t.Fatal("expected negative limit to fail")
} }