oficina / equipo Public

hostedwire: controller-time proof, bounded durations, signed empty query

Review round 1. StopProof.ObservedAt is controller time, sent as
integer nanoseconds (observed_at_ns), never wall time.
LaunchRequest.MaxLifetime checks max_lifetime_ms before multiplying and
returns ErrDuration for negative or unrepresentable values, which
previously wrapped round to plausible lifetimes. The signed target keeps
the "?" of an empty query, so adding or removing it breaks the signature.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 39cd8c03b803583691c9beb20786df731cdbb5be parent af8096a
5 files changed, +100 −11
  • docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md +9 −0
  • internal/hostedwire/api.go +24 −6
  • internal/hostedwire/api_test.go +35 −2
  • internal/hostedwire/sign.go +6 −3
  • internal/hostedwire/sign_test.go +26 −0
diff --git a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
index 362f7909..55b24bd0 100644
--- a/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
+++ b/docs/superpowers/plans/HOSTED-SLICE1-PROGRESS.md
@@ -181,3 +181,12 @@ Fix round 1 (review r1). `Vanish(ref)` makes the fake provider forget a VM: Get,
| shared public key allowed (`if false && other != id ...`) | `go test ./internal/hostedwire -run 'TestSharedPublicKeyRefused' -count=1` | `sign_test.go:461: Verify as op-alias with a shared public key = ("op-alias", <nil>), want ErrSharedKey` | yes, passes |
| repeated header allowed (`case 1, 2:` in `single`) | `go test ./internal/hostedwire -run 'TestRequestHeadersStrict' -count=1` | `sign_test.go:444: Verify with key id twice = <nil>, want hostedwire: signature header malformed or repeated` | yes, passes |
| oversize body allowed in Verify | `go test ./internal/hostedwire -run 'TestOversizeBodyRefused' -count=1` | `sign_test.go:389: Verify of 1048577 signed bytes = <nil>, want ErrTooLarge` | yes, passes |
+
+Fix round 1 (review r1). `StopProof.ObservedAt` is controller time, a `time.Duration` since the controller's epoch, sent as integer nanoseconds in `observed_at_ns` (no wall time on the wire). `LaunchRequest.MaxLifetime()` now returns `(time.Duration, error)`: a negative `max_lifetime_ms` or one too large for a `time.Duration` is `ErrDuration`, checked before the multiplication (unchecked, 288230376155311744 and -288230376148111744 both wrapped to one hour). The signed target keeps the `?` of an empty query (`ForceQuery || RawQuery != ""`). RED: the new `TestLaunchLifetimeBounds` and the changed `TestWireNames` row did not build against the old API; the empty-query rows failed with `sign_test.go:256: Verify after the empty query added = <nil>, want ErrBadSignature` (and `removed`). Line numbers in the Task 8 rows above are as of `af8096ad`; the rows below are from this round.
+
+| Mutation | Command | Observed failure | Restored |
+| --- | --- | --- | --- |
+| proof time sent as a string (`json:"observed_at_ns,string"`) | `go test ./internal/hostedwire -run 'TestWireNames' -count=1` | `api_test.go:119: json of hostedwire.StopProof = {...,"observed_at_ns":"4611686018427387905"}, want {...,"observed_at_ns":4611686018427387905}` | yes, passes |
+| lifetime unchecked (`if false && ms < 0 \|\| false && ms > ...`) | `go test ./internal/hostedwire -run 'TestLaunchLifetimeBounds' -count=1` | `api_test.go:157: MaxLifetime(9223372036855 ms) = (-2562047h47m16.854551616s, <nil>), want (0, ErrDuration)` | yes, passes |
+| negative lifetime allowed (`if ms > math...`) | same | `api_test.go:157: MaxLifetime(-288230376148111744 ms) = (1h0m0s, <nil>), want (0, ErrDuration)` | yes, passes |
+| empty query unsigned (`if r.URL.RawQuery != "" {`) | `go test ./internal/hostedwire -run 'TestSignatureCoversEveryField' -count=1` | `sign_test.go:256: Verify after the empty query added = <nil>, want ErrBadSignature` and `... empty query removed = <nil>, want ErrBadSignature` | yes, passes |
diff --git a/internal/hostedwire/api.go b/internal/hostedwire/api.go
index 879b3978..f42bce65 100644
--- a/internal/hostedwire/api.go
+++ b/internal/hostedwire/api.go
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
+ "math"
"time"
"amadan.net/oficina/equipo/internal/isolatedjob"
@@ -13,7 +14,9 @@ import (
// Wire types for the controller's routes. Every body is one JSON object with
// lower-case snake_case keys, read with DecodeStrict. Durations are whole
-// milliseconds in fields ending _ms; times are RFC 3339.
+// milliseconds in fields ending _ms; controller time is whole nanoseconds
+// since the controller's epoch in fields ending _ns. No field holds wall
+// time.
//
// POST /v1/jobs LaunchRequest -> JobView
// GET /v1/jobs/{attempt} (empty body) -> JobView
@@ -43,9 +46,24 @@ type LaunchRequest struct {
InstanceCode string `json:"instance_code"`
}
-// MaxLifetime is MaxLifetimeMS as a duration.
-func (q LaunchRequest) MaxLifetime() time.Duration {
- return time.Duration(q.MaxLifetimeMS) * time.Millisecond
+// MaxLifetime is MaxLifetimeMS as a duration. A negative value, or one too
+// large for a time.Duration, is ErrDuration: multiplied unchecked it would
+// wrap round to a plausible lifetime.
+func (q LaunchRequest) MaxLifetime() (time.Duration, error) {
+ return millis(q.MaxLifetimeMS)
+}
+
+// ErrDuration is a wire duration that is negative or does not fit a
+// time.Duration.
+var ErrDuration = errors.New("hostedwire: duration out of range")
+
+// millis converts whole milliseconds from the wire, checking the integer
+// before it is multiplied. Every _ms field read as a duration goes through it.
+func millis(ms int64) (time.Duration, error) {
+ if ms < 0 || ms > math.MaxInt64/int64(time.Millisecond) {
+ return 0, ErrDuration
+ }
+ return time.Duration(ms) * time.Millisecond, nil
}
// JobView is the controller's view of one attempt. The *_in_ms fields count
@@ -67,12 +85,12 @@ type StopRequest struct {
}
// StopProof says the provider reported the VM terminal, or gone after the
-// stop request.
+// stop request. ObservedAt is controller time (Task 9), never wall time.
type StopProof struct {
AttemptID string `json:"attempt_id"`
VMRef isolatedjob.Ref `json:"vm_ref"`
State isolatedjob.State `json:"state"`
- ObservedAt time.Time `json:"observed_at"`
+ ObservedAt time.Duration `json:"observed_at_ns"` // controller time: nanoseconds since the controller's epoch
}
// DrainRequest opens a new pause generation. It has no fields; its body is {}.
diff --git a/internal/hostedwire/api_test.go b/internal/hostedwire/api_test.go
index 7074f86f..386d2a93 100644
--- a/internal/hostedwire/api_test.go
+++ b/internal/hostedwire/api_test.go
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
+ "math"
"strings"
"testing"
"time"
@@ -73,7 +74,9 @@ func TestDecodeStrict(t *testing.T) {
// TestWireNames pins the JSON names, so the controller and the instance cannot
// drift apart silently.
func TestWireNames(t *testing.T) {
- at := time.Date(2026, 11, 22, 10, 0, 0, 0, time.UTC)
+ // Controller time, a duration since the controller's epoch, kept to the
+ // nanosecond and above 2^53 so a float on the way would show.
+ at := time.Duration(1<<62 + 1)
cases := []struct {
v any
into func() any
@@ -88,7 +91,7 @@ func TestWireNames(t *testing.T) {
{StopRequest{Reason: "done"}, func() any { return &StopRequest{} }, `{"reason":"done"}`},
{StopProof{AttemptID: "a1", VMRef: "vm-1", State: isolatedjob.StateStopped, ObservedAt: at},
func() any { return &StopProof{} },
- `{"attempt_id":"a1","vm_ref":"vm-1","state":"stopped","observed_at":"2026-11-22T10:00:00Z"}`},
+ `{"attempt_id":"a1","vm_ref":"vm-1","state":"stopped","observed_at_ns":4611686018427387905}`},
{DrainRequest{}, func() any { return &DrainRequest{} }, `{}`},
{DrainResponse{Generation: 4}, func() any { return &DrainResponse{} }, `{"generation":4}`},
{QuiescentRequest{Generation: 4}, func() any { return &QuiescentRequest{} }, `{"generation":4}`},
@@ -125,3 +128,33 @@ func TestWireNames(t *testing.T) {
}
}
}
+
+func TestLaunchLifetimeBounds(t *testing.T) {
+ const most = math.MaxInt64 / int64(time.Millisecond)
+ cases := []struct {
+ ms int64
+ want time.Duration
+ ok bool
+ }{
+ {0, 0, true},
+ {1000, time.Second, true},
+ {28_800_000, 8 * time.Hour, true},
+ {most, time.Duration(most) * time.Millisecond, true},
+ {most + 1, 0, false},
+ // Each of these wraps to exactly one hour if multiplied unchecked.
+ {288230376155311744, 0, false},
+ {-288230376148111744, 0, false},
+ {-1, 0, false},
+ {math.MaxInt64, 0, false},
+ {math.MinInt64, 0, false},
+ }
+ for _, c := range cases {
+ got, err := LaunchRequest{MaxLifetimeMS: c.ms}.MaxLifetime()
+ if c.ok && (err != nil || got != c.want) {
+ t.Fatalf("MaxLifetime(%d ms) = (%v, %v), want (%v, nil)", c.ms, got, err, c.want)
+ }
+ if !c.ok && (!errors.Is(err, ErrDuration) || got != 0) {
+ t.Fatalf("MaxLifetime(%d ms) = (%v, %v), want (0, ErrDuration)", c.ms, got, err)
+ }
+ }
+}
diff --git a/internal/hostedwire/sign.go b/internal/hostedwire/sign.go
index bea70f82..eb4cd311 100644
--- a/internal/hostedwire/sign.go
+++ b/internal/hostedwire/sign.go
@@ -140,8 +140,9 @@ func responseMessage(status int, requestNonce string, body []byte) []byte {
hex.EncodeToString(sum[:]))
}
-// requestTarget is the escaped path and, when there is one, "?" and the raw
-// query. Opaque URLs and control characters are refused.
+// requestTarget is the escaped path and, when the URL has a query (even an
+// empty one), "?" and the raw query. Opaque URLs and control characters are
+// refused.
func requestTarget(r *http.Request) (string, error) {
if r.URL == nil || r.URL.Opaque != "" {
return "", fmt.Errorf("%w: request URL", ErrMalformed)
@@ -150,7 +151,9 @@ func requestTarget(r *http.Request) (string, error) {
return "", fmt.Errorf("%w: method", ErrMalformed)
}
target := escapedPath(r.URL)
- if r.URL.RawQuery != "" {
+ // "/v1/vms?" and "/v1/vms" are different targets, so an empty query
+ // keeps its "?" (url.URL marks it with ForceQuery).
+ if r.URL.ForceQuery || r.URL.RawQuery != "" {
target += "?" + r.URL.RawQuery
}
if hasControlOrSpace(target) {
diff --git a/internal/hostedwire/sign_test.go b/internal/hostedwire/sign_test.go
index 3815deca..a164ed8c 100644
--- a/internal/hostedwire/sign_test.go
+++ b/internal/hostedwire/sign_test.go
@@ -138,6 +138,7 @@ func TestSignedRequestRoundTrip(t *testing.T) {
{"POST", "/v1/jobs/a%2Fb/drain?x=1&y=a+b", []byte(`{}`), "inst-1", f.inst, RoleInstance},
{"POST", "/v1/jobs", []byte(`{"attempt_id":"a1"}`), "inst-2", f.inst2, RoleInstance},
{"GET", "/v1/vms", nil, "op-1", f.op, RoleOperator},
+ {"GET", "/v1/vms?", nil, "op-1", f.op, RoleOperator},
}
for _, c := range cases {
var rd io.Reader
@@ -234,6 +235,31 @@ func TestSignatureCoversEveryField(t *testing.T) {
}
})
}
+
+ // An empty query is still part of the target: "/v1/vms?" and "/v1/vms"
+ // sign differently, in both directions.
+ for _, c := range []struct {
+ name, target string
+ force bool
+ }{
+ {"empty query added", "/v1/vms", true},
+ {"empty query removed", "/v1/vms?", false},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ f := newFixture()
+ r := f.signed(t, "GET", c.target, nil, "op-1", f.op, f.now, nonceA)
+ if r.URL.ForceQuery == c.force {
+ t.Fatalf("fixture: %s parsed with ForceQuery %v", c.target, r.URL.ForceQuery)
+ }
+ r.URL.ForceQuery = c.force
+ if _, _, err := f.v.Verify(r, nil); !errors.Is(err, ErrBadSignature) {
+ t.Fatalf("Verify after the %s = %v, want ErrBadSignature", c.name, err)
+ }
+ if n := f.nonces.count(); n != 0 {
+ t.Fatalf("Remember called %d times for a refused request, want 0", n)
+ }
+ })
+ }
}
func TestSkewRefused(t *testing.T) {