carlos / platform Public

demobuild: refuse an output that panics debug/elf as ErrBuild

validateBinary recovers a panic from parsing the build's own bytes
and answers ErrBuild, so a crafted header is a failed build and no
longer unwinds into net/http, which logged a stack trace per
anonymous build in root's log. The turn is released as for any
refused output.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 142889641e27309ae0759d28cb8f14c12944cab9 parent cd7c2f5
2 files changed, +31 −13
  • internal/demobuild/runner.go +9 −1
  • internal/demobuild/transfer_test.go +22 −12
diff --git a/internal/demobuild/runner.go b/internal/demobuild/runner.go
index ffa22bdfb..c2608284b 100644
--- a/internal/demobuild/runner.go
+++ b/internal/demobuild/runner.go
@@ -81,7 +81,15 @@ func readOutput(f *os.File) (BuildOutput, error) {
return validateBinary(b)
}
-func validateBinary(b []byte) (BuildOutput, error) {
+// validateBinary parses the build's own bytes. debug/elf can panic on a
+// crafted header; that output is refused like any other bad one, so an
+// anonymous build cannot put a stack trace in root's log.
+func validateBinary(b []byte) (out BuildOutput, err error) {
+ defer func() {
+ if recover() != nil {
+ out, err = BuildOutput{}, ErrBuild
+ }
+ }()
f, err := elf.NewFile(bytes.NewReader(b))
if err != nil {
return BuildOutput{}, ErrBuild
diff --git a/internal/demobuild/transfer_test.go b/internal/demobuild/transfer_test.go
index f68344a40..ec256989b 100644
--- a/internal/demobuild/transfer_test.go
+++ b/internal/demobuild/transfer_test.go
@@ -5,6 +5,7 @@ package demobuild
import (
"bytes"
"context"
+ "debug/elf"
"encoding/binary"
"encoding/json"
"errors"
@@ -289,22 +290,31 @@ func panicked(f func()) (p any) {
return nil
}
-// A panic while root validates an output (here a real one, inside
-// debug/elf, from the build's own bytes) unwinds through Run. net/http
-// recovers it and the service lives on, so the turn must come back with
-// the unwinding or every later build waits out its deadline.
-func TestDemoBuildOutputTransferValidationPanicReleases(t *testing.T) {
+// A hostile output that panics debug/elf (a real panic, from the build's
+// own bytes) is a failed build like any other: Run answers ErrBuild and
+// hands the turn back, and nothing unwinds into net/http to write a stack
+// trace per anonymous build into root's log.
+func TestDemoBuildOutputPanickingELFIsErrBuild(t *testing.T) {
+ if panicked(func() { elf.NewFile(bytes.NewReader(shstrndxPanicELF())) }) == nil {
+ t.Fatal("the fixture no longer panics debug/elf; this test needs another panic source")
+ }
+ if _, err := ValidateOutput(bytes.NewReader(shstrndxPanicELF())); !errors.Is(err, ErrBuild) {
+ t.Fatalf("ValidateOutput: err=%v", err)
+ }
ctx, dir := finishedBuild(t, shstrndxPanicELF())
- p := panicked(func() {
- out, err := Run(ctx, dir, testLock(t))
+ var err error
+ if p := panicked(func() {
+ var out BuildOutput
+ out, err = Run(ctx, dir, testLock(t))
out.Release()
- t.Logf("no panic: err=%v", err)
- })
- if p == nil {
- t.Fatal("the fixture no longer panics debug/elf; this test needs another panic source")
+ }); p != nil {
+ t.Fatalf("validation panicked out of Run: %v", p)
+ }
+ if !errors.Is(err, ErrBuild) {
+ t.Fatalf("Run: err=%v", err)
}
if !transferFree(t) {
- t.Fatalf("a validation panic (%v) kept the output turn", p)
+ t.Fatal("a refused output kept the output turn")
}
}