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

Check that uid is not used as function attribute #3112

Merged
merged 25 commits into from
Mar 15, 2019

Conversation

srfrog
Copy link
Contributor

@srfrog srfrog commented Mar 9, 2019

This change adds a check in newGraph() to prevent functions from using uid as an attribute. It also checks filter functions. uid is a protected attribute that should not be used directly but instead via function uid().

Additionally, this PR includes a change how empty string "" values are handled. Currently empty strings were blocked by the Gql parser. Now the empty string values are passed down to tasks and let the handler functions determine if the value is used.

e.g., New behavior with empty string values:

// Query: get all nodes with name that might be empty
{
  q(func: ge(name, "")) {
    uid
    name
  }
}

// Query: filter nodes with names at most 8 chars long.
{
  q(func: has(name)) @filter(match(name, "", 8)) {
    count(uid)
  }
}

Closes #3110


This change is Reviewable

srfrog added 2 commits March 8, 2019 19:35
This change adds a check in the Gql parser to prevent functions from
using `uid` as an attribute. `uid` is a protected attribute that
should not be used directly but instead via function `uid()`.
@srfrog srfrog requested a review from a team March 9, 2019 02:43
Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @srfrog)


gql/parser_test.go, line 4302 at r1 (raw file):

	}{
		{in: `{q(func: eq(name, "uid")) { uid }}`},
		{in: `{q(func: gt(name, "uid")) { uid }}`, failure: true},

Why is this a failure? name can "uid".

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @manishrjain)


gql/parser_test.go, line 4302 at r1 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Why is this a failure? name can "uid".

just preserving what was already there, I can change it to allow that.

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

Why do this at parser level? We can do this at Subgraph level, which would be more robust.

Reviewed 2 of 2 files at r1.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @manishrjain)

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

At parser level we can educate the user with an error. At subgraph level it is a failure, plus wasted effort.

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @manishrjain)

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

Reviewed 5 of 5 files at r2.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @manishrjain and @srfrog)


query/query.go, line 978 at r2 (raw file):

		} else {
			// Disallow uid as attribute - issue#3110
			if len(gq.Func.UID) == 0 {

Nice!


query/query1_test.go, line 1313 at r2 (raw file):

			failure: `Argument cannot be "uid`},
		{in: `{q(func:has(uid)) { uid }}`,
			failure: `Argument cannot be "uid`},

Can you bring the queries that you deleted in gql/parse_test.go here?


systest/queries_test.go, line 675 at r2 (raw file):

		{
			in:      `{q(func:match(term, "", 8)) {term}}`,
			failure: `Empty argument received`,

Why this change?

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

Reviewable status: 4 of 5 files reviewed, 4 unresolved discussions (waiting on @manishrjain)


query/query.go, line 978 at r2 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Nice!

Done.


query/query1_test.go, line 1313 at r2 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Can you bring the queries that you deleted in gql/parse_test.go here?

Sure, was missing one. It's back in.


systest/queries_test.go, line 675 at r2 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Why this change?

That test relied on the parser error, which is no longer needed. We will happily send the empty string and catch it later if it can't be allowed.

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @manishrjain and @srfrog)


query/query.go, line 978 at r2 (raw file):

Previously, srfrog (Gus) wrote…

Done.

And where do we check the empty argument for eq func?

@srfrog srfrog marked this pull request as ready for review March 14, 2019 21:22
@srfrog srfrog requested a review from a team March 14, 2019 21:23
Copy link
Contributor

@MichelDiz MichelDiz left a comment

Choose a reason for hiding this comment

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

Reviewable status: 3 of 8 files reviewed, 5 unresolved discussions (waiting on @manishrjain and @srfrog)


query/query1_test.go, line 1329 at r4 (raw file):

			out: `{"data":{"q":[]}}`},
		{in: `{q(func:uid(0x1)) { checkpwd(uid, "") }}`,
			out: `{"data":{"q":[{"checkpwd(uid)":false}]}}`},

I think this should return an error. UID is a reserved word. As checkpwd is a function to do comparison in Password type. So it should be denied the use of "uid" for this. Unless there's some other reason.

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

Reviewable status: 3 of 8 files reviewed, 5 unresolved discussions (waiting on @manishrjain and @MichelDiz)


query/query1_test.go, line 1329 at r4 (raw file):

Previously, MichelDiz (Michel Conrado) wrote…

I think this should return an error. UID is a reserved word. As checkpwd is a function to do comparison in Password type. So it should be denied the use of "uid" for this. Unless there's some other reason.

Good point, I will change it.

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

Reviewable status: 3 of 8 files reviewed, 5 unresolved discussions (waiting on @manishrjain and @MichelDiz)


query/query1_test.go, line 1329 at r4 (raw file):

Previously, srfrog (Gus) wrote…

Good point, I will change it.

Done.

Copy link
Contributor

@MichelDiz MichelDiz left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 5 files at r2, 3 of 5 files at r4, 2 of 2 files at r5.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @manishrjain)

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

:lgtm: Got a comment. Thanks!

Reviewed 3 of 5 files at r4, 2 of 2 files at r5, 1 of 1 files at r6.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @manishrjain and @srfrog)


worker/task.go, line 1598 at r6 (raw file):

		if err != nil {
			if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrSyntax {
				return nil, x.Errorf("Value in %s is not a number", q.SrcFunc.Name)

Might want to specify what the value is in the error. So, it's easier to figure out for the user.

Copy link
Contributor Author

@srfrog srfrog left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @manishrjain)


worker/task.go, line 1598 at r6 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

Might want to specify what the value is in the error. So, it's easier to figure out for the user.

good point, i will change it

@srfrog srfrog merged commit 98d7393 into master Mar 15, 2019
@srfrog srfrog deleted the srfrog/issue-3110_srcuids_assertion_fail branch March 15, 2019 21:40
dna2github pushed a commit to dna2fork/dgraph that referenced this pull request Jul 19, 2019
* gql/parser.go: disallow uid as attribute in functions

This change adds a check in the Gql parser to prevent functions from
using `uid` as an attribute. `uid` is a protected attribute that
should not be used directly but instead via function `uid()`.

* gql/parser_test.go: add more test cases for uid in functions

* gql/parser.go: remove uid attribute tests in parser

* query/query.go: add check for invalid uid attributes when newGraph

* gql/parser_test.go: remove obsolete parser tests

* query/query.go: match error with existing tests

* systest/queries_test.go: remove obsolete test expecting parser error

* query/query1_test.go: add query tests for uid attr

* query/query1_test.go: readd missing test case

* query/query0_test.go: add test for empty values comparison functions, filters.

* query/query.go: add check for uid attr in filters, minor syntax and typo cleanups

* query/query1_test.go: add tests for functions with uid attribute

* query/query3_test.go: add test for regexp() with empty value

* worker/task.go: nicer error for syntax error for conversion of NaN

* query/query0_test.go: add tests for functions with empty string value

* query/query.go: prevent some children functions from using uid attribute

* query/query1_test.go: fix typo in test

* worker/task.go: add value to error message, update the test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants