Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(ima): restrict resource media to the exact trusted host

Bingxi Zhao (Frank)719d8e78255a
diff --git a/deeptutor/services/rag/pipelines/ima/media.py b/deeptutor/services/rag/pipelines/ima/media.py
index 437d6047b..8e73473a1 100644
--- a/deeptutor/services/rag/pipelines/ima/media.py
+++ b/deeptutor/services/rag/pipelines/ima/media.py
@@ -37,9 +37,12 @@ from deeptutor.utils.document_extractor import (
from .envelope import ImaAPIError
# Official IMA media links are short-lived Tencent COS download URLs, and
-# note/file content is served from IMA's own resource host (res-pkb.ima.qq.com);
-# the downloader refuses anything outside these trusted Tencent domains.
-_ALLOWED_MEDIA_ROOT_DOMAINS = ("myqcloud.com", "ima.qq.com")
+# note/file content is served from IMA's exact resource host
+# (res-pkb.ima.qq.com). Do not trust every sibling under ima.qq.com: a broad
+# suffix allowlist would let an unrelated or later-compromised service become
+# an SSRF relay.
+_ALLOWED_MEDIA_ROOT_DOMAINS = ("myqcloud.com",)
+_ALLOWED_MEDIA_HOSTS = frozenset({"res-pkb.ima.qq.com"})
# Headers we never forward, whatever the API response asks for: hop-by-hop
# fields and anything that would carry identity to COS.
@@ -142,10 +145,11 @@ def validate_media_url(url: str) -> None:
hostname = (parsed.hostname or "").rstrip(".").lower()
if parsed.scheme != "https" or not hostname:
raise ImaAPIError("IMA media URL must use HTTPS.")
- if not any(
+ trusted_root = any(
hostname == root or hostname.endswith(f".{root}") for root in _ALLOWED_MEDIA_ROOT_DOMAINS
- ):
- raise ImaAPIError("IMA media URL is outside Tencent COS.")
+ )
+ if hostname not in _ALLOWED_MEDIA_HOSTS and not trusted_root:
+ raise ImaAPIError("IMA media URL is outside the trusted Tencent media hosts.")
def media_headers(raw: object) -> dict[str, str]:
diff --git a/tests/services/rag/test_ima_pipeline.py b/tests/services/rag/test_ima_pipeline.py
index a3854995c..7a842a159 100644
--- a/tests/services/rag/test_ima_pipeline.py
+++ b/tests/services/rag/test_ima_pipeline.py
@@ -314,6 +314,7 @@ class TestClientWire:
"http://bucket.cos.ap-guangzhou.myqcloud.com/file.pdf",
"https://example.com/file.pdf",
"https://myqcloud.com.evil.test/file.pdf",
+ "https://other.ima.qq.com/file.pdf",
],
)
def test_file_media_rejects_urls_outside_official_cos(self, url: str) -> None: