Skip to main content

A simple string.Formatter for disnake types

Project description

disnake.ext.formatter

disnake.ext.formatter is a module with a single class: a string.Formatter subclass.

This class, aptly named DisnakeFormatter, has special handling for disnake objects, in order to hide attributes that shouldn't be otherwise exposed.

This project is currently in an alpha state and should not be used in production code without understanding the risks.

Why is this needed?

With simple string format, user provided strings can easily give away your token if they know the attributes. There are some ways to get around these, but rely on hacks and validating the strings ahead of time, or scanning the output for known secrets, but this cannot catch all cases.

For example, the code below would reveal the bot token to the user.

USER_PROVIDED_STRING = "Welcome to {guild.name}, {member!s}! Also this bot's token is {member._state.http.token}!"


@client.event
async def on_member_join(member: disnake.Member):
    # process getting the guild and other config
    result = USER_PROVIDED_STRING.format(member=member)
    await member.send(result)

This example has been shortened for brevity. The typical usecase would be when there a configurable bot message that a user can change the content, and has access to a user/channel/guild/role object.

However, we require that none of the attributes that are attempted to access are private attributes, which mean this attack is not possible when using the DisnakeFormatter class correctly.

Future plans include having a hardcoded list of attributes which can be accessed on objects, the option to set that list to a desired mapping, and limiting attributes to specific types, to name but a few.

Examples

Because DisnakeFormatter is a subclass of string.Formatter, the behaviour is the same. However, this is not the same as using str.format. To use DisnakeFormatter, an instance of the class is required, of which there are no special arguments. From there, all that is necessary to do is use the format method, which follows the same behavior as string.Formatter.format().

from disnake.ext.formatter import DisnakeFormatter

USER_PROVIDED_STRING = "Welcome to {guild.name}, {member!s}! Also this bot's token is {member._state.http.token}!"


@client.event
async def on_member_join(member: disnake.Member):
    # process getting the guild and other config
    formatter = DisnakeFormatter()
    result = formatter.format(USER_PROVIDED_STRING, member=member)
    await member.send(result)

Instead of exposing the token, this will helpfully raise an error mentioning the attribute cannot be accessed on member.

Suppressing Errors

If desired, BlockedAttributeError errors can be suppressed without exposing the attribute. This can be done with the suppress_blocked_errors parameter to DisnakeFormatter. When enabled, rather than raising an error the formatter will not replace that specific attribute.

from disnake.ext.formatter import DisnakeFormatter

USER_PROVIDED_STRING = "Welcome to {guild.name}, {member!s}! Also this bot's token is {member._state.http.token}!"


@client.event
async def on_member_join(member: disnake.Member):
    # process getting the guild and other config
    formatter = DisnakeFormatter(suppress_blocked_errors=True)
    result = formatter.format(USER_PROVIDED_STRING, member=member)
    await member.send(result)
    # this sent the following message:
    # Welcome to disnake, Charlie#0000! Also this bot's token is {member._state.http.token}!


DocumentationGuideDiscord Server


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

disnake-ext-formatter-0.1.0a1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

disnake_ext_formatter-0.1.0a1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file disnake-ext-formatter-0.1.0a1.tar.gz.

File metadata

File hashes

Hashes for disnake-ext-formatter-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 78147b57afce711505dba6e1defdfb1ada9a4350964fe4b0516a11614b0297f5
MD5 f5c24a301584300a161ab9d2f0138eff
BLAKE2b-256 1f115ae2d22eeb232af5dd3086238c57745715dbb9d34f80b90a1f49432e125f

See more details on using hashes here.

File details

Details for the file disnake_ext_formatter-0.1.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for disnake_ext_formatter-0.1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad674df647c76347a0a0fade68b6890723376fcf45d761c6fa95b9b0efb3d4e9
MD5 e5706a67479d5666a7bce58850aa47ec
BLAKE2b-256 26483d8a28dfcf8c2bd5802fdd4785cd57c453e86ae99ca7d92125fc4547792f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page