From 9296546be367e4dafda0cba72b3bb5cdbde9523a Mon Sep 17 00:00:00 2001 From: David Walter <115982880+dj4oC@users.noreply.github.com> Date: Tue, 23 Jun 2026 12:23:54 +0300 Subject: [PATCH] fix(proxy): guard against empty signing key store in presigned URL auth (#12418) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit signatureIsValid indexed signingKey[0] without checking the slice length. When the signing-key store returns an empty result without an error — which the "noop" store does, and OCIS_CACHE_STORE=noop also switches the presigned-URL signing-key store to noop — every signed-URL download panicked with "index out of range [0] with length 0" (recovered per request as a 502), so all signed-URL downloads failed. Guard the index access and return a real authentication error. The previous empty-value branch also returned a nil error, which would have accepted an empty signing key; it now returns an error so the request is rejected. Signed-off-by: David Walter Co-authored-by: Claude Opus 4.8 (1M context) --- ...gfix-proxy-signed-url-empty-signing-key.md | 14 +++++++++ .../proxy/pkg/middleware/signed_url_auth.go | 10 +++++-- .../pkg/middleware/signed_url_auth_test.go | 29 +++++++++++++++++++ 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 changelog/unreleased/bugfix-proxy-signed-url-empty-signing-key.md diff --git a/changelog/unreleased/bugfix-proxy-signed-url-empty-signing-key.md b/changelog/unreleased/bugfix-proxy-signed-url-empty-signing-key.md new file mode 100644 index 00000000000..9f599ad728e --- /dev/null +++ b/changelog/unreleased/bugfix-proxy-signed-url-empty-signing-key.md @@ -0,0 +1,14 @@ +Bugfix: Prevent panic in presigned URL authentication when the signing key is missing + +The presigned-URL authenticator read the per-user signing key from a store and +indexed the first record without checking that the result was non-empty. When +the store returns an empty result without an error — which the `noop` store +does, and `OCIS_CACHE_STORE=noop` also switches the presigned-URL signing-key +store to `noop` — every signed-URL request panicked with `index out of range +[0] with length 0` (recovered per request as an HTTP 502), so all signed-URL +downloads failed. The authenticator now guards the lookup and returns a normal +authentication error instead of panicking; it also no longer returns a nil +error when the stored key is empty. + +https://github.com/owncloud/ocis/issues/12384 +https://github.com/owncloud/ocis/pull/12418 diff --git a/services/proxy/pkg/middleware/signed_url_auth.go b/services/proxy/pkg/middleware/signed_url_auth.go index 776bb0080c4..a06016b8102 100644 --- a/services/proxy/pkg/middleware/signed_url_auth.go +++ b/services/proxy/pkg/middleware/signed_url_auth.go @@ -150,9 +150,13 @@ func (m SignedURLAuthenticator) signatureIsValid(req *http.Request) (err error) m.Logger.Error().Err(err).Msg("could not retrieve signing key") return err } - if len(signingKey[0].Value) == 0 { - m.Logger.Error().Err(err).Msg("signing key empty") - return err + // A store may return an empty result set without an error (e.g. the "noop" + // store, or a store that has no signing key for this user). Guard the index + // access to avoid an out-of-range panic, and return a real error so the + // request fails authentication instead of proceeding with an empty key. + if len(signingKey) == 0 || len(signingKey[0].Value) == 0 { + m.Logger.Error().Str("userid", c.Id.OpaqueId).Msg("signing key empty or not found") + return errors.New("signing key not found") } u := m.buildUrlToSign(req) computedSignature := m.createSignature(u, signingKey[0].Value) diff --git a/services/proxy/pkg/middleware/signed_url_auth_test.go b/services/proxy/pkg/middleware/signed_url_auth_test.go index 58676aecf97..7aa7db68011 100644 --- a/services/proxy/pkg/middleware/signed_url_auth_test.go +++ b/services/proxy/pkg/middleware/signed_url_auth_test.go @@ -7,6 +7,7 @@ import ( "time" userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" + "github.com/owncloud/ocis/v2/ocis-pkg/log" "github.com/owncloud/ocis/v2/services/proxy/pkg/config" revactx "github.com/owncloud/reva/v2/pkg/ctx" "github.com/stretchr/testify/assert" @@ -158,6 +159,34 @@ func TestSignedURLAuth_createSignature(t *testing.T) { } } +// TestSignedURLAuth_validateEmptySigningKeyStore guards against an out-of-range +// panic when the signing-key store returns no record without an error. This +// happens with the "noop" store (e.g. when OCIS_CACHE_STORE=noop, which also +// governs the presigned-URL signing-key store). The request must fail +// authentication, not panic. +func TestSignedURLAuth_validateEmptySigningKeyStore(t *testing.T) { + pua := SignedURLAuthenticator{ + PreSignedURLConfig: config.PreSignedURL{ + AllowedHTTPMethods: []string{"get"}, + Enabled: true, + }, + Store: store.NewNoopStore(), + Logger: log.NewLogger(), + Now: func() time.Time { + tm, _ := time.Parse(time.RFC3339, "2019-05-14T11:02:00.000Z") + return tm + }, + } + + u := userpb.User{Id: &userpb.UserId{OpaqueId: "useri"}, DisplayName: "Test User"} + ctx := revactx.ContextSetUser(context.Background(), &u) + r := httptest.NewRequest("", "http://cloud.example.net/?OC-Credential=alice&OC-Date=2019-05-14T11%3A01%3A58.135Z&OC-Expires=1200&OC-Verb=GET&OC-Signature=f9e53a1ee23caef10f72ec392c1b537317491b687bfdd224c782be197d9ca2b6", nil).WithContext(ctx) + + var err error + assert.NotPanics(t, func() { err = pua.validate(r) }, "validate must not panic when the signing key store is empty") + assert.Error(t, err, "validate must reject the request when no signing key is found") +} + func TestSignedURLAuth_validate(t *testing.T) { nowFunc := func() time.Time { t, _ := time.Parse(time.RFC3339, "2020-02-02T12:30:00.000Z")