Skip to content

matchers: query now ANDs multiple keys #6054

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

Merged

Conversation

armadi1809
Copy link
Contributor

Closes #6053

@francislavoie please let me know if you would rather solve the issue some other way. Thanks!

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

Thanks for helping out with this! I wonder if it'd be a little more robust / readable if it instead checked for any that don't match?

Copy link
Member

@francislavoie francislavoie left a comment

Choose a reason for hiding this comment

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

Probably need a couple more test cases to cover when there's query keys with a value that doesn't match (not only keys that don't exist in the request)

@francislavoie francislavoie added the bug 🐞 Something isn't working label Jan 19, 2024
@francislavoie francislavoie added this to the v2.8.0 milestone Jan 19, 2024
@francislavoie francislavoie changed the title Changed logic in query matcher to only match when all keys are present matchers: query now ANDs multiple keys Jan 19, 2024
@armadi1809 armadi1809 force-pushed the fix-query-matcher-not-anding-keys branch 4 times, most recently from d598054 to 9102351 Compare January 20, 2024 04:37
@armadi1809
Copy link
Contributor Author

@mholt I am not sure I totally get what you mean by "if it instead checked for any that don't match?". I tried to tweak the logic a little bit to make it more readable / less nested. Happy to rework it again If you still don't like it.

@francislavoie I added a couple more tests around the case where the query key values do not match.

Also, I am not sure why the runner failed to download the action in the setup job of the failing test 😅

@francislavoie
Copy link
Member

francislavoie commented Jan 20, 2024

I think I see a possible bug. What if there's two values configured for a key and both match? It would increment twice. Need a test for that. I think the fix is to add a continue after incrementing (i.e. OR operation by short circuiting on the first positive match)

I re-ran the failing mac build. Probably just some flaky server.

@armadi1809
Copy link
Contributor Author

I thought about this but since we're only checking against paramVal[0], it won't increment the counter twice. However, now that I am thinking about it, there is actually another potential bug. Let's say the matcher is MatchQuery{"debug": []string{"1"}} and the input is "/?debug=1&debug=2". This would work and we would get true back from the matcher. However, if the input is reversed (i.e. "/?debug=2&debug=1") this would give us false because we are only checking against paramVal[0]. I don't exactly get why we're only checking agains the first element but I believe a better approach would be to change the check to slices.Contains(paramVal, v) and continue on the first match as you suggested above. Does this sound reasonable?

@francislavoie
Copy link
Member

Yeah, I think so. 👍

@armadi1809 armadi1809 force-pushed the fix-query-matcher-not-anding-keys branch from 9102351 to 8bd45b2 Compare January 20, 2024 15:46
@armadi1809
Copy link
Contributor Author

@francislavoie Any idea why some of the builds are failing to get the "slices" package?

@francislavoie
Copy link
Member

francislavoie commented Jan 20, 2024

Slices isn't available in Go 1.20, it's new in 1.21

@armadi1809
Copy link
Contributor Author

Should I then be using "golang.org/x/exp/slices" similar to the caddyconfig/httpcaddyfile/httptype.go file or manually check if the slice contains the value I am looking for?

@francislavoie
Copy link
Member

francislavoie commented Jan 20, 2024

Yeah, you can use "golang.org/x/exp/slices".

@armadi1809 armadi1809 force-pushed the fix-query-matcher-not-anding-keys branch 2 times, most recently from 81eb574 to 081d509 Compare January 20, 2024 16:33
@armadi1809
Copy link
Contributor Author

Should be good to go now. Thanks for the help!

@francislavoie
Copy link
Member

francislavoie commented Jan 20, 2024

Only thing is I'm not quite sure what the behaviour should be when there's multiple values provided for a query. There's an argument to be said that it should only match if there's a single matching value. But we didn't really envision this being used with multi-value query keys.

Whatever we decide (what you have now is probably fine), we'll need to document clearly the way it behaves. We should adjust the MatchQuery godoc comment for the JSON docs https://caddyserver.com/docs/json/apps/http/servers/routes/match/query/.

Edit: Oh we already document it as such:

NOTE: Notice that query string values are arrays, not singular values. This is because repeated keys are valid in query strings, and each one may have a different value. This matcher will match for a key if any one of its configured values is assigned in the query string. Backend applications relying on query strings MUST take into consideration that query string values are arrays and can have multiple values.

So yeah, should be fine.

@armadi1809
Copy link
Contributor Author

That sounds good. Let me know if we decide to treat that case differently then and I can adjust the code accordingly! Thanks again for your review/feedback.

@francislavoie francislavoie force-pushed the fix-query-matcher-not-anding-keys branch from 081d509 to 5f5279d Compare January 22, 2024 02:26
Copy link
Member

@francislavoie francislavoie left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for working on this!

@francislavoie francislavoie enabled auto-merge (squash) January 22, 2024 02:31
@francislavoie francislavoie merged commit ed7e3c9 into caddyserver:master Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query matcher doesn't AND different keys
3 participants