fix(security): remove SSL verification downgrade fallback - #9459
Open
Qixuan112 wants to merge 2 commits into
Open
fix(security): remove SSL verification downgrade fallback#9459Qixuan112 wants to merge 2 commits into
Qixuan112 wants to merge 2 commits into
Conversation
…s#9446) - Remove insecure SSL fallback in download_image_by_url() - Remove insecure SSL fallback in download_file() - Change allow_insecure_ssl_fallback default to False - Add 13 comprehensive SSL verification tests - Update documentation to reflect security hardening Fixes AstrBotDevs#9446 BREAKING CHANGE: SSL certificate errors now raise immediately instead of retrying with verification disabled. This prevents MITM attacks but may affect code accessing servers with invalid certificates.
📄 Knowledge reviewDosu skipped reviewing this PR because your organization has used its |
Contributor
There was a problem hiding this comment.
Hey - I've found 2 issues, and left some high level feedback:
- Since
allow_insecure_ssl_fallbackis now deprecated and ignored, consider emitting a runtime deprecation warning when it is explicitly passed to make this change more visible to existing callers. - With the SSL fallback removed, you might want to log SSL-related connection failures at the call site (or inside these helpers) so that operational debugging still has clear visibility into certificate issues without silently relying on exceptions alone.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since `allow_insecure_ssl_fallback` is now deprecated and ignored, consider emitting a runtime deprecation warning when it is explicitly passed to make this change more visible to existing callers.
- With the SSL fallback removed, you might want to log SSL-related connection failures at the call site (or inside these helpers) so that operational debugging still has clear visibility into certificate issues without silently relying on exceptions alone.
## Individual Comments
### Comment 1
<location path="astrbot/core/utils/io.py" line_range="128-137" />
<code_context>
- cafile=certifi.where(),
- ) # 使用 certifi 提供的 CA 证书
- connector = aiohttp.TCPConnector(ssl=ssl_context) # 使用 certifi 的根证书
- async with aiohttp.ClientSession(
- trust_env=True,
- connector=connector,
- ) as session:
- if post:
- async with session.post(url, json=post_data) as resp:
- if not path:
- return save_temp_img(await resp.read())
- with open(path, "wb") as f:
- f.write(await resp.read())
- return path
- else:
- async with session.get(url) as resp:
- if not path:
- return save_temp_img(await resp.read())
</code_context>
<issue_to_address>
**suggestion (performance):** Consider adding a timeout to image downloads to avoid hanging connections.
The updated `download_image_by_url` logic makes HTTP calls without a timeout, so a slow or stalled server can tie up this coroutine indefinitely. Please add an explicit timeout (similar to `download_file`) so that connections fail predictably and don’t consume resources indefinitely.
Suggested implementation:
```python
path: str | None = None,
timeout: float = 30.0,
) -> str:
"""下载图片, 返回 path
:param url: 图片 URL
:param path: 保存路径, 如果为 None 则保存到临时文件
:param timeout: 下载超时时间(秒)
:return: 图片文件路径
"""
```
To fully implement the timeout behavior consistently with `download_file`, you should also:
1. Locate the body of `download_image_by_url` where `aiohttp.ClientSession` is created and update it to use an explicit timeout, for example:
- Add/ensure the import:
```python
import aiohttp
```
and if not already present:
```python
from aiohttp import ClientTimeout
```
- Wrap the session with a timeout derived from the new argument:
```python
client_timeout = ClientTimeout(total=timeout)
async with aiohttp.ClientSession(
trust_env=True,
connector=connector,
timeout=client_timeout,
) as session:
```
2. If `download_file` uses a shared configuration or constant for timeouts (e.g. `DEFAULT_HTTP_TIMEOUT`), consider:
- Replacing the literal `30.0` in the function signature with that constant.
- Passing the same timeout value when constructing `ClientTimeout` so both helpers behave consistently.
3. Wherever `download_image_by_url` is called, verify whether a custom timeout should be passed or if the default is sufficient; adjust calls as needed to avoid type errors (new `timeout` parameter).
</issue_to_address>
### Comment 2
<location path="astrbot/core/utils/io.py" line_range="264" />
<code_context>
async def download_file(
</code_context>
<issue_to_address>
**suggestion:** The deprecated `allow_insecure_ssl_fallback` parameter is now ignored; consider making this clearer in behavior or removing it.
The parameter is still present in the signature and its default is now `False`, which may incorrectly imply it affects behavior. Consider either removing it in a breaking change, or keeping it but emitting a warning/log when `True` is passed so the deprecation is clear at runtime.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
- Add deprecation warning for allow_insecure_ssl_fallback parameter - Add timeout parameter (default 30s) to download_image_by_url() - Add SSL error logging for better debugging - Implement consistent error handling across download functions Addresses feedback from sourcery-ai[bot] on PR AstrBotDevs#9459
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #9446 - SSL Verification Downgrade Vulnerability Fix
🔐 Security Fix
This PR fixes a critical security vulnerability where
download_image_by_url()anddownload_file()would silently downgrade SSL certificate verification to
CERT_NONEon SSL errors, allowingman-in-the-middle (MITM) attacks.
Vulnerability Details
Before Fix:
ssl.CERT_NONEAfter Fix:
🔧 Changes
Modified Functions
download_image_by_url()(lines 116-144)CERT_NONEdownload_file()(lines 250-291)allow_insecure_ssl_fallback=False(wasTrue)download_dashboard()(lines 452-549)allow_insecure_ssl_fallbackBreaking Change⚠️
Parameter default changed:
download_file(allow_insecure_ssl_fallback=False)(wasTrue)Impact: Code that relied on automatic SSL verification downgrade will now raise exceptions
for certificate errors. This is intentional security hardening.
Migration Guide:
allow_insecure_ssl_fallbackparameter is now deprecated and ignored✅ Testing
Test Coverage
tests/unit/test_io_ssl_verification.pytests/unit/test_io_download_file.pyTest Results
download_image_by_url()anddownload_file()enforce SSL verification📝 Documentation Updates
Updated Documentation
download_file()- Parameter documentation updated to indicate deprecationdownload_dashboard()- Parameter documentation updated to indicate deprecationParameter Documentation
🔍 Security Considerations
Why This Fix Matters
Affected Code Paths
download_file()ordownload_image_by_url()📋 Checklist
🔗 Related
218e88755(earlier SSL error handling work)Summary by Sourcery
Enforce strict SSL certificate verification for all download utilities and remove insecure fallback behavior that silently downgraded verification on errors.
Bug Fixes:
Enhancements:
Documentation:
Tests: