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

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

Merged
merged 3 commits into from
Jul 27, 2021
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
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