Surrogates in UTF-8 encoding causes an unhandled exception in [p]debug #6485

Closed
opened 2024-12-04 17:40:40 +01:00 by Kreusada · 0 comments
Kreusada commented 2024-12-04 17:40:40 +01:00 (Migrated from github.com)

What Red version are you using?

3.5.13

Cog name

Dev

Command name

debug

What did you expect to happen?

A traceback to be gracefully sent as exception detail.

What actually happened?

The command itself errored:

[15:55:34] ERROR    [red] Exception in command 'debug'
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 6-7: surrogates not allowed

The above exception was the direct cause of the following exception:

╭───────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────╮
│ C:\Users\Kreusada\redenv\Lib\site-packages\discord\ext\commands\core.py:235 in wrapped                                   │
│ ❱  235             ret = await coro(*args, **kwargs)                                                                 │
│ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\dev_commands.py:486 in debug                                      │
│ ❱ 486         await output.send()                                                                                    │
│ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\dev_commands.py:167 in send                                       │
│ ❱ 167         await self.ctx.send_interactive(get_pages(str(self)), box_lang="py")                                   │
│ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\commands\context.py:183 in send_interactive                       │
│ ❱ 183         return await self.bot.send_interactive(                                                                │
│ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\bot.py:2377 in send_interactive                                   │
│ ❱ 2377                 msg = await channel.send(box(page, lang=box_lang))                                            │
│ C:\Users\Kreusada\redenv\Lib\site-packages\discord\abc.py:1618 in send                                                   │
│ ❱ 1618             data = await state.http.send_message(channel.id, params=params)                                   │
│ C:\Users\Kreusada\redenv\Lib\site-packages\discord\http.py:600 in request                                                │
│ ❱  600             kwargs['data'] = utils._to_json(kwargs.pop('json'))                                               │
│ C:\Users\Kreusada\redenv\Lib\site-packages\discord\utils.py:644 in _to_json                                              │
│ ❱  644         return orjson.dumps(obj).decode('utf-8')                                                              │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: str is not valid UTF-8: surrogates not allowed

How can we reproduce this error?

  1. Load a Red-DiscordBot instance with the --dev flag.
  2. Run [p]debug "\uD800" in any location

Anything else?

While the traceback suggests that this error can occur more broadly through Context.send, the root cause in that case would lie with the developer, as surrogate characters are prohibited in UTF-8 encoding (they are designed for UTF-16). For debugging purposes, I feel it would be necessary for the exception to be included in the debug output rather than being raised, so that the command doesn't fail. This would allow this invalid debugging statement to be gracefully evaluated. This exception may be reproduced for any surrogate characters (unicode code points beyond the basic multilingual plane).

Possible workarounds could include pre-validation/sanitization of the raw source, probably best fitted inside dev_commands.sanitize_output.

### What Red version are you using? 3.5.13 ### Cog name Dev ### Command name debug ### What did you expect to happen? A traceback to be gracefully sent as exception detail. ### What actually happened? The command itself errored: ```py [15:55:34] ERROR [red] Exception in command 'debug' UnicodeEncodeError: 'utf-8' codec can't encode characters in position 6-7: surrogates not allowed The above exception was the direct cause of the following exception: ╭───────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────╮ │ C:\Users\Kreusada\redenv\Lib\site-packages\discord\ext\commands\core.py:235 in wrapped │ │ ❱ 235 ret = await coro(*args, **kwargs) │ │ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\dev_commands.py:486 in debug │ │ ❱ 486 await output.send() │ │ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\dev_commands.py:167 in send │ │ ❱ 167 await self.ctx.send_interactive(get_pages(str(self)), box_lang="py") │ │ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\commands\context.py:183 in send_interactive │ │ ❱ 183 return await self.bot.send_interactive( │ │ C:\Users\Kreusada\redenv\Lib\site-packages\redbot\core\bot.py:2377 in send_interactive │ │ ❱ 2377 msg = await channel.send(box(page, lang=box_lang)) │ │ C:\Users\Kreusada\redenv\Lib\site-packages\discord\abc.py:1618 in send │ │ ❱ 1618 data = await state.http.send_message(channel.id, params=params) │ │ C:\Users\Kreusada\redenv\Lib\site-packages\discord\http.py:600 in request │ │ ❱ 600 kwargs['data'] = utils._to_json(kwargs.pop('json')) │ │ C:\Users\Kreusada\redenv\Lib\site-packages\discord\utils.py:644 in _to_json │ │ ❱ 644 return orjson.dumps(obj).decode('utf-8') │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ TypeError: str is not valid UTF-8: surrogates not allowed ``` ### How can we reproduce this error? 1. Load a Red-DiscordBot instance with the ``--dev`` flag. 2. Run `[p]debug "\uD800"` in any location ### Anything else? While the traceback suggests that this error can occur more broadly through `Context.send`, the root cause in that case would lie with the developer, as surrogate characters are prohibited in UTF-8 encoding (they are designed for UTF-16). For debugging purposes, I feel it would be necessary for the exception to be included in the debug output rather than being raised, so that the command doesn't fail. This would allow this invalid debugging statement to be gracefully evaluated. This exception may be reproduced for any surrogate characters (unicode code points beyond the basic multilingual plane). Possible workarounds could include pre-validation/sanitization of the raw source, probably best fitted inside `dev_commands.sanitize_output`.
Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot#6485
No description provided.