Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CVE: Fix Receiver malicious tenant #5969

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 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,13 @@ func (h *Handler) receiveHTTP(w http.ResponseWriter, r *http.Request) {
}
}

err = h.isTenantValid(tenant)
if err != nil {
level.Error(h.logger).Log("msg", "tenant name not valid", "tenant", tenant)
http.Error(w, err.Error(), http.StatusBadRequest)
saswatamcode marked this conversation as resolved.
Show resolved Hide resolved
return
}

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

writeGate := h.Limiter.WriteGate()
Expand Down
48 changes: 48 additions & 0 deletions pkg/receive/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,54 @@ 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 malicious tenant going out of receiver directory",
tenant: "./../../hacker_dir",
expectedErr: errors.New("Tenant name not valid"),
},
{
name: "test slash-only tenant",
tenant: "///",
expectedErr: errors.New("Tenant name not valid"),
},
{
name: "test default tenant",
tenant: "default-tenant",
},
{
name: "test tenant with uuid",
tenant: "528d0490-8720-4478-aa29-819d90fc9a9f",
},
{
name: "test valid tenant",
tenant: "foo",
},
saswatamcode marked this conversation as resolved.
Show resolved Hide resolved
} {
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.Error(), err.Error())
return
}
testutil.Ok(t, err)
})
}
}

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