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: detect misquoted tag values and return an error #22754

Merged
merged 5 commits into from
Oct 27, 2021

Conversation

davidby-influx
Copy link
Contributor

@davidby-influx davidby-influx commented Oct 25, 2021

SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has caused out-of-memory failures
at a customer. This fix short-circuits the query and returns
an error. This fix also handles SHOW TAG VALUES
with a misquoted WHERE clause.

Closes #22755

SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.
tsdb/store.go Outdated
@@ -1822,6 +1824,27 @@ func isTagKeyClause(e influxql.Expr) (bool, error) {
return false, nil
}

func fixBadQuoteTagValueClause(e influxql.Expr) influxql.Expr {
Copy link
Contributor

Choose a reason for hiding this comment

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

As discussed offline, I'd prefer that semantics we don't support become an error instead of hiding the problem by returning a false.

@davidby-influx davidby-influx marked this pull request as ready for review October 26, 2021 20:26
@davidby-influx davidby-influx changed the title fix: detect misquoted tag values and replace them fix: detect misquoted tag values and return an error Oct 26, 2021
@@ -1824,25 +1825,29 @@ func isTagKeyClause(e influxql.Expr) (bool, error) {
return false, nil
}

func fixBadQuoteTagValueClause(e influxql.Expr) influxql.Expr {
func isBadQuoteTagValueClause(e influxql.Expr) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should put in there permanently, but do any tests fail if we put this check on SELECT as well? might highlight a case we missed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SELECT ... WHERE supports the syntax this disallows, so this will err on legal queries.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here are the test failures where this test breaks correct queries in SELECT

--- FAIL: TestServer_Query_Where_With_Tags (0.57s)
    --- FAIL: TestServer_Query_Where_With_Tags/where_comparing_tag_and_field (0.00s)
        server_test.go:6457: where comparing tag and field: unexpected results
            query:  select foo from where_events where tennant != foo
            params:  map[db:[db0]]
            exp:    {"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:04Z","bat"],["2009-11-10T23:00:05Z","bar"],["2009-11-10T23:00:06Z","bap"]]}]}]}
            actual: {"results":[{"statement_id":0,"error":"bad WHERE clause. tag value must be inside single quotes: tennant != foo"}]}
            
    --- FAIL: TestServer_Query_Where_With_Tags/where_comparing_tag_and_tag (0.00s)
        server_test.go:6457: where comparing tag and tag: unexpected results
            query:  select foo from where_events where tennant = tennant
            params:  map[db:[db0]]
            exp:    {"results":[{"statement_id":0,"series":[{"name":"where_events","columns":["time","foo"],"values":[["2009-11-10T23:00:02Z","bar"],["2009-11-10T23:00:03Z","baz"],["2009-11-10T23:00:04Z","bat"],["2009-11-10T23:00:05Z","bar"],["2009-11-10T23:00:06Z","bap"]]}]}]}
            actual: {"results":[{"statement_id":0,"error":"bad WHERE clause. tag value must be inside single quotes: tennant = tennant"}]}

Copy link
Contributor

@lesam lesam left a comment

Choose a reason for hiding this comment

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

One comment

tsdb/store.go Outdated
_, lOk := e.LHS.(*influxql.VarRef)
_, rOk := e.RHS.(*influxql.VarRef)
if lOk && rOk {
return fmt.Errorf("bad WHERE clause. tag value must be inside single quotes: %s", e.String())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return fmt.Errorf("bad WHERE clause. tag value must be inside single quotes: %s", e.String())
return fmt.Errorf("bad WHERE clause for metaquery: either LHS or RHS must be a constant, not a tag or field: %s", e.String())

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should emphasize in the error that this is only a metaquery restriction, since normal selects are OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to mention single quotes so they know how to fix it. I don't think the fact that single quotes give literals and double quotes give fields and tags is very obvious. I also don't like LHS and RHS; too compiler-geeky. How about:

"bad WHERE clause for metaquery. one term must be a string literal tag value within single quotes: %s"

The user will know which term should be the tag value, presumably.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me, though I'd slightly prefer : instead of . since that's generally the style we use for error messages.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was formatting with period based on another message in that file:

"cannot delete data. DB contains shards using both inmem and tsi1 indexes. Please convert all shards to use the same index type to delete data."

Maybe I should have made a wider search for compound error statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

compromise, because I hate two colons in a singe sentence. semi-colon.

@davidby-influx davidby-influx merged commit af9e89a into master-1.x Oct 27, 2021
@davidby-influx davidby-influx deleted the DSB_bad_quote branch October 27, 2021 18:26
davidby-influx added a commit that referenced this pull request Oct 27, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)
davidby-influx added a commit that referenced this pull request Oct 27, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)
davidby-influx added a commit that referenced this pull request Oct 27, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)

closes #22756
davidby-influx added a commit that referenced this pull request Oct 27, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)
davidby-influx added a commit that referenced this pull request Oct 27, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)

closes #22786
davidby-influx added a commit that referenced this pull request Oct 27, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)

closes #22786
davidby-influx added a commit that referenced this pull request Oct 28, 2021
SHOW TAG KEYS FROM "foo" where bar="misquoted" is
erroneous, because the tag value must be enclosed
in single, not double, quotes. Although this
correctly returns no tag keys, it is very
inefficient and has cause out-of-memory failures
at a customer. This fix short-circuits the query.

closes #22755

(cherry picked from commit af9e89a)

closes #22757
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misquoted tag value in SHOW TAG KEYS query can cause OOM
2 participants