Skip to content

Commit 4c037c4

Browse files
authored
fix: resolve remote Slack file URLs before upload (#10355)
* fix: resolve remote Slack file URLs before upload * fix: clean up partial media downloads on cancellation * fix: preserve Slack uploads with local file URLs * fix: validate Slack file sources and normalize URL schemes
1 parent de31bbe commit 4c037c4

3 files changed

Lines changed: 309 additions & 7 deletions

File tree

‎astrbot/core/platform/sources/slack/slack_event.py‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from collections.abc import AsyncGenerator
44
from pathlib import Path
55
from typing import cast
6+
from urllib.parse import urlsplit
67

78
from slack_sdk.web.async_client import AsyncWebClient
89

@@ -15,6 +16,7 @@
1516
Plain,
1617
)
1718
from astrbot.api.platform import Group, MessageMember
19+
from astrbot.core.utils.media_utils import MediaResolver, file_uri_to_path
1820

1921

2022
class SlackMessageEvent(AstrMessageEvent):
@@ -67,12 +69,31 @@ async def _from_segment_to_slack_block(
6769
"alt_text": "图片",
6870
}
6971
if isinstance(segment, File):
70-
# upload file
71-
url = segment.url or segment.file
72-
response = await web_client.files_upload_v2(
73-
file=url,
74-
filename=segment.name or "file",
75-
)
72+
source = segment.url
73+
if source:
74+
if source.lower().startswith(("http://", "https://")):
75+
# MediaResolver expects lowercase HTTP(S) schemes.
76+
scheme, separator, remainder = source.partition(":")
77+
source = scheme.lower() + separator + remainder
78+
else:
79+
scheme = urlsplit(source).scheme
80+
if scheme not in ("", "file") and not Path(source).drive:
81+
raise ValueError("Slack file URLs must use HTTP or HTTPS.")
82+
local_path = Path(file_uri_to_path(source))
83+
if not await asyncio.to_thread(local_path.is_file):
84+
raise ValueError("Slack file upload requires an existing file.")
85+
source = str(local_path.absolute())
86+
source = source or await segment.get_file()
87+
if not source:
88+
raise ValueError(
89+
"Slack file upload requires a URL or an existing file."
90+
)
91+
# Resolve remote files and clean only resolver-owned temporary files.
92+
async with MediaResolver(source).as_path() as resolved:
93+
response = await web_client.files_upload_v2(
94+
file=await asyncio.to_thread(resolved.path.read_bytes),
95+
filename=segment.name or "file",
96+
)
7697
if not response["ok"]:
7798
logger.error(f"Slack file upload failed: {response['error']}")
7899
return {

‎astrbot/core/utils/media_utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ async def _materialize_media_ref(
486486
cleanup_paths.append(target_path)
487487
try:
488488
await download_file(media_ref, str(target_path))
489-
except Exception:
489+
except (Exception, asyncio.CancelledError):
490490
_cleanup_paths(cleanup_paths)
491491
raise
492492
mime_type = _guess_mime_type(target_path)

‎tests/test_slack_file_upload.py‎

Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
import asyncio
2+
from pathlib import Path
3+
from unittest.mock import AsyncMock
4+
5+
import pytest
6+
from slack_sdk.web.internal_utils import _to_v2_file_upload_item
7+
8+
from astrbot.api.event import MessageChain
9+
from astrbot.api.message_components import File, Plain
10+
from astrbot.api.platform import MessageType
11+
from astrbot.core.platform.astr_message_event import MessageSesion
12+
from astrbot.core.platform.platform import Platform
13+
from astrbot.core.platform.sources.slack.slack_adapter import SlackAdapter
14+
from astrbot.core.platform.sources.slack.slack_event import SlackMessageEvent
15+
from astrbot.core.utils import media_utils
16+
17+
18+
@pytest.fixture
19+
def download(monkeypatch, tmp_path):
20+
paths = []
21+
22+
async def fake_download(url, path):
23+
assert url in {
24+
f"{scheme}://example.com/README.md{query}"
25+
for scheme in ("http", "https")
26+
for query in ("", "?Token=AbC")
27+
}
28+
target = Path(path)
29+
target.write_bytes(b"# README\n")
30+
paths.append(target)
31+
32+
monkeypatch.setattr(media_utils, "get_astrbot_temp_path", lambda: str(tmp_path))
33+
mock = AsyncMock(side_effect=fake_download)
34+
monkeypatch.setattr(media_utils, "download_file", mock)
35+
return mock, paths
36+
37+
38+
@pytest.mark.asyncio
39+
@pytest.mark.parametrize("scheme", ["http", "https", "HTTP", "Https"])
40+
@pytest.mark.parametrize("name", ["README.md", "说明文档.md", ""])
41+
@pytest.mark.parametrize("query", ["", "?Token=AbC"])
42+
async def test_slack_remote_file_upload_uses_content_and_cleans_temp(
43+
download, scheme, name, query
44+
):
45+
client = AsyncMock()
46+
47+
async def upload(**kwargs):
48+
# Exercise the real SDK input conversion which previously opened the URL.
49+
item = _to_v2_file_upload_item(kwargs)
50+
assert download[1][0].exists()
51+
assert kwargs["filename"] == (name or "file")
52+
assert item["data"] == b"# README\n"
53+
assert item["filename"] == (name or "file")
54+
assert item["title"] == (name or "file")
55+
return {"ok": True, "files": [{"permalink": "https://slack.test/file"}]}
56+
57+
client.files_upload_v2.side_effect = upload
58+
segment = File(name=name, url=f"{scheme}://example.com/README.md{query}")
59+
blocks, text = await SlackMessageEvent._parse_slack_blocks(
60+
MessageChain([Plain("Attached README"), segment]), client
61+
)
62+
63+
assert blocks[0]["text"]["text"] == "Attached README"
64+
assert (
65+
blocks[1]["text"]["text"] == f"文件: <https://slack.test/file|{name or '文件'}>"
66+
)
67+
assert text == ""
68+
assert not download[1][0].exists()
69+
assert segment.file_ == ""
70+
download[0].assert_awaited_once_with(
71+
f"{scheme.lower()}://example.com/README.md{query}", str(download[1][0])
72+
)
73+
client.files_upload_v2.assert_awaited_once()
74+
75+
76+
@pytest.mark.asyncio
77+
@pytest.mark.parametrize("file_uri", [False, True])
78+
@pytest.mark.parametrize("fail_upload", [False, True])
79+
@pytest.mark.parametrize("source_fields", ["file", "url", "both", "different"])
80+
async def test_slack_local_file_is_preserved(
81+
tmp_path, download, file_uri, fail_upload, source_fields
82+
):
83+
path = tmp_path / "local.md"
84+
path.write_bytes(b"local content")
85+
client = AsyncMock()
86+
client.files_upload_v2.return_value = {
87+
"ok": True,
88+
"files": [{"permalink": "https://slack.test/local"}],
89+
}
90+
if fail_upload:
91+
client.files_upload_v2.side_effect = RuntimeError("upload failed")
92+
source = path.as_uri() if file_uri else str(path)
93+
other_path = tmp_path / "other.md"
94+
other_path.write_bytes(b"other content")
95+
segment = File(
96+
name="local.md",
97+
file=(
98+
str(other_path)
99+
if source_fields == "different"
100+
else source
101+
if source_fields in {"file", "both"}
102+
else ""
103+
),
104+
url=source if source_fields != "file" else "",
105+
)
106+
107+
if fail_upload:
108+
with pytest.raises(RuntimeError, match="upload failed"):
109+
await SlackMessageEvent._from_segment_to_slack_block(segment, client)
110+
else:
111+
await SlackMessageEvent._from_segment_to_slack_block(segment, client)
112+
113+
client.files_upload_v2.assert_awaited_once_with(
114+
file=b"local content", filename="local.md"
115+
)
116+
assert path.read_bytes() == b"local content"
117+
assert other_path.read_bytes() == b"other content"
118+
download[0].assert_not_awaited()
119+
120+
121+
@pytest.mark.asyncio
122+
@pytest.mark.parametrize("failure", ["download", "upload", "response"])
123+
async def test_slack_file_failure_cleans_download(download, failure):
124+
client = AsyncMock()
125+
if failure == "download":
126+
original = download[0].side_effect
127+
128+
async def fail_download(url, path):
129+
await original(url, path)
130+
raise RuntimeError("download failed")
131+
132+
download[0].side_effect = fail_download
133+
elif failure == "upload":
134+
client.files_upload_v2.side_effect = RuntimeError("upload failed")
135+
else:
136+
client.files_upload_v2.return_value = {"ok": False, "error": "upload failed"}
137+
138+
segment = File(name="README.md", url="https://example.com/README.md")
139+
if failure == "response":
140+
block = await SlackMessageEvent._from_segment_to_slack_block(segment, client)
141+
assert block["text"]["text"] == "文件上传失败"
142+
else:
143+
with pytest.raises(RuntimeError, match=f"{failure} failed"):
144+
await SlackMessageEvent._parse_slack_blocks(
145+
MessageChain([Plain("Attached README"), segment]), client
146+
)
147+
assert download[1]
148+
assert all(not path.exists() for path in download[1])
149+
client.chat_postMessage.assert_not_awaited()
150+
if failure == "download":
151+
client.files_upload_v2.assert_not_awaited()
152+
153+
154+
@pytest.mark.asyncio
155+
async def test_slack_cancelled_download_cleans_partial_file(download, tmp_path):
156+
client = AsyncMock()
157+
started = asyncio.Event()
158+
original = download[0].side_effect
159+
local_file = tmp_path / "keep.md"
160+
local_file.write_bytes(b"local content")
161+
162+
async def interrupted_download(url, path):
163+
await original(url, path)
164+
started.set()
165+
await asyncio.Event().wait()
166+
167+
download[0].side_effect = interrupted_download
168+
task = asyncio.create_task(
169+
SlackMessageEvent._from_segment_to_slack_block(
170+
File(name="README.md", url="https://example.com/README.md"), client
171+
)
172+
)
173+
try:
174+
await asyncio.wait_for(started.wait(), timeout=5)
175+
finally:
176+
task.cancel()
177+
with pytest.raises(asyncio.CancelledError):
178+
await task
179+
180+
assert download[1]
181+
assert all(not path.exists() for path in download[1])
182+
assert local_file.read_bytes() == b"local content"
183+
client.files_upload_v2.assert_not_awaited()
184+
185+
186+
@pytest.mark.asyncio
187+
@pytest.mark.parametrize("local_file", ["absent", "missing", "existing"])
188+
@pytest.mark.parametrize(
189+
"url",
190+
[
191+
"[README](https://example.com/README.md)",
192+
"ftp://example.com/file",
193+
"data:text/plain;base64,eA==",
194+
],
195+
)
196+
async def test_slack_invalid_file_url_fails_before_upload(
197+
download, tmp_path, url, local_file
198+
):
199+
client = AsyncMock()
200+
path = tmp_path / "local.md"
201+
if local_file == "existing":
202+
path.write_bytes(b"local content")
203+
with pytest.raises(ValueError):
204+
await SlackMessageEvent._from_segment_to_slack_block(
205+
File(
206+
name="README.md",
207+
file=str(path) if local_file != "absent" else "",
208+
url=url,
209+
),
210+
client,
211+
)
212+
download[0].assert_not_awaited()
213+
client.files_upload_v2.assert_not_awaited()
214+
215+
216+
@pytest.mark.asyncio
217+
@pytest.mark.parametrize("source_fields", ["file", "url", "different"])
218+
@pytest.mark.parametrize("file_uri", [False, True])
219+
async def test_slack_missing_file_fails_before_upload(
220+
tmp_path, download, source_fields, file_uri
221+
):
222+
client = AsyncMock()
223+
path = tmp_path / "missing"
224+
source = path.as_uri() if file_uri else str(path)
225+
other_path = tmp_path / "other.md"
226+
other_path.write_bytes(b"other content")
227+
with pytest.raises(ValueError, match="existing file"):
228+
await SlackMessageEvent._from_segment_to_slack_block(
229+
File(
230+
name="missing",
231+
file=(
232+
str(other_path)
233+
if source_fields == "different"
234+
else source
235+
if source_fields == "file"
236+
else ""
237+
),
238+
url=source if source_fields != "file" else "",
239+
),
240+
client,
241+
)
242+
download[0].assert_not_awaited()
243+
client.files_upload_v2.assert_not_awaited()
244+
245+
246+
@pytest.mark.asyncio
247+
@pytest.mark.parametrize("fail_upload", [False, True])
248+
async def test_slack_send_by_session_preserves_plain_and_failure_behavior(
249+
download, fail_upload, monkeypatch
250+
):
251+
monkeypatch.setattr(Platform, "send_by_session", AsyncMock())
252+
adapter = object.__new__(SlackAdapter)
253+
adapter.web_client = AsyncMock()
254+
adapter.web_client.files_upload_v2.return_value = {
255+
"ok": True,
256+
"files": [{"permalink": "https://slack.test/file"}],
257+
}
258+
if fail_upload:
259+
adapter.web_client.files_upload_v2.side_effect = RuntimeError("upload failed")
260+
session = MessageSesion(
261+
platform_name="slack", message_type=MessageType.GROUP_MESSAGE, session_id="C123"
262+
)
263+
chain = MessageChain(
264+
[
265+
Plain("Attached README"),
266+
File(name="README.md", url="https://example.com/README.md"),
267+
]
268+
)
269+
270+
if fail_upload:
271+
with pytest.raises(RuntimeError, match="upload failed"):
272+
await adapter.send_by_session(session, chain)
273+
adapter.web_client.chat_postMessage.assert_not_awaited()
274+
else:
275+
await adapter.send_by_session(session, chain)
276+
kwargs = adapter.web_client.chat_postMessage.await_args.kwargs
277+
assert kwargs["channel"] == "C123"
278+
assert kwargs["blocks"][0]["text"]["text"] == "Attached README"
279+
assert "https://slack.test/file" in kwargs["blocks"][1]["text"]["text"]
280+
assert download[1]
281+
assert all(not path.exists() for path in download[1])

0 commit comments

Comments
 (0)