Artifact 0d2d96ec8767f4317d0a6fb128167b3b4896c46db37a5708df00038e27a0b155:
- File
internal/sync/transport.go
— part of check-in
[3a1014cf60]
at
2026-10-11 13:28:30
on branch trunk
— fix(sync): send content only to readers; never follow HTTPS to HTTP (security) (#297)
* fix(sync): send content only to requesters allowed to read it
The sync server answered `gimme HASH` with the artifact whether or not a pull card had been accepted, and answered `pragma uv-hash`, `uvgimme` and `uvigot` with the unversioned catalog and files for any requester, anonymous ones included.
Match fossil's page_xfer:
- gimme is answered only when a pull card was accepted, or in a clone, which fossil also treats as pulling. - pragma uv-hash sends the catalog only to a requester with Read (o, i or Admin). uv-push-ok goes only to a requester with y; other readers get uv-pull-only, as fossil sends. - uvgimme and uvigot send nothing to a requester without Read, and uvigot adopts a client's mtime only for a requester with y. - Table sync follows the same rules: xgimme needs an accepted pull and pragma xtable-hash needs Read. - pragma ci-lock takes or reports a lock only for a requester with Write, so an anonymous requester cannot learn who holds one.
Pragma handling moves into handlePragma to keep handleControlCard short.
GHSA-mvx3-6793-8x4r
* docs(changelog): note that gating ci-lock on Write departs from fossil
GHSA-mvx3-6793-8x4r
* fix(sync): follow HTTP redirects as fossil does, never HTTPS to HTTP
Both HTTP transports used net/http's default redirect policy. It follows a redirect from HTTPS to plain HTTP, so a 307 or 308 resent the request, login card included, in cleartext. It also turns a 301 or 302 into a GET without the body and stops after 10 redirects, neither of which fossil does.
PostXfer now follows redirects the way fossil's http_exchange (src/http.c) and draft §4 describe. It replaces the client's redirect policy for these requests, then:
- refuses a redirect from HTTPS to HTTP, and to any scheme but http(s); - re-POSTs the same body and Content-Type for 301, 302, 307 and 308; - strips one trailing /xfer from the new location, as fossil's loop does (the draft says "repeatedly"; the code strips once); - fails on the twentieth redirect (MAX_REDIRECTS 20).
HTTPTransport and NewHTTPTransport both use it, including with a client passed through WithHTTPClient.
GHSA-3f8x-8fr5-mr98 (user: [email protected], size: 3146) [annotate] [blame] [check-ins using]
package sync import ( "context" "errors" "fmt" "net/http" "github.com/danmestas/go-libfossil/internal/xfer" ) // Transport sends an xfer request and returns the response. // Implementations handle encoding (the request Message is already decoded), // network I/O, and decoding. The xfer.Message uses zlib-compressed payloads // internally — see [xfer.Encode] and [xfer.Decode] for wire format details. // // Built-in implementations: [HTTPTransport] (Fossil HTTP /xfer protocol), // [MockTransport] (canned responses for testing). The leaf agent adds // NATSTransport for NATS-based peer-to-peer sync. type Transport interface { Exchange(ctx context.Context, request *xfer.Message) (*xfer.Message, error) } // MockTransport replays canned responses for testing. type MockTransport struct { Handler func(req *xfer.Message) *xfer.Message } func (t *MockTransport) Exchange(ctx context.Context, req *xfer.Message) (*xfer.Message, error) { if req == nil { panic("sync.MockTransport.Exchange: req must not be nil") } if t.Handler == nil { return &xfer.Message{}, nil } return t.Handler(req), nil } // HTTPTransport speaks Fossil's HTTP /xfer protocol. // Fossil routes to /xfer based on Content-Type: application/x-fossil, // NOT the URL path. URL should be the repo root (e.g. "http://localhost:8080"). type HTTPTransport struct { URL string // repo root, e.g. "http://localhost:8080" } func (t *HTTPTransport) Exchange(ctx context.Context, req *xfer.Message) (*xfer.Message, error) { if req == nil { panic("sync.HTTPTransport.Exchange: req must not be nil") } body, err := req.Encode() if err != nil { return nil, fmt.Errorf("sync.HTTPTransport encode: %w", err) } resp, err := PostXfer(ctx, http.DefaultClient, t.URL, xfer.ContentTypeCompressed, body) if err != nil { return nil, fmt.Errorf("sync.HTTPTransport do: %w", err) } defer resp.Body.Close() // An error page is not an xfer reply: report the status rather than // handing HTML to the decoder, which would report the peer's outage as a // framing defect. Whether the status is worth another attempt is // StatusError's to say. if resp.StatusCode < 200 || resp.StatusCode > 299 { return nil, &StatusError{Code: resp.StatusCode, Status: resp.Status} } respBody, err := xfer.ReadBody(resp.Body) if errors.Is(err, xfer.ErrBodyTooLarge) { // The reply is the peer's, and resending gets the same one. return nil, err } if err != nil { // The reply was cut short on the wire. Nothing has been interpreted // yet, so the same request may safely be sent again. return nil, &transportFault{ err: fmt.Errorf("sync.HTTPTransport read: %w", err), retryable: true, } } // §4: the reply's framing is given by its Content-Type, not guessed. A // clone-v3 reply arrives uncompressed; a pull reply arrives as the // compressed container. // // A failure from here on is a protocol fault: the reply arrived whole and // did not parse. It is left untagged so the retry classifier fails it // fast — resending would only make a malformed reply intermittent. return xfer.Decode(respBody, resp.Header.Get("Content-Type")) }