菜单
Enterprise 开源

配置安全加固

安全加固使您能够应用额外的安全性,这有助于阻止某些漏洞被恶意攻击者利用。

注意

这些设置可在 grafana.ini 配置文件中找到。要应用对配置文件的更改,请重新启动 Grafana 服务器。

Cookie 的附加安全性

如果 Grafana 使用 HTTPS,您可以进一步保护系统用于认证访问 Web UI 的 cookie。通过对 cookie 应用附加安全性,您可以减轻因攻击者获取 cookie 值而导致的某些攻击。

注意

以下配置要正常工作,Grafana 必须使用 HTTPS。

向 cookie 添加 Secure 属性

为了减轻某些 MITM 攻击,请向用于认证用户的 cookie 添加 Secure 属性。此属性强制用户仅通过有效的 HTTPS 安全连接发送 cookie。

示例

toml
[security]
# Set to true if you host Grafana behind HTTPS. The default value is false.
cookie_secure = true

向 cookie 添加 SameSite 属性

为了减轻几乎所有的 CSRF 攻击,将 cookie_samesite 选项设置为 strict。此设置阻止客户端在跨站请求中发送 cookie,但仅允许从创建 cookie 的站点发送。

示例

toml
[security]
# set cookie SameSite attribute. defaults to `lax`. can be set to "lax", "strict", "none" and "disabled"
cookie_samesite = strict

注意

通过将 SameSite 属性设置为“strict”,只有在 Grafana 实例内的用户点击才能生效。默认选项“lax”不会产生此行为。

您可以通过添加 Cookie 前缀来进一步保护 cookie 认证。即使站点使用 HTTPS,没有特殊前缀的 cookie 也可能在中间人攻击中被覆盖。Cookie 前缀强制客户端仅在满足特定条件时才接受 cookie。在当前 cookie 名称前添加 __Secure-__Host- 前缀,后者提供额外保护,仅允许 cookie 由发送 Set-Cookie 头的主机创建。

示例

toml
[auth]
# Login cookie name
login_cookie_name = __Host-grafana_session

安全头

Grafana 包含一些额外的头,您可以配置这些头来帮助减轻某些攻击,例如 XSS。

添加内容安全策略

内容安全策略 (CSP) 是一种 HTTP 响应头,用于控制 Web 浏览器如何处理内容,例如是否允许执行内联脚本或从特定域加载图像。默认的 CSP 模板已配置为提供足够的保护来抵御某些攻击。这使得攻击者在存在此类漏洞时更难执行任意 JavaScript。

示例

toml
[security]
# Enable adding the Content-Security-Policy header to your requests.
# CSP enables you to control the resources the user agent can load and helps prevent XSS attacks.
content_security_policy = true

# Set the Content Security Policy template that is used when the Content-Security-Policy header is added to your requests.
# $NONCE in the template includes a random nonce.
# $ROOT_PATH is server.root_url without the protocol.
content_security_policy_template = """script-src 'self' 'unsafe-eval' 'unsafe-inline' 'strict-dynamic' $NONCE;object-src 'none';font-src 'self';style-src 'self' 'unsafe-inline' blob:;img-src * data:;base-uri 'self';connect-src 'self' grafana.com ws://$ROOT_PATH wss://$ROOT_PATH;manifest-src 'self';media-src 'none';form-action 'self';"""

启用 Trusted Types

目前正在开发中。Trusted Types 是一种实验性 Javascript API,浏览器支持有限

Trusted Types 通过强制开发者对用于注入点(如设置元素的 innerHTML)的字符串进行清理来降低 DOM XSS 的风险。此外,启用 Trusted Types 后,这些注入点需要经过策略处理,该策略将对字符串进行清理,或者保持字符串不变并将其标记为“安全”。这为抵御第三方库(如 jQuery、Angular 甚至第三方插件)中的客户端注入漏洞提供了一定的保护。

要在强制模式下启用 Trusted Types,在此模式下,注入点会自动进行清理

  • 在配置中启用 content_security_policy
  • 在配置的 content_security_policy_template 中添加 require-trusted-types-for 'script'

要在报告模式下启用 Trusted Types,在此模式下,未使用 Trusted Types 清理的输入将被记录到控制台

  • 在配置中启用 content_security_policy_report_only
  • 在配置的 content_security_policy_report_only_template 中添加 require-trusted-types-for 'script'

由于此功能目前正在开发中,可能会出现问题。如果发生问题,或者您有其他反馈,请随时提出问题

其他安全加固

Grafana 服务器有几个内置的安全功能,您可以选择启用以增强安全性。本节介绍了一些可用于加强安全性的额外技术。

隐藏版本号

如果设置为 true,Grafana 服务器会为未认证用户隐藏正在运行的版本号。版本号可能会暴露您是否正在运行过时且存在漏洞的 Grafana 版本。

示例

toml
[anonymous.auth]
# mask the Grafana version number for unauthenticated users
hide_version = true

为指标启用认证

默认情况下,可以直接访问 Grafana 本身产生的指标而无需认证。这可能导致信息泄露。

示例

toml
[metrics]
# If both are set, basic auth will be required for the metrics endpoints
basic_auth_username =
basic_auth_password =

强制域名验证

如果设置为 true,Grafana 服务器会将 Host 头值与实际域名不匹配的请求重定向。这有助于减轻一些 DNS 重绑定攻击。

示例

toml
[sever]
# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
enforce_domain = true