carlos / platform Public

tests: write the region race's first body part before waiting

TestAppRegionBrowserRefusesAnAppReplacedMidRequest wrote "csrf=" from
a goroutine and the token from the test goroutine. The handler's first
body Read signals before any byte has to arrive, so the token's write
could take the pipe first and send "<token>&region=uk" with no csrf
field. The console then rightly answered 403 (csrfFormOK). Write
"csrf=" synchronously: a pipe write returns once the client has taken
every byte, so the order is fixed. Production is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev dacdd51badee044dff7c8e2d3beb6e97c229cf3a parent 619c64c
1 file changed, +9 −1
  • internal/console/appregionset_test.go +9 −1
diff --git a/internal/console/appregionset_test.go b/internal/console/appregionset_test.go
index b37fc0a03..092596181 100644
--- a/internal/console/appregionset_test.go
+++ b/internal/console/appregionset_test.go
@@ -575,7 +575,15 @@ func TestAppRegionBrowserRefusesAnAppReplacedMidRequest(t *testing.T) {
}
done <- res
}()
- go pw.Write([]byte("csrf="))
+ // The body's first part is written here, not from a goroutine: the
+ // handler's first Read signals before any byte need arrive, so a
+ // goroutine's write could lose the pipe to the token's write below and
+ // send "<token>&region=uk" with no csrf field (a true 403). A pipe
+ // write returns once the client has taken every byte, so "csrf=" is in
+ // the body before the rest can be.
+ if _, err := pw.Write([]byte("csrf=")); err != nil {
+ t.Fatal(err)
+ }
select {
case <-reading:
case <-time.After(10 * time.Second):