使用 Jekyll 实施内容安全策略
Implementing a content security policy with Jekyll
我的内容安全策略如下:
Content-Security-Policy: default-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; script-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; style-src 'self' https://fonts.googleapis.com; img-src 'self' ; font-src 'self' https://fonts.googleapis.com; connect-src 'self' https://ajax.googleapis.com; media-src 'self' ; object-src 'self' ; child-src 'self' ; frame-ancestors 'self' ; form-action 'none' ; sandbox allow-same-origin allow-scripts allow-pointer-lock;
您可以使用 meta http-equiv
部分实现它,但据我所知,它不允许您构建祖先和沙盒。您需要发送一个 http header。但是,我的虚拟主机不允许来自 HTML 文件的 php 命令,无论如何我都想避免它。
长话短说,在使用 Jekyll 作为生成器时,我有哪些选择来实施此策略?
听起来您有 quasi-answered 自己的问题,但您可能不喜欢这个答案。 Jekyll 只是一个静态 HTML 生成工具。除了生成 HTML 之外,它没有能力做任何事情,其中包括内联 HTML headers.
如果您无法访问网络服务器(nginx、apache、passenger standalone 等),因此无法从您的服务器配置文件写入 headers,那么您您必须找到一种方法在您的 Jekyll 模板中生成适当的 header。
您应该能够通过在您的 Jekyll 布局文件中正确设置 http-equiv
来设置自定义 header(可能是 _layouts/default.html
)。将以下内容添加到此文件应该有助于您的 http-equiv
CSP 元标记包含在 Jekyll 使用默认布局生成的所有 HTML 页面中:
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; script-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; style-src 'self' https://fonts.googleapis.com; img-src 'self' ; font-src 'self' https://fonts.googleapis.com; connect-src 'self' https://ajax.googleapis.com; media-src 'self' ; object-src 'self' ; child-src 'self' ; frame-ancestors 'self' ; form-action 'none' ; sandbox allow-same-origin allow-scripts allow-pointer-lock;" />
PS - 我意识到这没有解决您提到的 frame-ancestors
部分。不幸的是,除了与您的网络托管服务提供商合作以查看是否可以让他们以某种方式将您的 CSP header 插入您的站点配置之外,我不知道该问题的答案。
要在本地进行测试,您可以使用 Jekyll 指定自定义 headers。在您的 _config.yml
添加:
# Custom headers
webrick:
headers:
Content-Security-Policy: default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self';
My-Other-Header: My-Other-Value
来源:https://jekyllrb.com/docs/configuration/#custom-webrick-headers
我的内容安全策略如下:
Content-Security-Policy: default-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; script-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; style-src 'self' https://fonts.googleapis.com; img-src 'self' ; font-src 'self' https://fonts.googleapis.com; connect-src 'self' https://ajax.googleapis.com; media-src 'self' ; object-src 'self' ; child-src 'self' ; frame-ancestors 'self' ; form-action 'none' ; sandbox allow-same-origin allow-scripts allow-pointer-lock;
您可以使用 meta http-equiv
部分实现它,但据我所知,它不允许您构建祖先和沙盒。您需要发送一个 http header。但是,我的虚拟主机不允许来自 HTML 文件的 php 命令,无论如何我都想避免它。
长话短说,在使用 Jekyll 作为生成器时,我有哪些选择来实施此策略?
听起来您有 quasi-answered 自己的问题,但您可能不喜欢这个答案。 Jekyll 只是一个静态 HTML 生成工具。除了生成 HTML 之外,它没有能力做任何事情,其中包括内联 HTML headers.
如果您无法访问网络服务器(nginx、apache、passenger standalone 等),因此无法从您的服务器配置文件写入 headers,那么您您必须找到一种方法在您的 Jekyll 模板中生成适当的 header。
您应该能够通过在您的 Jekyll 布局文件中正确设置 http-equiv
来设置自定义 header(可能是 _layouts/default.html
)。将以下内容添加到此文件应该有助于您的 http-equiv
CSP 元标记包含在 Jekyll 使用默认布局生成的所有 HTML 页面中:
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; script-src 'self' https://fonts.googleapis.com https://ajax.googleapis.com; style-src 'self' https://fonts.googleapis.com; img-src 'self' ; font-src 'self' https://fonts.googleapis.com; connect-src 'self' https://ajax.googleapis.com; media-src 'self' ; object-src 'self' ; child-src 'self' ; frame-ancestors 'self' ; form-action 'none' ; sandbox allow-same-origin allow-scripts allow-pointer-lock;" />
PS - 我意识到这没有解决您提到的 frame-ancestors
部分。不幸的是,除了与您的网络托管服务提供商合作以查看是否可以让他们以某种方式将您的 CSP header 插入您的站点配置之外,我不知道该问题的答案。
要在本地进行测试,您可以使用 Jekyll 指定自定义 headers。在您的 _config.yml
添加:
# Custom headers
webrick:
headers:
Content-Security-Policy: default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self';
My-Other-Header: My-Other-Value
来源:https://jekyllrb.com/docs/configuration/#custom-webrick-headers