English 中文(简体)
How to Set content security policy in vue 3 compotion api typescript
原标题:

i need to set CSP in my vue 3 project. I tried to set below line in index.html from public folder and in head section

<meta http-equiv="Content-Security-Policy" content="default-src  self ; script-src  self ; style-src  self  fonts.googleapis.com; font-src  self  fonts.gstatic.com; img-src  self  data:; frame-src  self ;connect-src *;"/>

But when i run project it throws error because auto generated js and css files need to set by nonce or hash.. I don t know how to use nonce in vue3 app. (Note = i have api in dot net core 6 and this frontend and backend both will be hosted on windows server)

最佳回答

After few days of struggling i managed to set csp by below 2 packages.

npm install --save csp-html-webpack-plugin

npm install --save mini-css-extract-plugin

And i changed vue.config.js file as below:

const webpack = require( webpack );
const path = require( path );
const crypto = require( crypto );
const emotionalNonce = crypto.randomBytes(16).toString( base64 );
const emotionalNonce1 = crypto.randomBytes(16).toString( base64 );
const MiniCssExtractPlugin = require( mini-css-extract-plugin );
const CspHtmlWebpackPlugin = require( csp-html-webpack-plugin );
module.exports = {
  publicPath: process.env.VUE_APP_BASE_URL,
  configureWebpack: {
    devServer: {
      hot: false
  },
    devtool:  source-map ,
    plugins: [
      new webpack.DefinePlugin({
         process.env.EMOTIONAL_NONCE : JSON.stringify(emotionalNonce),
      }),
      new MiniCssExtractPlugin({
          filename: path.resolve(__dirname,  ../dist/css/[name].[hash:8].css ),
          chunkFilename: "[name].[hash:8].css",
          attributes: {
            nonce: emotionalNonce,
          }
      }),
          new CspHtmlWebpackPlugin({
             base-uri : " self ",
             object-src : " none ",
             script-src : [" self ",` nonce-${emotionalNonce1} `],
             style-src : [ " self ","fonts.googleapis.com",` nonce-${emotionalNonce} `]
          }, {
            enabled: true,
            hashingMethod:  sha256 ,
            hashEnabled: {
               script-src : true,
               style-src : true
            },
            nonceEnabled: {
               script-src : true,
               style-src : true
            },
          })
  ],
  module: {
      rules: [
        {
          test: /.s(c|a)ss$/,
          use: [
              MiniCssExtractPlugin.loader,
              {
                  loader:  css-loader ,
                  options: {
                      sourceMap: true,
                  }
              },
              {
                loader:  sass-loader ,
                options: {
                   implementation: require( sass ),
                   sassOptions: {
                      indentedSyntax: false // optional
                   }
                }
             }
          ]
      }
    ]
  }

  },
};
问题回答

暂无回答




相关问题
Blazor Server App : Unable to unprotect the message.State

I am getting an Exception as above in my Blazor server application. Below is my Program.cs File builder.Services.AddAuthentication(AzureADB2CDefaults.BearerAuthenticationScheme) .AddOpenIdConnect(...

System Text JsonSerializer Deserialization of TimeSpan

In researching how to deserialize a TimeSpan using Newtonsoft s JSON.net I came across code in my current project that did not use Json.net. It used System.Text.Json.JsonSerializer and appeared to not ...

TCP Connection Creation and Closing Event Hooking [closed]

What helper classes C# provides to monitor all TCP connection on an OS. A piece of Sample code would also be appreciated. Note: Original intent was to monitor this on Windows but would be nice to do ...

热门标签