gpt4 book ai didi

具有 (CSP) 内容安全策略的 Angular 5 和 ASP.Net Core

转载 作者:行者123 更新时间:2023-12-04 01:56:25 26 4
gpt4 key购买 nike

为了让我的 Angular 5 应用程序尽可能安全,我花了一些时间在 CSP 上,但努力让它工作。

我使用NWebSec启用了CSP,如下:

 app.UseCsp(options =>
{
options.DefaultSources(s => s.Self());
options.ScriptSources(s => s.Self());
options.StyleSources(s => s.Self().CustomSources("stackpath.bootstrapcdn.com"));
}); // Use Content Security Policy

我使用以下方法构建了 Angular 应用程序:

ng build --aot --prod

ASP.Net Core 应用程序使用 app.UseStaticFiles() 中间件托管(构建的)Angular 应用程序。

我已经阅读了几篇文章,并试图找到一个关于如何让它工作的直接答案,但我无法通过这个错误,它源自/main.ae5fbeccd9ff1305a55c.js:

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' stackpath.bootstrapcdn.com". Either the 'unsafe-inline' keyword, a hash ('sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU='), or a nonce ('nonce-...') is required to enable inline execution.

我相信 Angular 正在创建导致这些错误的代码,解决方案是在构建应用程序时使用“--aot”命令,但这对我不起作用。

我已经在 Angular 5 和 Angular 6 上尝试过这个(甚至尝试过使用一个新的 Angular 项目)。同样的问题。

我的问题是:目前是否有推荐的方法让 Angular 5/6 与 CSP 一起工作,而不牺牲安全性?如果不是,那么下一个最好的事情是什么?

干杯

其他详细信息:Index.html(默认构建 Angular 6 应用程序)

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Default Angular App</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.34c57ab7888ec1573f9c.css">
</head>

<body>
<app-root></app-root>

<script type="text/javascript" src="runtime.6afe30102d8fe7337431.js"></script>
<script type="text/javascript" src="polyfills.2903ad11212d7d797800.js"></script>
<script type="text/javascript" src="main.ae5fbeccd9ff1305a55c.js"></script>
</body>

</html>

最佳答案

Angular 中 CSS 的处理不符合现代安全标准,因为样式在所有组件中保持内联(为此 here is a ticket)。到目前为止,我们不得不忍受 CSP header 中令人不快的 style-src 'unsafe-inline' 指令。

对于使用 Google 字体的简单应用,CSP header 可能看起来像

Content-Security-Policy: "default-src 'self'; style-src 'self' fonts.googleapis.com 'unsafe-inline'; font-src 'self' fonts.gstatic.com";

This post详细介绍了 Angular 中的 CSP 策略。

关于具有 (CSP) 内容安全策略的 Angular 5 和 ASP.Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50313234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com