From 21626d7d2d03171dcb55968bb2a34cd5e357e18f Mon Sep 17 00:00:00 2001 From: Pascal Bleser Date: Wed, 2 Dec 2026 16:24:16 +0100 Subject: [PATCH] fix(webdav): don't pass internal search errors on to clients When the search service failed with anything but a bad request, a search REPORT answered with the error message of the service, which can carry internal details such as paths in the storage. Answer with a generic message instead: the error itself is still logged. Assisted-by: Claude Code:claude-opus-5-5 --- services/webdav/pkg/service/v0/search.go | 19 ++++++++++------- services/webdav/pkg/service/v0/search_test.go | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/services/webdav/pkg/service/v0/search.go b/services/webdav/pkg/service/v0/search.go index fc39c0d7dc..5d11c95740 100644 --- a/services/webdav/pkg/service/v0/search.go +++ b/services/webdav/pkg/service/v0/search.go @@ -109,19 +109,24 @@ func (g Webdav) Search(w http.ResponseWriter, r *http.Request) { rsp, err := g.searchClient.Search(ctx, req) if err != nil { - e := merrors.Parse(err.Error()) - switch e.Code { - case http.StatusBadRequest: - renderError(w, r, errBadRequest(e.Detail)) - default: - renderError(w, r, errInternalError(err.Error())) - } + renderError(w, r, searchErrorResponse(err)) logger.Error().Err(err).Msg("could not get search results") return } g.sendSearchResponse(davPrefix, rsp, w, r, user) } +// searchErrorResponse is the answer to an error of the search service. Only a +// bad request says what was wrong with it: any other error can carry internal +// details, such as paths in the storage, which stay in the log. +func searchErrorResponse(err error) *errResponse { + e := merrors.Parse(err.Error()) + if e.GetCode() == http.StatusBadRequest { + return errBadRequest(e.GetDetail()) + } + return errInternalError("could not get search results") +} + // searchRequestOf maps the search element of the report: limit -1 is no // limit, 0 the default, offset skips leading matches of the merged list. func searchRequestOf(search reportSearchFilesSearch) (*searchsvc.SearchRequest, error) { diff --git a/services/webdav/pkg/service/v0/search_test.go b/services/webdav/pkg/service/v0/search_test.go index 5d12e48862..4f2cbc7e08 100644 --- a/services/webdav/pkg/service/v0/search_test.go +++ b/services/webdav/pkg/service/v0/search_test.go @@ -2,6 +2,8 @@ package svc import ( "context" + "errors" + "net/http" "testing" "time" @@ -12,6 +14,8 @@ import ( "google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/types/known/timestamppb" + merrors "go-micro.dev/v4/errors" + "github.com/opencloud-eu/opencloud/pkg/conversions" searchmsg "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/search/v0" searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0" @@ -124,3 +128,20 @@ var _ = Describe("searchRequestOf", func() { Entry("a limit below -1", reportSearchFilesSearch{Pattern: "notes", Limit: -2}), ) }) + +var _ = Describe("searchErrorResponse", func() { + It("passes on what was wrong with a bad request", func() { + res := searchErrorResponse(merrors.BadRequest("search", "invalid query: missing closing quote")) + Expect(res.HTTPStatusCode).To(Equal(http.StatusBadRequest)) + Expect(res.Message).To(Equal("invalid query: missing closing quote")) + }) + DescribeTable("keeps the details of other errors to the log", + func(err error) { + res := searchErrorResponse(err) + Expect(res.HTTPStatusCode).To(Equal(http.StatusInternalServerError)) + Expect(res.Message).To(Equal("could not get search results")) + }, + Entry("an internal error of the search service", merrors.InternalServerError("search", "open /var/lib/opencloud/storage/users/indexes/x: no such file or directory")), + Entry("an error that isn't one of the search service", errors.New("dial tcp 10.0.0.1:9220: connect: connection refused")), + ) +})