Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

davsync: S3's If-Match/If-None-Match go outside the signature, so a kept ETag no longer turns a working key into 403 (CyberCaius on Discord)

yetonea1e91e500211
diff --git a/docs/subsystems/sync-backup.md b/docs/subsystems/sync-backup.md
index f1f9d0a7f..7c4ea953f 100644
--- a/docs/subsystems/sync-backup.md
+++ b/docs/subsystems/sync-backup.md
@@ -13,7 +13,10 @@ clone a working tree or synchronize arbitrary files.
magpie's settings. Secrets are local, never in the status response.
- The [`remote`](../../internal/davsync/dav.go) interface reads a version and
conditionally replaces it. WebDAV and S3 use ETags; GitHub uses the file's
- blob SHA. [`github.go`](../../internal/davsync/github.go)
+ blob SHA. S3's conditions (`If-Match`, `If-None-Match`) go outside the
+ SigV4 signature ([`unsigned`](../../internal/davsync/s3.go)): a server that
+ reads one back otherwise than it arrived refused the signed request with
+ 403 SignatureDoesNotMatch once a sync had an ETag to ask with. [`github.go`](../../internal/davsync/github.go)
interprets `github://owner/repo[/prefix]`, with `Config.Branch` selecting a
branch or using the repository's default.
- [`backup.go`](../../internal/backup/backup.go) owns bundle contents and
diff --git a/internal/davsync/s3.go b/internal/davsync/s3.go
index 27bd95247..6ed961f30 100644
--- a/internal/davsync/s3.go
+++ b/internal/davsync/s3.go
@@ -123,14 +123,33 @@ func (s *s3) sendTo(ctx context.Context, method string, u *url.URL, body []byte,
return nil, err
}
for k, v := range h {
- req.Header.Set(k, v)
+ if !unsigned(k) {
+ req.Header.Set(k, v)
+ }
}
payload := sum(body)
req.Header.Set("X-Amz-Content-Sha256", payload)
s.sig.sign(req, payload, s.now())
+ for k, v := range h {
+ if unsigned(k) {
+ req.Header.Set(k, v)
+ }
+ }
return s.client.Do(req)
}
+// unsigned are the headers sent outside the signature: the conditions
+// (If-Match, If-None-Match, If-Modified-Since). SigV4 needs only the host
+// and x-amz-* signed, and a server, or a proxy before it, that checks the
+// signature over a condition as it reads it back rather than as it
+// arrived refuses a signed one with 403 SignatureDoesNotMatch. That came only once a sync had
+// kept the server's ETag to ask with, until the state was started afresh
+// (CyberCaius on Discord: a working key refused after a while, back after
+// switching to WebDAV and to S3 again).
+func unsigned(name string) bool {
+ return strings.HasPrefix(strings.ToLower(name), "if-")
+}
+
// s3Error is what an S3 server says went wrong: its XML body's code and
// message, and the region it names for a bucket that is elsewhere.
type s3Error struct {
diff --git a/internal/davsync/s3_test.go b/internal/davsync/s3_test.go
index e26755209..9282a6d27 100644
--- a/internal/davsync/s3_test.go
+++ b/internal/davsync/s3_test.go
@@ -36,6 +36,9 @@ type fakeS3 struct {
log []string // each request: method, and the condition sent
onWrite func() // runs once, at the next PUT or HEAD (a write's look first), before it is answered
sent int // the bytes of objects read
+ // reread, when set, is how the server reads a header back before it
+ // checks the signature over it
+ reread func(name, value string) string
}
func newFakeS3(t *testing.T) (*fakeS3, *httptest.Server) {
@@ -145,7 +148,13 @@ func (f *fakeS3) signed(r *http.Request, body []byte) bool {
again := &http.Request{Method: r.Method, URL: u, Host: r.Host, Header: http.Header{}}
for _, n := range strings.Split(names, ";") {
if n != "host" {
- again.Header[http.CanonicalHeaderKey(n)] = r.Header.Values(n)
+ vs := slices.Clone(r.Header.Values(n))
+ for i := range vs {
+ if f.reread != nil {
+ vs[i] = f.reread(n, vs[i])
+ }
+ }
+ again.Header[http.CanonicalHeaderKey(n)] = vs
}
}
signer{id: f.id, secret: f.secret, region: f.region, service: "s3"}.sign(again, sum(body), at)
@@ -577,3 +586,49 @@ func TestS3Live(t *testing.T) {
t.Fatalf("b's providers: %v", got)
}
}
+
+// A server that checks the signature over a condition as it reads it back
+// (here an ETag without its quotes), not as it arrived, still syncs once
+// the ETag is kept: the conditions go outside the signature, and are
+// still honoured (CyberCaius on Discord: HTTP 403 SignatureDoesNotMatch
+// after a while, with a working key, until the sync started afresh).
+func TestS3ConditionsOutsideTheSignature(t *testing.T) {
+ f, srv := newFakeS3(t)
+ f.reread = func(name, v string) string {
+ if strings.HasPrefix(name, "if-") {
+ return strings.Trim(v, `"`)
+ }
+ return v
+ }
+ ctx := context.Background()
+ cfg := f.config(srv)
+ newComputer(t).use(t)
+ provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k1"})
+ if err := Configure(cfg); err != nil {
+ t.Fatal(err)
+ }
+ for i := range 3 {
+ if err := Now(ctx); err != nil {
+ t.Fatalf("sync %d: %v", i+1, err)
+ }
+ if v := Status(); v.Error != "" {
+ t.Fatalf("sync %d: %s", i+1, v.Error)
+ }
+ }
+ provider.Save(provider.Provider{ID: "two", Name: "Two", Chat: "https://y/v1", Key: "k2"})
+ if err := Now(ctx); err != nil {
+ t.Fatalf("a change pushed over the ETag: %v", err)
+ }
+ var get, put bool
+ for _, l := range f.log {
+ get = get || strings.HasPrefix(l, "GET none:\"")
+ put = put || strings.HasPrefix(l, "PUT \"")
+ }
+ if !get || !put {
+ t.Fatalf("the conditions didn't reach the server: %q", f.log)
+ }
+ remote, err := backup.Open(f.objects["team x+y/magpie/magpie.magpie-backup"], "correct horse")
+ if err != nil || len(remote.Providers) != 2 {
+ t.Fatalf("in the bucket: %v %+v", err, remote.Providers)
+ }
+}