Skip to content

fix: restrict debug endpoints to development mode#33428

Open
mango766 wants to merge 1 commit intolanggenius:mainfrom
mango766:fix/restrict-debug-endpoints
Open

fix: restrict debug endpoints to development mode#33428
mango766 wants to merge 1 commit intolanggenius:mainfrom
mango766:fix/restrict-debug-endpoints

Conversation

@mango766
Copy link

Wraps the /threads and /db-pool-stat debug endpoints in a development mode check so they're only registered when the app is running in debug or testing mode.

These endpoints expose internal server state (thread info, DB pool config) without authentication, which is fine during development but shouldn't be available in production.

Closes #33427

The /threads and /db-pool-stat endpoints expose internal server state
(thread info, DB pool configuration) without any authentication.
Wrap them with a dify_config.DEBUG check so they are only registered
when the app is running in debug or testing mode.

Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
@mango766 mango766 requested a review from QuantumGhost as a code owner March 13, 2026 16:19
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the application's security posture by ensuring that sensitive debug endpoints are not accessible in production. By gating these endpoints behind a development mode check, it prevents unauthorized access to internal system details, thereby reducing potential attack surfaces and safeguarding critical infrastructure information.

Highlights

  • Debug Endpoints Restriction: The /threads and /db-pool-stat debug endpoints are now conditionally registered, only becoming active when the application is running in debug or testing mode.
  • Enhanced Security: This change prevents the exposure of internal server state, such as thread information and database pool configuration, in production environments.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • api/extensions/ext_app_metrics.py
    • Wrapped the /threads endpoint registration within a conditional block that checks for dify_config.DEBUG or app.config.get("TESTING").
    • Wrapped the /db-pool-stat endpoint registration within the same conditional block to restrict its availability to development and testing environments.
Activity
  • No activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly restricts the debug endpoints /threads and /db-pool-stat to development and testing environments, which is an important security improvement. I have a couple of suggestions to improve the code's maintainability and readability within the affected functions.

Comment on lines +34 to +46
thread_list = []
for thread in threads:
thread_name = thread.name
thread_id = thread.ident
is_alive = thread.is_alive()

return {
"pid": os.getpid(),
"thread_num": num_threads,
"threads": thread_list,
}
thread_list.append(
{
"name": thread_name,
"id": thread_id,
"is_alive": is_alive,
}
)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This loop to build thread_list can be simplified into a more concise and Pythonic list comprehension. This improves readability by reducing boilerplate code.

            thread_list = [{
                "name": thread.name,
                "id": thread.ident,
                "is_alive": thread.is_alive(),
            } for thread in threads]

"checked_out_connections": engine.pool.checkedout(), # type: ignore
"overflow_connections": engine.pool.overflow(), # type: ignore
"connection_timeout": engine.pool.timeout(), # type: ignore
"recycle_time": db.engine.pool._recycle, # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Accessing the protected member _recycle is fragile as it's an internal implementation detail of SQLAlchemy and could change in a future version, breaking this code. If you are using SQLAlchemy 1.4+, you should use the public attribute db.engine.pool.recycle. If a public API is not available for your version, consider adding a comment to explain why _recycle is used and which SQLAlchemy version this code relies on.

                "recycle_time": db.engine.pool.recycle,  # type: ignore

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Mar 13, 2026
@github-actions
Copy link
Contributor

Pyrefly Diff

No changes detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Debug endpoints /threads and /db-pool-stat are accessible without authentication

1 participant