-
Notifications
You must be signed in to change notification settings - Fork 53.7k
fix(security): validate inbound Slack file URLs against SSRF #44026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
zapabob
wants to merge
1
commit into
NousResearch:main
from
zapabob:codex/sec-fix-slack-inbound-ssrf-20260611
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| """SSRF regression tests for inbound Slack file downloads. | ||
|
|
||
| ``_download_slack_file`` / ``_download_slack_file_bytes`` attach the bot | ||
| token and follow redirects, so they must validate the destination (CWE-918) | ||
| exactly like the already-guarded outbound ``send_image`` path: a pre-flight | ||
| ``is_safe_url`` check plus a per-redirect guard. | ||
| """ | ||
| import asyncio | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from gateway.platforms.base import _ssrf_redirect_guard | ||
| from gateway.platforms.slack import SlackAdapter | ||
|
|
||
|
|
||
| def _fake_adapter(): | ||
| self = SlackAdapter.__new__(SlackAdapter) | ||
| self.config = SimpleNamespace(token="xoxb-test-token") | ||
| self._team_clients = {} | ||
| return self | ||
|
|
||
|
|
||
| class _NetworkTouched(RuntimeError): | ||
| pass | ||
|
|
||
|
|
||
| class _RecordingClient: | ||
| """Captures AsyncClient kwargs; refuses to perform real I/O.""" | ||
|
|
||
| last_kwargs = None | ||
|
|
||
| def __init__(self, **kwargs): | ||
| type(self).last_kwargs = kwargs | ||
|
|
||
| async def __aenter__(self): | ||
| return self | ||
|
|
||
| async def __aexit__(self, *exc): | ||
| return False | ||
|
|
||
| async def get(self, *args, **kwargs): | ||
| raise _NetworkTouched("network access attempted") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "method_name", | ||
| ["_download_slack_file", "_download_slack_file_bytes"], | ||
| ) | ||
| def test_unsafe_url_blocked_before_network(monkeypatch, method_name): | ||
| import tools.url_safety as url_safety | ||
|
|
||
| calls = {"checked": []} | ||
|
|
||
| def fake_is_safe_url(url, *a, **k): | ||
| calls["checked"].append(url) | ||
| return False | ||
|
|
||
| monkeypatch.setattr(url_safety, "is_safe_url", fake_is_safe_url) | ||
|
|
||
| # If the guard is bypassed, the fake client raises _NetworkTouched; a | ||
| # correct implementation raises ValueError *before* touching httpx. | ||
| monkeypatch.setattr("httpx.AsyncClient", _RecordingClient) | ||
|
|
||
| self = _fake_adapter() | ||
| method = getattr(self, method_name) | ||
| args = ("http://169.254.169.254/latest/meta-data/", ".jpg") \ | ||
| if method_name == "_download_slack_file" \ | ||
| else ("http://169.254.169.254/latest/meta-data/",) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| asyncio.run(method(*args)) | ||
|
|
||
| assert calls["checked"], "download must call is_safe_url before fetching" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "method_name", | ||
| ["_download_slack_file", "_download_slack_file_bytes"], | ||
| ) | ||
| def test_redirect_guard_is_wired(monkeypatch, method_name): | ||
| import tools.url_safety as url_safety | ||
|
|
||
| monkeypatch.setattr(url_safety, "is_safe_url", lambda *a, **k: True) | ||
| monkeypatch.setattr("httpx.AsyncClient", _RecordingClient) | ||
|
|
||
| self = _fake_adapter() | ||
| method = getattr(self, method_name) | ||
| args = ("https://files.slack.com/x.jpg", ".jpg") \ | ||
| if method_name == "_download_slack_file" \ | ||
| else ("https://files.slack.com/x.jpg",) | ||
|
|
||
| # The fake client raises when .get() is called; we only care that the | ||
| # client was constructed with the redirect guard hook. | ||
| with pytest.raises(_NetworkTouched): | ||
| asyncio.run(method(*args)) | ||
|
|
||
| kwargs = _RecordingClient.last_kwargs | ||
| assert kwargs is not None | ||
| hooks = kwargs.get("event_hooks", {}) | ||
| assert _ssrf_redirect_guard in hooks.get("response", []), ( | ||
| "AsyncClient must register _ssrf_redirect_guard to block " | ||
| "redirect-based SSRF" | ||
| ) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current main moved SlackAdapter from
gateway/platforms/slack.pytoplugins/platforms/slack/adapter.pyin5600105478ffde29d7566b45421b100eaa29c4ef; this import must be retargeted along with the production change.