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

Require parameter for query endpoint #1975

Merged
merged 3 commits into from
Mar 16, 2015
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 @@ -5,6 +5,7 @@

### Bugfixes
- [#1971](https://github.com/influxdb/influxdb/pull/1971): Fix leader id initialization.
- [#1975](https://github.com/influxdb/influxdb/pull/1975): Require `q` parameter for query endpoint.

## v0.9.0-rc12 [2015-03-15]

Expand Down
12 changes: 10 additions & 2 deletions httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,18 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// serveQuery parses an incoming query and, if valid, executes the query.
func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user *influxdb.User) {
q := r.URL.Query()
p := influxql.NewParser(strings.NewReader(q.Get("q")))
db := q.Get("db")

pretty := q.Get("pretty") == "true"

qp := strings.TrimSpace(q.Get("q"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use TrimSpace?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a user can send in a parameter of q=+ which is just a space which will result in the parser still returning ok. While not exactly the same error, the result is you really didn't give me a query parameter, so it needs to return an error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

if qp == "" {
httpError(w, `missing required parameter "q"`, pretty, http.StatusBadRequest)
return
}

p := influxql.NewParser(strings.NewReader(qp))
db := q.Get("db")

// Parse query from query string.
query, err := p.ParseQuery()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,21 @@ func TestHandler_AuthenticatedDatabases_Unauthorized(t *testing.T) {
}
}

func TestHandler_QueryParamenterMissing(t *testing.T) {
c := test.NewMessagingClient()
defer c.Close()
srvr := OpenAuthlessServer(c)
s := NewHTTPServer(srvr)
defer s.Close()

status, body := MustHTTP("GET", s.URL+`/query`, nil, nil, "")
if status != http.StatusBadRequest {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"error":"missing required parameter \"q\""}` {
t.Fatalf("unexpected body: %s", body)
}
}

func TestHandler_AuthenticatedDatabases_AuthorizedQueryParams(t *testing.T) {
c := test.NewMessagingClient()
defer c.Close()
Expand Down