-
Notifications
You must be signed in to change notification settings - Fork 674
feat(web-api): add configs to toggle absolute url usage in dynamic api calls #2176
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
✅ All tests successful. No failed tests found. Additional details and impacted files@@ Coverage Diff @@
## main #2176 +/- ##
==========================================
+ Coverage 91.94% 91.99% +0.05%
==========================================
Files 38 38
Lines 10328 10386 +58
Branches 652 657 +5
==========================================
+ Hits 9496 9555 +59
+ Misses 820 819 -1
Partials 12 12
Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 A few quick notes on these changes for the kind reviewers!
A dynamic method name can either be in the format shown above, to be sent with the [`slackApiUrl`](#custom-api-url) | ||
option, or as an absolute URL. Setting the | ||
[`allowAbsoluteUrls`](/reference/web-api/interfaces/WebClientOptions#allowabsoluteurls) | ||
option to `false` sends all requests to the `slackApiUrl` option value: | ||
|
||
```javascript | ||
const { WebClient } = require('@slack/web-api'); | ||
|
||
const web = new WebClient(token, { | ||
allowAbsoluteUrls: false, | ||
}); | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔗 And a link for reference: https://tools.slack.dev/node-slack-sdk/web-api#call-a-method
packages/web-api/src/WebClient.ts
Outdated
const requestURL = url.startsWith('https' || 'http') && this.allowAbsoluteUrls | ||
? url | ||
: `${this.axios.getUri() + url}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 The requestURL
variable is only used in debug logs, axios
details configured separately, though this did seem close to what the logic of allowAbsoluteUrls
might do!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes look good 💯
I'm just not clear on when a developer would want to do this, why would they be passing and override URL if it will have not effect 🤔 instead of this addition should we advise them to not pass the override value if they don't want to override the URL?
packages/web-api/src/WebClient.ts
Outdated
@@ -618,7 +646,8 @@ export class WebClient extends Methods { | |||
// TODO: better input types - remove any | |||
const task = () => | |||
this.requestQueue.add(async () => { | |||
const requestURL = url.startsWith('https' || 'http') ? url : `${this.axios.getUri() + url}`; | |||
const requestURL = | |||
url.startsWith('https' || 'http') && this.allowAbsoluteUrls ? url : `${this.axios.getUri() + url}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The complexity of this line seems to be growing, could it be more readable if we broke it up in an if
statement 🤔 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the same... So much is happening here 😓
I'll explore the if
statements, but do also like the const
allowed in the current setup 👾
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe breaking the logic out into its own function could make sense
private deriveRequestUrl(url: string): string {
if (this.allowAbsoluteUrls) {
return url.startsWith('https' || 'http') ? url : `${this.axios.getUri() + url}`
}
return `${this.axios.getUri() + url}`;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar logic was moved into a deriveRequestUrl
function in 5f98b56! I forget private
functions can be useful sometimes - it's a great call 😅
@WilliamBergamin Thanks for checking out the changes! ✨ I don't believe this option is needed for scripts or apps making known API calls, but this option can be useful in setups that might pass unknown methods to The same guards against absolute URLs could definitely be added to application code, prefixing all |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how the changes could be useful for security use cases 🤔
I've added one more comment about breaking the requestUrl
logic into a method to increase readability, let me know what you think
Co-authored-by: William Bergamin <[email protected]>
@WilliamBergamin 🙏 Thanks for suggesting the improvement to code structures. I'm hoping this'll make later changes around this logic a bit quicker to understand! Tests continue to pass with that change too, but please let me know if other changes are requested! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 💯 this looks good
@WilliamBergamin Thanks so much calling out the changes of this PR 🙏 I did a bit more testing and found one thing strange with Now is seeming like an alright time to merge this! I'll open a release PR for |
Summary
This PR adds a new
allowAbsoluteUrls
option to theWebClient
constructor that can toggle if theslackApiUrl
is always the prefix of the request URL when calling Slack API methods or if an absolute URL can override this.To keep the current behavior,
allowAbsoluteUrls
defaults totrue
📚Preview
The following code attempts to make a request to "https://slack.com/api/https://example.com/method"!
Reviewers
The above example can be used with Slack API methods in "dot" notation -
chat.postMessage
- and different values of theallowAbsoluteUrls
option 👾For fast reference, it might be neat to use the absolute "https://slack.com/api/chat.postMessage" URL.
Requirements