当我启动我的express应用程序时,浏览器给我这个错误:
Refused to load the script 'http://localhost:1337/main.js' because it violates
the following Content Security Policy directive: "script-src unsafe-eval".
Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.在我的index.js文件中,我设置了如下头盔:
// Set Content Security Policies
const scriptSources = ["'self'", "'unsafe-inline'", "'unsafe-eval'"];
const styleSources = ["'self'", "'unsafe-inline'"];
const connectSources = ["'self'"];
app.use(
helmet.contentSecurityPolicy({
defaultSrc: ["'self'"],
scriptSrc: scriptSources,
scriptSrcElem: scriptSources,
styleSrc: styleSources,
connectSrc: connectSources,
reportUri: '/report-violation',
reportOnly: false,
safari5: false
})
);
app.use(helmet({
contentSecurityPolicy: false,
}));在.js文件中加载我的index.html,如下所示:
<script type="module" src="./main.js"></script>这是我的GitHub回购:https://github.com/jgonzales394/fsn.pw
发布于 2020-11-10 11:44:54
头盔维修员。这是因为您的指令需要嵌套在directives属性下。
例如:
app.use(
helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
scriptSrc: scriptSources,
// ...
},
})
)发布于 2020-11-12 03:28:24
好的,我设法让它正常工作。头盔允许我这样设置我的CSP:
app.use(
helmet.contentSecurityPolicy({
defaultSrc: ["'self'"],
scriptSrc: scriptSources,
scriptSrcElem: scriptSources,
styleSrc: styleSources,
connectSrc: connectSources,
reportUri: '/report-violation',
reportOnly: false,
safari5: false
})
);所以我的main.js文件是一个vue应用程序,我之前写的是这样的:
import * as Vue from './src/vue.esm-browser.js';
const App = Vue.createApp({
data() {
return {
slug,
url
}
},
methods: {
createUrl() {
console.log(this.slug, this.url);
}
}
}).mount('#app');我重写了代码,如下所示:
import { createApp, ref } from './src/vue.esm-browser.js';
const slug = ref('');
const url = ref('');
createApp({
setup() {
const createUrl = () => {
console.log(slug.value, url.value);
};
return {
slug,
url,
createUrl
};
}
}).mount('#app');在我的html文件中,可以在不调用createUrl的情况下调用它。
https://stackoverflow.com/questions/64762132
复制相似问题