Skip to content

feat(#92): add SSL/TLS configuration support for HTTP requests#282

Open
Oscaner wants to merge 1 commit intolanggenius:mainfrom
nodite:main
Open

feat(#92): add SSL/TLS configuration support for HTTP requests#282
Oscaner wants to merge 1 commit intolanggenius:mainfrom
nodite:main

Conversation

@Oscaner
Copy link

@Oscaner Oscaner commented Jan 16, 2026

Add comprehensive SSL/TLS configuration capabilities to the plugin system:

  • Add environment variables for SSL verification control, custom CA certificates, and mTLS client authentication
  • Implement httpx.Client monkey patch to automatically apply SSL settings from environment configuration
  • Support base64-encoded certificate data (CA cert, client cert, and client key) to avoid file system dependencies
  • Enable SSL verification bypass for development/testing environments
  • Apply SSL patches at module import time to ensure all HTTP requests use configured settings

This allows plugins to communicate securely with services using custom certificates or mutual TLS authentication without code changes in individual HTTP client implementations.

Pull Request Checklist

Thank you for your contribution! Before submitting your PR, please make sure you have completed the following checks:

Compatibility Check

  • I have checked whether this change affects the backward compatibility of the plugin declared in README.md
  • I have checked whether this change affects the forward compatibility of the plugin declared in README.md
  • If this change introduces a breaking change, I have discussed it with the project maintainer and specified the release version in the README.md
  • I have described the compatibility impact and the corresponding version number in the PR description
  • I have checked whether the plugin version is updated in the README.md

Available Checks

  • Code has passed local tests
  • Relevant documentation has been updated (if necessary)

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Oscaner, 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 enhances the security posture and flexibility of the plugin system by integrating comprehensive SSL/TLS configuration capabilities for outgoing HTTP requests. It allows users to configure certificate verification, custom CA certificates, and mutual TLS authentication through environment variables, which are then automatically applied to all httpx requests via a monkey patch. This change enables secure communication with diverse services without requiring modifications to individual plugin code, streamlining deployment in secure environments.

Highlights

  • Comprehensive SSL/TLS Support: Introduces robust SSL/TLS configuration capabilities for HTTP requests within the plugin system, enabling secure communication with various services.
  • Environment Variable Configuration: Allows control over SSL verification, custom CA certificates, and mutual TLS (mTLS) client authentication directly via environment variables.
  • Automatic SSL Application: Implements a monkey patch for httpx.Client to automatically apply SSL settings from environment configuration to all HTTP requests without requiring code changes in individual HTTP client implementations.
  • Base64 Certificate Support: Enables the use of base64-encoded certificate data for CA, client certificates, and client keys, reducing file system dependencies and simplifying deployment.
  • Development/Testing Bypass: Provides an option to bypass SSL verification for easier development and testing workflows, controlled by an environment variable.

🧠 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.

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.

@Oscaner
Copy link
Author

Oscaner commented Jan 16, 2026

/gemini review

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 introduces comprehensive SSL/TLS configuration for HTTP requests made via httpx, by monkey-patching httpx.Client. The changes allow configuring SSL verification, custom CA certificates, and mTLS through environment variables.

My review focuses on the security and implementation of the SSL context creation in python/dify_plugin/core/utils/ssl.py. I've identified a few areas for improvement:

  • A critical security enhancement to avoid writing CA certificates to disk by using the cadata parameter.
  • A high-severity security concern regarding the temporary storage of client private keys on the filesystem.
  • A recommendation to use more specific exception handling.

The new configuration options in DifyPluginEnv and the accompanying tests are well-implemented. Overall, this is a valuable feature for enhancing security.

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 introduces comprehensive SSL/TLS configuration for HTTP requests made via httpx, which is a great feature for enhancing security and connectivity in various environments. The implementation uses monkey-patching to apply settings globally, which is a clever approach to avoid changing all call sites.

My review focuses on improving the security and robustness of the new SSL utility module. I've identified a critical security vulnerability related to the handling of temporary files for certificates and private keys, and I've provided suggestions to mitigate this. I also have a few recommendations for improving exception handling and performance.

The addition of tests and example configuration is well done and makes the new feature easy to understand and verify.

@Oscaner
Copy link
Author

Oscaner commented Jan 16, 2026

/gemini review

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 introduces valuable SSL/TLS configuration capabilities for HTTP requests by monkey-patching httpx. The approach is solid, particularly the secure handling of temporary files for mTLS. However, I've identified a critical issue and a high-severity issue that need to be addressed:

  1. Critical: The monkey patch is not applied to httpx.AsyncClient, which means asynchronous requests will not use the new SSL configuration. This could lead to inconsistent behavior and security risks.
  2. High: The integration tests for the patching logic are flawed and do not correctly verify that the configuration is being applied, reducing confidence in the implementation.

I have provided detailed comments and suggestions to resolve these issues. The other changes, including the new configuration options and the security-focused tests for temporary files, are well-implemented.

@Oscaner
Copy link
Author

Oscaner commented Jan 16, 2026

/gemini review

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 introduces a robust mechanism for configuring SSL/TLS for HTTP requests via environment variables, including support for custom CA and mTLS. The implementation via monkey-patching httpx is clever and minimizes impact on existing code. The addition of comprehensive tests, especially for security aspects like temporary file handling, is excellent.

My review includes a couple of points for improvement. I've identified a high-severity security and portability issue related to file permissions on non-POSIX systems. I've also suggested a medium-severity refactoring to improve the readability of the SSL context creation logic.

Overall, this is a well-executed feature that significantly enhances the plugin's security capabilities.

@Oscaner
Copy link
Author

Oscaner commented Jan 16, 2026

/gemini review

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 introduces a robust mechanism for configuring SSL/TLS for all httpx requests by monkey-patching the client's __init__ methods. The changes are well-implemented, allowing for SSL verification control, custom CA certificates, and mTLS via environment variables. The use of base64-encoded data avoids filesystem dependencies, and the security of temporary files for mTLS keys is handled carefully. The accompanying tests are comprehensive, covering unit, integration, and security aspects. I have a couple of suggestions to improve code clarity and robustness.

@Oscaner
Copy link
Author

Oscaner commented Jan 16, 2026

/gemini review

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 introduces a well-designed and thoroughly tested feature for SSL/TLS configuration via environment variables. The approach of monkey-patching httpx at import time is clever and provides a seamless experience for plugin developers. The code quality is high, with excellent test coverage that includes functionality, edge cases, and security considerations. I've included a couple of minor suggestions to further improve code clarity in a security-sensitive area and enhance test suite portability.

@Oscaner
Copy link
Author

Oscaner commented Jan 17, 2026

/gemini review

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 introduces comprehensive SSL/TLS configuration for HTTP requests by monkey-patching httpx. The implementation is robust, covering custom CA certificates and mTLS, and is backed by an excellent set of tests that validate functionality, edge cases, and security aspects like temporary file handling. The changes are well-structured and the new configuration options are clearly documented. I have a couple of minor suggestions to improve code clarity and test precision, but overall this is a high-quality contribution.

Comment on lines +101 to +108
for file, data, path in [
(cert_file, client_cert_data, cert_file.name),
(key_file, client_key_data, key_file.name),
]:
if os.name == "posix":
os.chmod(path, 0o600)
file.write(data)
file.flush() # Ensure data is written to disk
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For improved readability and conciseness, you can simplify this loop by removing the redundant path variable. The file path can be accessed directly from the file object using file.name.

            for file, data in [(cert_file, client_cert_data), (key_file, client_key_data)]:
                if os.name == "posix":
                    os.chmod(file.name, 0o600)
                file.write(data)
                file.flush()  # Ensure data is written to disk

pass

# Verify that chmod was called with 0o600 for both files
assert len(permissions_set) >= 2
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For a more precise test assertion, it's better to check for an exact number of calls. Since os.chmod is expected to be called exactly twice (once for the certificate and once for the key), using == 2 makes the test stricter and more accurate.

Suggested change
assert len(permissions_set) >= 2
assert len(permissions_set) == 2

@Oscaner
Copy link
Author

Oscaner commented Jan 19, 2026

@Mairuis Please review

@Oscaner
Copy link
Author

Oscaner commented Jan 30, 2026

Hi @Yeuoly

httpx 不遵守 SSL_CERT_FILE、REQUESTS_CA_BUNDLE、CURL_CA_BUNDLE 这一类的 CA 证书环境参数,
所以通过自行打包 Docker 镜像进行 update-ca-certificates 也不行。httpx 必须要显式的传入证书链才支持自签名证书。
我们也开过两个企业版工单,但都没有解决我们的问题。

麻烦审核一下这个 PR,不然在企业内部环境,很多跟文件相关的插件都会因为自签名证书问题无法使用。

Add environment-based SSL configuration for httpx clients with automatic patching on module import. This enables plugins to handle enterprise scenarios requiring custom certificates or mTLS without code changes.

- Add SSL configuration environment variables to DifyPluginEnv:
  - HTTP_REQUEST_NODE_SSL_VERIFY: toggle SSL certificate verification
  - HTTP_REQUEST_NODE_SSL_CERT_DATA: custom CA certificate (base64)
  - HTTP_REQUEST_NODE_SSL_CLIENT_CERT_DATA: client cert for mTLS (base64)
  - HTTP_REQUEST_NODE_SSL_CLIENT_KEY_DATA: client key for mTLS (base64)
- Implement automatic SSL context patching for httpx.Client/AsyncClient
  - Monkey-patch client initialization to inject SSL configuration
  - Support custom CA certificates with secure temporary file handling
  - Support mutual TLS (mTLS) authentication
- Auto-apply patches on dify_plugin module import for zero-config usage
- Add comprehensive test suite covering SSL configuration and patching

This allows plugins to inherit SSL settings from environment variables transparently, supporting enterprise deployments with custom certificate authorities and mutual authentication requirements.
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.

1 participant