Skip to content

Commit bd084a1

Browse files
authored
fix(security): verify TLS for default PyPI mirror (#10685)
Remove the hard-coded --trusted-host mirrors.aliyun.com argument from the in-process pip installer so certificate verification stays enabled for the configured PyPI index. Users of self-signed/private-CA mirrors can opt in explicitly through pip_install_arg (--cert or --trusted-host). The Aliyun mirror certificate chain anchors at GlobalSign Root R46, which is included in certifi >= 2025.4.26; both pip 25.0.1 and pip 26.2.2 validate it without --trusted-host. The flag was a 2024 compatibility workaround and is no longer needed after the dependency floor updates in #10670. Fixes #10684
1 parent 84a8ec1 commit bd084a1

7 files changed

Lines changed: 41 additions & 23 deletions

File tree

‎astrbot/core/config/default.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4916,7 +4916,7 @@ def get_local_permission_defaults(system: str | None = None) -> dict:
49164916
"pip_install_arg": {
49174917
"description": "pip 安装额外参数",
49184918
"type": "string",
4919-
"hint": "安装插件依赖时,会使用 Python 的 pip 工具。这里可以填写额外的参数,如 `--break-system-package` 等。",
4919+
"hint": "安装插件依赖时,会使用 Python 的 pip 工具。这里可以填写额外的参数,如 `--break-system-package` 等。PyPI 镜像的 TLS 证书默认会校验;若使用自签名/私有 CA 镜像,请通过 `--cert <CA 路径>` 指定信任的证书(推荐),或显式添加 `--trusted-host <主机名>`。",
49204920
},
49214921
"pypi_index_url": {
49224922
"description": "PyPI 软件仓库地址",

‎astrbot/core/utils/pip_installer.py‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,6 @@ def _cleanup_added_root_handlers(original_handlers: list[logging.Handler]) -> No
116116
handler.close()
117117

118118

119-
def _get_trusted_host_for_index_url(index_url: str) -> str | None:
120-
parsed = urlparse(index_url if "://" in index_url else f"//{index_url}")
121-
host = parsed.hostname
122-
if host == "mirrors.aliyun.com":
123-
return host
124-
return None
125-
126-
127119
def _normalize_sensitive_pip_key(raw_key: str) -> str:
128120
return raw_key.lstrip("-").replace("-", "_").lower()
129121

@@ -1005,10 +997,10 @@ def _build_pip_args(
1005997
)
1006998

1007999
if not _package_specs_override_index([*args[1:], *pip_install_args]):
1000+
# Never bypass TLS verification for the configured index. Users of
1001+
# self-signed/private-CA mirrors can opt in explicitly through
1002+
# pip_install_arg (e.g. --cert /path/to/ca.pem or --trusted-host).
10081003
index_url = mirror or self.pypi_index_url or "https://pypi.org/simple"
1009-
trusted_host = _get_trusted_host_for_index_url(index_url)
1010-
if trusted_host:
1011-
args.extend(["--trusted-host", trusted_host])
10121004
args.extend(["-i", index_url])
10131005

10141006
if pip_install_args:

‎dashboard/src/i18n/locales/en-US/features/config-metadata.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@
13271327
},
13281328
"pip_install_arg": {
13291329
"description": "Additional pip Installation Arguments",
1330-
"hint": "When installing plugin dependencies, Python's pip tool will be used. Additional arguments can be provided here, such as `--break-system-package`."
1330+
"hint": "When installing plugin dependencies, Python's pip tool will be used. Additional arguments can be provided here, such as `--break-system-package`. TLS certificates of the PyPI index are verified by default; for self-signed/private-CA mirrors, pass `--cert <CA bundle path>` (recommended) or explicitly add `--trusted-host <host>`."
13311331
},
13321332
"pypi_index_url": {
13331333
"description": "PyPI Repository URL",

‎dashboard/src/i18n/locales/ja-JP/features/config-metadata.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@
14281428
},
14291429
"pip_install_arg": {
14301430
"description": "pip インストールの追加引数",
1431-
"hint": "プラグインの依存関係をインストールする際は、Python の pip を使用します。`--break-system-package` などの追加引数を指定できます。"
1431+
"hint": "プラグインの依存関係をインストールする際は、Python の pip を使用します。`--break-system-package` などの追加引数を指定できます。PyPI インデックスの TLS 証明書は既定で検証されます。自己署名/プライベート CA のミラーを使用する場合は、`--cert <CA バンドルのパス>` を指定する(推奨)か、明示的に `--trusted-host <ホスト名>` を追加してください。"
14321432
},
14331433
"pypi_index_url": {
14341434
"description": "PyPI リポジトリアドレス",

‎dashboard/src/i18n/locales/ru-RU/features/config-metadata.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@
14321432
},
14331433
"pip_install_arg": {
14341434
"description": "Дополнительные аргументы установки pip",
1435-
"hint": "При установке зависимостей можно указать аргументы pip, напр. --break-system-package."
1435+
"hint": "При установке зависимостей плагинов используется pip Python. Здесь можно указать дополнительные аргументы, например --break-system-package. TLS-сертификаты индекса PyPI по умолчанию проверяются; для зеркал с самоподписанным/частным CA укажите --cert <путь к набору CA> (рекомендуется) или явно добавьте --trusted-host <хост>."
14361436
},
14371437
"pypi_index_url": {
14381438
"description": "URL репозитория PyPI",

‎dashboard/src/i18n/locales/zh-CN/features/config-metadata.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@
13481348
},
13491349
"pip_install_arg": {
13501350
"description": "pip 安装额外参数",
1351-
"hint": "安装插件依赖时,会使用 Python 的 pip 工具。这里可以填写额外的参数,如 `--break-system-package` 等。"
1351+
"hint": "安装插件依赖时,会使用 Python 的 pip 工具。这里可以填写额外的参数,如 `--break-system-package` 等。PyPI 镜像的 TLS 证书默认会校验;若使用自签名/私有 CA 镜像,请通过 `--cert <CA 路径>` 指定信任的证书(推荐),或显式添加 `--trusted-host <主机名>`。"
13521352
},
13531353
"pypi_index_url": {
13541354
"description": "PyPI 软件仓库地址",

‎tests/test_pip_installer.py‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ async def test_install_logs_redacted_pip_argv_when_credentials_present(monkeypat
18441844

18451845

18461846
@pytest.mark.asyncio
1847-
async def test_install_does_not_add_aliyun_trusted_host_for_default_index(monkeypatch):
1847+
async def test_install_does_not_add_trusted_host_for_default_index(monkeypatch):
18481848
run_pip = _make_run_pip_mock()
18491849

18501850
monkeypatch.setattr(PipInstaller, "_run_pip_in_process", run_pip)
@@ -1855,13 +1855,17 @@ async def test_install_does_not_add_aliyun_trusted_host_for_default_index(monkey
18551855
run_pip.assert_awaited_once()
18561856
recorded_args = run_pip.await_args_list[0].args[0]
18571857

1858-
assert "-i" in recorded_args
1859-
assert "https://pypi.org/simple" in recorded_args
1860-
assert "--trusted-host" not in recorded_args
1858+
assert recorded_args.count("-i") == 1
1859+
index_option = recorded_args.index("-i")
1860+
assert recorded_args[index_option + 1] == "https://pypi.org/simple"
1861+
assert not any(
1862+
arg == "--trusted-host" or arg.startswith("--trusted-host=")
1863+
for arg in recorded_args
1864+
)
18611865

18621866

18631867
@pytest.mark.asyncio
1864-
async def test_install_adds_aliyun_trusted_host_only_for_aliyun_index(monkeypatch):
1868+
async def test_install_does_not_add_trusted_host_for_aliyun_index(monkeypatch):
18651869
run_pip = _make_run_pip_mock()
18661870

18671871
monkeypatch.setattr(PipInstaller, "_run_pip_in_process", run_pip)
@@ -1872,8 +1876,30 @@ async def test_install_adds_aliyun_trusted_host_only_for_aliyun_index(monkeypatc
18721876
run_pip.assert_awaited_once()
18731877
recorded_args = run_pip.await_args_list[0].args[0]
18741878

1875-
assert "-i" in recorded_args
1876-
assert "https://mirrors.aliyun.com/simple" in recorded_args
1879+
assert recorded_args.count("-i") == 1
1880+
index_option = recorded_args.index("-i")
1881+
assert recorded_args[index_option + 1] == "https://mirrors.aliyun.com/simple"
1882+
assert not any(
1883+
arg == "--trusted-host" or arg.startswith("--trusted-host=")
1884+
for arg in recorded_args
1885+
)
1886+
1887+
1888+
@pytest.mark.asyncio
1889+
async def test_install_keeps_explicit_trusted_host_from_pip_install_arg(monkeypatch):
1890+
run_pip = _make_run_pip_mock()
1891+
1892+
monkeypatch.setattr(PipInstaller, "_run_pip_in_process", run_pip)
1893+
1894+
installer = PipInstaller(
1895+
"--trusted-host mirrors.aliyun.com",
1896+
pypi_index_url="https://mirrors.aliyun.com/simple",
1897+
)
1898+
await installer.install(package_name="demo-package")
1899+
1900+
run_pip.assert_awaited_once()
1901+
recorded_args = run_pip.await_args_list[0].args[0]
1902+
18771903
trusted_host_index = recorded_args.index("--trusted-host")
18781904
assert recorded_args[trusted_host_index + 1] == "mirrors.aliyun.com"
18791905

0 commit comments

Comments
 (0)