From fc60fa634b03484079bea711c45bda7ea6f1a3b4 Mon Sep 17 00:00:00 2001 From: Kevin Tang <73975146+vt128@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:12:10 +0800 Subject: [PATCH 1/3] [feat] add opt-in single-request worker (STAR-101) --- README.md | 5 + SECURITY.md | 5 +- cmd/starbox-worker/main.go | 14 + cmd/starbox-worker/main_test.go | 15 ++ worker/README.md | 56 ++++ worker/protocol.go | 182 +++++++++++++ worker/protocol_test.go | 123 +++++++++ worker/worker.go | 256 +++++++++++++++++++ worker/worker_test.go | 439 ++++++++++++++++++++++++++++++++ 9 files changed, 1094 insertions(+), 1 deletion(-) create mode 100644 cmd/starbox-worker/main.go create mode 100644 cmd/starbox-worker/main_test.go create mode 100644 worker/README.md create mode 100644 worker/protocol.go create mode 100644 worker/protocol_test.go create mode 100644 worker/worker.go create mode 100644 worker/worker_test.go diff --git a/README.md b/README.md index a0f97c6..036296f 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,11 @@ box.SetMaxOutputEntries(100) // cap the number of top-level result entrie A run that exceeds the step budget fails with a `starlet.MaxStepsExceededError`; one that produces too many result entries is withheld with an `OutputLimitExceededError`. Both are reachable via `errors.As`, and both are enforced on **every** run path (`Run`, `RunFile`, `RunTimeout`, `RunInspect*`, and the `RunnerConfig.Execute()` builder). +For an opt-in single-request child process, see [worker](worker/README.md). +It supplies a parent-enforced deadline, a combined stdout/stderr capture limit, +and direct-child termination/reaping. Existing execution APIs remain in-process. +The worker is not a full OS or multi-tenant sandbox. + ## 🔎 Inspection without Execution Learn what a script *would* see, and catch problems, without running it: diff --git a/SECURITY.md b/SECURITY.md index 23c05ea..15dadae 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -30,7 +30,10 @@ Documentation states this boundary; it does not remove the vulnerable code. For attacker-controlled source, the host needs a separately reviewed deployment with process isolation and external CPU, memory, time and output limits, or an explicit interpreter upgrade/backport with compatibility testing. Those are -host changes, not protections supplied by this package. A newer Go compiler +host deployment requirements. The opt-in [worker](worker/README.md) package +supplies direct-child termination/reaping and a combined stdout/stderr capture +limit, but does not supply CPU/RSS quotas, descendant containment or full OS +filesystem/network isolation. Existing execution APIs stay in-process. A newer Go compiler alone does not patch the old interpreter. The library is not an in-process security boundary for untrusted or multi-tenant script execution. diff --git a/cmd/starbox-worker/main.go b/cmd/starbox-worker/main.go new file mode 100644 index 0000000..48742a8 --- /dev/null +++ b/cmd/starbox-worker/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "io" + "os" + + "github.com/1set/starbox/worker" +) + +func run(args []string, in io.Reader, out, diagnostics io.Writer) int { + return worker.Main(args, in, out, diagnostics) +} + +func main() { os.Exit(run(os.Args[1:], os.Stdin, os.Stdout, os.Stderr)) } diff --git a/cmd/starbox-worker/main_test.go b/cmd/starbox-worker/main_test.go new file mode 100644 index 0000000..11bcfe8 --- /dev/null +++ b/cmd/starbox-worker/main_test.go @@ -0,0 +1,15 @@ +package main + +import ( + "bytes" + "strings" + "testing" +) + +func TestRunActualProtocolIO(t *testing.T) { + var out, diagnostics bytes.Buffer + status := run([]string{"--protocol=1", "--max-input=4096"}, strings.NewReader("{\"protocol\":1,\"source\":\"output(42)\"}"), &out, &diagnostics) + if status != 0 || !strings.Contains(out.String(), "\"result\":42") { + t.Fatalf("status=%d out=%s diagnostics=%s", status, &out, &diagnostics) + } +} diff --git a/worker/README.md b/worker/README.md new file mode 100644 index 0000000..7f741fd --- /dev/null +++ b/worker/README.md @@ -0,0 +1,56 @@ +# Single-request worker + +This opt-in package launches a trusted worker executable for each request. +Build the command from the same reviewed checkout: + + go build -o /absolute/trusted/path/starbox-worker ./cmd/starbox-worker + +Use positive budgets and an explicit executable path: + + runner, err := worker.New(worker.Config{ + Executable: "/absolute/trusted/path/starbox-worker", + Timeout: 2 * time.Second, + MaxRequestBytes: 1 << 20, + MaxOutputBytes: 64 << 10, + }) + // Check err before calling Run. + result, err := runner.Run(context.Background(), worker.Request{ + Source: "print(\"hello\"); output({\"ok\": True})", + }) + +The request limit counts the encoded protocol message, including source modules. +The output limit counts stdout and stderr together, including result framing and +error/fatal diagnostics. Exactly the limit is allowed; the first excess byte +causes termination and an ErrOutputLimit result. Retained output never exceeds +the limit. A failed or truncated response does not expose a successful Payload. + +Each worker accepts one protocol-v1 request on stdin and exits. print and log +write to stderr; stdout carries the structured completion and optional JSON +result from output(value). Functions and other JSON-incompatible results fail +explicitly. A script error is a *RemoteError, distinct from a crash, malformed +protocol or external deadline. No arbitrary Go globals, callbacks, module +loaders or host filesystem cross the protocol. + +Source modules are a finite map of relative .star paths in a new memory +filesystem. Only log is enabled as a builtin module; this is not the existing +Safe or Full module profile. Unknown loads cannot fall back to the host's +working directory. Each request has a private working directory and an empty +environment except required Windows SystemRoot. + +The supervisor kills the direct worker when the external deadline/cancellation +or captured-output limit fires, waits for it exactly once, closes its parent +pipe endpoints and joins the I/O goroutines before returning. Reaped and +PipesClosed report that cleanup. Output ordering across the two streams is +unspecified; a byte limit can split UTF-8 diagnostics. + +This does not limit CPU usage or RSS, contain descendants, restrict the entire +OS filesystem/network, or provide an untrusted multi-tenant sandbox. A child can +allocate while constructing a result before emitting any bytes. A kernel task +that cannot be interrupted may delay Wait. The trusted executable path must be +immutable to the host's threat model; protocol=1 is not proof of binary origin. + +Existing starbox.New, Run and RunnerConfig APIs keep their in-process behavior. +Go 1.19 and the fixed interpreter/module requirements remain unchanged. No +runtime downloading, module-version resolution or automatic PATH discovery is +performed. + diff --git a/worker/protocol.go b/worker/protocol.go new file mode 100644 index 0000000..280fb69 --- /dev/null +++ b/worker/protocol.go @@ -0,0 +1,182 @@ +package worker + +import ( + "bytes" + "encoding/json" + "flag" + "fmt" + "io" + "io/fs" + "sort" + "strings" + "unicode/utf8" + + "github.com/1set/starbox" + "github.com/1set/starlet" + "github.com/1set/starlet/dataconv" + "github.com/psanford/memfs" + "go.starlark.net/starlark" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +type wireRequest struct { + Protocol int `json:"protocol"` + Request +} +type wireResponse struct { + Protocol int `json:"protocol"` + OK bool `json:"ok"` + Result json.RawMessage `json:"result,omitempty"` + Error string `json:"error,omitempty"` + Kind string `json:"kind,omitempty"` +} + +func decodeOne(data []byte, value interface{}) error { + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.DisallowUnknownFields() + if err := decoder.Decode(value); err != nil { + return fmt.Errorf("%w: %v", ErrProtocol, err) + } + if err := decoder.Decode(new(interface{})); err != io.EOF { + return fmt.Errorf("%w: extra or malformed message", ErrProtocol) + } + return nil +} +func validateRequest(request Request, limit int64) error { + remaining := limit + add := func(s string) error { + if !utf8.ValidString(s) { + return fmt.Errorf("%w: invalid UTF-8", ErrProtocol) + } + if int64(len(s)) > remaining { + return ErrInputLimit + } + remaining -= int64(len(s)) + return nil + } + if err := add(request.Source); err != nil { + return err + } + if err := add(request.Filename); err != nil { + return err + } + for name, source := range request.Modules { + if !fs.ValidPath(name) || !strings.HasSuffix(name, ".star") { + return fmt.Errorf("%w: module must be a relative .star path", ErrProtocol) + } + for _, builtin := range starlet.GetAllBuiltinModuleNames() { + if name == builtin+".star" { + return fmt.Errorf("%w: reserved module name", ErrProtocol) + } + } + if err := add(name); err != nil { + return err + } + if err := add(source); err != nil { + return err + } + } + return nil +} + +// Serve reads exactly one bounded protocol-v1 request and writes one response. +// It accepts no host filesystem or Go callbacks. The caller must supervise this +// process; byte quotas do not bound the child's intermediate encoding memory. +func Serve(in io.Reader, out, diagnostics io.Writer, maxInput int64) error { + if maxInput <= 0 || maxInput == int64(^uint64(0)>>1) { + return fmt.Errorf("%w: invalid input budget", ErrProtocol) + } + data, err := io.ReadAll(io.LimitReader(in, maxInput+1)) + if err != nil { + return err + } + if int64(len(data)) > maxInput { + return ErrInputLimit + } + request := wireRequest{} + if err := decodeOne(data, &request); err != nil { + return err + } + if request.Protocol != 1 { + return fmt.Errorf("%w: unsupported version", ErrProtocol) + } + if err := validateRequest(request.Request, maxInput); err != nil { + return err + } + return json.NewEncoder(out).Encode(execute(request.Request, diagnostics)) +} +func execute(request Request, diagnostics io.Writer) wireResponse { + response := wireResponse{Protocol: 1} + fail := func(kind string, err error) wireResponse { + response.Kind = kind + response.Error = err.Error() + return response + } + box := starbox.NewWithPolicy("worker", starbox.Policy{Modules: starbox.ModuleAllow{Names: []string{"log"}}}) + box.SetModuleSet(starbox.EmptyModuleSet) + box.AddNamedModules("log") + box.SetPrintFunc(func(_ *starlark.Thread, message string) { _, _ = fmt.Fprintln(diagnostics, message) }) + logger := zap.New(zapcore.NewCore(zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), zapcore.AddSync(diagnostics), zap.DebugLevel)) + box.SetLogger(logger.Sugar()) + memory := memfs.New() + names := make([]string, 0, len(request.Modules)) + for name := range request.Modules { + names = append(names, name) + } + sort.Strings(names) + for _, name := range names { + if slash := strings.LastIndex(name, "/"); slash >= 0 { + if err := memory.MkdirAll(name[:slash], 0700); err != nil { + return fail("module", err) + } + } + if err := memory.WriteFile(name, []byte(request.Modules[name]), 0600); err != nil { + return fail("module", err) + } + } + box.SetFS(memory) + box.AddResultBuiltin("output") + box.GetMachine().SetOutputConversionEnabled(false) + filename := request.Filename + if filename == "" { + filename = "worker.star" + } + source := request.Source + if source == "" { + source = "\n" + } + if _, err := box.CreateRunConfig().FileName(filename).Script(source).Execute(); err != nil { + return fail(starbox.ClassifyRunError(err).Kind.String(), err) + } + if value, set := box.GetResult(); set { + result, err := dataconv.EncodeStarlarkJSON(value) + if err != nil { + return fail("result", err) + } + response.Result = json.RawMessage(result) + } + response.OK = true + return response +} + +// Main is the command's actual noninteractive I/O boundary. Build the executable +// from the same reviewed module; protocol=1 is not a module-version claim. +func Main(args []string, in io.Reader, out, diagnostics io.Writer) int { + flags := flag.NewFlagSet("starbox-worker", flag.ContinueOnError) + flags.SetOutput(diagnostics) + protocol := flags.Int("protocol", 0, "required protocol version") + input := flags.Int64("max-input", 0, "required request byte limit") + if err := flags.Parse(args); err != nil { + return 2 + } + if *protocol != 1 || *input <= 0 || flags.NArg() != 0 { + _, _ = fmt.Fprintln(diagnostics, "protocol=1 and a positive max-input are required") + return 2 + } + if err := Serve(in, out, diagnostics, *input); err != nil { + _, _ = fmt.Fprintln(diagnostics, err) + return 2 + } + return 0 +} diff --git a/worker/protocol_test.go b/worker/protocol_test.go new file mode 100644 index 0000000..2679f03 --- /dev/null +++ b/worker/protocol_test.go @@ -0,0 +1,123 @@ +package worker + +// Sections: public protocol I/O; command arguments; result/module errors. +import ( + "bytes" + "encoding/json" + "errors" + "io" + "strings" + "testing" +) + +type failedWriter struct{} + +func (failedWriter) Write([]byte) (int, error) { return 0, errors.New("fixture write failure") } + +type failedReader struct{} + +func (failedReader) Read([]byte) (int, error) { return 0, errors.New("fixture read failure") } + +func TestServeProtocolIO(t *testing.T) { + for _, wire := range []string{ + "{\"protocol\":1,\"source\":\"output(7)\"}", + "{\"protocol\":1,\"source\":\"load(\\\"m.star\\\",\\\"v\\\"); output(v)\",\"modules\":{\"m.star\":\"v=7\"}}", + } { + var out, diagnostics bytes.Buffer + if err := Serve(strings.NewReader(wire), &out, &diagnostics, 4096); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "\"result\":7") { + t.Fatalf("out=%s diagnostics=%s", &out, &diagnostics) + } + } + for _, wire := range []string{ + "bad", "{\"protocol\":2,\"source\":\"x=1\"}", + "{\"protocol\":1,\"source\":\"x=1\",\"unknown\":true}", + "{\"protocol\":1,\"source\":\"x=1\"} {}", + "{\"protocol\":1,\"modules\":{\"../x.star\":\"x=1\"}}", + } { + if err := Serve(strings.NewReader(wire), io.Discard, io.Discard, 4096); err == nil { + t.Fatalf("accepted %s", wire) + } + } + if err := Serve(strings.NewReader(strings.Repeat(" ", 9)), io.Discard, io.Discard, 8); err == nil { + t.Fatal("input cap missed") + } + if err := Serve(failedReader{}, io.Discard, io.Discard, 16); err == nil { + t.Fatal("reader failure missed") + } + if err := Serve(strings.NewReader("{\"protocol\":1,\"source\":\"x=1\"}"), failedWriter{}, io.Discard, 4096); err == nil { + t.Fatal("writer failure missed") + } + if err := Serve(strings.NewReader(""), io.Discard, io.Discard, 0); err == nil { + t.Fatal("zero cap accepted") + } +} + +func TestMainIO(t *testing.T) { + var out, diagnostics bytes.Buffer + if status := Main([]string{"--protocol=1", "--max-input=4096"}, strings.NewReader("{\"protocol\":1,\"source\":\"output(7)\"}"), &out, &diagnostics); status != 0 { + t.Fatalf("status=%d diagnostics=%s", status, &diagnostics) + } + for _, args := range [][]string{{}, {"--protocol=2", "--max-input=8"}, {"--protocol=1"}, {"--unknown"}, {"--protocol=1", "--max-input=8", "extra"}} { + if Main(args, strings.NewReader("{}"), io.Discard, io.Discard) == 0 { + t.Fatalf("invalid args %v", args) + } + } + if Main([]string{"--protocol=1", "--max-input=8"}, strings.NewReader("bad"), io.Discard, io.Discard) == 0 { + t.Fatal("invalid request accepted") + } +} + +func TestServeResultAndModuleErrors(t *testing.T) { + for _, source := range []string{"x = ???", "fail(\"expected\")", "output(lambda x:x)", "load(\"file\",\"read_string\")"} { + wire, _ := json.Marshal(map[string]interface{}{"protocol": 1, "source": source}) + var out bytes.Buffer + if err := Serve(bytes.NewReader(wire), &out, io.Discard, 4096); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "\"ok\":false") || !strings.Contains(out.String(), "\"error\":") { + t.Fatalf("failure disguised: %s", &out) + } + } +} + +func TestServeDiagnosticsAndNestedModules(t *testing.T) { + request := wireRequest{Protocol: 1, Request: Request{Source: "print(\"printed\"); log.info(\"logged\"); load(\"lib/m.star\",\"v\"); output(v)", Filename: "label.star", Modules: map[string]string{"lib/m.star": "v=7"}}} + data, err := json.Marshal(request) + if err != nil { + t.Fatal(err) + } + var out, diagnostics bytes.Buffer + if err := Serve(bytes.NewReader(data), &out, &diagnostics, 4096); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "\"result\":7") || !strings.Contains(diagnostics.String(), "printed") || !strings.Contains(diagnostics.String(), "logged") { + t.Fatalf("out=%s diagnostics=%s", &out, &diagnostics) + } + // A supplied file cannot also be the directory of another source module. + request = wireRequest{Protocol: 1, Request: Request{Source: "x=1", Modules: map[string]string{"a.star": "x=1", "a.star/b.star": "x=2"}}} + data, err = json.Marshal(request) + if err != nil { + t.Fatal(err) + } + out.Reset() + if err := Serve(bytes.NewReader(data), &out, io.Discard, 4096); err != nil { + t.Fatal(err) + } + if !strings.Contains(out.String(), "\"kind\":\"module\"") { + t.Fatalf("module conflict hidden: %s", &out) + } +} + +func TestEmptySourceIsNotAFileRequest(t *testing.T) { + var out bytes.Buffer + wire := "{\"protocol\":1,\"source\":\"\",\"filename\":\"missing.star\"}" + if err := Serve(strings.NewReader(wire), &out, io.Discard, 4096); err != nil { + t.Fatal(err) + } + if out.String() != "{\"protocol\":1,\"ok\":true}\n" { + t.Fatalf("empty source attempted a file load: %s", &out) + } +} diff --git a/worker/worker.go b/worker/worker.go new file mode 100644 index 0000000..e7a54d7 --- /dev/null +++ b/worker/worker.go @@ -0,0 +1,256 @@ +// Package worker supervises an explicitly supplied single-request executable. +// It bounds captured stdout/stderr and kills/reaps the direct child. It is not +// an OS sandbox and supplies no CPU/RSS quotas or descendant containment. +package worker + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "runtime" + "strconv" + "sync" + "time" +) + +// Sentinel errors identify transport limits and malformed responses. +var ( + ErrOutputLimit = errors.New("worker output byte limit exceeded") + ErrInputLimit = errors.New("worker request byte limit exceeded") + ErrProtocol = errors.New("invalid worker protocol") +) + +// Config requires positive budgets and a trusted immutable absolute executable. +// The host builds it from reviewed source; protocol version is not authentication. +type Config struct { + Executable string + Timeout time.Duration + MaxRequestBytes, MaxOutputBytes int64 +} + +// Request carries source, not host objects or callbacks. The caller must not +// mutate Modules during Run. +type Request struct { + Source string `json:"source"` + Filename string `json:"filename,omitempty"` + Modules map[string]string `json:"modules,omitempty"` +} + +// Result retains at most MaxOutputBytes across Stdout and Stderr, with no +// promised cross-stream ordering. Payload requires an intact successful reply. +// Reaped/PipesClosed describe this direct child and the joined parent I/O. +type Result struct { + Stdout, Stderr []byte + Payload json.RawMessage + PID, ExitCode int + Reaped, PipesClosed, OutputTruncated bool +} + +// RemoteError reports a script or result-encoding failure. The child's original +// Go error chain cannot be transported by this JSON protocol. +type RemoteError struct{ Kind, Message string } + +func (e *RemoteError) Error() string { return "worker " + e.Kind + ": " + e.Message } + +// Runner is immutable after New; every concurrent Run creates a fresh child. +type Runner struct { + config Config + args, env []string +} + +// New validates a finite, opt-in execution contract; it never searches PATH. +func New(config Config) (*Runner, error) { + if !filepath.IsAbs(config.Executable) || config.Timeout <= 0 || config.MaxRequestBytes <= 0 || + config.MaxRequestBytes == int64(^uint64(0)>>1) || config.MaxOutputBytes <= 0 { + return nil, fmt.Errorf("worker requires an absolute executable and positive finite budgets") + } + r := &Runner{config: config, args: []string{"--protocol=1", "--max-input=" + strconv.FormatInt(config.MaxRequestBytes, 10)}, env: []string{}} + if runtime.GOOS == "windows" { + r.env = append(r.env, "SystemRoot="+os.Getenv("SystemRoot")) + } + return r, nil +} + +// Run covers startup, stdin, parsing, execution and transport with an external +// deadline. It kills the direct child on cancellation/overflow and joins I/O +// after exactly one Wait. OS-uninterruptible processes can still delay Wait. +func (r *Runner) Run(ctx context.Context, request Request) (result Result, err error) { + if ctx == nil { + return result, fmt.Errorf("worker requires a context") + } + ctx, cancel := context.WithTimeout(ctx, r.config.Timeout) + defer cancel() + if err = ctx.Err(); err != nil { + return result, err + } + if err = validateRequest(request, r.config.MaxRequestBytes); err != nil { + return result, err + } + payload, err := json.Marshal(wireRequest{Protocol: 1, Request: request}) + if err != nil { + return result, err + } + if int64(len(payload)) > r.config.MaxRequestBytes { + return result, ErrInputLimit + } + if err = ctx.Err(); err != nil { + return result, err + } + dir, err := os.MkdirTemp("", "starbox-worker-") + if err != nil { + return result, err + } + defer os.RemoveAll(dir) + files := make([]*os.File, 0, 6) + closeFiles := func() { + for _, file := range files { + _ = file.Close() + } + } + defer closeFiles() + for i := 0; i < 3; i++ { + read, write, e := os.Pipe() + if e != nil { + return result, e + } + files = append(files, read, write) + } + inR, inW, outR, outW, errR, errW := files[0], files[1], files[2], files[3], files[4], files[5] + cmd := exec.Command(r.config.Executable, r.args...) + cmd.Dir = dir + cmd.Env = append([]string{}, r.env...) + cmd.Stdin = inR + cmd.Stdout = outW + cmd.Stderr = errW + if err = cmd.Start(); err != nil { + return result, err + } + result.PID = cmd.Process.Pid + _ = inR.Close() + _ = outW.Close() + _ = errW.Close() + captured := &capture{limit: r.config.MaxOutputBytes, exceeded: make(chan struct{})} + var group sync.WaitGroup + pipeErrors := make(chan error, 3) + group.Add(3) + copyStream := func(reader *os.File, dst *bytes.Buffer) { + defer group.Done() + _, e := io.Copy(streamCapture{captured, dst}, reader) + pipeErrors <- e + } + go copyStream(outR, &captured.stdout) + go copyStream(errR, &captured.stderr) + go func() { defer group.Done(); _, e := inW.Write(payload); _ = inW.Close(); pipeErrors <- e }() + ioDone := make(chan struct{}) + go func() { group.Wait(); close(ioDone) }() + // Manual pipes avoid Cmd.Wait closing readers before final bytes are consumed. + waited := make(chan error, 1) + go func() { waited <- cmd.Wait() }() + var waitErr, cause error + select { + case waitErr = <-waited: + case <-ctx.Done(): + cause = ctx.Err() + case <-captured.exceeded: + cause = ErrOutputLimit + } + if cause != nil { + _ = cmd.Process.Kill() + waitErr = <-waited + } + result.Reaped = cmd.ProcessState != nil + if result.Reaped { + result.ExitCode = cmd.ProcessState.ExitCode() + } + _ = inW.Close() + cause = finishIO(ctx, ioDone, closeFiles, cause) + closeFiles() + result.PipesClosed = true + result.Stdout = captured.stdout.Bytes() + result.Stderr = captured.stderr.Bytes() + result.OutputTruncated = captured.truncated + if cause == nil && captured.truncated { + cause = ErrOutputLimit + } + if cause != nil { + return result, cause + } + if waitErr != nil { + return result, fmt.Errorf("worker exited: %w", waitErr) + } + response := wireResponse{} + if e := decodeOne(result.Stdout, &response); e != nil { + return result, e + } + if response.Protocol != 1 || (response.OK && (response.Error != "" || response.Kind != "")) || + (!response.OK && (response.Error == "" || len(response.Result) > 0)) { + return result, ErrProtocol + } + for i := 0; i < 3; i++ { + if e := <-pipeErrors; e != nil { + return result, fmt.Errorf("worker pipe: %w", e) + } + } + if !response.OK { + return result, &RemoteError{Kind: response.Kind, Message: response.Error} + } + result.Payload = response.Result + return result, nil +} + +// Bound parent cleanup if an uncontained descendant holds a pipe open. Closing +// the endpoints must wake the I/O goroutines; done joins them before return. +func finishIO(ctx context.Context, done <-chan struct{}, closeFiles func(), cause error) error { + grace := time.NewTimer(200 * time.Millisecond) + defer grace.Stop() + select { + case <-done: + case <-ctx.Done(): + if cause == nil { + cause = ctx.Err() + } + closeFiles() + <-done + case <-grace.C: + if cause == nil { + cause = fmt.Errorf("%w: pipes did not close after exit", ErrProtocol) + } + closeFiles() + <-done + } + return cause +} + +type capture struct { + mu sync.Mutex + stdout, stderr bytes.Buffer + limit, kept int64 + exceeded chan struct{} + truncated bool +} +type streamCapture struct { + c *capture + dst *bytes.Buffer +} + +func (s streamCapture) Write(p []byte) (int, error) { + s.c.mu.Lock() + defer s.c.mu.Unlock() + keep := int64(len(p)) + if remain := s.c.limit - s.c.kept; keep > remain { + keep = remain + if !s.c.truncated { + s.c.truncated = true + close(s.c.exceeded) + } + } + _, _ = s.dst.Write(p[:int(keep)]) + s.c.kept += keep + return len(p), nil +} diff --git a/worker/worker_test.go b/worker/worker_test.go new file mode 100644 index 0000000..88222ae --- /dev/null +++ b/worker/worker_test.go @@ -0,0 +1,439 @@ +package worker + +// Sections: admission; protocol/output quota; process/fatal isolation; child helper. +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "runtime" + "runtime/debug" + "strings" + "syscall" + "testing" + "time" +) + +const testResponse = "{\"protocol\":1,\"ok\":true}\n" + +func helperRunner(t *testing.T, mode string) *Runner { + t.Helper() + executable, err := os.Executable() + if err != nil { + t.Fatal(err) + } + r, err := New(Config{Executable: executable, Timeout: 5 * time.Second, MaxRequestBytes: 1 << 20, MaxOutputBytes: 1 << 20}) + if err != nil { + t.Fatal(err) + } + r.args = []string{"-test.run=^TestWorkerHelper$"} + r.env = append(r.env, "STARBOX_WORKER_HELPER="+mode, "GORACE=atexit_sleep_ms=0", "GOCOVERDIR="+t.TempDir()) + return r +} + +func assertReaped(t *testing.T, result Result) { + t.Helper() + if !result.Reaped || !result.PipesClosed || result.PID <= 0 { + t.Fatalf("not reaped/closed: pid=%d reaped=%v closed=%v", result.PID, result.Reaped, result.PipesClosed) + } + t.Logf("pid=%d exit=%d reaped=%v pipesClosed=%v captured=%d truncated=%v", result.PID, result.ExitCode, result.Reaped, result.PipesClosed, len(result.Stdout)+len(result.Stderr), result.OutputTruncated) + if runtime.GOOS == "windows" { + return + } + p, err := os.FindProcess(result.PID) + if err != nil { + return + } + defer p.Release() + if err := p.Signal(syscall.Signal(0)); err == nil { + t.Fatalf("worker %d still present", result.PID) + } +} + +func TestConfigAdmission(t *testing.T) { + valid := Config{Executable: os.Args[0], Timeout: time.Second, MaxRequestBytes: 1024, MaxOutputBytes: 1024} + for name, change := range map[string]func(*Config){ + "relative": func(c *Config) { c.Executable = "worker" }, + "timeout": func(c *Config) { c.Timeout = 0 }, + "input": func(c *Config) { c.MaxRequestBytes = 0 }, + "output": func(c *Config) { c.MaxOutputBytes = 0 }, + "overflow": func(c *Config) { c.MaxRequestBytes = int64(^uint64(0) >> 1) }, + } { + t.Run(name, func(t *testing.T) { + cfg := valid + change(&cfg) + if _, err := New(cfg); err == nil { + t.Fatal("invalid config accepted") + } + }) + } +} + +func TestRequestAdmission(t *testing.T) { + r := helperRunner(t, "serve") + for _, request := range []Request{ + {Source: strings.Repeat("x", 1<<20)}, + {Source: "x=1", Modules: map[string]string{"../x.star": "x=1"}}, + {Source: "x=1", Modules: map[string]string{"bad": "x=1"}}, + {Source: "x=1", Modules: map[string]string{"log.star": "x=1"}}, + } { + result, err := r.Run(context.Background(), request) + if err == nil || result.PID != 0 { + t.Fatalf("invalid request started: pid=%d err=%v", result.PID, err) + } + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + result, err := r.Run(ctx, Request{Source: "x=1"}) + if !errors.Is(err, context.Canceled) || result.PID != 0 { + t.Fatalf("cancel admission: %v", err) + } + if _, err := r.Run(nil, Request{}); err == nil { + t.Fatal("nil context accepted") + } +} + +func TestRunProtocol(t *testing.T) { + r := helperRunner(t, "serve") + for _, tc := range []struct { + name, source string + modules map[string]string + want string + failure bool + }{ + {"result", "print(\"hi\"); output({\"n\":7})", nil, "{\"n\":7}", false}, + {"none", "output(None)", nil, "null", false}, + {"no_result", "x=7", nil, "", false}, + {"empty", "", nil, "", false}, + {"load", "load(\"m.star\",\"n\"); output(n)", map[string]string{"m.star": "n=7"}, "7", false}, + {"syntax", "x = ???", nil, "", true}, + {"eval", "fail(\"expected\")", nil, "", true}, + {"result_type", "output(lambda x:x)", nil, "", true}, + {"log", "log.info(\"logged\"); output(True)", nil, "true", false}, + {"missing_load", "load(\"missing.star\",\"n\")", nil, "", true}, + } { + t.Run(tc.name, func(t *testing.T) { + result, err := r.Run(context.Background(), Request{Source: tc.source, Modules: tc.modules}) + assertReaped(t, result) + if (err != nil) != tc.failure { + t.Fatalf("err=%v stderr=%s", err, result.Stderr) + } + if tc.failure { + var remote *RemoteError + if !errors.As(err, &remote) || remote.Kind == "" || !strings.Contains(remote.Error(), remote.Message) { + t.Fatalf("untyped remote failure: %v", err) + } + } + if !tc.failure && string(result.Payload) != tc.want { + t.Fatalf("payload=%s want=%s", result.Payload, tc.want) + } + if tc.name == "result" && !bytes.Contains(result.Stderr, []byte("hi")) { + t.Fatal("print missing") + } + if tc.name == "log" && !bytes.Contains(result.Stderr, []byte("logged")) { + t.Fatal("log missing") + } + }) + } +} + +func TestCombinedOutputBoundary(t *testing.T) { + total := int64(len(testResponse) + len("é")) + for _, limit := range []int64{total, total - 1} { + r := helperRunner(t, "boundary") + r.config.MaxOutputBytes = limit + result, err := r.Run(context.Background(), Request{}) + assertReaped(t, result) + if int64(len(result.Stdout)+len(result.Stderr)) > limit { + t.Fatal("combined quota exceeded") + } + if limit == total { + if err != nil || result.OutputTruncated { + t.Fatalf("exact boundary: %v", err) + } + } else { + if !errors.Is(err, ErrOutputLimit) || !result.OutputTruncated || result.Payload != nil { + t.Fatalf("over boundary: %v", err) + } + } + } + r := helperRunner(t, "flood") + r.config.MaxOutputBytes = 97 + result, err := r.Run(context.Background(), Request{}) + assertReaped(t, result) + if !errors.Is(err, ErrOutputLimit) || len(result.Stdout)+len(result.Stderr) != 97 { + t.Fatalf("flood bytes=%d err=%v", len(result.Stdout)+len(result.Stderr), err) + } +} + +func TestNoncooperativeChildStopsAndClosesInput(t *testing.T) { + for i := 0; i < 2; i++ { + r := helperRunner(t, "stall") + r.config.Timeout = time.Second + start := time.Now() + result, err := r.Run(context.Background(), Request{Source: strings.Repeat(" ", 128<<10)}) + if !errors.Is(err, context.DeadlineExceeded) { + t.Fatalf("want deadline: %v", err) + } + assertReaped(t, result) + if !bytes.Contains(result.Stderr, []byte("READY")) { + t.Fatal("child not ready") + } + if time.Since(start) > 4*time.Second { + t.Fatal("cleanup exceeded outer bound") + } + } + r := helperRunner(t, "serve") + result, err := r.Run(context.Background(), Request{Source: "output(9)"}) + assertReaped(t, result) + if err != nil || string(result.Payload) != "9" { + t.Fatalf("successor: %v", err) + } +} + +func TestParserFatalIsContained(t *testing.T) { + deep := "x = " + strings.Repeat("(", 12000) + "0" + strings.Repeat(")", 12000) + for _, loaded := range []bool{false, true} { + r := helperRunner(t, "small-stack") + request := Request{Source: deep} + if loaded { + request = Request{Source: "load(\"deep.star\",\"x\")", Modules: map[string]string{"deep.star": deep}} + } + result, err := r.Run(context.Background(), request) + assertReaped(t, result) + if err == nil || result.ExitCode == 0 || !bytes.Contains(result.Stderr, []byte("stack overflow")) { + t.Fatalf("not parser fatal: err=%v exit=%d stderr bytes=%d", err, result.ExitCode, len(result.Stderr)) + } + prefix := result.Stderr + if len(prefix) > 300 { + prefix = prefix[:300] + } + t.Logf("actual fatal loaded=%v diagnostic-prefix=%q", loaded, prefix) + } + r := helperRunner(t, "serve") + result, err := r.Run(context.Background(), Request{Source: "output(\"alive\")"}) + assertReaped(t, result) + if err != nil || string(result.Payload) != "\"alive\"" { + t.Fatalf("successor: %v", err) + } +} + +func TestExitAndProtocolFailures(t *testing.T) { + for _, mode := range []string{"exit", "bad-protocol", "empty"} { + r := helperRunner(t, mode) + result, err := r.Run(context.Background(), Request{}) + assertReaped(t, result) + if err == nil { + t.Fatalf("%s falsely successful", mode) + } + if mode != "exit" && !errors.Is(err, ErrProtocol) { + t.Fatalf("%s: %v", mode, err) + } + } + r := helperRunner(t, "serve") + r.config.Executable = t.TempDir() + "/absent" + result, err := r.Run(context.Background(), Request{}) + if err == nil || result.PID != 0 || result.Reaped { + t.Fatalf("bad launch: %v", err) + } +} + +// Test-only helper: no debug/loop switches are present in the shipped command. +func TestWorkerHelper(t *testing.T) { + mode := os.Getenv("STARBOX_WORKER_HELPER") + if mode == "" { + return + } + switch mode { + case "stall": + fmt.Fprintln(os.Stderr, "READY") + for { + time.Sleep(time.Hour) + } + case "flood": + for i := 0; i < 100000; i++ { + fmt.Fprint(os.Stdout, "out") + fmt.Fprint(os.Stderr, "err") + } + os.Exit(0) + case "boundary": + _, _ = io.Copy(io.Discard, os.Stdin) + fmt.Fprint(os.Stderr, "é") + fmt.Fprint(os.Stdout, testResponse) + os.Exit(0) + case "exit": + os.Exit(7) + case "empty": + os.Exit(0) + case "bad-protocol": + fmt.Fprint(os.Stdout, "not json") + os.Exit(0) + case "no-input": + fmt.Fprint(os.Stdout, testResponse) + os.Exit(0) + case "environment": + _, _ = io.Copy(io.Discard, os.Stdin) + dir, err := os.Getwd() + if err != nil { + os.Exit(98) + } + payload, _ := json.Marshal(map[string]interface{}{"secret": os.Getenv("STARBOX_WORKER_TEST_SECRET") != "", "dir": dir}) + _ = json.NewEncoder(os.Stdout).Encode(wireResponse{Protocol: 1, OK: true, Result: payload}) + os.Exit(0) + case "wrong-version", "extra-response", "contradictory", "missing-error", "unknown-response": + _, _ = io.Copy(io.Discard, os.Stdin) + responses := map[string]string{ + "wrong-version": "{\"protocol\":2,\"ok\":true}", + "extra-response": testResponse + "{}", + "contradictory": "{\"protocol\":1,\"ok\":true,\"error\":\"bad\"}", + "missing-error": "{\"protocol\":1,\"ok\":false}", + "unknown-response": "{\"protocol\":1,\"ok\":true,\"unknown\":true}", + } + fmt.Fprint(os.Stdout, responses[mode]) + os.Exit(0) + case "small-stack": + debug.SetMaxStack(256 << 10) + case "serve": + default: + os.Exit(99) + } + os.Exit(Main([]string{"--protocol=1", "--max-input=1048576"}, os.Stdin, os.Stdout, os.Stderr)) +} + +func TestHostFilesAreNotLoaded(t *testing.T) { + r := helperRunner(t, "serve") + file := t.TempDir() + "/private.star" + if err := os.WriteFile(file, []byte("fail(\"host file was read\")"), 0600); err != nil { + t.Fatal(err) + } + result, err := r.Run(context.Background(), Request{Filename: file}) + assertReaped(t, result) + if err != nil { + t.Fatalf("empty source treated as a filename: %v", err) + } + result, err = r.Run(context.Background(), Request{Source: fmt.Sprintf("load(%q,\"secret\")", file)}) + assertReaped(t, result) + var remote *RemoteError + if !errors.As(err, &remote) || strings.Contains(remote.Message, "host file was read") { + t.Fatalf("host FS fallback: %v", err) + } +} + +func TestResponseContract(t *testing.T) { + for _, mode := range []string{"wrong-version", "extra-response", "contradictory", "missing-error", "unknown-response"} { + r := helperRunner(t, mode) + result, err := r.Run(context.Background(), Request{}) + assertReaped(t, result) + if !errors.Is(err, ErrProtocol) { + t.Fatalf("%s: %v", mode, err) + } + } +} + +func TestRequestTransportEdges(t *testing.T) { + r := helperRunner(t, "serve") + cases := []Request{ + {Source: string([]byte{0xff})}, + {Filename: string([]byte{0xff})}, + {Filename: strings.Repeat("n", 1<<20)}, + {Modules: map[string]string{"x.star": string([]byte{0xff})}}, + {Modules: map[string]string{"x.star": strings.Repeat(" ", 1<<20)}}, + } + for _, request := range cases { + result, err := r.Run(context.Background(), request) + if err == nil || result.PID != 0 { + t.Fatalf("bad request admitted: %v", err) + } + } + r.config.MaxRequestBytes = 64 + result, err := r.Run(context.Background(), Request{Source: strings.Repeat(" ", 40), Modules: map[string]string{strings.Repeat("x", 30) + ".star": ""}}) + if !errors.Is(err, ErrInputLimit) || result.PID != 0 { + t.Fatalf("module name budget: %v", err) + } + result, err = r.Run(context.Background(), Request{Source: strings.Repeat("\x01", 20)}) + if !errors.Is(err, ErrInputLimit) || result.PID != 0 { + t.Fatalf("encoded quota: %v", err) + } + r = helperRunner(t, "no-input") + result, err = r.Run(context.Background(), Request{Source: strings.Repeat(" ", 128<<10)}) + assertReaped(t, result) + if err == nil || !strings.Contains(err.Error(), "pipe") { + t.Fatalf("unconsumed request accepted: %v", err) + } + r = helperRunner(t, "serve") + name := "TMPDIR" + if runtime.GOOS == "windows" { + name = "TMP" + t.Setenv("TEMP", t.TempDir()+"/absent") + } + t.Setenv(name, t.TempDir()+"/absent") + result, err = r.Run(context.Background(), Request{}) + if err == nil || result.PID != 0 { + t.Fatalf("temp directory failure ignored: %v", err) + } +} + +func TestCleanupClosesRealBlockedPipes(t *testing.T) { + for _, cancelled := range []bool{false, true} { + for _, prior := range []error{nil, ErrOutputLimit} { + reader, writer, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + done := make(chan struct{}) + readError := make(chan error, 1) + go func() { _, err := io.Copy(io.Discard, reader); readError <- err; close(done) }() + ctx, cancel := context.WithCancel(context.Background()) + if cancelled { + cancel() + } + start := time.Now() + got := finishIO(ctx, done, func() { _ = reader.Close() }, prior) + cancel() + _ = writer.Close() + if prior != nil { + if !errors.Is(got, prior) { + t.Fatalf("lost primary cause: %v", got) + } + } else if cancelled { + if !errors.Is(got, context.Canceled) { + t.Fatalf("cancel cleanup: %v", got) + } + } else if !errors.Is(got, ErrProtocol) { + t.Fatalf("held pipe falsely complete: %v", got) + } + if err := <-readError; err == nil { + t.Fatal("fixture reached EOF before parent close") + } + if time.Since(start) > time.Second { + t.Fatal("blocked pipe cleanup did not join") + } + } + } +} + +func TestEnvironmentAndWorkingDirectory(t *testing.T) { + t.Setenv("STARBOX_WORKER_TEST_SECRET", "synthetic-do-not-inherit") + r := helperRunner(t, "environment") + result, err := r.Run(context.Background(), Request{}) + assertReaped(t, result) + if err != nil { + t.Fatal(err) + } + var value struct { + Secret bool + Dir string + } + if err := json.Unmarshal(result.Payload, &value); err != nil { + t.Fatal(err) + } + if value.Secret || !strings.Contains(value.Dir, "starbox-worker-") { + t.Fatalf("environment/directory boundary: %s", result.Payload) + } + if _, err := os.Stat(value.Dir); !os.IsNotExist(err) { + t.Fatalf("per-run directory retained: %v", err) + } +} From 757774e910d7d34a8eeb8c58a920236f8399cb23 Mon Sep 17 00:00:00 2001 From: Kevin Tang <73975146+vt128@users.noreply.github.com> Date: Wed, 11 Nov 2026 08:21:39 +0800 Subject: [PATCH 2/3] [test] verify worker command and cancellation boundaries --- runner_test.go | 14 +++++ worker/worker.go | 2 + worker/worker_test.go | 134 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) diff --git a/runner_test.go b/runner_test.go index 0cb1303..368174b 100644 --- a/runner_test.go +++ b/runner_test.go @@ -36,6 +36,20 @@ func TestRunnerConfig_Empty(t *testing.T) { } } +func TestRunnerConfigRejectsUnknownModuleSet(t *testing.T) { + box := starbox.New("invalid-modules") + box.SetModuleSet(starbox.ModuleSetName("unknown")) + if _, err := box.CreateRunConfig().Script("x=1").Execute(); err == nil || !strings.Contains(err.Error(), "unknown module set") { + t.Fatalf("invalid module set accepted: %v", err) + } + // A rejected first execution must leave the box usable after host correction. + box.SetModuleSet(starbox.EmptyModuleSet) + out, err := box.CreateRunConfig().Script("x=1").Execute() + if err != nil || out["x"] != int64(1) { + t.Fatalf("execution after corrected configuration: out=%v err=%v", out, err) + } +} + func TestRunnerConfig_Full(t *testing.T) { box := starbox.New("aloha") box.SetModuleSet(starbox.SafeModuleSet) diff --git a/worker/worker.go b/worker/worker.go index e7a54d7..5554554 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -122,6 +122,8 @@ func (r *Runner) Run(ctx context.Context, request Request) (result Result, err e files = append(files, read, write) } inR, inW, outR, outW, errR, errW := files[0], files[1], files[2], files[3], files[4], files[5] + // The executable comes from trusted host configuration; argv contains only + // fixed protocol options. Request data travels on stdin, never through a shell. cmd := exec.Command(r.config.Executable, r.args...) cmd.Dir = dir cmd.Env = append([]string{}, r.env...) diff --git a/worker/worker_test.go b/worker/worker_test.go index 88222ae..2a579b1 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -9,6 +9,7 @@ import ( "fmt" "io" "os" + "reflect" "runtime" "runtime/debug" "strings" @@ -72,6 +73,120 @@ func TestConfigAdmission(t *testing.T) { } } +func TestRequestCannotChangeCommand(t *testing.T) { + t.Setenv("STARBOX_WORKER_TEST_SECRET", "synthetic-do-not-inherit") + executable, err := os.Executable() + if err != nil { + t.Fatal(err) + } + config := Config{Executable: executable, Timeout: 5 * time.Second, MaxRequestBytes: 4096, MaxOutputBytes: 4096} + r, err := New(config) + if err != nil { + t.Fatal(err) + } + config.Executable = "untrusted-after-new" + config.MaxRequestBytes = 1 + if r.config.Executable != executable || !reflect.DeepEqual(r.args, []string{"--protocol=1", "--max-input=4096"}) { + t.Fatalf("command changed with caller configuration: executable=%q args=%q", r.config.Executable, r.args) + } + // A helper reports the command boundary as seen by the actual child. + r.args = []string{"-test.run=^TestWorkerHelper$"} + r.env = append(r.env, "STARBOX_WORKER_HELPER=command-boundary", "GORACE=atexit_sleep_ms=0", "GOCOVERDIR="+t.TempDir()) + request := Request{ + Source: "; echo injected | sh\n$(echo injected) `echo injected`", + Filename: "--max-input=0; injected.star", + Modules: map[string]string{"--protocol=2.star": "source sent on stdin"}, + } + result, err := r.Run(context.Background(), request) + assertReaped(t, result) + if err != nil { + t.Fatal(err) + } + var got struct { + Args []string + Request Request + Secret bool + } + if err := json.Unmarshal(result.Payload, &got); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(got.Args, r.args) || !reflect.DeepEqual(got.Request, request) || got.Secret { + t.Fatalf("request crossed command boundary: %+v", got) + } +} + +func TestConcurrentRequestsRemainIndependent(t *testing.T) { + r := helperRunner(t, "serve") + type completion struct { + label string + result Result + err error + } + done := make(chan completion, 4) + for i := 0; i < cap(done); i++ { + label := fmt.Sprintf("request-%d", i) + go func() { + result, err := r.Run(context.Background(), Request{Source: fmt.Sprintf("print(%q); output(%q)", label, label)}) + done <- completion{label, result, err} + }() + } + pids := make(map[int]bool) + for i := 0; i < cap(done); i++ { + got := <-done + assertReaped(t, got.result) + if got.err != nil || string(got.result.Payload) != fmt.Sprintf("%q", got.label) || + string(got.result.Stderr) != got.label+"\n" || pids[got.result.PID] { + t.Fatalf("requests mixed: label=%s pid=%d payload=%s stderr=%s err=%v", got.label, got.result.PID, got.result.Payload, got.result.Stderr, got.err) + } + pids[got.result.PID] = true + } +} + +func TestCancellationAfterChildIsReady(t *testing.T) { + r := helperRunner(t, "cancel-ready") + ready := t.TempDir() + "/ready" + r.env = append(r.env, "STARBOX_WORKER_READY_FILE="+ready) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + type completion struct { + result Result + err error + } + done := make(chan completion, 1) + go func() { + result, err := r.Run(ctx, Request{Source: strings.Repeat(" ", 128<<10)}) + done <- completion{result, err} + }() + deadline := time.NewTimer(4 * time.Second) + defer deadline.Stop() + ticker := time.NewTicker(5 * time.Millisecond) + defer ticker.Stop() + for { + select { + case early := <-done: + t.Fatalf("child exited before readiness: %v", early.err) + case <-deadline.C: + cancel() + <-done + t.Fatal("child did not report readiness") + case <-ticker.C: + if _, err := os.Stat(ready); err == nil { + cancel() + select { + case got := <-done: + assertReaped(t, got.result) + if !errors.Is(got.err, context.Canceled) || got.result.Payload != nil { + t.Fatalf("cancellation lost: %v payload=%s", got.err, got.result.Payload) + } + case <-deadline.C: + t.Fatal("cancelled child did not return") + } + return + } + } + } +} + func TestRequestAdmission(t *testing.T) { r := helperRunner(t, "serve") for _, request := range []Request{ @@ -248,6 +363,25 @@ func TestWorkerHelper(t *testing.T) { return } switch mode { + case "command-boundary": + var request wireRequest + if err := json.NewDecoder(os.Stdin).Decode(&request); err != nil { + os.Exit(98) + } + payload, _ := json.Marshal(struct { + Args []string + Request Request + Secret bool + }{os.Args[1:], request.Request, os.Getenv("STARBOX_WORKER_TEST_SECRET") != ""}) + _ = json.NewEncoder(os.Stdout).Encode(wireResponse{Protocol: 1, OK: true, Result: payload}) + os.Exit(0) + case "cancel-ready": + if err := os.WriteFile(os.Getenv("STARBOX_WORKER_READY_FILE"), []byte("ready"), 0600); err != nil { + os.Exit(98) + } + for { + time.Sleep(time.Hour) + } case "stall": fmt.Fprintln(os.Stderr, "READY") for { From 6058b1ca466e1781bb1e2951777c60b344385c33 Mon Sep 17 00:00:00 2001 From: Kevin Tang <73975146+vt128@users.noreply.github.com> Date: Wed, 11 Nov 2026 08:40:16 +0800 Subject: [PATCH 3/3] [test] isolate coverage output for concurrent worker children --- worker/worker_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/worker/worker_test.go b/worker/worker_test.go index 2a579b1..8e566dd 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -31,7 +31,9 @@ func helperRunner(t *testing.T, mode string) *Runner { t.Fatal(err) } r.args = []string{"-test.run=^TestWorkerHelper$"} - r.env = append(r.env, "STARBOX_WORKER_HELPER="+mode, "GORACE=atexit_sleep_ms=0", "GOCOVERDIR="+t.TempDir()) + // Each Run gives its child a private working directory. Keep instrumented + // helper output there so concurrent children cannot race to replace covmeta. + r.env = append(r.env, "STARBOX_WORKER_HELPER="+mode, "GORACE=atexit_sleep_ms=0", "GOCOVERDIR=.") return r }