Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,254 @@
|
||||
package main
|
||||
|
||||
// Regression tests for the editor read routes' missing read cap. The 10MB
|
||||
// editor cap used to be enforced only against an os.Lstat snapshot taken
|
||||
// before the file was opened; handleFilesRead then read the open file with
|
||||
// plain io.ReadAll, so a file that grew between the check and the read (a
|
||||
// tenant-controlled, actively-appending log) was buffered in full — and
|
||||
// copied twice more into the JSON response. handleFSRead capped the read but
|
||||
// silently truncated a grown file to exactly the cap, serving a partial view
|
||||
// as if it were the whole file.
|
||||
//
|
||||
// Both routes now read through readEditorCapped, which buffers at most
|
||||
// maxEditorRead+1 bytes and turns residual growth into an honest 400 with
|
||||
// the same "file too large (max 10MB)" message as the pre-open size check.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"testing/iotest"
|
||||
"time"
|
||||
)
|
||||
|
||||
// countingReader records how many bytes readEditorCapped actually pulled from
|
||||
// the source, so the tests can assert the memory bound (≤ cap+1) instead of
|
||||
// trusting the LimitReader.
|
||||
type countingReader struct {
|
||||
r io.Reader
|
||||
consumed int64
|
||||
}
|
||||
|
||||
func (c *countingReader) Read(p []byte) (int, error) {
|
||||
n, err := c.r.Read(p)
|
||||
c.consumed += int64(n)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func TestReadEditorCapped_UnderCapReturnsContent(t *testing.T) {
|
||||
want := []byte("hello editor")
|
||||
got, err := readEditorCapped(bytes.NewReader(want), maxEditorRead)
|
||||
if err != nil {
|
||||
t.Fatalf("under-cap read errored: %v", err)
|
||||
}
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Fatalf("under-cap content mismatch: %d bytes", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// A file of exactly the cap must still open: the pre-open check rejects
|
||||
// size > cap, so the reader must accept len == cap and only error on more.
|
||||
func TestReadEditorCapped_ExactlyCapReturnsContent(t *testing.T) {
|
||||
want := bytes.Repeat([]byte("x"), maxEditorRead)
|
||||
got, err := readEditorCapped(bytes.NewReader(want), maxEditorRead)
|
||||
if err != nil {
|
||||
t.Fatalf("exactly-cap read errored: %v", err)
|
||||
}
|
||||
if len(got) != maxEditorRead {
|
||||
t.Fatalf("exactly-cap read returned %d bytes, want %d", len(got), maxEditorRead)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadEditorCapped_OverCapReturnsErrEditorReadTooLarge(t *testing.T) {
|
||||
got, err := readEditorCapped(bytes.NewReader(bytes.Repeat([]byte("x"), maxEditorRead+1)), maxEditorRead)
|
||||
if !errors.Is(err, errEditorReadTooLarge) {
|
||||
t.Fatalf("cap+1 read: err %v, want errEditorReadTooLarge", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("cap+1 read returned %d bytes alongside the error", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// The residual-growth shape: the source keeps yielding data past the cap in
|
||||
// small chunks (an actively-appending file), delivered one byte at a time so
|
||||
// the bound cannot be satisfied by read-call size alone. The read must stop
|
||||
// after cap+1 bytes and error — never slurp the rest.
|
||||
func TestReadEditorCapped_GrowingSourceStopsAtCapPlusOne(t *testing.T) {
|
||||
cr := &countingReader{r: io.MultiReader(
|
||||
bytes.NewReader(bytes.Repeat([]byte("y"), 1024)),
|
||||
iotest.OneByteReader(bytes.NewReader(bytes.Repeat([]byte("y"), maxEditorRead+3072))),
|
||||
)}
|
||||
got, err := readEditorCapped(cr, maxEditorRead)
|
||||
if !errors.Is(err, errEditorReadTooLarge) {
|
||||
t.Fatalf("growing source: err %v, want errEditorReadTooLarge", err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("growing source returned %d bytes alongside the error", len(got))
|
||||
}
|
||||
if cr.consumed > maxEditorRead+1 {
|
||||
t.Fatalf("reader consumed %d bytes, want ≤ cap+1 (%d)", cr.consumed, maxEditorRead+1)
|
||||
}
|
||||
}
|
||||
|
||||
// --- handler-level coverage ---
|
||||
|
||||
func seedEditorFile(t *testing.T, a *Agent, name string, size int) string {
|
||||
t.Helper()
|
||||
srvDir := a.serverDir(name)
|
||||
if err := os.MkdirAll(srvDir, 0o750); err != nil {
|
||||
t.Fatalf("mkdir server dir: %v", err)
|
||||
}
|
||||
p := filepath.Join(srvDir, "app.log")
|
||||
if err := os.WriteFile(p, bytes.Repeat([]byte("L"), size), 0o644); err != nil {
|
||||
t.Fatalf("seed %s: %v", p, err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func filesReadRequest(t *testing.T, a *Agent, server, rel string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
rec := httptest.NewRecorder()
|
||||
a.handleFilesRead(rec, httptest.NewRequest("GET", "/v1/servers/"+server+"/files?path="+rel, nil), server)
|
||||
return rec
|
||||
}
|
||||
|
||||
// Over-cap via the pre-open size check keeps the exact 400 shape.
|
||||
func TestFilesReadHandler_OverCapRejected(t *testing.T) {
|
||||
a, _ := newTestAgent(t)
|
||||
seedEditorFile(t, a, "alpha", maxEditorRead+(1<<20))
|
||||
rec := filesReadRequest(t, a, "alpha", "app.log")
|
||||
if rec.Code != 400 || !bytes.Contains(rec.Body.Bytes(), []byte("file too large (max 10MB)")) {
|
||||
t.Fatalf("over-cap files read: got %d (%s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A file of exactly the cap still reads, byte-complete.
|
||||
func TestFilesReadHandler_ExactlyCapStillReads(t *testing.T) {
|
||||
a, _ := newTestAgent(t)
|
||||
seedEditorFile(t, a, "alpha", maxEditorRead)
|
||||
rec := filesReadRequest(t, a, "alpha", "app.log")
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("exactly-cap files read: got %d (%s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
var out struct {
|
||||
OK bool `json:"ok"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if !out.OK || len(out.Content) != maxEditorRead {
|
||||
t.Fatalf("exactly-cap content length %d, want %d (ok=%v)", len(out.Content), maxEditorRead, out.OK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFSReadHandler_OverCapRejected(t *testing.T) {
|
||||
a, srvDir, _ := newFSScopeTestAgent(t)
|
||||
if err := os.WriteFile(filepath.Join(srvDir, "big.log"), bytes.Repeat([]byte("L"), maxEditorRead+(1<<20)), 0o644); err != nil {
|
||||
t.Fatalf("seed big.log: %v", err)
|
||||
}
|
||||
rr := fsPost(t, a.handleFSRead, "/v1/fs/read", map[string]any{
|
||||
"path": filepath.Join(srvDir, "big.log"),
|
||||
"server": "alpha",
|
||||
})
|
||||
if rr.Code != 400 || !bytes.Contains(rr.Body.Bytes(), []byte("file too large (max 10MB)")) {
|
||||
t.Fatalf("over-cap fs read: got %d (%s)", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFSReadHandler_ExactlyCapStillReads(t *testing.T) {
|
||||
a, srvDir, _ := newFSScopeTestAgent(t)
|
||||
if err := os.WriteFile(filepath.Join(srvDir, "edge.log"), bytes.Repeat([]byte("L"), maxEditorRead), 0o644); err != nil {
|
||||
t.Fatalf("seed edge.log: %v", err)
|
||||
}
|
||||
rr := fsPost(t, a.handleFSRead, "/v1/fs/read", map[string]any{
|
||||
"path": filepath.Join(srvDir, "edge.log"),
|
||||
"server": "alpha",
|
||||
})
|
||||
if rr.Code != 200 {
|
||||
t.Fatalf("exactly-cap fs read: got %d (%s)", rr.Code, rr.Body.String())
|
||||
}
|
||||
var out struct {
|
||||
OK bool `json:"ok"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &out); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if !out.OK || len(out.Content) != maxEditorRead {
|
||||
t.Fatalf("exactly-cap content length %d, want %d (ok=%v)", len(out.Content), maxEditorRead, out.OK)
|
||||
}
|
||||
}
|
||||
|
||||
// The canary for the race the cap exists for: a file that starts exactly at
|
||||
// the cap (the pre-open check passes it) while a background appender grows
|
||||
// it. Whatever the timing, a 200 response must never carry more than the cap
|
||||
// in content — the old io.ReadAll path violated exactly that when an append
|
||||
// landed mid-read, returning the whole grown file. If the append lands before
|
||||
// the handler's size check the route answers 400; both outcomes are honest.
|
||||
func TestFilesReadHandler_ConcurrentAppendNeverServesOverCapContent(t *testing.T) {
|
||||
a, _ := newTestAgent(t)
|
||||
seedEditorFile(t, a, "alpha", maxEditorRead)
|
||||
srvDir := a.serverDir("alpha")
|
||||
logPath := filepath.Join(srvDir, "app.log")
|
||||
|
||||
chunk := bytes.Repeat([]byte("A"), 64<<10)
|
||||
stop := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND, 0o644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
}
|
||||
if _, err := f.Write(chunk); err != nil {
|
||||
return
|
||||
}
|
||||
time.Sleep(200 * time.Microsecond)
|
||||
}
|
||||
}()
|
||||
defer func() { close(stop); wg.Wait() }()
|
||||
|
||||
sawGrownFile := false
|
||||
for i := 0; i < 10; i++ {
|
||||
if st, err := os.Stat(logPath); err == nil && st.Size() > maxEditorRead {
|
||||
sawGrownFile = true
|
||||
}
|
||||
rec := filesReadRequest(t, a, "alpha", "app.log")
|
||||
switch rec.Code {
|
||||
case 400:
|
||||
if !bytes.Contains(rec.Body.Bytes(), []byte("file too large (max 10MB)")) {
|
||||
t.Fatalf("grown file read: unexpected 400 body %s", rec.Body.String())
|
||||
}
|
||||
case 200:
|
||||
var out struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if len(out.Content) > maxEditorRead {
|
||||
t.Fatalf("200 response carried %d bytes of an appended file, cap is %d", len(out.Content), maxEditorRead)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("grown file read: unexpected status %d (%s)", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
if !sawGrownFile {
|
||||
t.Skip("appender never outgrew the cap during the run; canary exercised nothing")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user