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)

CyberCaius on Discord: S3 backup works, then every sync fails with
"the S3 server refused the access key ID or secret (HTTP 403
SignatureDoesNotMatch)", and edits to the S3 settings "won't save".
The keys work in other S3 clients, and switching to WebDAV and back
to S3 fixes it until the next time.

Cause: the SigV4 signer signs every header on the request, and
7ab91961d's conditional requests added If-None-Match (GET, the ETag
kept in sync-state.json) and If-Match (PUT). SigV4 needs only host
and x-amz-* signed. A server, or a proxy in front of it, that checks
the signature over a condition as it reads it back rather than as it
arrived refuses a signed one with SignatureDoesNotMatch. The first
sync has no ETag kept, so it passes; from the second on every sync is
conditional and refused. WebDAV and back changes stateKey, so the
state starts again with no ETag and one sync works. "Won't save" is
the same 403: /api/davsync/save writes the settings and then syncs,
and the form stays open with the sync's error.

Fix: sendTo signs every header except if-*, and sets the conditions
after signing. The docs page says so.

Test: TestS3ConditionsOutsideTheSignature, where the fake S3 rereads
an If-* header without its quotes before checking the signature. It
syncs three times, saves a second provider and syncs again, and
checks that the conditional GET and PUT reached the server.
On the old s3.go it fails with the reporter's text:
  s3_test.go:612: sync 2: the S3 server refused the access key ID or
  secret (HTTP 403 SignatureDoesNotMatch)

Run at this head (base e5dd86965), sandbox HOME, caches pinned:
- gofmt -l: clean. go vet (darwin), GOOS=linux go vet -tags nogui and
  GOOS=windows go vet on internal/davsync; builds for darwin, linux
  nogui and windows: ok.
- go test -tags nogui -count=1 ./internal/davsync ./internal/cli
  ./internal/gui (each package that imports davsync): davsync and cli
  ok. gui had one red, TestRestartHandsTheGatewayOver ("9 of 10
  requests during the restart answered", connection reset). That is
  #3451, at load 50; it passes 3 of 3 alone. Commented on #3451.
- No full ./... suite: other sessions were running full suites on
  this machine (gateway, agent).

Not tried: the reporter's own S3 vendor, which isn't known yet.
This commit is contained in:
yetone
2026-10-26 15:08:14 +08:00
parent dce7fe6096
commit a1e91e5002
3 changed files with 80 additions and 3 deletions
+4 -1
View File
@@ -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
+20 -1
View File
@@ -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 {
+56 -1
View File
@@ -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)
}
}