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

feat(Sharding*): contexts for broadcastEval #5756

Merged
merged 14 commits into from Jun 9, 2021

Conversation

amishshah
Copy link
Member

@amishshah amishshah commented Jun 5, 2021

Please describe the changes this PR makes and why it should be merged:

This allows developers to write safer eval code by allowing them to pass functions and contexts to broadcastEval in both ShardClientUtil and ShardingManager.

This avoids directly injecting malicious code into your eval scripts. Instead, you can treat your functions as templates (which you can make sure are safe) which are then injected with a context later on.

For example, the following code is vulnerable (taken and adapted from https://github.com/oadpoaw/discordjs-bot-rce). It's important to recognise that this isn't a security issue with discord.js -- using eval is inherently dangerous, and this is the clearest case of passing unsanitised input to eval.

function findUser(userId) {
  return this.users.cache.get(userId);
}

const unsafe = `');process.exit();a=('`; // causes the shard to exit
const results = await client.shard.broadcastEval(`(${findUser}).call(this, '${unsafe}')`);

While this isn't a security issue with discord.js, we can definitely make it much easier to write safer code. The following code snippet is not vulnerable.

function findUser(client, { userId }) {
  return this.users.cache.get(userId);
}

const unsafe = `');process.exit();a=('`;
const results = await client.shard.broadcastEval(findUser, { context: { userId: unsafe }});

It's important to note that the context you pass (e.g. in the above example, the context is { userId: unsafe } must be JSON-serializable. This means you can't pass complex data types in the context directly.

This PR removes support for calling broadcastEval with strings to enforce better practice with security.

Status and versioning classification:

  • Code changes have been tested against the Discord API, or there are no code changes
  • I know how to update typings and have done so, or typings don't need updating
  • This PR changes the library's interface (methods or parameters added)
  • This PR includes breaking changes (methods removed or renamed, parameters moved or removed)

@amishshah amishshah changed the title feat(Sharding*): parameter lists for broadcastEval feat(Sharding*): contexts for broadcastEval Jun 5, 2021
@SpaceEEC SpaceEEC self-requested a review June 5, 2021 14:54
src/sharding/ShardClientUtil.js Outdated Show resolved Hide resolved
src/sharding/ShardingManager.js Outdated Show resolved Hide resolved
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
@amishshah amishshah requested a review from kyranet June 6, 2021 09:18
@SpaceEEC
Copy link
Member

SpaceEEC commented Jun 7, 2021

Is there something speaking against requiring a function to be passed?
I fear this otherwise might go unnoticed / continues to be an issue with snippets / examples floating around.

@amishshah
Copy link
Member Author

Is there something speaking against requiring a function to be passed?
I fear this otherwise might go unnoticed / continues to be an issue with snippets / examples floating around.

Updated the PR with this suggestion! 😄

@amishshah amishshah requested review from kyranet and iCrawl June 8, 2021 20:25
@amishshah amishshah requested a review from vladfrangu June 8, 2021 20:25
src/sharding/ShardClientUtil.js Show resolved Hide resolved
src/sharding/ShardingManager.js Show resolved Hide resolved
@amishshah amishshah requested a review from SpaceEEC June 9, 2021 13:15
src/sharding/ShardingManager.js Outdated Show resolved Hide resolved
…n error

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Copy link
Member

@SpaceEEC SpaceEEC left a comment

Choose a reason for hiding this comment

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

One more thing

typings/index.d.ts Outdated Show resolved Hide resolved
typings/index.d.ts Outdated Show resolved Hide resolved
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
@amishshah amishshah requested a review from SpaceEEC June 9, 2021 13:28
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.

None yet

5 participants