Skip to content

Commit

Permalink
fix: return 404 instead of links page on bad /api/v2 requests (influx…
Browse files Browse the repository at this point in the history
…data#21950)

* fix: return 404 instead of links page on bad /api/v2 requests

* chore: update CHANGELOG
  • Loading branch information
williamhbaker authored Jul 27, 2021
1 parent 9d81f4a commit 096a478
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ This release adds an embedded SQLite database for storing metadata required by t
1. [21850](https://github.com/influxdata/influxdb/pull/21850): Systemd unit should block on startup until http endpoint is ready
1. [21925](https://github.com/influxdata/influxdb/pull/21925): Upgrade to golang-jwt 3.2.1.
1. [21946](https://github.com/influxdata/influxdb/pull/21946): Prevent silently dropped writes when there are overlapping shards.
1. [21950](https://github.com/influxdata/influxdb/pull/21950): Invalid requests to /api/v2 subroutes now return 404 instead of a list of links.

## v2.0.7 [2021-06-04]

Expand Down
2 changes: 1 addition & 1 deletion http/api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func NewAPIHandler(b *APIBackend, opts ...APIHandlerOptFn) *APIHandler {

b.UserResourceMappingService = authorizer.NewURMService(b.OrgLookupService, b.UserResourceMappingService)

h.Mount("/api/v2", serveLinksHandler(b.HTTPErrorHandler))
h.Handle("/api/v2", serveLinksHandler(b.HTTPErrorHandler))

checkBackend := NewCheckBackend(b.Logger.With(zap.String("handler", "check")), b)
checkBackend.CheckService = authorizer.NewCheckService(b.CheckService,
Expand Down
55 changes: 55 additions & 0 deletions http/api_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,66 @@ import (
"github.com/google/go-cmp/cmp"
kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
"github.com/influxdata/influxdb/v2/pkg/httpc"
"github.com/stretchr/testify/require"
"github.com/yudai/gojsondiff"
"github.com/yudai/gojsondiff/formatter"
"go.uber.org/zap/zaptest"
)

func TestAPIHandlerServeLinks(t *testing.T) {
tests := []struct {
name string
path string
method string
want int
}{
{
name: "correct path - GET",
path: "/api/v2",
method: "GET",
want: http.StatusOK,
},
{
name: "correct path with slash - GET",
path: "/api/v2/",
method: "GET",
want: http.StatusOK,
},
{
name: "correct path - POST",
path: "/api/v2",
method: "POST",
want: http.StatusOK,
},
{
name: "incorrect arbitrary path",
path: "/api/v2/asdf",
method: "GET",
want: http.StatusNotFound,
},
{
// regression test for https://github.com/influxdata/influxdb/issues/21620
name: "incorrect path at a subroute",
path: "/api/v2/query&foo=bar",
method: "GET",
want: http.StatusNotFound,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(tt.method, tt.path, nil)
w := httptest.NewRecorder()
h := NewAPIHandler(&APIBackend{Logger: zaptest.NewLogger(t)})

h.ServeHTTP(w, r)

res := w.Result()
require.Equal(t, tt.want, res.StatusCode)
})
}
}

func TestAPIHandler_NotFound(t *testing.T) {
type args struct {
method string
Expand Down

0 comments on commit 096a478

Please sign in to comment.