跳到主要内容

保护您的跟踪服务器免受网络暴露

MLflow 3.5.0+ 包含安全中间件,可防止 DNS 重新绑定、CORS 攻击和点击劫持。这些功能可用于默认的基于 FastAPI 的跟踪服务器 (uvicorn)。

DNS 重新绑定保护

验证 Host 标头,防止攻击内部服务

CORS 保护

控制哪些 Web 应用可以访问您的 MLflow API

点击劫持防护

X-Frame-Options 标头控制 iframe 嵌入

安全标头

自动标头可防止 MIME 嗅探和 XSS

可以通过 CLI 选项或环境变量配置安全设置。默认设置在本地开发中是安全的,但在生产环境中需要显式配置。

要求

安全中间件功能需要基于 FastAPI 的跟踪服务器 (uvicorn),这是 MLflow 3.5.0+ 中的默认服务器。

在使用 --gunicorn-opts--waitress-opts 时,这些功能不可用。

允许的主机

控制服务器接受哪些 Host 标头。这通过验证传入请求来防止 DNS 重新绑定攻击。

CLI 选项环境变量默认值
--allowed-hostsMLFLOW_SERVER_ALLOWED_HOSTSlocalhost,私有 IP

示例:

bash
# Specific hosts
mlflow server --allowed-hosts "mlflow.company.com,192.168.1.100"

# Wildcard patterns
mlflow server --allowed-hosts "*.company.com,192.168.*"

# Allow all (not recommended)
mlflow server --allowed-hosts "*"

CORS 源

指定哪些 Web 应用程序可以从浏览器发出 API 请求。

CLI 选项环境变量默认值
--cors-allowed-originsMLFLOW_SERVER_CORS_ALLOWED_ORIGINSlocalhost:*(任何 localhost 源)
bash
# Specific origins
mlflow server --cors-allowed-origins "https://app.company.com,https://notebook.company.com"

# Wildcard for subdomains
mlflow server --cors-allowed-origins "https://*.company.com"

# Allow all origins (development only)
mlflow server --cors-allowed-origins "*"

X-Frame-Options

设置 X-Frame-Options 标头以控制 iframe 嵌入行为。

CLI 选项环境变量默认值
--x-frame-optionsMLFLOW_SERVER_X_FRAME_OPTIONSSAMEORIGIN
  • SAMEORIGIN - 仅同源可以嵌入(默认)
  • DENY - 不允许嵌入
  • NONE - 任何站点都可以嵌入
bash
# Allow cross-origin iframe embedding
mlflow server --x-frame-options NONE

禁用安全中间件

完全禁用安全中间件。仅当安全由反向代理或网关处理时使用此选项。

CLI 选项环境变量默认值
--disable-security-middlewareMLFLOW_SERVER_DISABLE_SECURITY_MIDDLEWAREfalse
bash
mlflow server --disable-security-middleware

常见场景

本地开发

默认配置即可开箱即用。

bash
mlflow server

此设置接受来自 localhost 和任何私有 IP 的连接。

远程跟踪服务器

对于由多个已知用户访问的共享服务器,您可以将其配置为允许来自特定主机的连接。

bash
mlflow server --host 0.0.0.0 --allowed-hosts "mlflow.internal:5000,localhost:*"

然后使用 MLflow Python SDK 连接到远程跟踪服务器。

python
import mlflow

# Connect to remote server
mlflow.set_tracking_uri("http://mlflow.company.com:5000")

with mlflow.start_run():
mlflow.log_param("alpha", 0.5)
mlflow.log_metric("rmse", 0.1)
Host 与 Allowed Hosts

在 Jupyter Notebook 中渲染 MLflow UI

当 Notebook 在与 MLflow 服务器不同的域上运行时,请配置 CORS 和 frame 选项。

bash
# Allow embedding from notebook domain
mlflow server --host 0.0.0.0 \
--x-frame-options NONE \
--cors-allowed-origins "https://jupyter.company.com"

然后直接在 Notebook 单元格中嵌入特定的 MLflow 视图。

python
from IPython.display import IFrame

# Embed MLflow UI
IFrame(src="http://mlflow.company.com:5000", width=1000, height=600)

将 MLflow UI 嵌入 Web 应用程序

创建一个组件来显示 MLflow 内容。

jsx
function MLflowDashboard() {
return (
<iframe
src="http://mlflow.company.com:5000/experiments/1"
style={{ width: '100%', height: '800px' }}
title="MLflow"
/>
);
}

配置 MLflow 服务器以接受来自您的 Web 应用程序的请求。

bash
mlflow server --host 0.0.0.0 \
--x-frame-options NONE \
--cors-allowed-origins "https://:3000,https://app.company.com"

使用此 HTML 文件验证 iframe 配置是否正常工作。

html
<!DOCTYPE html>
<html>
<head>
<title>MLflow iframe Test</title>
</head>
<body>
<h1>MLflow Embedding Test</h1>
<iframe
src="https://:5000"
style="width: 100%; height: 600px; border: 1px solid #ccc;"
onload="document.getElementById('status').innerHTML = '✅ Loaded'"
onerror="document.getElementById('status').innerHTML = '❌ Failed'">
</iframe>
<div id="status">Loading...</div>
</body>
</html>