简体   繁体   English

如何在 Vue.js 应用程序中设置内容安全策略标头

[英]How to set Content Security Policy Headers in a Vue.js app

I'm trying to make my Vue app secure, by eliminating clickjacking.我试图通过消除点击劫持来确保我的 Vue 应用程序安全。

I'm not sure how to set the headers of the Vue server, the methods the X Frame or Frame Ancestor directives in a meta tag like:我不确定如何设置 Vue 服务器的标头、X Frame 或 Frame Ancestor 指令在元标记中的方法,例如:

   <meta http-equiv="Content-Security-Policy" content="X-Frame-Options: DENY:"
    />

However, the links below say that this is unsupported https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors但是,下面的链接说这是不受支持的https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options https://developer.mozilla.org/en-US /docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors

I realise that for API requests, these headers should be set in the backend, however, this is to load the home page of the app (no api requests made).我意识到对于 API 请求,这些标头应该在后端设置,但是,这是为了加载应用程序的主页(没有 API 请求)。

How can I set these headers in my Vue.js server如何在我的 Vue.js 服务器中设置这些标头

The homepage of the app is still loaded over HTTP.该应用程序的主页仍通过 HTTP 加载。

The browser makes an HTTP request to get an HTML document.浏览器发出 HTTP 请求以获取 HTML 文档。 That HTML document includes <script> elements which cause the browser to fetch some JS.该 HTML 文档包含<script>元素,这些元素会导致浏览器获取一些 JS。 The browser runs some JS and uses it to modify the HTML document in the Vue app.浏览器运行一些 JS 并使用它来修改 Vue 应用程序中的 HTML 文档。

You should configure the X-Frame-Options and Content-Security-Policy to be served in the HTTP response headers for the response that delivers that initial HTML document which bootstraps the application.您应该将X-Frame-OptionsContent-Security-Policy配置为在 HTTP 响应标头中提供,以用于提供引导应用程序的初始 HTML 文档的响应。


The not-so-good approach is to set a CSP using a <meta> tag (in that same HTML document).不太好的方法是使用<meta>标签(在同一个 HTML 文档中)设置 CSP。 You can't set X-Frame-Options that way.你不能那样设置X-Frame-Options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM