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: double quotes will be trimmed from the search token #8035

Merged
merged 3 commits into from
Dec 20, 2023

Conversation

jvillafanez
Copy link
Member

Description

Search terms from the graph API can be enclosed with double quotes. The double quotes are intended to prevent the interpretation of some tokens from the parser. However, the parser doesn't remove the double quotes.
This PR will remove those double quotes.

As a practical example, searching by email (such as [email protected]) can give problems because the @ char is interpreted as a different token (likely as a parameter alias, although I'm not fully sure). With this PR, you can search it by enclosing the email in double quotes, like $search="[email protected]" (note that url-encoding might be needed)

Related Issue

#7990

Motivation and Context

Allow searching users by email

How Has This Been Tested?

Manually tested via API, running something like curl -vk -ueinstein:relativity https://localhost:9200/graph/v1.0/users?%24search=%22marie%40example.org%22

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Technical debt
  • Tests only (no source changes)

Checklist:

  • Code changes
  • Unit tests added
  • Acceptance tests added
  • Documentation ticket raised:

@jvillafanez jvillafanez self-assigned this Dec 20, 2023
Copy link

update-docs bot commented Dec 20, 2023

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a changelog item based on your changes.

Copy link
Contributor

@rhafer rhafer left a comment

Choose a reason for hiding this comment

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

lgtm. Just a minor nitpick. Feel free to ignore.

searchValue := req.Search.Tree.Token.Value
if strings.HasPrefix(searchValue, "\"") && strings.HasSuffix(searchValue, "\"") {
searchValue = strings.Trim(searchValue, "\"")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: I guess in this case you can just call strings.Trim unconditionally. There is no need for the HasPrefix and HasSuffix calls. Or am I missing something?

The OData parser will already choke on strings that only contain a single double quote.

Copy link
Member Author

Choose a reason for hiding this comment

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

The OData parser will already choke on strings that only contain a single double quote.

I thought it was still possible. I'll remove the condition.

Copy link
Contributor

Choose a reason for hiding this comment

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

How are we dealing the same with single quotes?

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 single quotes have a completely different meaning here and are handled by the OData parser. In this case, as far as I understand the odata specs, using single quotes here won't even work. (our parsers doesn't allow it here at least https://github.com/CiscoM31/godata)

Copy link
Contributor

Choose a reason for hiding this comment

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

does a user (the one who is on the keyboard...) know this difference...?

Copy link
Contributor

Choose a reason for hiding this comment

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

The user shouldn't need to worry about quoting at all.

The UI/client which is constructing the query should know the difference, yes.

Copy link
Contributor

@rhafer rhafer left a comment

Choose a reason for hiding this comment

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

Hm, actually I think I overlooked something. We still have minimum search length check in https://github.com/owncloud/ocis/blob/master/services/graph/pkg/service/v0/users.go#L226.
Where check of the length of the possibly quoted string. I think we need to adjust that. Otherwise it would be simple to circumvent that limit (or at least reduce it by 2) by just adding quotes.

@jvillafanez jvillafanez force-pushed the fix/graph_double_quote_handling branch from cb78ef4 to 70f2e75 Compare December 20, 2023 15:06
@jvillafanez
Copy link
Member Author

Refactored the code a bit. The tests should pass now.
I'll manually retest the code just to make sure it works with the changes.

Copy link

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

2 New issues
0 Security Hotspots
54.5% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

@jvillafanez
Copy link
Member Author

I'll manually retest the code just to make sure it works with the changes.

Checked. Looks good now.

@rhafer rhafer merged commit 1bcc559 into master Dec 20, 2023
4 checks passed
@delete-merged-branch delete-merged-branch bot deleted the fix/graph_double_quote_handling branch December 20, 2023 16:24
@micbar
Copy link
Contributor

micbar commented Dec 20, 2023

I would love it to see a unit test for that 😍

@rhafer
Copy link
Contributor

rhafer commented Dec 21, 2023

I would love it to see a unit test for that 😍

They are part of #8050 now.

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

Successfully merging this pull request may close these issues.

4 participants