Skip to content

Commit

Permalink
CVE: Fix Receiver malicious tenant
Browse files Browse the repository at this point in the history
If running as root or with enough privileges, receiver can create a
directory outside of the configured TenantHeader.

This commit fixes it up by sanitizing the user input and explicity not
allowing such behavior.

Signed-off-by: Daniel Mellado <dmellado@redhat.com>
  • Loading branch information
danielmellado committed Jan 26, 2023
1 parent ff5a8b0 commit c5391f8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/receive/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
stdlog "log"
"net"
"net/http"
"path"
"sort"
"strconv"
"sync"
Expand Down Expand Up @@ -403,6 +404,13 @@ func (h *Handler) handleRequest(ctx context.Context, rep uint64, tenant string,
return h.forward(ctx, tenant, r, wreq)
}

func (h *Handler) isTenantValid(tenant string) error {
if tenant != path.Base(tenant) {
return errors.New("Tenant name not valid")
}
return nil
}

func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) {
var err error
span, ctx := tracing.StartSpan(r.Context(), "receive_http")
Expand All @@ -422,6 +430,12 @@ func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) {
}
}

err = h.isTenantValid(tenant)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

tLogger := log.With(h.logger, "tenant", tenant)

writeGate := h.Limiter.WriteGate()
Expand Down
30 changes: 30 additions & 0 deletions pkg/receive/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,36 @@ func Heap(dir string) (err error) {
return pprof.WriteHeapProfile(f)
}

func TestIsTenantValid(t *testing.T) {
for _, tcase := range []struct {
name string
tenant string

expectedErr error
}{
{
name: "test malicious tenant",
tenant: "/etc/foo",
expectedErr: errors.New("Tenant name not valid"),
},
{
name: "test valid tenant",
tenant: "foo",
},
} {
t.Run(tcase.name, func(t *testing.T) {
h := NewHandler(nil, &Options{})
err := h.isTenantValid(tcase.tenant)
if tcase.expectedErr != nil {
testutil.NotOk(t, err)
testutil.Equals(t, tcase.expectedErr, err)
return
}
testutil.Ok(t, err)
})
}
}

func TestRelabel(t *testing.T) {
for _, tcase := range []struct {
name string
Expand Down

0 comments on commit c5391f8

Please sign in to comment.