Help us learn about your current experience with the documentation. Take the survey.
自定义 HTML 头部标签
- Tier: 免费版、专业版、旗舰版
- Offering: GitLab 自管版
如果您在欧盟或其他需要 Cookie 同意横幅的地区自管 GitLab 实例,您需要添加额外的 HTML 头部标签来引入脚本和样式表。
安全影响
在启用此功能前,您应了解其可能带来的安全影响。
一个原本合法的外部资源可能会遭到入侵,然后被用来从 GitLab 实例中的任何用户那里提取几乎所有数据。因此,您绝不应添加来自不受信任的外部来源的资源。如果可能,您应始终对第三方资源使用 Subresource Integrity 之类的完整性检查,以确认所加载资源的真实性。
请将您通过 HTML 头部标签添加的功能限制在最低限度。否则,如果您与 GitLab 的其他应用代码产生交互,可能会导致稳定性或功能问题。
添加自定义 HTML 头部标签
您必须将外部源添加到内容安全策略 (Content Security Policy) 中,该策略在 content_security_policy 选项中可用。在以下示例中,您需要扩展 script_src 和 style_src。
要添加自定义 HTML 头部标签:
-
编辑
/etc/gitlab/gitlab.rb文件并添加您的配置。例如:gitlab_rails['custom_html_header_tags'] = <<-'EOS' <script src="https://example.com/cookie-consent.js" integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://example.com/cookie-consent.css" integrity="sha384-+/M6kredJcxdsqkczBUjMLvqyHb1K/JThDXWsBVxMEeZHEaMKEOEct339VItX1zB" crossorigin="anonymous"> EOS gitlab_rails['content_security_policy'] = { # extend the following directives 'directives' => { 'script_src' => "'self' 'unsafe-eval' https://example.com https://www.google.com/recaptcha/ https://www.recaptcha.net/ https://www.gstatic.com/recaptcha/ https://apis.google.com", 'style_src' => "'self' 'unsafe-inline' https://example.com", } }
-
编辑
/home/git/gitlab/config/gitlab.yml文件:production: &base gitlab: custom_html_header_tags: | <script src="https://example.com/cookie-consent.js" integrity="sha384-Li9vy3DqF8tnTXuiaAJuML3ky+er10rcgNR/VqsVpcw+ThHmYcwiB1pbOxEbzJr7" crossorigin="anonymous"></script> <link rel="stylesheet" href="https://example.com/cookie-consent.css" integrity="sha384-+/M6kredJcxdsqkczBUjMLvqyHb1K/JThDXWsBVxMEeZHEaMKEOEct339VItX1zB" crossorigin="anonymous"> content_security_policy: directives: script_src: "'self' 'unsafe-eval' https://example.com http://localhost:* https://www.google.com/recaptcha/ https://www.recaptcha.net/ https://www.gstatic.com/recaptcha/ https://apis.google.com" style_src: "'self' 'unsafe-inline' https://example.com" -
保存文件并重启 GitLab:
# For systems running systemd sudo systemctl restart gitlab.target # For systems running SysV init sudo service gitlab restart