WIP: Allow dropping and elevating owner permissions #5419

Closed
Jackenmen wants to merge 37 commits from Jackenmen/su_and_sudo into V3/develop
Jackenmen commented 2021-10-24 04:03:00 +02:00 (Migrated from github.com)

Description of the changes

Supersedes #3999, fixes #3598

New user features

  • --enable-sudo flag that enables this whole functionality
    • This allows the bot owner to, for the most time, act as a regular user while still being able to make some administrative actions when needed
    • Added [p]su/[p]unsu commands that allow the bot owner to elevate/de-elevate their bot owner permissions
      • [p]su command will automatically de-elevate after a configurable timeout (15 minutes by default) passes
    • Added [p]sudo <command> command that allows the bot owner to run a specific command with bot owner permissions while keeping de-elevated permissions for everything else

Implementation

The feature is quite cool, I must say that... I cannot however say the same about the way this is (and probably how it has to be) implemented.
On the high level, this implementation provides 2 separate properties: all_owner_ids and owner_ids. The former is a simple (frozen) set which is pretty much the same as what we have in current stable releases under owner_ids property.
owner_ids in this implementation is where the magic happens. It magically provides you IDs of owners that are elevated in the current context and it is used for all kinds of bot owner checks like bot.is_owner(), @commands.is_owner(), etc. This all sounds relatively straightforward until we dive into how all of this "magic" is done.

Internally, we have a ContextVar that keeps track of owners with elevated privileges in the different contexts. Whenever owner_ids is accessed, we get the current value of that context var, or if it's not set, we use the current "globally" elevated owner IDs (as in the ones that are currently elevated through [p]su command). The context var is set for each processed command which means that within the context of the command, it is a constant, and the changes that are made with [p]su/unsu/sudo while it runs do not affect it. Because asyncio.Tasks have their own context, setting a value of a context var does not set that value in the global context and therefore accessing the global context will keep giving us the current "globally" elevated owner IDs as the default.

So based on this, we can expect that whenever the user runs the command, the command will get the currently elevated owner IDs when it tries to access owner_ids and the changes made to the global context during its runtime will not affect it. For better or worse, this however also means that any tasks spawned within that command will inherit the value of owner_ids that was set for it right before its execution. This is quite intentional for short-lived tasks like waiting for a message or reaction but might not be something that cog creator wants when trying to spawn a long-lived task that needs to access the currently elevated owner ids. Another somewhat similar problem is that the same long-lived tasks can also be created on cog load and they inevitably will inherit the value of owner_ids that was set for [p]load.
Currently, this is a limitation that I have not really tried addressing (and I don't exactly have an idea how to address it) and I'm not sure how much we can do if we don't want to make APIs completely impractical.

### Description of the changes Supersedes #3999, fixes #3598 #### New user features - `--enable-sudo` flag that enables this whole functionality - This allows the bot owner to, for the most time, act as a regular user while still being able to make some administrative actions when needed - Added `[p]su`/`[p]unsu` commands that allow the bot owner to elevate/de-elevate their bot owner permissions - `[p]su` command will automatically de-elevate after a configurable timeout (15 minutes by default) passes - Added `[p]sudo <command>` command that allows the bot owner to run a specific command with bot owner permissions while keeping de-elevated permissions for everything else #### Implementation The feature is quite cool, I must say that... I cannot however say the same about the way this is (and probably how it has to be) implemented. On the high level, this implementation provides 2 separate properties: `all_owner_ids` and `owner_ids`. The former is a simple (frozen) set which is pretty much the same as what we have in current stable releases under `owner_ids` property. `owner_ids` in this implementation is where the magic happens. It *magically* provides you IDs of owners that are elevated in the current context and it is used for all kinds of bot owner checks like `bot.is_owner()`, `@commands.is_owner()`, etc. This all sounds relatively straightforward until we dive into how all of this "magic" is done. Internally, we have a `ContextVar` that keeps track of owners with elevated privileges in the different contexts. Whenever `owner_ids` is accessed, we get the current value of that context var, or if it's not set, we use the current "globally" elevated owner IDs (as in the ones that are currently elevated through `[p]su` command). The context var is set for each processed command which means that within the context of the command, it is a constant, and the changes that are made with `[p]su/unsu/sudo` while it runs do not affect it. Because `asyncio.Task`s have their own context, setting a value of a context var does not set that value in the global context and therefore accessing the global context will keep giving us the current "globally" elevated owner IDs as the default. So based on this, we can expect that whenever the user runs the command, the command will get the currently elevated owner IDs when it tries to access `owner_ids` and the changes made to the global context during its runtime will not affect it. For better or worse, this however also means that any tasks spawned within that command will *inherit* the value of `owner_ids` that was set for it right before its execution. This is quite intentional for short-lived tasks like waiting for a message or reaction but might not be something that cog creator wants when trying to spawn a long-lived task that needs to access the currently elevated owner ids. Another somewhat similar problem is that the same long-lived tasks can also be created on cog load and they inevitably will inherit the value of `owner_ids` that was set for `[p]load`. Currently, this is a limitation that I have not really tried addressing (and I don't exactly have an idea how to address it) and I'm not sure how much we can do if we don't want to make APIs completely impractical.
Owner

Ich habe den PR geprüft und lasse ihn offen. Er ist WIP, der Autor beschreibt die Implementierung selbst als unschön, und er greift tief in die Owner-Prüfung ein (is_owner, Checks, Permissions-Cog). Ein falscher Merge würde dort direkt die Sicherheit betreffen. Ein Port auf den aktuellen Stand ist möglich, sollte aber mit einem frischen Review und Tests für su/unsu/sudo passieren.


Generated by Claude Code

Ich habe den PR geprüft und lasse ihn offen. Er ist WIP, der Autor beschreibt die Implementierung selbst als unschön, und er greift tief in die Owner-Prüfung ein (`is_owner`, Checks, Permissions-Cog). Ein falscher Merge würde dort direkt die Sicherheit betreffen. Ein Port auf den aktuellen Stand ist möglich, sollte aber mit einem frischen Review und Tests für `su`/`unsu`/`sudo` passieren. --- _Generated by [Claude Code](https://claude.ai/code)_
Owner

Auf Wunsch des Repo-Owners geschlossen; die Gründe stehen im Kommentar oben. Der PR kann jederzeit wieder geöffnet oder neu eingereicht werden.


Generated by Claude Code

Auf Wunsch des Repo-Owners geschlossen; die Gründe stehen im Kommentar oben. Der PR kann jederzeit wieder geöffnet oder neu eingereicht werden. --- _Generated by [Claude Code](https://claude.ai/code)_
Mrcoolgamer11 closed this pull request 2026-11-22 15:56:32 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
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
2 participants
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!5419
No description provided.