2017-07-28 58 views
4

我的反應WEBAPP給這個錯誤在瀏覽器控制檯拒絕加載字體'data:font/woff .....'它違反了以下內容安全策略指令:「default-src'self'」。請注意, '字體-SRC'

Refused to load the font 'data:font/woff;base64,d09........' because it` 
`violates the following Content Security Policy directive: "default-src` `'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback. 

另外:

Refused to connect to 'ws://localhost:3000/sockjs-node/782/oawzy1fx/websocket' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback. 

瀏覽器控制檯 enter image description here

指數的屏幕截圖.html有沒有這個元:

<meta http-equiv="Content-Security-Policy" content="img-src 'self' data:; default-src 'self' http://121.0.0:3000/"> 

WebPack.cofig.js

var debug = process.env.NODE_ENV !== "production"; 
var webpack = require('webpack'); 
var path = require('path'); 

module.exports = { 
    context: path.join(__dirname, "./src"), 
    devtool: debug ? "inline-sourcemap" : true, 
    entry: "../src/index.js", 

    module: { 
     rules: [ 
      { 
       test: /\.(jpe?g|png|gif|svg|woff|woff2|eot|ttf)$/i, // a regular expression that catches .js files 
       exclude: /node_modules/, 
       loader: 'url-loader', 
      }, 
      { 
       test: /\.(js|.jsx)$/, 
       exclude: /(node_modules|bower_components)/, 
       loader: 'babel-loader', 
       query: { 
        presets: ['react','es2016', 'stage-0',], 
        plugins: ['react-html-attrs', 'transform-decorators-legacy', 'transform-class-properties'], 
       } 
      }, 
      { 
       test: /\.css$/, 
       use: [ 
        "style-loader", 
        { 
         loader: "css-loader", 
        } 
       ] 
      } 
     ] 
    }, 
    resolve: { 
     modules: [ 
      path.join(__dirname, "src"), 
      "node_modules", 
     ] 
    }, 
    output: { 
     path: __dirname + "/public/", 
     // publicPath: "/public/", 
     filename: "build.js" 
    }, 
    plugins: debug ? [] : [ 
     new webpack.optimize.DedupePlugin(), 
     new webpack.optimize.OccurrenceOrderPlugin(), 
     new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }), 
    ], 
    devServer: { 
     port: 3000, // most common port 
     contentBase: './public', 
     inline: true, 
     historyApiFallback: true, 
    } 
}; 
+0

你可以試試 Danish

+0

沒有任何變化; '仍然給錯誤 –

回答

1

CSP可以幫助你的白名單,你信任的來源。所有其他來源都不允許訪問。仔細閱讀this Q&A,然後確保您將字體,套接字連接和其他來源列入白名單如果您信任它們

如果你知道你在做什麼,你可以註釋掉meta標籤來測試,可能一切正常。但意識到你/你的用戶在這裏受到保護,因此保留meta標記可能是件好事。

+0

謝謝你好的答案,但我仍然可以解決'拒絕加載字體'數據:font/woff; base64,d09'這個錯誤 –

+0

但是這個元解決最大錯誤: '

2

它的價值 - 我有類似的問題,假設它涉及到Chrome更新。

我不得不添加字體-SRC,然後因爲我使用的是CDN

<meta http-equiv="Content-Security-Policy" content="default-src 'self'; font-src 'self' data: fonts.gstatic.com;"> 
+0

但我是從我的本地計算機使用字體,所以我會寫什麼? 'data:fonts.gstatic.com' –

+3

@JavaScriptLearner - 在這種情況下,您可以嘗試數據: – IonicBurger

0

的JavaScript學習指定的網址:如果您添加你給了大部分的保護CSP提供的不安全的內聯。

相關問題