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: encodeURIComponent filters #342

Merged
merged 5 commits into from
Dec 13, 2021
Merged

fix: encodeURIComponent filters #342

merged 5 commits into from
Dec 13, 2021

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Dec 7, 2021

Summary

This PR encodeURIComponent filter values.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 7, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 60b35ee:

Sandbox Source
Vanilla Configuration

@eunjae-lee eunjae-lee marked this pull request as ready for review December 8, 2021 09:03
@eunjae-lee eunjae-lee requested a review from a team December 9, 2021 15:37
Copy link

@marchelbling marchelbling left a comment

Choose a reason for hiding this comment

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

👌

Copy link
Contributor

@JonathanMontane JonathanMontane left a comment

Choose a reason for hiding this comment

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

Not blocking per se, because I don't think the situation would happen frequently, but this encoding strategy is slightly different from the decoding strategy on the API side and could introduce subtle mismatches.

eventType: "click",
eventName: "my-event",
index: "my-index",
filters: ["brand:Cool Brand"]
Copy link
Contributor

Choose a reason for hiding this comment

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

A more interesting example could be filters: ["discount:20%"] because this would break with the current implementation, and is fixed with the changes.

Comment on lines 26 to 31
if (filter.indexOf(":") >= 0) {
const parts = filter.split(":");
return [parts[0], encodeURIComponent(parts[1])].join(":")
} else {
filter;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, looking at the behaviour on the insights API side, it seems that we do a decodeURIComponent on the entirety of the filter string, so this could probably be simplified with a filters.map(encodeURIComponent)

Code in the go monorepo: https://github.com/algolia/go/blob/master/pkg/searchapi/filter.go#L122

Here's a test:

curl -X POST \
https://insights.algolia.io/1/events \
-H 'x-algolia-api-key: ${API_KEY}' --referer 'http://example.com/debug' \
-H 'x-algolia-application-id: ${APP_ID}' -H 'x-algolia-agent: insights-js (2.1.0); insights-gtm (1.2.1)' \
-H "Content-Type: application/json" \
-d '{
    "events": [
        {
          "eventType": "click",
          "eventName": "Product Clicked",
          "index": "products",
          "userToken": "user-123456",
          "timestamp": 1639129812691,
          "filters": ["brand%3ACool%25%20Brand","promotion:20%25"],
          "queryID": "43b15df305339e827f0ac0bdc5ebcaa7"
        }
      ]
    }'

{"status":200,"message":"OK"}

Choose a reason for hiding this comment

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

I advised that we encoded only the filter value but it's indeed more complex. I overlooked pathological facet names.
Sorry Eunjae, I think that as Jon says, we should just urlencode the all filter.

Copy link
Contributor Author

@eunjae-lee eunjae-lee Dec 10, 2021

Choose a reason for hiding this comment

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

Yeah I think you're right. Last confirmation from @marchelbling ?

Choose a reason for hiding this comment

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

Yes (not sure if you saw my previous message), I overlooked the facet name; let's protect the full filter string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marchelbling Oh I didn't see your previous message 👍🏼

@eunjae-lee eunjae-lee merged commit d4012a7 into master Dec 13, 2021
@eunjae-lee eunjae-lee deleted the fix/encode-filters branch December 13, 2021 14:22
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.

3 participants