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

Ignore query params when using customRoutes with SocketModeReceiver #1207

Merged
merged 4 commits into from
Nov 16, 2021
Merged

Ignore query params when using customRoutes with SocketModeReceiver #1207

merged 4 commits into from
Nov 16, 2021

Conversation

moustacheful
Copy link
Contributor

Summary

#1206

Allows for using query params in custom routes. Currently it will attempt to match the whole url including the query params.
With this change, it will skip the query params when matching.

Requirements (place an x in each [ ])

@CLAassistant
Copy link

CLAassistant commented Nov 11, 2021

CLA assistant check
All committers have signed the CLA.

@misscoded
Copy link
Contributor

Thanks again for taking the initiative to address the bug you've identified, @moustacheful! 🙌🏼

The same (flawed) logic is also present in the HTTPReceiver. If possible, would you mind implementing the same fix there, as well as the corresponding test?

@misscoded misscoded added the bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented label Nov 11, 2021
@seratch seratch added this to the 3.9.0 milestone Nov 11, 2021
@moustacheful
Copy link
Contributor Author

moustacheful commented Nov 12, 2021

Thanks again for taking the initiative to address the bug you've identified, @moustacheful! 🙌🏼

The same (flawed) logic is also present in the HTTPReceiver. If possible, would you mind implementing the same fix there, as well as the corresponding test?

Hey! no problem @misscoded. I believe the HTTPReceiver might not run into the same issue because of this:
https://github.com/slackapi/bolt-js/blob/main/src/receivers/HTTPReceiver.ts#L436

URL.pathname does not include the query params section of the URL.

that is, unless I'm looking at the wrong file 🕵️

@moustacheful
Copy link
Contributor Author

I've updated the PR to have the same implementation as HTTPReceiver, using URL instead. I'm sure it can save us some headache on some random edge case I might not have been considering with the .split('?').

@moustacheful moustacheful changed the title feat: ignore query params when matching customRoutes feat: ignore query params when matching customRoutes on SocketModeReceiver Nov 12, 2021
Copy link
Contributor

@misscoded misscoded left a comment

Choose a reason for hiding this comment

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

You're absolutely right! I'd entirely forgotten that the two receivers handled the req.url differently.

I left a note about the hardcoded localhost reference and what I anticipate will be an associated failing test, but other than that, everything looks to be in order. 👍🏼

const match = this.routes[req.url] && this.routes[req.url][method] !== undefined;
// NOTE: the domain and scheme are irrelevant here.
// The URL object is only used to safely obtain the path to match
const { pathname: path } = new URL(req.url as string, 'http://localhost');
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want to switch this hard-coded string out for the same http://${req.headers.host} that's featured in the HTTPReceiver implementation.

I believe that as a result, one of the tests will fail and the fakeReq will need to be augmented with the headers object (similar to what's seen in the test you've introduced).

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 actually changed it from that because it made it look like it had a purpose. As the comment on the other side explained

    // NOTE: the domain and scheme of the following URL object are not necessarily accurate. The URL object is only
    // meant to be used to parse the path and query

it doesn't actually serve any purpose beyond extracting the pathname, it's just there because URL needs a URL and not a URI.

I don't feel too strongly though, let me know if you still want it that way, I can fix it tomorrow.

Copy link
Member

Choose a reason for hiding this comment

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

@moustacheful Thanks for elaborating here!

You're right that, for the second argument in the URL constructor, choosing either localhost or req.headers.host does not matter in this scenario. That being said, we don't have a strong reason to use localhost (I know that's a common code example, though). Plus, we prefer consistency across the code base in general.

Could you do the same with the code in HTTPReceiver tomorrow? Thanks again for taking the time to report and fix this issue!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey yeah, no problem, I've updated HTTPReceiver to be consistent with SocketReceiver, and added a test case for this scenario on the former.

Copy link
Contributor

@filmaj filmaj left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks for creating the PR!

@seratch
Copy link
Member

seratch commented Nov 15, 2021

My suggestion in my last comment was to use req.headers.host for both receivers but the changes here are the opposite (using localhost consistently).

That being said, how we construct the URL objects is not important here. Either way, we don't extract the hostname value from URL objects. If other maintainers are fine with this change, please go ahead 👍

@codecov
Copy link

codecov bot commented Nov 15, 2021

Codecov Report

Merging #1207 (5201db9) into main (02061d7) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1207      +/-   ##
==========================================
+ Coverage   72.22%   72.26%   +0.03%     
==========================================
  Files          17       17              
  Lines        1433     1435       +2     
  Branches      430      430              
==========================================
+ Hits         1035     1037       +2     
  Misses        322      322              
  Partials       76       76              
Impacted Files Coverage Δ
src/receivers/HTTPReceiver.ts 44.82% <100.00%> (ø)
src/receivers/SocketModeReceiver.ts 88.88% <100.00%> (+0.25%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 02061d7...5201db9. Read the comment docs.

Copy link
Contributor

@misscoded misscoded left a comment

Choose a reason for hiding this comment

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

Since the other maintainers have expressed that they're OK with this change, I won't block it

@misscoded misscoded changed the title feat: ignore query params when matching customRoutes on SocketModeReceiver customRoutes doesn't properly handle query parameters using SocketModeReceiver Nov 16, 2021
@misscoded misscoded changed the title customRoutes doesn't properly handle query parameters using SocketModeReceiver Ignore query params when using customRoutes with SocketModeReceiver Nov 16, 2021
@misscoded misscoded merged commit 3392d14 into slackapi:main Nov 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug M-T: confirmed bug report. Issues are confirmed when the reproduction steps are documented
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants