Docusaurus V2设置安全标头

发布于 2025-02-06 23:30:58 字数 874 浏览 3 评论 0原文

docusaurus v2 我想实现一些安全标头。但是我找不到有关Docusaurus中安全标头的任何文件。 这是我要添加的一些标题,尤其是为了防止其他嵌入我的Docusaurus Web作为iframe。

 headers: [
                {
                    key: 'X-XSS-Protection',
                    value: '1; mode=block',
                },
                {
                    key: 'X-Content-Type-Options',
                    value: 'nosniff',
                },
                {
                    key: 'X-Download-Options',
                    value: 'noopen',
                },
                {
                    key: 'Cache-Control',
                    value: 'no-store',
                },
                {
                    key: 'X-Frame-Options',
                    value: 'SAMEORIGIN',
                },
            ],

我该怎么做?

In Docusaurus v2 I want to implement some security headers. But I couldn't find any documentation about security headers in Docusaurus.
Here are some header I want to add, especially to prevent other embed my docusaurus web as iframe.

 headers: [
                {
                    key: 'X-XSS-Protection',
                    value: '1; mode=block',
                },
                {
                    key: 'X-Content-Type-Options',
                    value: 'nosniff',
                },
                {
                    key: 'X-Download-Options',
                    value: 'noopen',
                },
                {
                    key: 'Cache-Control',
                    value: 'no-store',
                },
                {
                    key: 'X-Frame-Options',
                    value: 'SAMEORIGIN',
                },
            ],

How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不醒的梦 2025-02-13 23:30:59

如果使用Express Application,则可以按以下方式进行操作 -

为此,您可以在docusaurus.config.js文件中修改Express Server配置,如下所示:

  • 编辑docusaurus.config.js文件:

打开docusaurus.config.js file在您的Docusaurus项目的根目录中。

  • 安装依赖项(如果尚未安装)

确保您已安装了前一个响应中提到的头盔中间件。

npm install helmet
# or
yarn add helmet
  • 使用使用自定义安全标头的头盔中间件:

配置头盔中间件以包括您的自定义安全标头。以下是如何添加您提到的标头的示例:

const express = require('express');
const helmet = require('helmet');
const app = express();

// Enable Helmet middleware with custom security headers
app.use(
  helmet({
    contentSecurityPolicy: {
      directives: {
        defaultSrc: ["'self'"],
        scriptSrc: ["'self'", 'trusted-cdn.com'],
        // Add more CSP directives as needed
      },
    },
    // Add your custom headers here
    // For example, X-XSS-Protection, X-Content-Type-Options, etc.
    // You can add more headers following the same pattern
    // as shown below:
    // customHeaders: [
    //   {
    //     key: 'X-XSS-Protection',
    //     value: '1; mode=block',
    //   },
    //   {
    //     key: 'X-Content-Type-Options',
    //     value: 'nosniff',
    //   },
    //   {
    //     key: 'X-Download-Options',
    //     value: 'noopen',
    //   },
    //   {
    //     key: 'Cache-Control',
    //     value: 'no-store',
    //   },
    //   {
    //     key: 'X-Frame-Options',
    //     value: 'SAMEORIGIN',
    //   },
    // ],
  })
);

// ... Rest of your Docusaurus configuration ...

// Start the Docusaurus server
module.exports = {
  // ...
  scripts: {
    start: 'node server.js', // Use your server file name here
    // ...
  },
};

使用要添加的标题,自定义了Helmet()配置中的自定义标题阵列。您提供的示例包括用于XSS保护,内容类型选项,下载选项,缓存控制和框架选项的标题。根据您的特定安全要求调整它们。

使用您在docusaurus.config.js文件的脚本部分中指定的命令启动docusaurus应用程序。

通过这些修改,您的Docusaurus应用程序应包括您指定的自定义安全标头。这将通过减轻各种常见的Web安全漏洞来帮助提高应用程序的安全性。

如果您不使用Express,则可以创建一个替代应用程序,该应用程序将用作两个端点之间的中间件,并且该应用程序将用作为您添加安全标头的助手。

希望这会有所帮助。

If using express Application, you can do as follows -

To do this, you can modify your Express server configuration in the docusaurus.config.js file as follows:

  • Edit the docusaurus.config.js File:

Open the docusaurus.config.js file in the root directory of your Docusaurus project.

  • Install Dependencies (if not already installed)

Ensure that you have the helmet middleware installed as mentioned in the previous response.

npm install helmet
# or
yarn add helmet
  • Use Helmet Middleware with Custom Security Headers:

Configure the Helmet middleware to include your custom security headers. Here's an example of how to add the headers you've mentioned:

const express = require('express');
const helmet = require('helmet');
const app = express();

// Enable Helmet middleware with custom security headers
app.use(
  helmet({
    contentSecurityPolicy: {
      directives: {
        defaultSrc: ["'self'"],
        scriptSrc: ["'self'", 'trusted-cdn.com'],
        // Add more CSP directives as needed
      },
    },
    // Add your custom headers here
    // For example, X-XSS-Protection, X-Content-Type-Options, etc.
    // You can add more headers following the same pattern
    // as shown below:
    // customHeaders: [
    //   {
    //     key: 'X-XSS-Protection',
    //     value: '1; mode=block',
    //   },
    //   {
    //     key: 'X-Content-Type-Options',
    //     value: 'nosniff',
    //   },
    //   {
    //     key: 'X-Download-Options',
    //     value: 'noopen',
    //   },
    //   {
    //     key: 'Cache-Control',
    //     value: 'no-store',
    //   },
    //   {
    //     key: 'X-Frame-Options',
    //     value: 'SAMEORIGIN',
    //   },
    // ],
  })
);

// ... Rest of your Docusaurus configuration ...

// Start the Docusaurus server
module.exports = {
  // ...
  scripts: {
    start: 'node server.js', // Use your server file name here
    // ...
  },
};

Customize the customHeaders array within the helmet() configuration with the headers you want to add. The example you provided includes headers for XSS protection, content type options, download options, cache control, and frame options. Adjust them to your specific security requirements.

Start your Docusaurus application using the command you specified in the scripts section of your docusaurus.config.js file.

With these modifications, your Docusaurus application should include the custom security headers you've specified. This will help enhance the security of your application by mitigating various common web security vulnerabilities.

If you are not using express,then you can create an alternate application that will serve as a middleware between the two end points and that application will serve as a helper in adding the security headers for you.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文