在我的nonce与csp和flask的内联js的实现是错的?



我已经为csp inline js实现了一个nonce。努力寻找为什么它不起作用。当访问一个页面时得到这个错误:

Content Security Policy: The page's settings blocked the loading of a resource at https://loca.../script.js ("script-src").
Content Security Policy: The page's settings blocked the loading of a resource at inline ("script-src").

实现到flask://在timer.py文件中实现了一个创建nonce的函数。

def GetCspNonce():
"""Returns a random nonce."""
NONCE_LENGTH = 16
return base64.b64encode(os.urandom(NONCE_LENGTH))
nonce = GetCspNonce()
function to redirect to a template:
return render_template('base/timer.html', timer= timer, nonce=nonce)

//添加到init.py csp nonce.

def create_app():
# create and configure the app
....
csp = {
'default-src': [''self''],
'script-src': ['https://www.goog....], 'nonce-{nonce}'],...

//timer.html

<td> 
<p> ...</p>
<script type="text/javascript" src="../static/text/script.js" nonce="{nonce}"></script>
</td>

您确定在策略和模板中传递的nonce是相同的吗?另外,你不应该在这里使用f字符串'nonce-{nonce}'吗?

最新更新